import rt 3.8.7
[freeside.git] / rt / t / api / user.t
1
2 use strict;
3 use warnings;
4 use RT;
5 use RT::Test tests => 108;
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 RT->Config->Set('ValidateUserEmailAddresses' => 1);
63 # Make sur we can't create a user with multiple email adresses separated by comma
64 my $u8 = RT::User->new($RT::SystemUser);
65 ($id, $msg) = $u8->Create(Name => 'CreateTest8'.$$, EmailAddress => $$.'create-test-81@example.com, '.$$.'create-test-82@example.com');
66 ok (!$id, $msg);
67
68 # Make sur we can't create a user with multiple email adresses separated by space
69 my $u9 = RT::User->new($RT::SystemUser);
70 ($id, $msg) = $u9->Create(Name => 'CreateTest9'.$$, EmailAddress => $$.'create-test-91@example.com '.$$.'create-test-92@example.com');
71 ok (!$id, $msg);
72
73 # Make sur we can't create a user with invalid email address
74 my $u10 = RT::User->new($RT::SystemUser);
75 ($id, $msg) = $u10->Create(Name => 'CreateTest10'.$$, EmailAddress => $$.'create-test10}@[.com');
76 ok (!$id, $msg);
77 RT->Config->Set('ValidateUserEmailAddresses' => undef);
78
79 }
80
81 {
82
83
84 ok(my $user = RT::User->new($RT::SystemUser));
85 ok($user->Load('root'), "Loaded user 'root'");
86 ok($user->Privileged, "User 'root' is privileged");
87 ok(my ($v,$m) = $user->SetPrivileged(0));
88 is ($v ,1, "Set unprivileged suceeded ($m)");
89 ok(!$user->Privileged, "User 'root' is no longer privileged");
90 ok(my ($v2,$m2) = $user->SetPrivileged(1));
91 is ($v2 ,1, "Set privileged suceeded ($m2");
92 ok($user->Privileged, "User 'root' is privileged again");
93
94
95 }
96
97 {
98
99 ok(my $u = RT::User->new($RT::SystemUser));
100 ok($u->Load(1), "Loaded the first user");
101 is($u->PrincipalObj->ObjectId , 1, "user 1 is the first principal");
102 is($u->PrincipalObj->PrincipalType, 'User' , "Principal 1 is a user, not a group");
103
104
105 }
106
107 {
108
109 my $root = RT::User->new($RT::SystemUser);
110 $root->Load('root');
111 ok($root->Id, "Found the root user");
112 my $rootq = RT::Queue->new($root);
113 $rootq->Load(1);
114 ok($rootq->Id, "Loaded the first queue");
115
116 ok ($rootq->CurrentUser->HasRight(Right=> 'CreateTicket', Object => $rootq), "Root can create tickets");
117
118 my $new_user = RT::User->new($RT::SystemUser);
119 my ($id, $msg) = $new_user->Create(Name => 'ACLTest'.$$);
120
121 ok ($id, "Created a new user for acl test $msg");
122
123 my $q = RT::Queue->new($new_user);
124 $q->Load(1);
125 ok($q->Id, "Loaded the first queue");
126
127
128 ok (!$q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "Some random user doesn't have the right to create tickets");
129 ok (my ($gval, $gmsg) = $new_user->PrincipalObj->GrantRight( Right => 'CreateTicket', Object => $q), "Granted the random user the right to create tickets");
130 ok ($gval, "Grant succeeded - $gmsg");
131
132
133 ok ($q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "The user can create tickets after we grant him the right");
134 ok ( ($gval, $gmsg) = $new_user->PrincipalObj->RevokeRight( Right => 'CreateTicket', Object => $q), "revoked the random user the right to create tickets");
135 ok ($gval, "Revocation succeeded - $gmsg");
136 ok (!$q->CurrentUser->HasRight(Right => 'CreateTicket', Object => $q), "The user can't create tickets anymore");
137
138
139
140
141
142 # Create a ticket in the queue
143 my $new_tick = RT::Ticket->new($RT::SystemUser);
144 my ($tickid, $tickmsg) = $new_tick->Create(Subject=> 'ACL Test', Queue => 'General');
145 ok($tickid, "Created ticket: $tickid");
146 # Make sure the user doesn't have the right to modify tickets in the queue
147 ok (!$new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
148 # Create a new group
149 my $group = RT::Group->new($RT::SystemUser);
150 $group->CreateUserDefinedGroup(Name => 'ACLTest'.$$);
151 ok($group->Id, "Created a new group Ok");
152 # Grant a group the right to modify tickets in a queue
153 ok(my ($gv,$gm) = $group->PrincipalObj->GrantRight( Object => $q, Right => 'ModifyTicket'),"Granted the group the right to modify tickets");
154 ok($gv,"Grant succeeed - $gm");
155 # Add the user to the group
156 ok( my ($aid, $amsg) = $group->AddMember($new_user->PrincipalId), "Added the member to the group");
157 ok ($aid, "Member added to group: $amsg");
158 # Make sure the user does have the right to modify tickets in the queue
159 ok ($new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can modify the ticket with group membership");
160
161
162 # Remove the user from the group
163 ok( my ($did, $dmsg) = $group->DeleteMember($new_user->PrincipalId), "Deleted the member from the group");
164 ok ($did,"Deleted the group member: $dmsg");
165 # Make sure the user doesn't have the right to modify tickets in the queue
166 ok (!$new_user->HasRight( Object => $new_tick, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
167
168
169 my $q_as_system = RT::Queue->new($RT::SystemUser);
170 $q_as_system->Load(1);
171 ok($q_as_system->Id, "Loaded the first queue");
172
173 # Create a ticket in the queue
174 my $new_tick2 = RT::Ticket->new($RT::SystemUser);
175 (my $tick2id, $tickmsg) = $new_tick2->Create(Subject=> 'ACL Test 2', Queue =>$q_as_system->Id);
176 ok($tick2id, "Created ticket: $tick2id");
177 is($new_tick2->QueueObj->id, $q_as_system->Id, "Created a new ticket in queue 1");
178
179
180 # make sure that the user can't do this without subgroup membership
181 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
182
183 # Create a subgroup
184 my $subgroup = RT::Group->new($RT::SystemUser);
185 $subgroup->CreateUserDefinedGroup(Name => 'Subgrouptest'.$$);
186 ok($subgroup->Id, "Created a new group ".$subgroup->Id."Ok");
187 #Add the subgroup as a subgroup of the group
188 my ($said, $samsg) =  $group->AddMember($subgroup->PrincipalId);
189 ok ($said, "Added the subgroup as a member of the group");
190 # Add the user to a subgroup of the group
191
192 my ($usaid, $usamsg) =  $subgroup->AddMember($new_user->PrincipalId);
193 ok($usaid,"Added the user ".$new_user->Id."to the subgroup");
194 # Make sure the user does have the right to modify tickets in the queue
195 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket with subgroup membership");
196
197 #  {{{ Deal with making sure that members of subgroups of a disabled group don't have rights
198
199 ($id, $msg) =  $group->SetDisabled(1);
200 ok ($id,$msg);
201 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket when the group ".$group->Id. " is disabled");
202  ($id, $msg) =  $group->SetDisabled(0);
203 ok($id,$msg);
204 # Test what happens when we disable the group the user is a member of directly
205
206 ($id, $msg) =  $subgroup->SetDisabled(1);
207  ok ($id,$msg);
208 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket when the group ".$subgroup->Id. " is disabled");
209  ($id, $msg) =  $subgroup->SetDisabled(0);
210  ok ($id,$msg);
211 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket without group membership");
212
213 # }}}
214
215
216 my ($usrid, $usrmsg) =  $subgroup->DeleteMember($new_user->PrincipalId);
217 ok($usrid,"removed the user from the group - $usrmsg");
218 # Make sure the user doesn't have the right to modify tickets in the queue
219 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
220
221 #revoke the right to modify tickets in a queue
222 ok(($gv,$gm) = $group->PrincipalObj->RevokeRight( Object => $q, Right => 'ModifyTicket'),"Granted the group the right to modify tickets");
223 ok($gv,"revoke succeeed - $gm");
224
225 # {{{ Test the user's right to modify a ticket as a _queue_ admincc for a right granted at the _queue_ level
226
227 # Grant queue admin cc the right to modify ticket in the queue 
228 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");
229 ok($qv, "Granted the right successfully - $qm");
230
231 # Add the user as a queue admincc
232 ok (my ($add_id, $add_msg) = $q_as_system->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
233 ok ($add_id, "the user is now a queue admincc - $add_msg");
234
235 # Make sure the user does have the right to modify tickets in the queue
236 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
237 # Remove the user from the role  group
238 ok (my ($del_id, $del_msg) = $q_as_system->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
239
240 # Make sure the user doesn't have the right to modify tickets in the queue
241 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
242
243 # }}}
244
245 # {{{ Test the user's right to modify a ticket as a _ticket_ admincc with the right granted at the _queue_ level
246
247 # Add the user as a ticket admincc
248 ok (my( $uadd_id, $uadd_msg) = $new_tick2->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
249 ok ($add_id, "the user is now a queue admincc - $add_msg");
250
251 # Make sure the user does have the right to modify tickets in the queue
252 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
253
254 # Remove the user from the role  group
255 ok (( $del_id, $del_msg) = $new_tick2->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
256
257 # Make sure the user doesn't have the right to modify tickets in the queue
258 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
259
260
261 # Revoke the right to modify ticket in the queue 
262 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");
263 ok($rqv, "Revoked the right successfully - $rqm");
264
265 # }}}
266
267
268
269 # {{{ Test the user's right to modify a ticket as a _queue_ admincc for a right granted at the _system_ level
270
271 # Before we start Make sure the user does not have the right to modify tickets in the queue
272 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can not modify the ticket without it being granted");
273 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue without it being granted");
274
275 # Grant queue admin cc the right to modify ticket in the queue 
276 ok(($qv,$qm) = $q_as_system->AdminCc->PrincipalObj->GrantRight( Object => $RT::System, Right => 'ModifyTicket'),"Granted the queue adminccs the right to modify tickets");
277 ok($qv, "Granted the right successfully - $qm");
278
279 # Make sure the user can't modify the ticket before they're added as a watcher
280 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can not modify the ticket without being an admincc");
281 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can not modify tickets in the queue without being an admincc");
282
283 # Add the user as a queue admincc
284 ok (($add_id, $add_msg) = $q_as_system->AddWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Added the new user as a queue admincc");
285 ok ($add_id, "the user is now a queue admincc - $add_msg");
286
287 # Make sure the user does have the right to modify tickets in the queue
288 ok ($new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can modify the ticket as an admincc");
289 ok ($new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can modify tickets in the queue as an admincc");
290 # Remove the user from the role  group
291 ok (($del_id, $del_msg) = $q_as_system->DeleteWatcher(Type => 'AdminCc', PrincipalId => $new_user->PrincipalId)  , "Deleted the new user as a queue admincc");
292
293 # Make sure the user doesn't have the right to modify tickets in the queue
294 ok (!$new_user->HasRight( Object => $new_tick2, Right => 'ModifyTicket'), "User can't modify the ticket without group membership");
295 ok (!$new_user->HasRight( Object => $new_tick2->QueueObj, Right => 'ModifyTicket'), "User can't modify tickets in the queue without group membership");
296
297 # }}}
298
299 # {{{ Test the user's right to modify a ticket as a _ticket_ admincc with the right granted at the _queue_ level
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
330 # Grant "privileged users" the system right to create users
331 # Create a privileged user.
332 # have that user create another user
333 # Revoke the right for privileged users to create users
334 # have the privileged user try to create another user and fail the ACL check
335
336
337 }
338
339 1;