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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
use strict;
use warnings;
use 5.10.1;
use lib qw(. lib t);
use Test::More tests => 15;
use Capture::Tiny ':all';
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::WebService::Util qw(extract_flags);
use Bugzilla::Bug;
BEGIN {
$ENV{PROJECT} = 'tests';
Bugzilla->extensions;
}
my $e;
my $dbh = Bugzilla->dbh;
$dbh->do('DELETE FROM workflow_groupreq');
Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
my $system_user
= Bugzilla::User->new({name => Bugzilla->params->{system_user}});
Bugzilla->set_user($system_user);
use_ok('Bugzilla::Extension::Workflows::GroupRequest');
use_ok('Bugzilla::Extension::Workflows::WebService::GroupRequest');
# configure the admin group
my $admin_grp = Bugzilla::Group->new({name => 'admin'});
$admin_grp->set_category('Admin');
$admin_grp->update();
my %bug_groups = (
open_group => {description => 'A group anyone can join', category => 'Public',},
partner_group => {
description => 'A group for partners',
category => 'Partner',
regex => '@partner.example.org$'
},
partner_managers => {
description => 'A group for partner managers',
category => 'Partner',
regex => '@partner.example.org$'
},
red_hat => {description => 'A group for Red Hat staff', category => 'Red Hat',},
red_hat_managers =>
{description => 'A group for Red Hat managers', category => 'Red Hat',},
security => {description => 'A group for security use', category => 'Red Hat',},
);
foreach my $grp (keys %bug_groups) {
my $group = Bugzilla::Group->new({name => $grp});
if (!$group) {
my %params = (
name => $grp,
description => $bug_groups{$grp}->{description},
isbuggroup => 1,
category => $bug_groups{$grp}->{category},
);
## $params{userregexp} = $bug_groups{$grp}->{regex}
## if ($bug_groups{$grp}->{regex});
$group = Bugzilla::Group->create(\%params);
}
$bug_groups{$grp}->{group} = $group;
}
my %test_users = (
'jfearn@redhat.com' => {groups => ['red_hat_managers']},
'partner1@partner.example.org' => {},
'partner2@partner.example.org' => {},
'joepublic@example.org' => {},
'manager@partner.example.org' =>
{groups => ['partner_managers'], bless_groups => ['partner_group'],},
);
foreach my $usr (keys %test_users) {
my $user = Bugzilla::User->new({name => $usr});
unless ($user) {
$user = Bugzilla::User->create({login_name => $usr, cryptpassword => '*'});
}
$user->set_groups({set => []});
$user->set_bless_groups({set => []});
$user->update;
if ($test_users{$usr}->{groups}) {
$user->set_groups({add => $test_users{$usr}->{groups}});
}
if ($test_users{$usr}->{bless_groups}) {
$user->set_bless_groups({add => $test_users{$usr}->{bless_groups}});
}
$user->update;
$test_users{$usr}->{user} = $user;
}
eval {
$test_users{'manager@partner.example.org'}->{user}
->set_groups({add => ['red_hat_managers']});
};
like(
$@,
qr/Only \@redhat.com accounts may be added to protected groups such as/,
"Cannot add non-redhat user to Red Hat group"
);
my $test_user = $test_users{'joepublic@example.org'}->{user};
my $bug;
eval {
$bug
= $test_user->apply_for_groups($test_user,
'testing', [$bug_groups{'open_group'}->{group}]);
};
like($@, qr/You tried to request group/,
"Cannot apply for an un-managed group");
Bugzilla->set_user($test_user);
eval {
$test_user->set_groups({add => ['open_group']});
$test_user->update;
};
ok((not $e), "Un-blessed user cannot directly add user to un-managed group");
$test_user = $test_users{'partner1@partner.example.org'}->{user};
my $manager = $test_users{'manager@partner.example.org'}->{user};
Bugzilla->set_user($manager);
eval {
$test_user->set_groups({add => ['partner_group']});
$test_user->update;
};
like(
$@,
qr/You tried to request group/,
"Blessed user cannot directly add user to un-managed Partner group"
);
Bugzilla->set_user($system_user);
eval {
$manager->set_bless_groups({add => ['open_group']});
$manager->update();
};
$e = $@;
diag explain $e if ($e);
Bugzilla->set_user($manager);
eval {
$test_user->set_groups({add => ['open_group']});
$test_user->update;
};
$e = $@;
ok((not $e), "Blessed user can directly add user to un-managed group");
diag explain $e if ($e);
Bugzilla->set_user($system_user);
eval {
my %args = (
group => 'partner_group',
managed => 1,
regex => '@(redhat.com|partner.example.org)$',
);
Bugzilla::Extension::Workflows::WebService::GroupRequest->update_group(\%args);
};
like(
$@,
qr/allows non Red Hat members/,
"Cannot manage non-Red Hat group without an approval group or CC"
);
eval {
my %args = (
group => 'partner_group',
managed => 1,
regex => '@(redhat.com|partner.example.org)$',
cc_list => 'manager@partner.example.org',
);
Bugzilla::Extension::Workflows::WebService::GroupRequest->update_group(\%args);
};
$e = $@;
ok((not $e), "Can start managing a group");
diag explain $e if ($e);
Bugzilla->set_user($system_user);
my $wfg = $bug_groups{'partner_group'}->{group}->group_workflow;
$wfg->set_approval_group('partner_managers');
$wfg->update;
Bugzilla->set_user($manager);
$test_user = $test_users{'joepublic@example.org'}->{user};
Bugzilla->set_user($manager);
eval {
$test_user->set_groups({add => ['partner_group']});
$test_user->update;
};
like(
$@,
qr/Only user accounts matching/,
"Cannot add a non-matching user to a managed group with a regex"
);
$test_user = $test_users{'partner2@partner.example.org'}->{user};
eval {
$test_user->set_groups({add => ['partner_group']});
$test_user->update;
# You need to reload the bug to get the updated flags and comments
$bug = Bugzilla::Bug->check($test_user->{_workflow_bug}->id);
};
$e = $@;
ok((not $e) && defined $bug && $bug->isa('Bugzilla::Bug'),
"Bless user triggers workflow for user request with a matching regex");
diag explain $e if ($e);
my @flags;
foreach my $flag (@{$bug->flags}) {
if ($flag->name =~ /^approvals_request_/ && $flag->status eq '?') {
push(@flags, {id => $flag->id, status => '+'});
}
}
eval {
$bug->set_flags(\@flags, []);
$bug->set_all({comment => {body => 'approved!'}});
$bug->update;
};
$e = $@;
ok((not $e), "Approver can approve group membership");
# You need to reload the bug to get the updated flags and comments
my $updated_bug = Bugzilla::Bug->check($bug->id);
delete $test_user->{groups};
is($updated_bug->bug_status, 'CLOSED', 'Bug has been closed');
ok($test_user->in_group('partner_group'), 'User was added to group');
Bugzilla->set_user($system_user);
my $group_rules;
eval {
my %args = (category => 'Partner',);
$group_rules
= Bugzilla::Extension::Workflows::WebService::GroupRequest->get_groups(
\%args);
};
$e = $@;
ok((not $e), "Get group approval settings for the Partner category");
diag explain $e if ($e);
|