RT 4.2.11, ticket#13852
[freeside.git] / rt / t / api / groups.t
1 use strict;
2 use warnings;
3 use RT::Test nodata => 1, tests => 27;
4
5 RT::Group->AddRight( General =>
6     'RTxGroupRight' => 'Just a right for testing rights',
7 );
8
9 {
10     my $g = RT::Group->new(RT->SystemUser);
11     my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'GroupsNotEqualTest');
12     ok ($id, "created group #". $g->id) or diag("error: $msg");
13
14     my $groups = RT::Groups->new(RT->SystemUser);
15     $groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => $g->id );
16     $groups->LimitToUserDefinedGroups();
17     my $bug = grep $_->id == $g->id, @{$groups->ItemsArrayRef};
18     ok (!$bug, "didn't find group");
19 }
20
21
22 {
23     my $u = RT::User->new(RT->SystemUser);
24     my ($id, $msg) = $u->Create( Name => 'Membertests'. $$ );
25     ok ($id, 'created user') or diag "error: $msg";
26
27     my $g = RT::Group->new(RT->SystemUser);
28     ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
29     ok ($id, $msg);
30
31     my ($aid, $amsg) =$g->AddMember($u->id);
32     ok ($aid, $amsg);
33     ok($g->HasMember($u->PrincipalObj),"G has member u");
34
35     my $groups = RT::Groups->new(RT->SystemUser);
36     $groups->LimitToUserDefinedGroups();
37     $groups->WithMember(PrincipalId => $u->id);
38     is ($groups->Count , 1,"found the 1 group - " . $groups->Count);
39     is ($groups->First->Id , $g->Id, "it's the right one");
40 }
41
42 no warnings qw/redefine once/;
43
44 my $q = RT::Queue->new(RT->SystemUser);
45 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
46 ok ($id, $msg);
47
48 my $testuser = RT::User->new(RT->SystemUser);
49 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
50 ok ($id,$msg);
51
52 my $global_admin_cc = RT->System->RoleGroup( 'AdminCc' );
53 ok($global_admin_cc->id, "Found the global admincc group");
54 my $groups = RT::Groups->new(RT->SystemUser);
55 $groups->WithRight(Right => 'OwnTicket', Object => $q);
56 is($groups->Count, 1);
57 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> RT->System);
58 ok ($id,$msg);
59 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
60 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
61 ok($id,$msg);
62 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
63
64 $groups = RT::Groups->new(RT->SystemUser);
65 $groups->WithRight(Right => 'OwnTicket', Object => $q);
66 ok ($id,$msg);
67 is($groups->Count, 3);
68
69 my $RTxGroup = RT::Group->new(RT->SystemUser);
70 ($id, $msg) = $RTxGroup->CreateUserDefinedGroup( Name => 'RTxGroup', Description => "RTx extension group");
71 ok ($id,$msg);
72 is ($RTxGroup->id, $id, "group loaded");
73
74 my $RTxSysObj = {};
75 bless $RTxSysObj, 'RTx::System';
76 *RTx::System::Id = sub { 1; };
77 *RTx::System::id = *RTx::System::Id;
78 my $ace = RT::Record->new(RT->SystemUser);
79 $ace->Table('ACL');
80 $ace->_BuildTableAttributes unless ($RT::Record::_TABLE_ATTR->{ref($ace)});
81 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System', ObjectId  => 1);
82 ok ($id, "ACL for RTxSysObj created");
83
84 my $RTxObj = {};
85 bless $RTxObj, 'RTx::System::Record';
86 *RTx::System::Record::Id = sub { 4; };
87 *RTx::System::Record::id = *RTx::System::Record::Id;
88
89 $groups = RT::Groups->new(RT->SystemUser);
90 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxSysObj);
91 is($groups->Count, 1, "RTxGroupRight found for RTxSysObj");
92
93 $groups = RT::Groups->new(RT->SystemUser);
94 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj);
95 is($groups->Count, 0, "RTxGroupRight not found for RTxObj");
96
97 $groups = RT::Groups->new(RT->SystemUser);
98 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj, EquivObjects => [ $RTxSysObj ]);
99 is($groups->Count, 1, "RTxGroupRight found for RTxObj using EquivObjects");
100
101 $ace = RT::Record->new(RT->SystemUser);
102 $ace->Table('ACL');
103 $ace->_BuildTableAttributes unless ($RT::Record::_TABLE_ATTR->{ref($ace)});
104 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System::Record', ObjectId  => 5 );
105 ok ($id, "ACL for RTxObj created");
106
107 my $RTxObj2 = {};
108 bless $RTxObj2, 'RTx::System::Record';
109 *RTx::System::Record::Id = sub { 5; };
110
111 $groups = RT::Groups->new(RT->SystemUser);
112 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2);
113 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
114
115 $groups = RT::Groups->new(RT->SystemUser);
116 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2, EquivObjects => [ $RTxSysObj ]);
117 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");