summaryrefslogtreecommitdiffstats
path: root/test/t/salsa.pm
blob: 800d1e59d9422c8af4dd2b3249ae44a2ed754688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{

    package MockRESTClient;
    use URI;
    use Moo;
    extends 'GitLab::API::v4::RESTClient';

    has _mocks => (
        is       => 'ro',
        default  => sub { [] },
        init_arg => undef,
    );

    sub mock_endpoints {
        my $self = shift;

        while (@_) {
            my $method  = shift;
            my $path_re = shift;
            my $sub     = shift;

            push @{ $self->_mocks() }, [$method, $path_re, $sub];
        }

        return;
    }

    sub _http_tiny_request {
        my ($self, $req_method, $req) = @_;

        die "req_method may only be 'request' at this time"
          if $req_method ne 'request';

        my ($method, $url, $options) = @$req;

        my $path = URI->new($url)->path();
        $path =~ s{^.*api/v4/}{};

        foreach my $mock (@{ $self->_mocks() }) {
            my ($handler_method, $path_re, $sub) = @$mock;

            next if $method ne $handler_method;

            my @captures = ($path =~ $path_re);
            next if !@captures;    # No captures still returns a 1.

            my ($status, $content)
              = $sub->([$method, $url, $options], @captures);
            $content = JSON::to_json($content) if ref $content;

            return {
                status  => $status,
                success => ($status =~ m{^2\d\d$}) ? 1 : 0,
                defined($content) ? (content => $content) : (),
            };
        }

        die "No mock endpoint matched the $method '$path' endpoint";
    }
}

sub api {
    my ($gitdir) = @_;
    my @users = ({
        id       => 11,
        username => 'me',
        name     => 'Me',
        email    => 'me@debian.org',
        state    => 'active'
    });
    my @teams = ({
        id        => 2099,
        name      => 'Debian JavaScript Maintainers',
        full_name => 'Debian JavaScript Maintainers',
        full_path => 'js-team',
    });
    my @projects;
    my $next_id = 1;

    my $api = GitLab::API::v4->new(
        url               => 'https://example.com/api/v4',
        rest_client_class => 'MockRESTClient',
    );

    $api->rest_client->mock_endpoints(
        GET  => qr{^user$}  => sub { 200, $users[0] },
        GET  => qr{^users$} => sub { 200, \@users },
        POST => qr{^users$} => sub {
            my ($req) = @_;
            my $user = decode_json($req->[2]->{content});
            $user->{id} = $next_id;
            $next_id++;
            push @users, $user;
            return 204;
        },
        GET => qr{^users?/(\d+)$} => sub {
            my ($req, $id) = @_;
            foreach my $user (@users) {
                next if $user->{id} != $id;
                return 200, $user;
            }
            return 404;
        },
        GET => qr{^users/(\D+)$} => sub {
            my ($req, $id) = @_;
            foreach my $user (@users) {
                next if $user->{username} != $id;
                return 200, $user;
            }
            return 404;
        },
        GET => qr{^groups$} => sub {
            200, \@teams;
        },
        GET => qr{^groups/([^/]+)$} => sub {
            my ($req, $name) = @_;
            foreach my $team (@teams) {
                next if $team->{full_path} ne $name;
                return 200, $team;
            }
            return 404;
        },
        PUT => qr{^users/(\d+)$} => sub {
            my ($req, $id) = @_;
            my $data = decode_json($req->[2]->{content});
            foreach my $user (@users) {
                next if $user->{id} != $id;
                %$user = (%$user, %$data,);
                return 204;
            }
            return 404;
        },
        DELETE => qr{^users/(\d+)$} => sub {
            my ($req, $id) = @_;
            my @new;
            foreach my $user (@users) {
                next if $user->{id} == $id;
                push @new, $user;
            }
            return 404 if @new == @users;
            @users = @new;
            return 204;
        },
        # Projects
        POST => qr{^projects$} => sub {
            my $content = JSON::from_json($_[0]->[2]->{content});
            mkdir "$gitdir/me/$content->{path}";
            $ENV{"GIT_CONFIG_NOGLOBAL"} = 1;
            print
`cd $gitdir/me/$content->{path};git init;git config receive.denyCurrentBranch ignore;cd -`;
            $content->{id}        = scalar @projects + 1;
            $content->{hooks}     = [];
            $content->{namespace} = {
                kind => 'user',
                id   => 11,
                name => 'me',
            };
            $content->{path_with_namespace} = 'me/' . $content->{path};
            $content->{web_url} = 'http://no.org/me/' . $content->{path};
            push @projects, $content;
            return 200, $content;
        },
        GET => qr{^projects/(\d+)/hooks} => sub {
            my ($req, $id) = @_;
            my $res = eval { $projects[$id - 1]->{hooks} };
            return ($res ? (200, $res) : (404));
        },
        GET => qr{^projects/(\d+)/services/(\w+)} => sub {
            my ($req, $id, $service) = @_;
            return 404;
        },
        GET => qr{^projects$} => sub {
            my ($req) = @_;
            return (200, \@projects) unless ($req->[1] =~ /search=([^&]+)/);
            my $str = $1;
            my @res;
            foreach (@projects) {
                if ($_->{name} =~ /\Q$str\E/) {
                    push @res, $_;
                }
            }
            return 200, \@res;
        },
        GET => qr{^projects/([a-z]+)(?:%2F(\w+))*$} => sub {
            my ($req, @path) = @_;
            my $repo = pop @path;
            my $path = join '/', @path;
            foreach (@projects) {
                if ($_->{namespace}->{name} eq $path and $_->{path} eq $repo) {
                    return 200, $_;
                }
            }
            return 404;
        },
        GET => qr{^projects/(\d+)$} => sub {
            my ($req, $id) = @_;
            return 404 unless ($_ = $projects[$id - 1]);
            return 200, $_;
        },
        PUT => qr{^projects/(\d+)} => sub {
            my ($req, $id) = @_;
            return 404 unless ($_ = $projects[$id - 1]);
            my $content = JSON::from_json($req->[2]->{content});
            foreach my $k (keys %$content) {
                $_->{$k} = $content->{$k};
            }
            return 200, {};
        },
        POST => qr{^projects/(\d+)/hooks} => sub {
            my ($req, $id) = @_;
            return 404 unless ($_ = $projects[$id - 1]);
            my $content = JSON::from_json($req->[2]->{content});
            push @{ $_->{hooks} }, $content;
            return 200, {};
        },
        POST => qr{^projects/(\d+)/repository/branches} => sub {
            return 200, {};
        },
        DELETE => qr{^projects/(\d+)/repository/branches/([\w\-\.]+)$} => sub {
            return 200, {};
        },
    );
    return $api;
}

1;