first pass RT4 merge, RT#13852
[freeside.git] / rt / t / api / user.t
1
2 use strict;
3 use warnings;
4 use RT;
5 use RT::Test tests => 111;
6
7
8 {
9
10 ok(require RT::User);
11
12
13 }
14
15 {
16
17 # Make sure we can create a user
18
19 my $u1 = RT::User->new(RT->SystemUser);
20 is(ref($u1), 'RT::User');
21 my ($id, $msg) = $u1->Create(Name => 'CreateTest1'.$$, EmailAddress => $$.'create-test-1@example.com');
22 ok ($id, "Creating user CreateTest1 - " . $msg );
23
24 # Make sure we can't create a second user with the same name
25 my $u2 = RT::User->new(RT->SystemUser);
26 ($id, $msg) = $u2->Create(Name => 'CreateTest1'.$$, EmailAddress => $$.'create-test-2@example.com');
27 ok (!$id, $msg);
28
29
30 # Make sure we can't create a second user with the same EmailAddress address
31 my $u3 = RT::User->new(RT->SystemUser);
32 ($id, $msg) = $u3->Create(Name => 'CreateTest2'.$$, EmailAddress => $$.'create-test-1@example.com');
33 ok (!$id, $msg);
34
35 # Make sure we can create a user with no EmailAddress address
36 my $u4 = RT::User->new(RT->SystemUser);
37 ($id, $msg) = $u4->Create(Name => 'CreateTest3'.$$);
38 ok ($id, $msg);
39
40 # make sure we can create a second user with no EmailAddress address
41 my $u5 = RT::User->new(RT->SystemUser);
42 ($id, $msg) = $u5->Create(Name => 'CreateTest4'.$$);
43 ok ($id, $msg);
44
45 # make sure we can create a user with a blank EmailAddress address
46 my $u6 = RT::User->new(RT->SystemUser);
47 ($id, $msg) = $u6->Create(Name => 'CreateTest6'.$$, EmailAddress => '');
48 ok ($id, $msg);
49 # make sure we can create a second user with a blankEmailAddress address
50 my $u7 = RT::User->new(RT->SystemUser);
51 ($id, $msg) = $u7->Create(Name => 'CreateTest7'.$$, EmailAddress => '');
52 ok ($id, $msg);
53
54 # Can we change the email address away from from "";
55 ($id,$msg) = $u7->SetEmailAddress('foo@bar'.$$);
56 ok ($id, $msg);
57 # can we change the address back to "";  
58 ($id,$msg) = $u7->SetEmailAddress('');
59 ok ($id, $msg);
60 is_empty ($u7->EmailAddress);
61
62 # back to something, so we can set undef next successfully
63 ($id,$msg) = $u7->SetEmailAddress('foo@bar'.$$);
64 ok ($id, $msg);
65
66 ($id,$msg) = $u7->SetEmailAddress(undef);
67 ok ($id, $msg);
68 is_empty ($u7->EmailAddress);
69
70 RT->Config->Set('ValidateUserEmailAddresses' => 1);
71 # Make sur we can't create a user with multiple email adresses separated by comma
72 my $u8 = RT::User->new(RT->SystemUser);
73 ($id, $msg) = $u8->Create(Name => 'CreateTest8'.$$, EmailAddress => $$.'create-test-81@example.com, '.$$.'create-test-82@example.com');
74 ok (!$id, $msg);
75
76 # Make sur we can't create a user with multiple email adresses separated by space
77 my $u9 = RT::User->new(RT->SystemUser);
78 ($id, $msg) = $u9->Create(Name => 'CreateTest9'.$$, EmailAddress => $$.'create-test-91@example.com '.$$.'create-test-92@example.com');
79 ok (!$id, $msg);
80
81 # Make sur we can't create a user with invalid email address
82 my $u10 = RT::User->new(RT->SystemUser);
83 ($id, $msg) = $u10->Create(Name => 'CreateTest10'.$$, EmailAddress => $$.'create-test10}@[.com');
84 ok (!$id, $msg);
85 RT->Config->Set('ValidateUserEmailAddresses' => undef);
86
87 }
88
89 {
90
91
92 ok(my $user = RT::User->new(RT->SystemUser));
93 ok($user->Load('root'), "Loaded user 'root'");
94 ok($user->Privileged, "User 'root' is privileged");
95 ok(my ($v,$m) = $user->SetPrivileged(0));
96 is ($v ,1, "Set unprivileged suceeded ($m)");
97 ok(!$user->Privileged, "User 'root' is no longer privileged");
98 ok(my ($v2,$m2) = $user->SetPrivileged(1));
99 is ($v2 ,1, "Set privileged suceeded ($m2");
100 ok($user->Privileged, "User 'root' is privileged again");
101
102
103 }
104
105 {
106
107 ok(my $u = RT::User->new(RT->SystemUser));
108 ok($u->Load(1), "Loaded the first user");
109 is($u->PrincipalObj->ObjectId , 1, "user 1 is the first principal");
110 is($u->PrincipalObj->PrincipalType, 'User' , "Principal 1 is a user, not a group");
111
112
113 }
114
115 {
116
117 my $root = RT::User->new(RT->SystemUser);
118 $root->Load('root');
119 ok($root->Id, "Found the root user");
120 my $rootq = RT::Queue->new($root);
121 $rootq->Load(1);
122 ok($rootq->Id, "Loaded the first queue");
123
124 ok ($rootq->CurrentUser->HasRight(Right=> 'CreateTicket', Object => $rootq), "Root can create tickets");
125
126 my $new_user = RT::User->new(RT->SystemUser);
127 my ($id, $msg) = $new_user->Create(Name => 'ACLTest'.$$);
128
129 ok ($id, "Created a new user for acl test $msg");
130
131 my $q = RT::Queue->new($new_user);
132 $q->Load(1);
133 ok($q->Id, "Loaded the first queue");
134
135
136 ok (!$q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "Some random user doesn't have the right to create tickets");
137 ok (my ($gval, $gmsg) = $new_user->PrincipalObj->GrantRight( Right => 'CreateTicket', Object => $q), "Granted the random user the right to create tickets");
138 ok ($gval, "Grant succeeded - $gmsg");
139
140
141 ok ($q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "The user can create tickets after we grant him the right");
142 ok ( ($gval, $gmsg) = $new_user->PrincipalObj->RevokeRight( Right => 'CreateTicket', Object => $q), "revoked the random user the right to create tickets");
143 ok ($gval, "Revocation succeeded - $gmsg");
144 ok (!$q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "The user can't create tickets anymore");
145
146
147
148
149
150 # Create a ticket in the queue
151 my $new_tick = RT::Ticket->new(RT->SystemUser);
152 my ($tickid, $tickmsg) = $new_tick->Create(Subject=> 'ACL Test', Queue => 'General');
153 ok($tickid, "Created ticket: $tickid");
154 # Make sure the user doesn't have the right to modify tickets in the queue
155 ok (!$new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
156 # Create a new group
157 my $group = RT::Group->new(RT->SystemUser);
158 $group->CreateUserDefinedGroup(Name => 'ACLTest'.$$);
159 ok($group->Id, "Created a new group Ok");
160 # Grant a group the right to modify tickets in a queue
161 ok(my ($gv,$gm) = $group->PrincipalObj->GrantRight( Object => $q, Right => 'ModifyTicket'),"Granted the group the right to modify tickets");
162 ok($gv,"Grant succeeed - $gm");
163 # Add the user to the group
164 ok( my ($aid, $amsg) = $group->AddMember($new_user->PrincipalId), "Added the member to the group");
165 ok ($aid, "Member added to group: $amsg");
166 # Make sure the user does have the right to modify tickets in the queue
167 ok ($new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can modify the ticket with group membership");
168
169
170 # Remove the user from the group
171 ok( my ($did, $dmsg) = $group->DeleteMember($new_user->PrincipalId), "Deleted the member from the group");
172 ok ($did,"Deleted the group member: $dmsg");
173 # Make sure the user doesn't have the right to modify tickets in the queue
174 ok (!$new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
175
176
177 my $q_as_system = RT::Queue->new(RT->SystemUser);
178 $q_as_system->Load(1);
179 ok($q_as_system->Id, "Loaded the first queue");
180
181 # Create a ticket in the queue
182 my $new_tick2 = RT::Ticket->new(RT->SystemUser);
183 (my $tick2id, $tickmsg) = $new_tick2->Create(Subject=> 'ACL Test 2', Queue =>$q_as_system->Id);
184 ok($tick2id, "Created ticket: $tick2id");
185 is($new_tick2->QueueObj->id, $q_as_system->Id, "Created a new ticket in queue 1");
186
187
188 # make sure that the user can't do this without subgroup membership
189 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
190
191 # Create a subgroup
192 my $subgroup = RT::Group->new(RT->SystemUser);
193 $subgroup->CreateUserDefinedGroup(Name => 'Subgrouptest'.$$);
194 ok($subgroup->Id, "Created a new group ".$subgroup->Id."Ok");
195 #Add the subgroup as a subgroup of the group
196 my ($said, $samsg) =  $group->AddMember($subgroup->PrincipalId);
197 ok ($said, "Added the subgroup as a member of the group");
198 # Add the user to a subgroup of the group
199
200 my ($usaid, $usamsg) =  $subgroup->AddMember($new_user->PrincipalId);
201 ok($usaid,"Added the user ".$new_user->Id."to the subgroup");
202 # Make sure the user does have the right to modify tickets in the queue
203 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket with subgroup membership");
204
205 #  {{{ Deal with making sure that members of subgroups of a disabled group don't have rights
206
207 ($id, $msg) =  $group->SetDisabled(1);
208 ok ($id,$msg);
209 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket when the group ".$group->Id. " is disabled");
210  ($id, $msg) =  $group->SetDisabled(0);
211 ok($id,$msg);
212 # Test what happens when we disable the group the user is a member of directly
213
214 ($id, $msg) =  $subgroup->SetDisabled(1);
215  ok ($id,$msg);
216 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket when the group ".$subgroup->Id. " is disabled");
217  ($id, $msg) =  $subgroup->SetDisabled(0);
218  ok ($id,$msg);
219 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket without group membership");
220
221
222
223 my ($usrid, $usrmsg) =  $subgroup->DeleteMember($new_user->PrincipalId);
224 ok($usrid,"removed the user from the group - $usrmsg");
225 # Make sure the user doesn't have the right to modify tickets in the queue
226 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
227
228 #revoke the right to modify tickets in a queue
229 ok(($gv,$gm) = $group->PrincipalObj->RevokeRight( Object => $q, Right => 'ModifyTicket'),"Granted the group the right to modify tickets");
230 ok($gv,"revoke succeeed - $gm");
231
232
233 # Grant queue admin cc the right to modify ticket in the queue 
234 ok(my ($qv,$qm) = $q_as_system->AdminCc->PrincipalObj->GrantRight( Object => $q_as_system, Right => 'ModifyTicket'),"Granted the queue adminccs the right to modify tickets");
235 ok($qv, "Granted the right successfully - $qm");
236
237 # Add the user as a queue admincc
238 ok (my ($add_id, $add_msg) = $q_as_system->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
239 ok ($add_id, "the user is now a queue admincc - $add_msg");
240
241 # Make sure the user does have the right to modify tickets in the queue
242 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
243 # Remove the user from the role  group
244 ok (my ($del_id, $del_msg) = $q_as_system->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
245
246 # Make sure the user doesn't have the right to modify tickets in the queue
247 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
248
249
250
251 # Add the user as a ticket admincc
252 ok (my( $uadd_id, $uadd_msg) = $new_tick2->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
253 ok ($add_id, "the user is now a queue admincc - $add_msg");
254
255 # Make sure the user does have the right to modify tickets in the queue
256 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
257
258 # Remove the user from the role  group
259 ok (( $del_id, $del_msg) = $new_tick2->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
260
261 # Make sure the user doesn't have the right to modify tickets in the queue
262 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
263
264
265 # Revoke the right to modify ticket in the queue 
266 ok(my ($rqv,$rqm) = $q_as_system->AdminCc->PrincipalObj->RevokeRight( Object => $q_as_system, Right => 'ModifyTicket'),"Revokeed the queue adminccs the right to modify tickets");
267 ok($rqv, "Revoked the right successfully - $rqm");
268
269
270
271
272
273 # Before we start Make sure the user does not have the right to modify tickets in the queue
274 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can not modify the ticket without it being granted");
275 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue without it being granted");
276
277 # Grant queue admin cc the right to modify ticket in the queue 
278 ok(($qv,$qm) = $q_as_system->AdminCc->PrincipalObj->GrantRight( Object => $RT::System, Right => 'ModifyTicket'),"Granted the queue adminccs the right to modify tickets");
279 ok($qv, "Granted the right successfully - $qm");
280
281 # Make sure the user can't modify the ticket before they're added as a watcher
282 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can not modify the ticket without being an admincc");
283 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue without being an admincc");
284
285 # Add the user as a queue admincc
286 ok (($add_id, $add_msg) = $q_as_system->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
287 ok ($add_id, "the user is now a queue admincc - $add_msg");
288
289 # Make sure the user does have the right to modify tickets in the queue
290 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
291 ok ($new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can modify tickets in the queue as an admincc");
292 # Remove the user from the role  group
293 ok (($del_id, $del_msg) = $q_as_system->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
294
295 # Make sure the user doesn't have the right to modify tickets in the queue
296 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
297 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can't modify tickets in the queue without group membership");
298
299
300
301 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can not modify the ticket without being an admincc");
302 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue obj without being an admincc");
303
304
305 # Add the user as a ticket admincc
306 ok ( ($uadd_id, $uadd_msg) = $new_tick2->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
307 ok ($add_id, "the user is now a queue admincc - $add_msg");
308
309 # Make sure the user does have the right to modify tickets in the queue
310 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
311 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue obj being only a ticket admincc");
312
313 # Remove the user from the role  group
314 ok ( ($del_id, $del_msg) = $new_tick2->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
315
316 # Make sure the user doesn't have the right to modify tickets in the queue
317 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without being an admincc");
318 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue obj without being an admincc");
319
320
321 # Revoke the right to modify ticket in the queue 
322 ok(($rqv,$rqm) = $q_as_system->AdminCc->PrincipalObj->RevokeRight( Object => $RT::System, Right => 'ModifyTicket'),"Revokeed the queue adminccs the right to modify tickets");
323 ok($rqv, "Revoked the right successfully - $rqm");
324
325
326
327
328
329 # Grant "privileged users" the system right to create users
330 # Create a privileged user.
331 # have that user create another user
332 # Revoke the right for privileged users to create users
333 # have the privileged user try to create another user and fail the ACL check
334
335
336 }
337