import rt 3.6.6
[freeside.git] / rt / lib / t / regression / 26command_line.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::Expect;
5 #use Test::More qw/no_plan/;
6 use Test::More tests => 218;
7
8 use RT;
9 RT::LoadConfig();
10 RT::Init;
11
12 use RT::User;
13 use RT::Queue;
14
15 my $rt_tool_path = "$RT::BinPath/rt";
16
17 # {{{  test configuration options
18
19 # config directives:
20 #    (in $CWD/.rtrc)
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
26 #
27 #    Blank and #-commented lines are ignored.
28
29 # environment variables
30 #    The following environment variables override any corresponding
31 #    values defined in configuration files:
32 #
33 #    - RTUSER
34 $ENV{'RTUSER'} = 'root';
35 #    - RTPASSWD
36 $ENV{'RTPASSWD'} = 'password';
37 #    - RTSERVER
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
43 #                    configuration file.
44 #
45 #    - RTQUERY       Default RT Query for rt list
46 #    - RTORDERBY     Default order for rt list
47
48
49 # }}}
50
51 # {{{ test ticket manipulation
52
53 # create a ticket
54 expect_run(
55     command => "$rt_tool_path shell",
56     prompt => 'rt> ',
57     quit => 'quit',
58 );
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/;
62 my $ticket_id = $1;
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");
66
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");
70
71 # {{{ test queue manipulation
72
73 # creating queues
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/;
77 my $queue_id = $1;
78 ok($queue_id, "Got queue id=$queue_id");
79 # updating users
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');
85 TODO: { 
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');
89 }
90
91 # }}}
92
93
94 # Set up a custom field for editing tests
95 my $cf = RT::CustomField->new($RT::SystemUser);
96 my ($val,$msg) = $cf->Create(Name => 'MyCF'.$$, Type => 'FreeformSingle', Queue => $queue_id);
97 ok($val,$msg);
98
99 my $othercf = RT::CustomField->new($RT::SystemUser);
100 ($val,$msg) = $othercf->Create(Name => 'My CF'.$$, Type => 'FreeformSingle', Queue => $queue_id);
101 ok($val,$msg);
102
103
104
105 # add a comment to ticket
106     expect_send("comment -m 'comment-$$' $ticket_id", "Adding a comment...");
107     expect_like(qr/Message recorded/, "Added the comment");
108     ### should test to make sure it actually got added
109     # add correspondance to ticket (?)
110     expect_send("correspond -m 'correspond-$$' $ticket_id", "Adding correspondence...");
111     expect_like(qr/Message recorded/, "Added the correspondence");
112     ### should test to make sure it actually got added
113
114     # add attachments to a ticket
115     # text attachment
116     check_attachment("$RT::BasePath/lib/t/data/lorem-ipsum");
117     # binary attachment
118     check_attachment($RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif');
119
120 # change a ticket's Owner
121 expect_send("edit ticket/$ticket_id set owner=root", 'Changing owner...');
122 expect_like(qr/Ticket $ticket_id updated/, 'Changed owner');
123 expect_send("show ticket/$ticket_id -f owner", 'Verifying change...');
124 expect_like(qr/Owner: root/, 'Verified change');
125 # change a ticket's Requestor
126 expect_send("edit ticket/$ticket_id set requestors=foo\@example.com", 'Changing Requestor...');
127 expect_like(qr/Ticket $ticket_id updated/, 'Changed Requestor');
128 expect_send("show ticket/$ticket_id -f requestors", 'Verifying change...');
129 expect_like(qr/Requestors: foo\@example.com/, 'Verified change');
130 # change a ticket's Cc
131 expect_send("edit ticket/$ticket_id set cc=bar\@example.com", 'Changing Cc...');
132 expect_like(qr/Ticket $ticket_id updated/, 'Changed Cc');
133 expect_send("show ticket/$ticket_id -f cc", 'Verifying change...');
134 expect_like(qr/Cc: bar\@example.com/, 'Verified change');
135 # change a ticket's priority
136 expect_send("edit ticket/$ticket_id set priority=10", 'Changing priority...');
137 expect_like(qr/Ticket $ticket_id updated/, 'Changed priority');
138 expect_send("show ticket/$ticket_id -f priority", 'Verifying change...');
139 expect_like(qr/Priority: 10/, 'Verified change');
140 # move a ticket to a different queue
141 expect_send("edit ticket/$ticket_id set queue=EditedQueue$$", 'Changing queue...');
142 expect_like(qr/Ticket $ticket_id updated/, 'Changed queue');
143 expect_send("show ticket/$ticket_id -f queue", 'Verifying change...');
144 expect_like(qr/Queue: EditedQueue$$/, 'Verified change');
145 # cannot move ticket to a nonexistent queue
146 expect_send("edit ticket/$ticket_id set queue=nonexistent-$$", 'Changing to nonexistent queue...');
147 expect_like(qr/queue does not exist/i, 'Errored out');
148 expect_send("show ticket/$ticket_id -f queue", 'Verifying lack of change...');
149 expect_like(qr/Queue: EditedQueue$$/, 'Verified lack of change');
150
151 # Test reading and setting custom fields without spaces
152 expect_send("show ticket/$ticket_id -f CF-myCF$$", 'Checking initial value');
153 expect_like(qr/CF-myCF$$:/i, 'Verified initial empty value');
154 expect_send("edit ticket/$ticket_id set 'CF-myCF$$=VALUE' ", 'Changing CF...');
155 expect_like(qr/Ticket $ticket_id updated/, 'Changed cf');
156 expect_send("show ticket/$ticket_id -f CF-myCF$$", 'Checking new value');
157 expect_like(qr/CF-myCF$$: VALUE/i, 'Verified change');
158 # Test reading and setting custom fields with spaces
159 expect_send("show ticket/$ticket_id -f 'CF-my CF$$'", 'Checking initial value');
160 expect_like(qr/my CF$$:/i, 'Verified change');
161 expect_send("edit ticket/$ticket_id set 'CF-my CF$$=VALUE' ", 'Changing CF...');
162 expect_like(qr/Ticket $ticket_id updated/, 'Changed cf');
163 expect_send("show ticket/$ticket_id -f 'CF-my CF$$'", 'Checking new value');
164 expect_like(qr/my CF$$: VALUE/i, 'Verified change');
165 expect_send("ls 'id = $ticket_id' -f 'CF-my CF$$'", 'Checking new value');
166 expect_like(qr/my CF$$: VALUE/i, 'Verified change');
167
168 # ...
169 # change a ticket's ...[other properties]...
170 # ...
171 # stall a ticket
172 expect_send("edit ticket/$ticket_id set status=stalled", 'Changing status to "stalled"...');
173 expect_like(qr/Ticket $ticket_id updated/, 'Changed status');
174 expect_send("show ticket/$ticket_id -f status", 'Verifying change...');
175 expect_like(qr/Status: stalled/, 'Verified change');
176 # resolve a ticket
177 expect_send("edit ticket/$ticket_id set status=resolved", 'Changing status to "resolved"...');
178 expect_like(qr/Ticket $ticket_id updated/, 'Changed status');
179 expect_send("show ticket/$ticket_id -f status", 'Verifying change...');
180 expect_like(qr/Status: resolved/, 'Verified change');
181 # try to set status to an illegal value
182 expect_send("edit ticket/$ticket_id set status=quux", 'Changing status to an illegal value...');
183 expect_like(qr/illegal value/i, 'Errored out');
184 expect_send("show ticket/$ticket_id -f status", 'Verifying lack of change...');
185 expect_like(qr/Status: resolved/, 'Verified change');
186
187 # }}}
188
189 # {{{ display
190
191 # show ticket list
192 expect_send("ls -s -t ticket -o +id \"Status='resolved'\"", 'Listing resolved tickets...');
193 expect_like(qr/$ticket_id: new ticket/, 'Found our ticket');
194 # show ticket list verbosely
195 expect_send("ls -l -t ticket -o +id \"Status='resolved'\"", 'Listing resolved tickets verbosely...');
196 expect_like(qr/id: ticket\/$ticket_id/, 'Found our ticket');
197 # show ticket
198 expect_send("show -t ticket $ticket_id", 'Showing our ticket...');
199 expect_like(qr/id: ticket\/$ticket_id/, 'Got our ticket');
200 # show ticket history
201 expect_send("show ticket/$ticket_id/history", 'Showing our ticket\'s history...');
202 expect_like(qr/Ticket created by root/, 'Got our history');
203 TODO: {
204     local $TODO = "Cannot show verbose ticket history right now";
205     # show ticket history verbosely
206     expect_send("show -v ticket/$ticket_id/history", 'Showing our ticket\'s history verbosely...');
207     expect_like(qr/Ticket created by root/, 'Got our history');
208 }
209 # get attachments from a ticket
210 expect_send("show ticket/$ticket_id/attachments", 'Showing ticket attachments...');
211 expect_like(qr/id: ticket\/$ticket_id\/attachments/, 'Got our ticket\'s attachments');
212 expect_like(qr/Attachments: \d+:\s*\(\S+ \/ \d+\w+\)/, 'Our ticket has an attachment');
213 expect_handle->before() =~ /Attachments: (\d+):\s*\((\S+)/;
214 my $attachment_id = $1;
215 my $attachment_type = $2;
216 ok($attachment_id, "Got attachment id=$attachment_id $attachment_type");
217 expect_send("show ticket/$ticket_id/attachments/$attachment_id", "Showing attachment $attachment_id...");
218 expect_like(qr/ContentType: $attachment_type/, 'Got the attachment');
219
220 # }}}
221
222 # {{{ test user manipulation
223
224 # creating users
225 expect_send("create -t user set Name='NewUser$$' EmailAddress='fbar$$\@example.com'", 'Creating a user...');
226 expect_like(qr/User \d+ created/, 'Created the user');
227 expect_handle->before() =~ /User (\d+) created/;
228 my $user_id = $1;
229 ok($user_id, "Got user id=$user_id");
230 # updating users
231 expect_send("edit user/$user_id set Name='EditedUser$$'", 'Editing the user');
232 expect_like(qr/User $user_id updated/, 'Edited the user');
233 expect_send("show user/$user_id", 'Showing the user...');
234 expect_like(qr/id: user\/$user_id/, 'Saw the user');
235 expect_like(qr/Name: EditedUser$$/, 'Saw the modification');
236 TODO: { 
237     todo_skip "Listing non-ticket items doesn't work", 2;
238     expect_send("list -t user 'id > 0'", 'Listing the users...');
239     expect_like(qr/$user_id: EditedUser$$/, 'Found the user');
240 }
241
242 # }}}
243
244 # {{{ test group manipulation
245
246 TODO: {
247 todo_skip "Group manipulation doesn't work right now", 8;
248 # creating groups
249 expect_send("create -t group set Name='NewGroup$$'", 'Creating a group...');
250 expect_like(qr/Group \d+ created/, 'Created the group');
251 expect_handle->before() =~ /Group (\d+) created/;
252 my $group_id = $1;
253 ok($group_id, "Got group id=$group_id");
254 # updating groups
255 expect_send("edit group/$group_id set Name='EditedGroup$$'", 'Editing the group');
256 expect_like(qr/Group $group_id updated/, 'Edited the group');
257 expect_send("show group/$group_id", 'Showing the group...');
258 expect_like(qr/id: group\/$group_id/, 'Saw the group');
259 expect_like(qr/Name: EditedGroup$$/, 'Saw the modification');
260 TODO: { 
261     local $TODO = "Listing non-ticket items doesn't work";
262     expect_send("list -t group 'id > 0'", 'Listing the groups...');
263     expect_like(qr/$group_id: EditedGroup$$/, 'Found the group');
264 }
265 }
266
267 # }}}
268
269 TODO: {
270 todo_skip "Custom field manipulation not yet implemented", 8;
271 # {{{ test custom field manipulation
272
273 # creating custom fields
274 expect_send("create -t custom_field set Name='NewCF$$'", 'Creating a custom field...');
275 expect_like(qr/Custom Field \d+ created/, 'Created the custom field');
276 expect_handle->before() =~ /Custom Field (\d+) created/;
277 my $cf_id = $1;
278 ok($cf_id, "Got custom field id=$cf_id");
279 # updating custom fields
280 expect_send("edit cf/$cf_id set Name='EditedCF$$'", 'Editing the custom field');
281 expect_like(qr/Custom field $cf_id updated/, 'Edited the custom field');
282 expect_send("show cf/$cf_id", 'Showing the queue...');
283 expect_like(qr/id: custom_field\/$cf_id/, 'Saw the custom field');
284 expect_like(qr/Name: EditedCF$$/, 'Saw the modification');
285 TODO: { 
286     todo_skip "Listing non-ticket items doesn't work", 2;
287     expect_send("list -t custom_field 'id > 0'", 'Listing the CFs...');
288     expect_like(qr/$cf_id: EditedCF$$/, 'Found the custom field');
289 }
290 }
291
292 # }}}
293
294 # {{{ test merging tickets
295 expect_send("create -t ticket set subject='CLIMergeTest1-$$'", 'Creating first ticket to merge...');
296 expect_like(qr/Ticket \d+ created/, 'Created first ticket');
297 expect_handle->before() =~ /Ticket (\d+) created/;
298 my $merge_ticket_A = $1;
299 ok($merge_ticket_A, "Got first ticket to merge id=$merge_ticket_A");
300 expect_send("create -t ticket set subject='CLIMergeTest2-$$'", 'Creating second ticket to merge...');
301 expect_like(qr/Ticket \d+ created/, 'Created second ticket');
302 expect_handle->before() =~ /Ticket (\d+) created/;
303 my $merge_ticket_B = $1;
304 ok($merge_ticket_B, "Got second ticket to merge id=$merge_ticket_B");
305 expect_send("merge $merge_ticket_B $merge_ticket_A", 'Merging the tickets...');
306 expect_like(qr/Merge completed/, 'Merged the tickets');
307 expect_send("show ticket/$merge_ticket_A/history", 'Checking merge on first ticket');
308 expect_like(qr/Merged into ticket #$merge_ticket_A by root/, 'Merge recorded in first ticket');
309 expect_send("show ticket/$merge_ticket_B/history", 'Checking merge on second ticket');
310 expect_like(qr/Merged into ticket #$merge_ticket_A by root/, 'Merge recorded in second ticket');
311 # }}}
312
313 # {{{ test taking/stealing tickets
314 {
315     # create a user; give them privileges to take and steal
316     ### TODO: implement 'grant' in the CLI tool; use that here instead.
317     ###       this breaks the abstraction barrier, like, a lot.
318     my $steal_user = RT::User->new($RT::SystemUser);
319     my ($steal_user_id, $msg) = $steal_user->Create( Name => "fooser$$",
320                                           EmailAddress => "fooser$$\@localhost",
321                                           Privileged => 1,
322                                           Password => 'foobar',
323                                         );
324     ok($steal_user_id, "Created the user? $msg");
325     my $steal_queue = RT::Queue->new($RT::SystemUser);
326     my $steal_queue_id;
327     ($steal_queue_id, $msg) = $steal_queue->Create( Name => "Steal$$" );
328     ok($steal_queue_id, "Got the queue? $msg");
329     ok($steal_queue->id, "queue obj has id");
330     my $status;
331     ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'ShowTicket', Object => $steal_queue );
332     ok($status, "Gave 'SeeTicket' to our user? $msg");
333     ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'OwnTicket', Object => $steal_queue );
334     ok($status, "Gave 'OwnTicket' to our user? $msg");
335     ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'StealTicket', Object => $steal_queue );
336     ok($status, "Gave 'StealTicket' to our user? $msg");
337     ($status, $msg) = $steal_user->PrincipalObj->GrantRight( Right => 'TakeTicket', Object => $steal_queue );
338     ok($status, "Gave 'TakeTicket' to our user? $msg");
339
340     # create a ticket to take/steal
341     expect_send("create -t ticket set queue=$steal_queue_id subject='CLIStealTest-$$'", 'Creating ticket to steal...');
342     expect_like(qr/Ticket \d+ created/, 'Created ticket');
343     expect_handle->before() =~ /Ticket (\d+) created/;
344     my $steal_ticket_id = $1;
345     ok($steal_ticket_id, "Got ticket to steal id=$steal_ticket_id");
346
347     # root takes the ticket
348     expect_send("take $steal_ticket_id", 'root takes the ticket...');
349     expect_like(qr/Owner changed from Nobody to root/, 'root took the ticket');
350
351     # log in as the non-root user
352     #expect_quit();      # this is apparently unnecessary, but I'll leave it in
353                          # until I'm sure
354     $ENV{'RTUSER'} = "fooser$$";
355     $ENV{'RTPASSWD'} = 'foobar';
356     expect_run( command => "$rt_tool_path shell", prompt => 'rt> ', quit => 'quit',);
357
358     # user tries to take the ticket, fails
359     # shouldn't be able to 'take' a ticket which someone else has taken out from
360     # under you; that should produce an error.  should have to explicitly 
361     # 'steal' it back from them.  'steal' can automatically 'take' a ticket,
362     # though.
363     expect_send("take $steal_ticket_id", 'user tries to take the ticket...');
364     expect_like(qr/You can only take tickets that are unowned/, '...and fails.');
365     expect_send("show ticket/$steal_ticket_id -f owner", 'Double-checking...');
366     expect_like(qr/Owner: root/, '...no change.');
367
368     # user steals the ticket
369     expect_send("steal $steal_ticket_id", 'user tries to *steal* the ticket...');
370     expect_like(qr/Owner changed from root to fooser$$/, '...and succeeds!');
371     expect_send("show ticket/$steal_ticket_id -f owner", 'Double-checking...');
372     expect_like(qr/Owner: fooser$$/, '...yup, it worked.');
373
374     # log back in as root
375     #expect_quit();     # ditto
376     $ENV{'RTUSER'} = 'root';
377     $ENV{'RTPASSWD'} = 'password';
378     expect_run( command => "$rt_tool_path shell", prompt => 'rt> ', quit => 'quit',);
379
380     # root steals the ticket back
381     expect_send("steal $steal_ticket_id", 'root steals the ticket back...');
382     expect_like(qr/Owner changed from fooser$$ to root/, '...and succeeds.');
383 }
384 # }}}
385
386 # {{{ test ticket linking
387     my @link_relns = ( 'DependsOn', 'DependedOnBy', 'RefersTo', 'ReferredToBy',
388                        'MemberOf', 'HasMember', );
389     my %display_relns = map { $_ => $_ } @link_relns;
390     $display_relns{HasMember} = 'Members';
391
392     my $link1_id = ok_create_ticket( "LinkTicket1-$$" );
393     my $link2_id = ok_create_ticket( "LinkTicket2-$$" );
394
395     foreach my $reln (@link_relns) {
396         # create link
397         expect_send("link $link1_id $reln $link2_id", "Link by $reln...");
398         expect_like(qr/Created link $link1_id $reln $link2_id/, 'Linked');
399         expect_send("show ticket/$link1_id/links", "Checking creation of $reln...");
400         expect_like(qr/$display_relns{reln}: [\w\d\.\-]+:\/\/[\w\d\.]+\/ticket\/$link2_id/, "Created link $reln");
401
402         # delete link
403         expect_send("link -d $link1_id $reln $link2_id", "Delete $reln...");
404         expect_like(qr/Deleted link $link1_id $reln $link2_id/, 'Deleted');
405         expect_send("show ticket/$link1_id/links", "Checking removal of $reln...");
406         ok( expect_handle->before() !~ /\Q$display_relns{$reln}: \E[\w\d\.\-]+:\/\/[w\d\.]+\/ticket\/$link2_id/, "Removed link $reln" );
407         #expect_unlike(qr/\Q$reln: \E[\w\d\.]+\Q://\E[w\d\.]+\/ticket\/$link2_id/, "Removed link $reln");
408
409     }
410 # }}}
411
412
413 # helper function
414 sub ok_create_ticket {
415     my $subject = shift;
416
417     expect_send("create -t ticket set subject='$subject'", 'Creating ticket...');
418     expect_like(qr/Ticket \d+ created/, "Created ticket '$subject'");
419     expect_handle->before() =~ /Ticket (\d+) created/;
420     my $id = $1;
421     ok($id, "Got ticket id=$id");
422     
423     return $id;
424 }
425
426 # wrap up all the file handling stuff for attachment testing
427 sub check_attachment {
428     my $attachment_path = shift;
429     (my $filename = $attachment_path) =~ s/.*\/(.*?)$/$1/;
430     expect_send("comment -m 'attach file' -a $attachment_path $ticket_id", "Adding an attachment ($filename)");
431     expect_like(qr/Message recorded/, "Added the attachment");
432     expect_send("show ticket/$ticket_id/attachments","Finding Attachment");
433     my $attachment_regex = qr/(\d+):\s+$filename/;
434     expect_like($attachment_regex,"Attachment Uploaded");
435     expect_handle->before() =~ $attachment_regex;
436     my $attachment_id = $1;
437     expect_send("show ticket/$ticket_id/attachments/$attachment_id/content","Fetching Attachment");
438     open (my $fh, $attachment_path) or die "Can't open $attachment_path: $!";
439     my $attachment_content = do { local($/); <$fh> };
440     close $fh;
441     chomp $attachment_content;
442     expect_is($attachment_content,"Attachment contains original text");
443 }
444
445 1;