File: regression.t

package info (click to toggle)
libgitlab-api-v4-perl 0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: perl: 5,960; sh: 838; python: 63; makefile: 12
file content (221 lines) | stat: -rwxr-xr-x 6,297 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl
use strictures 2;

use Test2::Require::AuthorTesting;
use Test2::V0;

use Log::Any::Adapter 'TAP';
use MIME::Base64 qw( decode_base64 );
use Path::Tiny;

use GitLab::API::v4::Config;
use GitLab::API::v4::WWWClient;
use GitLab::API::v4;

my $config = GitLab::API::v4::Config->new();
my $api = GitLab::API::v4->new( $config->args() );

subtest projects => sub{
    my $stamp = time();
    my $project_name = "gitlab-api-v4-test-$stamp";

    my $created_project = $api->create_project(
        { name=>$project_name },
    );
    ok( $created_project, 'project created' );

    my $project_id = $created_project->{id};
    my $project = $api->project( $project_id );
    ok( $project, 'project found' );

    subtest upload_file_to_project => sub{
        my $file = Path::Tiny->tempfile( SUFFIX => '.txt' );
        my $file_content = 'Hello GitLab, this is a test of ' . ref($api) . '.';
        $file->spew( $file_content );

        my $upload = $api->upload_file_to_project(
            $project_id,
            { file=>"$file" },
        );
        ok( $upload->{url}, 'got an upload response' );

        my $www_base_url = $api->url();
        $www_base_url =~ s{/api/v4.*$}{};

        my $www_client = GitLab::API::v4::WWWClient->new(
            url => $www_base_url,
        );

        $www_client->sign_in(
            'root',
            $ENV{GITLAB_API_V4_ROOT_PASSWORD},
        );

        my $project_path = $project->{path_with_namespace};
        my $upload_path = $upload->{url};
        my $download_path = "$project_path/$upload_path";

        my $res = $www_client->get( $download_path );
        is( $res->{content}, $file_content, 'upload_file_to_project worked' );
    };

    subtest 'file methods' => sub{
        $api->create_file(
            $project_id,
            'foo/bar.txt',
            {
                branch => 'master',
                content => 'Test of create file.',
                commit_message => 'This is a commit.',
            },
        );

        my $file = $api->file(
            $project_id,
            'foo/bar.txt',
            { ref=>'master' },
        );
        is(
            decode_base64( $file->{content} ),
            'Test of create file.',
            'created file is there; and looks right',
        );

        my $content = $api->raw_file(
            $project_id,
            'foo/bar.txt',
            { ref=>'master' },
        );
        is(
            $content,
            'Test of create file.',
            'able to retrieve the file raw',
        );

        $api->edit_file(
            $project_id,
            'foo/bar.txt',
            {
                branch => 'master',
                content => 'Test of edit file.',
                commit_message => 'This is the next commit.',
            },
        );
        my $edited_file = $api->file(
            $project_id,
            'foo/bar.txt',
            { ref=>'master' },
        );
        is(
            decode_base64( $edited_file->{content} ),
            'Test of edit file.',
            'editing a file worked',
        );

        $api->delete_file(
            $project_id,
            'foo/bar.txt',
            {
                branch => 'master',
                commit_message => 'This is the last commit.',
            },
        );
        $file = $api->file(
            $project_id,
            'foo/bar.txt',
            { ref=>'master' },
        );
        is(
            $file, undef,
            'file was deleted',
        );
    };

    subtest hooks => sub{
        my $hook = $api->create_project_hook(
            $project->{id},
            { url=>'http://example.com/gitlab-hook-1' },
        );
        ok( $hook, 'create_project_hook returned the hook' );

        $hook = $api->edit_project_hook(
            $project->{id}, $hook->{id},
            { url=>'http://example.com/gitlab-hook-2' },
        );
        ok( $hook, 'edit_project_hook returned the hook' );
        my $hook_id = $hook->{id};

        $hook = $api->project_hook( $project->{id}, $hook_id );
        ok( $hook, 'project_hook returned the hook' );
        is( $hook->{url}, 'http://example.com/gitlab-hook-2', 'hook looks right' );

        $api->delete_project_hook( $project->{id}, $hook_id );
        $hook = $api->project_hook( $project->{id}, $hook_id );
        ok( (!$hook), 'delete_project_hook seems to have worked' );

        like(
            dies { $api->delete_project_hook( $project->{id}, $hook_id ) },
            qr{\b404\b},
            'a subsequent delete_project_hook throws',
        );
    };

    $api->delete_project( $project_id );
    pass 'project deleted';
};

subtest users => sub{
    my $stamp = time();
    my $username = "gitlab-api-v4-test-$stamp";
    $api->create_user({
        username   => $username,
        email      => "$username\@example.com",
        password   => 'd5fzHF7tfgh',
        name       => 'GitLabAPIv4 Test',
    });
    pass 'user created';

    my $users = $api->users({ username => $username });
    is( @$users+0, 1, 'one user found' );

    my $user = shift @$users;
    is( $user->{username}, $username, 'user has correct username' );
    die 'Incorrect user found' if $user->{username} ne $username;

    my $user_id = $user->{id};
    ok( $api->block_user($user_id), 'user blocked' );
    ok( (!$api->block_user($user_id)), 'user cannot be blocked again' );
    ok( $api->unblock_user($user_id), 'user unblocked' );
    ok( (!$api->unblock_user($user_id)), 'user cannot be unblocked again' );

    $api->delete_user($user_id);
    pass 'user deleted';
};

subtest failures => sub{
    is( $api->user( 12345678 ), undef, 'GETing an unknown entity returns undef' );
    my $err_re = qr{^Error PUTing \S+/users/12345678 \(HTTP 404\): Not Found \{"message":"404 User Not Found"\}};
    like( dies{ $api->edit_user( 12345678, {} ) }, $err_re, 'POSTing an unknown entity throws a specific exception' );
};

done_testing;

sub join_paths {
    my @paths = @_;

    return() if !@paths;
    return @paths if @paths==1;

    my $first = shift @paths;
    $first =~ s{/$}{};

    my $last = pop @paths;
    $last =~ s{^/}{};

    @paths = (
        map { $_ =~ s{^/?(.*?)/?$}{$1}; $_ }
        @paths
    );

    return join('/', $first, @paths, $last);
}