summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/t/basic.t
blob: 9a6723ccbf9b4e1745a6f0343cb6df5dc7270538 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/perl
# 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 5.10.1;
use lib qw( . lib local/lib/perl5 );
use Bugzilla;

BEGIN { Bugzilla->extensions };

use Test::More;
use Test2::Tools::Mock;
use Data::Dumper;
use JSON::MaybeXS;
use Carp;
use Try::Tiny;

use ok 'Bugzilla::Extension::PhabBugz::Feed';
use ok 'Bugzilla::Extension::PhabBugz::Util', qw( get_attachment_revisions );
can_ok('Bugzilla::Extension::PhabBugz::Feed', 'group_query');

our @group_members;
our @project_members;


my $User = mock 'Bugzilla::Extension::PhabBugz::User' => (
    add_constructor => [
        'fake_new' => 'hash',
    ],
    override => [
        'match' => sub { [ mock() ] },
    ],
);

my $Feed = mock 'Bugzilla::Extension::PhabBugz::Feed' => (
    override => [
        get_group_members => sub {
            return [ map { Bugzilla::Extension::PhabBugz::User->fake_new(%$_) } @group_members ];
        }
    ]
);

my $Project = mock 'Bugzilla::Extension::PhabBugz::Project' => (
    override_constructor => [
        new_from_query => 'ref_copy',
    ],
    override => [
        'members' => sub {
            return [ map { Bugzilla::Extension::PhabBugz::User->fake_new(%$_) } @project_members ];
        }
    ]
);

local Bugzilla->params->{phabricator_enabled} = 1;
local Bugzilla->params->{phabricator_api_key} = 'FAKE-API-KEY';
local Bugzilla->params->{phabricator_base_uri} = 'http://fake.fabricator.tld';

my $Bugzilla = mock 'Bugzilla' => (
    override => [
        'dbh'  => sub { mock() },
        'user' => sub { Bugzilla::User->new({ name => 'phab-bot@bmo.tld' }) },
    ],
);

my $BugzillaGroup = mock 'Bugzilla::Group' => (
    add_constructor => [
        'fake_new' => 'hash',
    ],
    override => [
        'match' => sub { [ Bugzilla::Group->fake_new(id => 1, name => 'firefox-security' ) ] },
    ],
);

my $BugzillaUser = mock 'Bugzilla::User' => (
    add_constructor => [
        'fake_new' => 'hash',
    ],
    override => [
        'new' => sub {
            my ($class, $hash) = @_;
            if ($hash->{name} eq 'phab-bot@bmo.tld') {
                return $class->fake_new( id => 8_675_309, login_name => 'phab-bot@bmo.tld', realname => 'Fake PhabBot' );
            }
            else {
            }
        },
        'match' => sub { [ mock() ] },
    ],
);


my $feed = Bugzilla::Extension::PhabBugz::Feed->new;

# Same members in both
do {
    my $UserAgent = mock 'LWP::UserAgent' => (
        override => [
            'post' => sub {
                my ($self, $url, $params) = @_;
                my $data = decode_json($params->{params});
                is_deeply($data->{transactions}, [], 'no-op');
                return mock({is_error => 0, content => '{}'});
            },
        ],
    );
    local @group_members = (
        { phid => 'foo' },
    );
    local @project_members = (
        { phid => 'foo' },
    );
    $feed->group_query;
};

# Project has members not in group
do {
    my $UserAgent = mock 'LWP::UserAgent' => (
        override => [
            'post' => sub {
                my ($self, $url, $params) = @_;
                my $data = decode_json($params->{params});
                my $expected = [ { type => 'members.remove', value => ['foo'] } ];
                is_deeply($data->{transactions}, $expected, 'remove foo');
                return mock({is_error => 0, content => '{}'});
            },
        ]
    );
    local @group_members = ();
    local @project_members = (
        { phid => 'foo' },
    );
    $feed->group_query;
};

# Group has members not in project
do {
    my $UserAgent = mock 'LWP::UserAgent' => (
        override => [
            'post' => sub {
                my ($self, $url, $params) = @_;
                my $data = decode_json($params->{params});
                my $expected = [ { type => 'members.add', value => ['foo'] } ];
                is_deeply($data->{transactions}, $expected, 'add foo');
                return mock({is_error => 0, content => '{}'});
            },
        ]
    );
    local @group_members = (
        { phid => 'foo' },
    );
    local @project_members = (
    );
    $feed->group_query;
};

do {
    my $Revision  = mock 'Bugzilla::Extension::PhabBugz::Revision' => (
        override => [
            'update' => sub { 1 },
        ],
    );
    my $UserAgent = mock 'LWP::UserAgent' => (
        override => [
            'post' => sub {
                my ($self, $url, $params) = @_;
                if ($url =~ /differential\.revision\.search/) {
                    my $content = <<JSON;
{
    "error_info": null,
    "error_code": null,
    "result": {
        "data": [
            {
                "id": 9999,
                "type": "DREV",
                "phid": "PHID-DREV-uozm3ggfp7e7uoqegmc3",
                "fields": {
                    "title": "Added .arcconfig",
                    "summary": "Added .arcconfig",
                    "authorPHID": "PHID-USER-4wigy3sh5fc5t74vapwm",
                    "dateCreated": 1507666113,
                    "dateModified": 1508514027,
                    "policy": {
                        "view": "public",
                        "edit": "admin"
                    },
                    "bugzilla.bug-id": "23",
                    "status": {
                        "value": "needs-review",
                        "name": "Needs Review",
                        "closed": false,
                        "color.ansi": "magenta"
                    }
                },
                "attachments": {
                    "reviewers": {
                        "reviewers": []
                    },
                    "subscribers": {
                        "subscriberPHIDs": [],
                        "subscriberCount": 0,
                        "viewerIsSubscribed": true
                    },
                    "projects": {
                        "projectPHIDs": []
                    }
                }
            }
        ]
    }
}
JSON
                    return mock { is_error => 0, content => $content };
                }
                else {
                    return mock { is_error => 1, message => "bad request" };
                }
            },
        ],
    );
    my $Attachment = mock 'Bugzilla::Attachment' => (
        add_constructor => [ fake_new => 'hash' ],
    );
    my $Bug = mock 'Bugzilla::Bug' => (
        add_constructor => [ fake_new => 'hash' ],
    );
    my $bug = Bugzilla::Bug->fake_new(
        bug_id      => 23,
        attachments => [
            Bugzilla::Attachment->fake_new(
                mimetype => 'text/x-phabricator-request',
                filename => 'phabricator-D9999-url.txt',
            ),
        ]
    );

    my $revisions = get_attachment_revisions($bug);
    is(ref($revisions), 'ARRAY', 'it is an array ref');
    isa_ok($revisions->[0], 'Bugzilla::Extension::PhabBugz::Revision');
    is($revisions->[0]->bug_id, 23, 'Bugzila ID is 23');
    ok( try { $revisions->[0]->update }, 'update revision');

};

done_testing;