5 #use Test::More qw/no_plan/;
6 use Test::More tests => 202;
15 my $rt_tool_path = "$RT::BinPath/rt";
17 # {{{ test configuration options
21 # - server <URL> URL to RT server.
22 # - user <username> RT username.
23 # - passwd <passwd> RT user's password.
24 # - query <RT Query> Default RT Query for list action
25 # - orderby <order> Default RT order for list action
27 # Blank and #-commented lines are ignored.
29 # environment variables
30 # The following environment variables override any corresponding
31 # values defined in configuration files:
34 $ENV{'RTUSER'} = 'root';
36 $ENV{'RTPASSWD'} = 'password';
38 $RT::Logger->debug("Connecting to server at $RT::WebBaseURL...");
39 $ENV{'RTSERVER'} = $RT::WebBaseURL;
40 # - RTDEBUG Numeric debug level. (Set to 3 for full logs.)
41 $ENV{'RTDEBUG'} = '1';
42 # - RTCONFIG Specifies a name other than ".rtrc" for the
45 # - RTQUERY Default RT Query for rt list
46 # - RTORDERBY Default order for rt list
51 # {{{ test ticket manipulation
55 command => "$rt_tool_path shell",
59 expect_send(q{create -t ticket set subject='new ticket' add cc=foo@example.com}, "Creating a ticket...");
60 expect_like(qr/Ticket \d+ created/, "Created the ticket");
61 expect_handle->before() =~ /Ticket (\d+) created/;
63 ok($ticket_id, "Got ticket id=$ticket_id");
64 expect_send(q{create -t ticket set subject='new ticket'}, "Creating a ticket as just a subject...");
65 expect_like(qr/Ticket \d+ created/, "Created the ticket");
67 # make sure we can request things as 'rt foo'
68 expect_send(q{rt create -t ticket set subject='rt ticket'}, "Creating a ticket with 'rt create'...");
69 expect_like(qr/Ticket \d+ created/, "Created the ticket");
71 # {{{ test queue manipulation
74 expect_send("create -t queue set Name='NewQueue$$'", 'Creating a queue...');
75 expect_like(qr/Queue \d+ created/, 'Created the queue');
76 expect_handle->before() =~ /Queue (\d+) created/;
78 ok($queue_id, "Got queue id=$queue_id");
80 expect_send("edit queue/$queue_id set Name='EditedQueue$$'", 'Editing the queue');
81 expect_like(qr/Queue $queue_id updated/, 'Edited the queue');
82 expect_send("show queue/$queue_id", 'Showing the queue...');
83 expect_like(qr/id: queue\/$queue_id/, 'Saw the queue');
84 expect_like(qr/Name: EditedQueue$$/, 'Saw the modification');
86 todo_skip "Listing non-ticket items doesn't work", 2;
87 expect_send("list -t queue 'id > 0'", 'Listing the queues...');
88 expect_like(qr/$queue_id: EditedQueue$$/, 'Found the queue');
93 # add a comment to ticket
94 expect_send("comment -m 'comment-$$' $ticket_id", "Adding a comment...");
95 expect_like(qr/Message recorded/, "Added the comment");
96 ### should test to make sure it actually got added
97 # add correspondance to ticket (?)
98 expect_send("correspond -m 'correspond-$$' $ticket_id", "Adding correspondence...");
99 expect_like(qr/Message recorded/, "Added the correspondence");
100 ### should test to make sure it actually got added
102 # add attachments to a ticket
104 check_attachment("$RT::BasePath/lib/t/data/lorem-ipsum");
106 check_attachment($RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif');
108 # change a ticket's Owner
109 expect_send("edit ticket/$ticket_id set owner=root", 'Changing owner...');
110 expect_like(qr/Ticket $ticket_id updated/, 'Changed owner');
111 expect_send("show ticket/$ticket_id -f owner", 'Verifying change...');
112 expect_like(qr/Owner: root/, 'Verified change');
113 # change a ticket's Requestor
114 expect_send("edit ticket/$ticket_id set requestors=foo\@example.com", 'Changing Requestor...');
115 expect_like(qr/Ticket $ticket_id updated/, 'Changed Requestor');
116 expect_send("show ticket/$ticket_id -f requestors", 'Verifying change...');
117 expect_like(qr/Requestors: foo\@example.com/, 'Verified change');
118 # change a ticket's Cc
119 expect_send("edit ticket/$ticket_id set cc=bar\@example.com", 'Changing Cc...');
120 expect_like(qr/Ticket $ticket_id updated/, 'Changed Cc');
121 expect_send("show ticket/$ticket_id -f cc", 'Verifying change...');
122 expect_like(qr/Cc: bar\@example.com/, 'Verified change');
123 # change a ticket's priority
124 expect_send("edit ticket/$ticket_id set priority=10", 'Changing priority...');
125 expect_like(qr/Ticket $ticket_id updated/, 'Changed priority');
126 expect_send("show ticket/$ticket_id -f priority", 'Verifying change...');
127 expect_like(qr/Priority: 10/, 'Verified change');
128 # move a ticket to a different queue
129 expect_send("edit ticket/$ticket_id set queue=EditedQueue$$", 'Changing queue...');
130 expect_like(qr/Ticket $ticket_id updated/, 'Changed queue');
131 expect_send("show ticket/$ticket_id -f queue", 'Verifying change...');
132 expect_like(qr/Queue: EditedQueue$$/, 'Verified change');
133 # cannot move ticket to a nonexistent queue
134 expect_send("edit ticket/$ticket_id set queue=nonexistent-$$", 'Changing to nonexistent queue...');
135 expect_like(qr/queue does not exist/i, 'Errored out');
136 expect_send("show ticket/$ticket_id -f queue", 'Verifying lack of change...');
137 expect_like(qr/Queue: EditedQueue$$/, 'Verified lack of change');
139 # change a ticket's ...[other properties]...
142 expect_send("edit ticket/$ticket_id set status=stalled", 'Changing status to "stalled"...');
143 expect_like(qr/Ticket $ticket_id updated/, 'Changed status');
144 expect_send("show ticket/$ticket_id -f status", 'Verifying change...');
145 expect_like(qr/Status: stalled/, 'Verified change');
147 expect_send("edit ticket/$ticket_id set status=resolved", 'Changing status to "resolved"...');
148 expect_like(qr/Ticket $ticket_id updated/, 'Changed status');
149 expect_send("show ticket/$ticket_id -f status", 'Verifying change...');
150 expect_like(qr/Status: resolved/, 'Verified change');
151 # try to set status to an illegal value
152 expect_send("edit ticket/$ticket_id set status=quux", 'Changing status to an illegal value...');
153 expect_like(qr/illegal value/i, 'Errored out');
154 expect_send("show ticket/$ticket_id -f status", 'Verifying lack of change...');
155 expect_like(qr/Status: resolved/, 'Verified change');
162 expect_send("ls -s -t ticket -o +id \"Status='resolved'\"", 'Listing resolved tickets...');
163 expect_like(qr/$ticket_id: new ticket/, 'Found our ticket');
164 # show ticket list verbosely
165 expect_send("ls -l -t ticket -o +id \"Status='resolved'\"", 'Listing resolved tickets verbosely...');
166 expect_like(qr/id: ticket\/$ticket_id/, 'Found our ticket');
168 expect_send("show -t ticket $ticket_id", 'Showing our ticket...');
169 expect_like(qr/id: ticket\/$ticket_id/, 'Got our ticket');
170 # show ticket history
171 expect_send("show ticket/$ticket_id/history", 'Showing our ticket\'s history...');
172 expect_like(qr/Ticket created by root/, 'Got our history');
174 local $TODO = "Cannot show verbose ticket history right now";
175 # show ticket history verbosely
176 expect_send("show -v ticket/$ticket_id/history", 'Showing our ticket\'s history verbosely...');
177 expect_like(qr/Ticket created by root/, 'Got our history');
179 # get attachments from a ticket
180 expect_send("show ticket/$ticket_id/attachments", 'Showing ticket attachments...');
181 expect_like(qr/id: ticket\/$ticket_id\/attachments/, 'Got our ticket\'s attachments');
182 expect_like(qr/Attachments: \d+:\s*\(\S+ \/ \d+\w+\)/, 'Our ticket has an attachment');
183 expect_handle->before() =~ /Attachments: (\d+):\s*\((\S+)/;
184 my $attachment_id = $1;
185 my $attachment_type = $2;
186 ok($attachment_id, "Got attachment id=$attachment_id $attachment_type");
187 expect_send("show ticket/$ticket_id/attachments/$attachment_id", "Showing attachment $attachment_id...");
188 expect_like(qr/ContentType: $attachment_type/, 'Got the attachment');
192 # {{{ test user manipulation
195 expect_send("create -t user set Name='NewUser$$' EmailAddress='fbar$$\@example.com'", 'Creating a user...');
196 expect_like(qr/User \d+ created/, 'Created the user');
197 expect_handle->before() =~ /User (\d+) created/;
199 ok($user_id, "Got user id=$user_id");
201 expect_send("edit user/$user_id set Name='EditedUser$$'", 'Editing the user');
202 expect_like(qr/User $user_id updated/, 'Edited the user');
203 expect_send("show user/$user_id", 'Showing the user...');
204 expect_like(qr/id: user\/$user_id/, 'Saw the user');
205 expect_like(qr/Name: EditedUser$$/, 'Saw the modification');
207 todo_skip "Listing non-ticket items doesn't work", 2;
208 expect_send("list -t user 'id > 0'", 'Listing the users...');
209 expect_like(qr/$user_id: EditedUser$$/, 'Found the user');
214 # {{{ test group manipulation
217 todo_skip "Group manipulation doesn't work right now", 8;
219 expect_send("create -t group set Name='NewGroup$$'", 'Creating a group...');
220 expect_like(qr/Group \d+ created/, 'Created the group');
221 expect_handle->before() =~ /Group (\d+) created/;
223 ok($group_id, "Got group id=$group_id");
225 expect_send("edit group/$group_id set Name='EditedGroup$$'", 'Editing the group');
226 expect_like(qr/Group $group_id updated/, 'Edited the group');
227 expect_send("show group/$group_id", 'Showing the group...');
228 expect_like(qr/id: group\/$group_id/, 'Saw the group');
229 expect_like(qr/Name: EditedGroup$$/, 'Saw the modification');
231 local $TODO = "Listing non-ticket items doesn't work";
232 expect_send("list -t group 'id > 0'", 'Listing the groups...');
233 expect_like(qr/$group_id: EditedGroup$$/, 'Found the group');
240 todo_skip "Custom field manipulation not yet implemented", 8;
241 # {{{ test custom field manipulation
243 # creating custom fields
244 expect_send("create -t custom_field set Name='NewCF$$'", 'Creating a custom field...');
245 expect_like(qr/Custom Field \d+ created/, 'Created the custom field');
246 expect_handle->before() =~ /Custom Field (\d+) created/;
248 ok($cf_id, "Got custom field id=$cf_id");
249 # updating custom fields
250 expect_send("edit cf/$cf_id set Name='EditedCF$$'", 'Editing the custom field');
251 expect_like(qr/Custom field $cf_id updated/, 'Edited the custom field');
252 expect_send("show cf/$cf_id", 'Showing the queue...');
253 expect_like(qr/id: custom_field\/$cf_id/, 'Saw the custom field');
254 expect_like(qr/Name: EditedCF$$/, 'Saw the modification');
256 todo_skip "Listing non-ticket items doesn't work", 2;
257 expect_send("list -t custom_field 'id > 0'", 'Listing the CFs...');
258 expect_like(qr/$cf_id: EditedCF$$/, 'Found the custom field');
264 # {{{ test merging tickets
265 expect_send("create -t ticket set subject='CLIMergeTest1-$$'", 'Creating first ticket to merge...');
266 expect_like(qr/Ticket \d+ created/, 'Created first ticket');
267 expect_handle->before() =~ /Ticket (\d+) created/;
268 my $merge_ticket_A = $1;
269 ok($merge_ticket_A, "Got first ticket to merge id=$merge_ticket_A");
270 expect_send("create -t ticket set subject='CLIMergeTest2-$$'", 'Creating second ticket to merge...');
271 expect_like(qr/Ticket \d+ created/, 'Created second ticket');
272 expect_handle->before() =~ /Ticket (\d+) created/;
273 my $merge_ticket_B = $1;
274 ok($merge_ticket_B, "Got second ticket to merge id=$merge_ticket_B");
275 expect_send("merge $merge_ticket_B $merge_ticket_A", 'Merging the tickets...');
276 expect_like(qr/Merge completed/, 'Merged the tickets');
277 expect_send("show ticket/$merge_ticket_A/history", 'Checking merge on first ticket');
278 expect_like(qr/Merged into ticket #$merge_ticket_A by root/, 'Merge recorded in first ticket');
279 expect_send("show ticket/$merge_ticket_B/history", 'Checking merge on second ticket');
280 expect_like(qr/Merged into ticket #$merge_ticket_A by root/, 'Merge recorded in second ticket');
283 # {{{ test taking/stealing tickets
285 # create a user; give them privileges to take and steal
286 ### TODO: implement 'grant' in the CLI tool; use that here instead.
287 ### this breaks the abstraction barrier, like, a lot.
288 my $steal_user = RT::User->new($RT::SystemUser);
289 my ($steal_user_id, $msg) = $steal_user->Create( Name => "fooser$$",
290 EmailAddress => "fooser$$\@localhost",
292 Password => 'foobar',
294 ok($steal_user_id, "Created the user? $msg");
295 my $steal_queue = RT::Queue->new($RT::SystemUser);
297 ($steal_queue_id, $msg) = $steal_queue->Create( Name => "Steal$$" );
298 ok($steal_queue_id, "Got the queue? $msg");
299 ok($steal_queue->id, "queue obj has id");
301 ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'ShowTicket', Object => $steal_queue );
302 ok($status, "Gave 'SeeTicket' to our user? $msg");
303 ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'OwnTicket', Object => $steal_queue );
304 ok($status, "Gave 'OwnTicket' to our user? $msg");
305 ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'StealTicket', Object => $steal_queue );
306 ok($status, "Gave 'StealTicket' to our user? $msg");
307 ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'TakeTicket', Object => $steal_queue );
308 ok($status, "Gave 'TakeTicket' to our user? $msg");
310 # create a ticket to take/steal
311 expect_send("create -t ticket set queue=$steal_queue_id subject='CLIStealTest-$$'", 'Creating ticket to steal...');
312 expect_like(qr/Ticket \d+ created/, 'Created ticket');
313 expect_handle->before() =~ /Ticket (\d+) created/;
314 my $steal_ticket_id = $1;
315 ok($steal_ticket_id, "Got ticket to steal id=$steal_ticket_id");
317 # root takes the ticket
318 expect_send("take $steal_ticket_id", 'root takes the ticket...');
319 expect_like(qr/Owner changed from Nobody to root/, 'root took the ticket');
321 # log in as the non-root user
322 #expect_quit(); # this is apparently unnecessary, but I'll leave it in
324 $ENV{'RTUSER'} = "fooser$$";
325 $ENV{'RTPASSWD'} = 'foobar';
326 expect_run( command => "$rt_tool_path shell", prompt => 'rt> ', quit => 'quit',);
328 # user tries to take the ticket, fails
329 # shouldn't be able to 'take' a ticket which someone else has taken out from
330 # under you; that should produce an error. should have to explicitly
331 # 'steal' it back from them. 'steal' can automatically 'take' a ticket,
333 expect_send("take $steal_ticket_id", 'user tries to take the ticket...');
334 expect_like(qr/You can only take tickets that are unowned/, '...and fails.');
335 expect_send("show ticket/$steal_ticket_id -f owner", 'Double-checking...');
336 expect_like(qr/Owner: root/, '...no change.');
338 # user steals the ticket
339 expect_send("steal $steal_ticket_id", 'user tries to *steal* the ticket...');
340 expect_like(qr/Owner changed from root to fooser$$/, '...and succeeds!');
341 expect_send("show ticket/$steal_ticket_id -f owner", 'Double-checking...');
342 expect_like(qr/Owner: fooser$$/, '...yup, it worked.');
344 # log back in as root
345 #expect_quit(); # ditto
346 $ENV{'RTUSER'} = 'root';
347 $ENV{'RTPASSWD'} = 'password';
348 expect_run( command => "$rt_tool_path shell", prompt => 'rt> ', quit => 'quit',);
350 # root steals the ticket back
351 expect_send("steal $steal_ticket_id", 'root steals the ticket back...');
352 expect_like(qr/Owner changed from fooser$$ to root/, '...and succeeds.');
356 # {{{ test ticket linking
357 my @link_relns = ( 'DependsOn', 'DependedOnBy', 'RefersTo', 'ReferredToBy',
358 'MemberOf', 'HasMember', );
359 my %display_relns = map { $_ => $_ } @link_relns;
360 $display_relns{HasMember} = 'Members';
362 my $link1_id = ok_create_ticket( "LinkTicket1-$$" );
363 my $link2_id = ok_create_ticket( "LinkTicket2-$$" );
365 foreach my $reln (@link_relns) {
367 expect_send("link $link1_id $reln $link2_id", "Link by $reln...");
368 expect_like(qr/Created link $link1_id $reln $link2_id/, 'Linked');
369 expect_send("show ticket/$link1_id/links", "Checking creation of $reln...");
370 expect_like(qr/$display_relns{reln}: [\w\d\.\-]+:\/\/[\w\d\.]+\/ticket\/$link2_id/, "Created link $reln");
373 expect_send("link -d $link1_id $reln $link2_id", "Delete $reln...");
374 expect_like(qr/Deleted link $link1_id $reln $link2_id/, 'Deleted');
375 expect_send("show ticket/$link1_id/links", "Checking removal of $reln...");
376 ok( expect_handle->before() !~ /\Q$display_relns{$reln}: \E[\w\d\.\-]+:\/\/[w\d\.]+\/ticket\/$link2_id/, "Removed link $reln" );
377 #expect_unlike(qr/\Q$reln: \E[\w\d\.]+\Q://\E[w\d\.]+\/ticket\/$link2_id/, "Removed link $reln");
384 sub ok_create_ticket {
387 expect_send("create -t ticket set subject='$subject'", 'Creating ticket...');
388 expect_like(qr/Ticket \d+ created/, "Created ticket '$subject'");
389 expect_handle->before() =~ /Ticket (\d+) created/;
391 ok($id, "Got ticket id=$id");
396 # wrap up all the file handling stuff for attachment testing
397 sub check_attachment {
398 my $attachment_path = shift;
399 (my $filename = $attachment_path) =~ s/.*\/(.*?)$/$1/;
400 expect_send("comment -m 'attach file' -a $attachment_path $ticket_id", "Adding an attachment ($filename)");
401 expect_like(qr/Message recorded/, "Added the attachment");
402 expect_send("show ticket/$ticket_id/attachments","Finding Attachment");
403 my $attachment_regex = qr/(\d+):\s+$filename/;
404 expect_like($attachment_regex,"Attachment Uploaded");
405 expect_handle->before() =~ $attachment_regex;
406 my $attachment_id = $1;
407 expect_send("show ticket/$ticket_id/attachments/$attachment_id/content","Fetching Attachment");
408 open (my $fh, $attachment_path) or die "Can't open $attachment_path: $!";
409 my $attachment_content = do { local($/); <$fh> };
411 chomp $attachment_content;
412 expect_is($attachment_content,"Attachment contains original text");