summaryrefslogtreecommitdiffstats
path: root/extensions/Push/t/ReviewBoard.t
blob: c752e34ef61a4889c762cef86662d932f7ed207d (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
#!/usr/bin/perl -T
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
use strict;
use warnings;
use lib qw( . lib local/lib/perl5 );

use Test::More;
use Bugzilla;
use Bugzilla::Extension;
use Bugzilla::Attachment;
use Scalar::Util 'blessed';

BEGIN {
    eval {
        require Test::LWP::UserAgent;
        require Test::MockObject;
    };
    if ($@) {
        plan skip_all =>
          'Tests require Test::LWP::UserAgent and Test::MockObject';
        exit;
    }
}

BEGIN {
    Bugzilla->extensions; # load all of them
    use_ok 'Bugzilla::Extension::Push::Connector::ReviewBoard::Client';
    use_ok 'Bugzilla::Extension::Push::Constants';
}

my ($push) = grep { blessed($_) eq 'Bugzilla::Extension::Push' } @{Bugzilla->extensions };
my $connectors = $push->_get_instance->connectors;
my $con = $connectors->by_name('ReviewBoard');

my $ua_204 = Test::LWP::UserAgent->new;
$ua_204->map_response(
                  qr{https://reviewboard-dev\.allizom\.org/api/review-requests/\d+},
                  HTTP::Response->new('204'));

my $ua_404 = Test::LWP::UserAgent->new;
$ua_404->map_response(
    qr{https://reviewboard-dev\.allizom\.org/api/review-requests/\d+},
    HTTP::Response->new('404', undef, undef, q[{ "err": { "code": 100, "msg": "Object does not exist" }, "stat": "fail" }]));

# forbidden
my $ua_403 = Test::LWP::UserAgent->new;
$ua_403->map_response(
    qr{https://reviewboard-dev\.allizom\.org/api/review-requests/\d+},
    HTTP::Response->new('403', undef, undef, q[ {"err":{"code":101,"msg":"You don't have permission for this"},"stat":"fail"}]));

# not logged in
my $ua_401 = Test::LWP::UserAgent->new;
$ua_401->map_response(
    qr{https://reviewboard-dev\.allizom\.org/api/review-requests/\d+},
    HTTP::Response->new('401', undef, undef, q[ { "err": { "code": 103, "msg": "You are not logged in" }, "stat": "fail" } ]));

# not logged in
my $ua_500 = Test::LWP::UserAgent->new;
$ua_500->map_response(
    qr{https://reviewboard-dev\.allizom\.org/api/review-requests/\d+},
    HTTP::Response->new('500'));

$con->client->{useragent} = $ua_204;
$con->config->{base_uri} = 'https://reviewboard-dev.allizom.org';
$con->client->{base_uri} = 'https://reviewboard-dev.allizom.org';

{
    my $msg = message(
        event => {
            routing_key => 'attachment.modify:is_private',
            target => 'attachment',
        },
        attachment => {
            is_private => 1,
            content_type => 'text/plain',
            bug => { id => 1, is_private => 0 },
        },
    );

    ok(not($con->should_send($msg)), "text/plain message should not be sent");
}

my $data = slurp("extensions/Push/t/rblink.txt");
Bugzilla::User::DEFAULT_USER->{userid} = 42;
Bugzilla->set_user(Bugzilla::User->super_user);
diag " " . Bugzilla::User->super_user->id;

my $dbh = Bugzilla->dbh;
$dbh->bz_start_transaction;
my $timestamp = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
my $bug = Bugzilla::Bug->new({id => 9000});
my $attachment = Bugzilla::Attachment->create({
    bug         => $bug,
    creation_ts => $timestamp,
    data        => $data,
    attach_size => length($data),
    description => "rblink.txt",
    filename    => "rblink.txt",
    isprivate   => 1,
    ispatch     => 0,
    mimetype    => 'text/x-review-board-request'
});
diag "".$attachment->id;
$dbh->bz_commit_transaction;

{
    my $msg = message(
        event => {
            routing_key => 'attachment.modify:cc,is_private',
            target => 'attachment',
        },
        attachment => {
            id => $attachment->id,
            is_private => 1,
            content_type => 'text/x-review-board-request',
            bug => { id => $bug->id, is_private => 0 },
        },
    );
    ok($con->should_send($msg), "rb attachment should be sent");

    {
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_OK, "good push result");
        diag $err if $err;
    }

    {
        local $con->client->{useragent} = $ua_404;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_OK, "good push result for 404");
        diag $err if $err;
    }


    {
        local $con->client->{useragent} = $ua_403;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 403");
        diag $err if $err;
    }


    {
        local $con->client->{useragent} = $ua_401;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 401");
        diag $err if $err;
    }

    {
        local $con->client->{useragent} = $ua_500;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 500");
        diag $err if $err;
    }
}

{
    my $msg = message(
        event => {
            routing_key => 'bug.modify:is_private',
            target => 'bug',
        },
        bug => {
            is_private => 1,
            id => $bug->id,
        },
    );

    ok($con->should_send($msg), "rb attachment should be sent");
    my ($rv, $err) = $con->send($msg);
    is($rv, PUSH_RESULT_OK, "good push result");

    {
        local $con->client->{useragent} = $ua_404;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_OK, "good push result for 404");
    }

    {
        local $con->client->{useragent} = $ua_403;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 404");
        diag $err if $err;
    }


    {
        local $con->client->{useragent} = $ua_401;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 401");
        diag $err if $err;
    }

    {
        local $con->client->{useragent} = $ua_401;
        my ($rv, $err) = $con->send($msg);
        is($rv, PUSH_RESULT_TRANSIENT, "transient error on 401");
        diag $err if $err;
    }
}

sub message {
    my $msg_data = { @_ };

    return Test::MockObject->new
        ->set_always( routing_key => $msg_data->{event}{routing_key} )
        ->set_always( payload_decoded => $msg_data );
}

sub slurp {
    my $file = shift;
    local $/ = undef;
    open my $fh, '<', $file or die "unable to open $file";
    my $s = readline $fh;
    close $fh;
    return $s;
}

done_testing;