import rt 3.6.4
[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 => 202;
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 # 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
101
102     # add attachments to a ticket
103     # text attachment
104     check_attachment("$RT::BasePath/lib/t/data/lorem-ipsum");
105     # binary attachment
106     check_attachment($RT::MasonComponentRoot.'/NoAuth/images/bplogo.gif');
107
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');
138 # ...
139 # change a ticket's ...[other properties]...
140 # ...
141 # stall a ticket
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');
146 # resolve a ticket
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');
156
157 # }}}
158
159 # {{{ display
160
161 # show ticket list
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');
167 # show 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');
173 TODO: {
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');
178 }
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');
189
190 # }}}
191
192 # {{{ test user manipulation
193
194 # creating users
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/;
198 my $user_id = $1;
199 ok($user_id, "Got user id=$user_id");
200 # updating users
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');
206 TODO: { 
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');
210 }
211
212 # }}}
213
214 # {{{ test group manipulation
215
216 TODO: {
217 todo_skip "Group manipulation doesn't work right now", 8;
218 # creating groups
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/;
222 my $group_id = $1;
223 ok($group_id, "Got group id=$group_id");
224 # updating groups
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');
230 TODO: { 
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');
234 }
235 }
236
237 # }}}
238
239 TODO: {
240 todo_skip "Custom field manipulation not yet implemented", 8;
241 # {{{ test custom field manipulation
242
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/;
247 my $cf_id = $1;
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');
255 TODO: { 
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');
259 }
260 }
261
262 # }}}
263
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');
281 # }}}
282
283 # {{{ test taking/stealing tickets
284 {
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",
291                                           Privileged => 1,
292                                           Password => 'foobar',
293                                         );
294     ok($steal_user_id, "Created the user? $msg");
295     my $steal_queue = RT::Queue->new($RT::SystemUser);
296     my $steal_queue_id;
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");
300     my $status;
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");
309
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");
316
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');
320
321     # log in as the non-root user
322     #expect_quit();      # this is apparently unnecessary, but I'll leave it in
323                          # until I'm sure
324     $ENV{'RTUSER'} = "fooser$$";
325     $ENV{'RTPASSWD'} = 'foobar';
326     expect_run( command => "$rt_tool_path shell", prompt => 'rt> ', quit => 'quit',);
327
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,
332     # though.
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.');
337
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.');
343
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',);
349
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.');
353 }
354 # }}}
355
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';
361
362     my $link1_id = ok_create_ticket( "LinkTicket1-$$" );
363     my $link2_id = ok_create_ticket( "LinkTicket2-$$" );
364
365     foreach my $reln (@link_relns) {
366         # create link
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");
371
372         # delete link
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");
378
379     }
380 # }}}
381
382
383 # helper function
384 sub ok_create_ticket {
385     my $subject = shift;
386
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/;
390     my $id = $1;
391     ok($id, "Got ticket id=$id");
392     
393     return $id;
394 }
395
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> };
410     close $fh;
411     chomp $attachment_content;
412     expect_is($attachment_content,"Attachment contains original text");
413 }
414
415 1;