fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / ticket_owner.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test nodata => 1, tests => undef;
6
7 my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
8 ok $queue && $queue->id, 'loaded or created queue';
9
10 my $user_a = RT::Test->load_or_create_user(
11     Name => 'user_a', Password => 'password',
12 );
13 ok $user_a && $user_a->id, 'loaded or created user: ' . $user_a->Name;
14
15 my $user_b = RT::Test->load_or_create_user(
16     Name => 'user_b', Password => 'password',
17 );
18 ok $user_b && $user_b->id, 'loaded or created user: ' . $user_b->Name;
19
20 # To give ReassignTicket
21 my $user_c = RT::Test->load_or_create_user(
22     Name => 'user_c', Password => 'password',
23 );
24 ok $user_c && $user_c->id, 'loaded or created user: ' . $user_c->Name;
25
26 my ($baseurl, $agent_a) = RT::Test->started_ok;
27
28 ok( RT::Test->set_rights(
29     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket ReplyToTicket)] },
30     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
31 ), 'set rights');
32
33 ok $agent_a->login('user_a', 'password'), 'logged in as user A';
34
35 diag "current user has no right to own, nobody selected as owner on create";
36 {
37     $agent_a->get_ok('/', 'open home page');
38     $agent_a->form_name('CreateTicketInQueue');
39     $agent_a->select( 'Queue', $queue->id );
40     $agent_a->submit;
41
42     $agent_a->content_contains('Create a new ticket', 'opened create ticket page');
43     my $form = $agent_a->form_name('TicketCreate');
44     is $form->value('Owner'), RT->Nobody->id, 'correct owner selected';
45     ok !grep($_ == $user_a->id, $form->find_input('Owner')->possible_values),
46         'user A can not own tickets';
47     $agent_a->submit;
48
49     $agent_a->content_like(qr/Ticket \d+ created in queue/i, 'created ticket');
50     my ($id) = ($agent_a->content =~ /Ticket (\d+) created in queue/);
51     ok $id, 'found id of the ticket';
52
53     my $ticket = RT::Ticket->new( RT->SystemUser );
54     $ticket->Load( $id );
55     ok $ticket->id, 'loaded the ticket';
56     is $ticket->Owner, RT->Nobody->id, 'correct owner';
57 }
58
59 diag "user can chose owner of a new ticket";
60 {
61     $agent_a->get_ok('/', 'open home page');
62     $agent_a->form_name('CreateTicketInQueue');
63     $agent_a->select( 'Queue', $queue->id );
64     $agent_a->submit;
65
66     $agent_a->content_contains('Create a new ticket', 'opened create ticket page');
67     my $form = $agent_a->form_name('TicketCreate');
68     is $form->value('Owner'), RT->Nobody->id, 'correct owner selected';
69
70     ok grep($_ == $user_b->id,  $form->find_input('Owner')->possible_values),
71         'user B is listed as potential owner';
72     $agent_a->select('Owner', $user_b->id);
73     $agent_a->submit;
74
75     $agent_a->content_like(qr/Ticket \d+ created in queue/i, 'created ticket');
76     my ($id) = ($agent_a->content =~ /Ticket (\d+) created in queue/);
77     ok $id, 'found id of the ticket';
78
79     my $ticket = RT::Ticket->new( RT->SystemUser );
80     $ticket->Load( $id );
81     ok $ticket->id, 'loaded the ticket';
82     is $ticket->Owner, $user_b->id, 'correct owner';
83 }
84
85 my $agent_b = RT::Test::Web->new;
86 ok $agent_b->login('user_b', 'password'), 'logged in as user B';
87
88 diag "user A can not change owner after create";
89 {
90     my $ticket = RT::Ticket->new( $user_a );
91     my ($id, $txn, $msg) = $ticket->Create(
92         Queue => $queue->id,
93         Owner => $user_b->id,
94         Subject => 'test',
95     );
96     ok $id, 'created a ticket #'. $id or diag "error: $msg";
97     is $ticket->Owner, $user_b->id, 'correct owner';
98
99     # try the following group of tests twice with different agents(logins)
100     my $test_cb = sub {
101         my $agent = shift;
102         $agent->get("/Ticket/Modify.html?id=$id");
103         my $form = $agent->form_name('TicketModify');
104         is $form->value('Owner'), $user_b->id, 'correct owner selected';
105         $agent->select('Owner', RT->Nobody->id);
106         $agent->submit;
107
108         $agent->content_contains(
109             'Permission Denied',
110             'no way to change owner after create if you have no rights'
111         );
112
113         my $ticket = RT::Ticket->new( RT->SystemUser );
114         $ticket->Load( $id );
115         ok $ticket->id, 'loaded the ticket';
116         is $ticket->Owner, $user_b->id, 'correct owner';
117     };
118
119     $test_cb->($agent_a);
120     diag "even owner(user B) can not change owner";
121     $test_cb->($agent_b);
122 }
123
124 diag "on reply correct owner is selected";
125 {
126     my $ticket = RT::Ticket->new( $user_a );
127     my ($id, $txn, $msg) = $ticket->Create(
128         Queue => $queue->id,
129         Owner => $user_b->id,
130         Subject => 'test',
131     );
132     ok $id, 'created a ticket #'. $id or diag "error: $msg";
133     is $ticket->Owner, $user_b->id, 'correct owner';
134
135     $agent_a->goto_ticket( $id );
136     $agent_a->follow_link_ok({text => 'Reply'}, 'Ticket -> Reply');
137
138     my $form = $agent_a->form_name('TicketUpdate');
139     is $form->value('Owner'), '', 'empty value selected';
140     $agent_a->submit;
141
142     $ticket = RT::Ticket->new( RT->SystemUser );
143     $ticket->Load( $id );
144     ok $ticket->id, 'loaded the ticket';
145     is $ticket->Owner, $user_b->id, 'correct owner';
146 }
147
148 ok( RT::Test->set_rights(
149     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket)] },
150     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
151 ), 'set rights');
152
153 diag "Couldn't take without coresponding right";
154 {
155     my $ticket = RT::Ticket->new( $user_a );
156     my ($id, $txn, $msg) = $ticket->Create(
157         Queue => $queue->id,
158         Subject => 'test',
159     );
160     ok $id, 'created a ticket #'. $id or diag "error: $msg";
161     is $ticket->Owner, RT->Nobody->id, 'correct owner';
162
163     $agent_a->goto_ticket( $id );
164     ok !($agent_a->find_all_links( text => 'Take' ))[0],
165         'no Take link';
166     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
167         'no Steal link as well';
168 }
169
170 diag "Couldn't steal without coresponding right";
171 {
172     my $ticket = RT::Ticket->new( $user_a );
173     my ($id, $txn, $msg) = $ticket->Create(
174         Queue => $queue->id,
175         Owner => $user_b->id,
176         Subject => 'test',
177     );
178     ok $id, 'created a ticket #'. $id or diag "error: $msg";
179     is $ticket->Owner, $user_b->id, 'correct owner';
180
181     $agent_a->goto_ticket( $id );
182     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
183         'no Steal link';
184     ok !($agent_a->find_all_links( text => 'Take' ))[0],
185         'no Take link as well';
186 }
187
188 ok( RT::Test->set_rights(
189     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket TakeTicket)] },
190 ), 'set rights');
191
192 diag "TakeTicket require OwnTicket to work";
193 {
194     my $ticket = RT::Ticket->new( $user_a );
195     my ($id, $txn, $msg) = $ticket->Create(
196         Queue => $queue->id,
197         Subject => 'test',
198     );
199     ok $id, 'created a ticket #'. $id or diag "error: $msg";
200     is $ticket->Owner, RT->Nobody->id, 'correct owner';
201
202     $agent_a->goto_ticket( $id );
203     ok !($agent_a->find_all_links( text => 'Take' ))[0],
204         'no Take link';
205     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
206         'no Steal link as well';
207 }
208
209 ok( RT::Test->set_rights(
210     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket TakeTicket)] },
211     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
212 ), 'set rights');
213
214 diag "TakeTicket+OwnTicket work";
215 {
216     my $ticket = RT::Ticket->new( $user_a );
217     my ($id, $txn, $msg) = $ticket->Create(
218         Queue => $queue->id,
219         Subject => 'test',
220     );
221     ok $id, 'created a ticket #'. $id or diag "error: $msg";
222     is $ticket->Owner, RT->Nobody->id, 'correct owner';
223
224     $agent_a->goto_ticket( $id );
225     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
226         'no Steal link';
227     $agent_a->follow_link_ok({text => 'Take'}, 'Ticket -> Take');
228
229     $ticket = RT::Ticket->new( RT->SystemUser );
230     $ticket->Load( $id );
231     ok $ticket->id, 'loaded the ticket';
232     is $ticket->Owner, $user_a->id, 'correct owner';
233 }
234
235 diag "TakeTicket+OwnTicket don't work when owner is not nobody";
236 {
237     my $ticket = RT::Ticket->new( $user_a );
238     my ($id, $txn, $msg) = $ticket->Create(
239         Queue => $queue->id,
240         Owner => $user_b->id,
241         Subject => 'test',
242     );
243     ok $id, 'created a ticket #'. $id or diag "error: $msg";
244     is $ticket->Owner, $user_b->id, 'correct owner';
245
246     $agent_a->goto_ticket( $id );
247     ok !($agent_a->find_all_links( text => 'Take' ))[0],
248         'no Take link';
249     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
250         'no Steal link too';
251 }
252
253 ok( RT::Test->set_rights(
254     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket StealTicket)] },
255     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
256 ), 'set rights');
257
258 diag "StealTicket require OwnTicket to work";
259 {
260     my $ticket = RT::Ticket->new( $user_a );
261     my ($id, $txn, $msg) = $ticket->Create(
262         Queue => $queue->id,
263         Owner => $user_b->id,
264         Subject => 'test',
265     );
266     ok $id, 'created a ticket #'. $id or diag "error: $msg";
267     is $ticket->Owner, $user_b->id, 'correct owner';
268
269     $agent_a->goto_ticket( $id );
270     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
271         'no Steal link';
272     ok !($agent_a->find_all_links( text => 'Take' ))[0],
273         'no Take link too';
274 }
275
276 ok( RT::Test->set_rights(
277     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket StealTicket)] },
278     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
279 ), 'set rights');
280
281 diag "StealTicket+OwnTicket work";
282 {
283     my $ticket = RT::Ticket->new( $user_a );
284     my ($id, $txn, $msg) = $ticket->Create(
285         Queue => $queue->id,
286         Owner => $user_b->id,
287         Subject => 'test',
288     );
289     ok $id, 'created a ticket #'. $id or diag "error: $msg";
290     is $ticket->Owner, $user_b->id, 'correct owner';
291
292     $agent_a->goto_ticket( $id );
293     ok !($agent_a->find_all_links( text => 'Take' ))[0],
294         'but no Take link';
295     $agent_a->follow_link_ok({text => 'Steal'}, 'Ticket -> Steal');
296
297     $ticket = RT::Ticket->new( RT->SystemUser );
298     $ticket->Load( $id );
299     ok $ticket->id, 'loaded the ticket';
300     is $ticket->Owner, $user_a->id, 'correct owner';
301 }
302
303 diag "StealTicket+OwnTicket don't work when owner is nobody";
304 {
305     my $ticket = RT::Ticket->new( $user_a );
306     my ($id, $txn, $msg) = $ticket->Create(
307         Queue => $queue->id,
308         Subject => 'test',
309     );
310     ok $id, 'created a ticket #'. $id or diag "error: $msg";
311     is $ticket->Owner, RT->Nobody->id, 'correct owner';
312
313     $agent_a->goto_ticket( $id );
314     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
315         'no Steal link';
316     ok !($agent_a->find_all_links( text => 'Take' ))[0],
317         'no Take link as well (no right)';
318 }
319
320 ok( RT::Test->set_rights(
321     { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket TakeTicket StealTicket)] },
322     { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
323 ), 'set rights');
324
325 diag "no Steal link when owner nobody";
326 {
327     my $ticket = RT::Ticket->new( $user_a );
328     my ($id, $txn, $msg) = $ticket->Create(
329         Queue => $queue->id,
330         Subject => 'test',
331     );
332     ok $id, 'created a ticket #'. $id or diag "error: $msg";
333     is $ticket->Owner, RT->Nobody->id, 'correct owner';
334
335     $agent_a->goto_ticket( $id );
336     ok !($agent_a->find_all_links( text => 'Steal' ))[0],
337         'no Steal link';
338     ok( ($agent_a->find_all_links( text => 'Take' ))[0],
339         'but have Take link');
340 }
341
342 diag "no Take link when owner is not nobody";
343 {
344     my $ticket = RT::Ticket->new( $user_a );
345     my ($id, $txn, $msg) = $ticket->Create(
346         Queue => $queue->id,
347         Owner => $user_b->id,
348         Subject => 'test',
349     );
350     ok $id, 'created a ticket #'. $id or diag "error: $msg";
351     is $ticket->Owner, $user_b->id, 'correct owner';
352
353     $agent_a->goto_ticket( $id );
354     ok !($agent_a->find_all_links( text => 'Take' ))[0],
355         'no Take link';
356     ok( ($agent_a->find_all_links( text => 'Steal' ))[0],
357         'but have Steal link');
358 }
359
360 ok(
361     RT::Test->set_rights(
362         {
363             Principal => $user_a,
364             Right     => [
365                 qw(SeeQueue ShowTicket CreateTicket ReplyToTicket OwnTicket TakeTicket StealTicket)
366             ]
367         },
368         { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
369         { Principal => $user_c, Right => [qw(SeeQueue ShowTicket ReassignTicket)] },
370     ),
371     'set rights'
372 );
373
374 diag
375 "action is Take if old owner is nobody and new owner is current user in update page";
376
377     my $ticket = RT::Ticket->new( $user_a );
378     my ( $id, $txn, $msg ) = $ticket->Create(
379         Queue   => $queue->id,
380         Subject => 'test',
381     );
382     ok $id, 'created a ticket #'. $id or diag "error: $msg";
383     is $ticket->Owner, RT->Nobody->id, 'correct owner';
384
385     $agent_a->goto_ticket( $id );
386     $agent_a->content_lacks('Taken', 'no Taken');
387     $agent_a->follow_link_ok({text => 'Reply'}, 'Ticket -> Reply');
388     $agent_a->submit_form(
389         form_name => 'TicketUpdate',
390         fields    => { Owner => $user_a->id },
391         button => 'SubmitTicket',
392     );
393     like($agent_a->dom->at('.transaction.people .description')->all_text,
394          qr/user_a\s*-\s*Taken/, 'got user_a Taken message' );
395
396     $agent_b->goto_ticket($id);
397     like($agent_b->dom->at('.transaction.people .description')->all_text,
398          qr/user_a\s*-\s*Taken/, 'got user_a Taken message for user b' );
399 }
400
401 diag
402 "action is Take if old owner is nobody and new owner is current user in basics page";
403 {
404     my $ticket = RT::Ticket->new($user_a);
405     my ( $id, $txn, $msg ) = $ticket->Create(
406         Queue   => $queue->id,
407         Subject => 'test',
408     );
409     ok $id, 'created a ticket #' . $id or diag "error: $msg";
410     is $ticket->Owner, RT->Nobody->id, 'correct owner';
411
412     $agent_a->goto_ticket($id);
413     $agent_a->content_lacks('Taken', 'no Taken');
414     $agent_a->follow_link_ok( { text => 'Basics' }, 'Ticket -> Basics' );
415     $agent_a->submit_form(
416         form_name => 'TicketModify',
417         fields    => { Owner => $user_a->id },
418     );
419     $agent_a->content_contains( 'Owner changed from Nobody to user_a',
420         'got set message in Basics' );
421     $agent_a->goto_ticket($id);
422     like($agent_a->dom->at('.transaction.people .description')->all_text,
423          qr/user_a\s*-\s*Taken/, 'got user_a Taken message' );
424
425     $agent_b->goto_ticket($id);
426     like($agent_b->dom->at('.transaction.people .description')->all_text,
427          qr/user_a\s*-\s*Taken/, 'got user_a Taken message for user b' );
428 }
429
430 my $agent_c = RT::Test::Web->new;
431 ok $agent_c->login('user_c', 'password'), 'logged in as user C';
432
433 diag "user can assign ticket to new owner with ReassignTicket right";
434 {
435     my $ticket = RT::Ticket->new($user_a);
436     my ( $id, $txn, $msg ) = $ticket->Create(
437         Queue   => $queue->id,
438         Subject => 'test',
439     );
440     ok $id, 'created a ticket #' . $id or diag "error: $msg";
441     is $ticket->Owner, RT->Nobody->id, 'correct owner';
442
443     $agent_c->goto_ticket($id);
444     ok !($agent_c->find_all_links( text => 'Take' ))[0], 'no Take link';
445     ok !($agent_c->find_all_links( text => 'Steal' ))[0], 'no Steal link';
446
447     $agent_a->goto_ticket($id);
448     $agent_a->content_lacks('Taken', 'no Taken');
449     $agent_a->follow_link_ok( { text => 'Basics' }, 'Ticket -> Basics' );
450     $agent_a->submit_form(
451         form_name => 'TicketModify',
452         fields    => { Owner => $user_a->id },
453     );
454     $agent_a->content_contains( 'Owner changed from Nobody to user_a',
455         'got set message in Basics' );
456     $agent_a->goto_ticket($id);
457     like($agent_a->dom->at('.transaction.people .description')->all_text,
458          qr{user_a\s*-\s*Taken}, 'got user_a Taken message' );
459
460     $agent_c->goto_ticket($id);
461     ok !($agent_c->find_all_links( text => 'Take' ))[0], 'no Take link';
462     ok !($agent_c->find_all_links( text => 'Steal' ))[0], 'no Steal link';
463     $agent_c->follow_link_ok( { text => 'Basics' }, 'Ticket -> Basics' );
464     my $form = $agent_c->form_name('TicketModify');
465     is $form->value('Owner'), $user_a->id, 'correct owner selected';
466
467     ok grep($_ == $user_b->id,  $form->find_input('Owner')->possible_values),
468         'user B is listed as potential owner';
469     $agent_c->select('Owner', $user_b->id);
470     $agent_c->submit;
471     $agent_c->content_contains( 'Owner changed from user_a to user_b',
472         'got set message in Basics' );
473     $agent_c->goto_ticket($id);
474     $agent_c->content_like( qr{Owner forcibly changed}, 'got owner forcibly changed message' );
475     ok !($agent_c->find_all_links( text => 'Take' ))[0], 'no Take link';
476 }
477
478 ok(
479     RT::Test->add_rights(
480         { Principal => $user_c, Right => [qw(OwnTicket)] },
481     ),
482     'add rights'
483 );
484 diag "user can take/steal ticket with ReassignTicket+OwnTicket right";
485 {
486     my $ticket = RT::Ticket->new($user_a);
487     my ( $id, $txn, $msg ) = $ticket->Create(
488         Queue   => $queue->id,
489         Subject => 'test',
490     );
491     ok $id, 'created a ticket #' . $id or diag "error: $msg";
492     is $ticket->Owner, RT->Nobody->id, 'correct owner';
493
494     $agent_c->goto_ticket($id);
495     ok( ($agent_c->find_all_links( text => 'Take' ))[0], 'has Take link' );
496     ok !($agent_c->find_all_links( text => 'Steal' ))[0], 'no Steal link';
497
498     $agent_a->goto_ticket($id);
499     $agent_a->content_lacks('Taken', 'no Taken');
500     $agent_a->follow_link_ok( { text => 'Basics' }, 'Ticket -> Basics' );
501     $agent_a->submit_form(
502         form_name => 'TicketModify',
503         fields    => { Owner => $user_a->id },
504     );
505     $agent_a->content_contains( 'Owner changed from Nobody to user_a',
506         'got set message in Basics' );
507     $agent_a->goto_ticket($id);
508     like($agent_a->dom->at('.transaction.people .description')->all_text,
509          qr{user_a\s*-\s*Taken}, 'got user_a Taken message' );
510
511     $agent_c->goto_ticket($id);
512     ok !($agent_c->find_all_links( text => 'Take' ))[0], 'no Take link';
513     ok( ($agent_c->find_all_links( text => 'Steal' ))[0], 'has Steal link' );
514     $agent_c->follow_link_ok( { text => 'Steal' }, 'Ticket -> Steal' );
515     $agent_c->content_contains( 'Owner changed from user_a to user_c', 'steal message' );
516     ok !($agent_c->find_all_links( text => 'Take' ))[0], 'no Take link';
517     ok !($agent_c->find_all_links( text => 'Steal' ))[0], 'no Steal link';
518 }
519
520
521 undef $agent_a;
522 undef $agent_b;
523 undef $agent_c;
524 done_testing;