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);