3 use RT::Interface::REST;
5 use RT::Test tests => 34;
7 my ($baseurl, $m) = RT::Test->started_ok;
9 for my $name ("severity", "fu()n:k/") {
10 my $cf = RT::Test->load_or_create_custom_field(
12 Type => 'FreeformMultiple',
15 ok($cf->Id, "created a CustomField");
16 is($cf->Name, $name, "correct CF name");
19 my $cf = RT::Test->load_or_create_custom_field(
21 Type => 'FreeformSingle',
24 ok($cf->Id, "created a CustomField");
27 my $queue = RT::Test->load_or_create_queue(Name => 'General');
28 ok($queue->Id, "loaded the General queue");
30 $m->post("$baseurl/REST/1.0/ticket/new", [
36 my $text = $m->content;
37 my @lines = $text =~ m{.*}g;
38 shift @lines; # header
40 # CFs aren't in the default ticket form
41 push @lines, "CF-fu()n:k/: maximum"; # old style
42 push @lines, "CF.{severity}: explosive"; # new style
44 $text = join "\n", @lines;
46 ok($text =~ s/Subject:\s*$/Subject: REST interface/m, "successfully replaced subject");
48 $m->post("$baseurl/REST/1.0/ticket/edit", [
53 ], Content_Type => 'form-data');
55 my ($id) = $m->content =~ /Ticket (\d+) created/;
56 ok($id, "got ticket #$id");
58 my $ticket = RT::Ticket->new(RT->SystemUser);
60 is($ticket->Id, $id, "loaded the REST-created ticket");
61 is($ticket->Subject, "REST interface", "subject successfully set");
62 is($ticket->FirstCustomFieldValue("fu()n:k/"), "maximum", "CF successfully set");
64 $m->post("$baseurl/REST/1.0/search/ticket", [
68 fields => "Subject,CF-fu()n:k/,CF.{severity},Status",
71 # the fields are interpreted server-side a hash (why?), so we can't depend
74 "Subject: REST interface",
75 "CF.{fu()n:k/}: maximum",
76 "CF.{severity}: explosive",
78 $m->content_contains($_);
81 # Create ticket 2 for testing ticket links
83 $m->post("$baseurl/REST/1.0/ticket/edit", [
87 ], Content_Type => 'form-data');
90 "$baseurl/REST/1.0/ticket/1/links",
95 Content_Type => 'form-data',
98 my $link_data = form_parse($m->content);
100 push @{$link_data->[0]->[1]}, 'DependsOn';
101 vpush($link_data->[0]->[2], 'DependsOn', $_);
104 "$baseurl/REST/1.0/ticket/1/links",
108 content => form_compose($link_data),
110 Content_Type => 'form-data',
115 # See what links get reported for ticket 1.
117 "$baseurl/REST/1.0/ticket/1/links/show",
122 Content_Type => 'form-data',
125 # Verify that the link was added correctly.
126 my $content = form_parse($m->content);
127 my $depends_on = vsplit($content->[0]->[2]->{DependsOn});
128 @$depends_on = sort @$depends_on;
130 $depends_on->[0], qr{/ticket/2$},
131 "Check ticket link.",
132 ) or diag("'content' obtained:\n", $m->content);
135 $depends_on->[1], qr{/ticket/3$},
136 "Check ticket link.",
137 ) or diag("'content' obtained:\n", $m->content);
140 "$baseurl/REST/1.0/ticket/2/links/show",
145 Content_Type => 'form-data',
147 my ($link) = $m->content =~ m|DependedOnBy:.*ticket/(\d+)|;
148 is($link, 1, "Check ticket link.") or diag("'content' obtained:\n", $m->content);
151 "$baseurl/REST/1.0/ticket/3/links/show",
156 Content_Type => 'form-data',
158 ($link) = $m->content =~ m|DependedOnBy:.*ticket/(\d+)|;
159 is($link, 1, "Check ticket link.") or diag("'content' obtained:\n", $m->content);
163 $m->post("$baseurl/REST/1.0/ticket/new", [
169 my $text = $m->content;
170 my @lines = $text =~ m{.*}g;
171 shift @lines; # header
172 push @lines, "CF.{severity}: explosive";
173 push @lines, "CF.{severity}: very";
174 $text = join "\n", @lines;
176 $m->post("$baseurl/REST/1.0/ticket/edit", [
181 ], Content_Type => 'form-data');
183 my ($id) = $m->content =~ /Ticket (\d+) created/;
184 ok($id, "got ticket #$id");
186 my $ticket = RT::Ticket->new(RT->SystemUser);
188 is($ticket->Id, $id, "loaded the REST-created ticket");
190 [sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
191 ["explosive", "very"],
192 "CF successfully set"
196 "$baseurl/REST/1.0/ticket/show",
205 $text =~ s/.*?\n\n//;
207 $text =~ s{CF\.\{severity\}:.*\n}{}img;
208 $text .= "CF.{severity}: explosive, a bit\n";
210 "$baseurl/REST/1.0/ticket/edit",
216 Content_Type => 'form-data'
218 $m->content =~ /Ticket ($id) updated/;
222 [sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
223 ['a bit', 'explosive'],
224 "CF successfully set"
228 "$baseurl/REST/1.0/ticket/show",
237 $text =~ s{CF\.\{severity\}:.*\n}{}img;
238 $text .= "CF.{severity}:\n";
240 "$baseurl/REST/1.0/ticket/edit",
246 Content_Type => 'form-data'
248 $m->content =~ /Ticket ($id) updated/;
252 [sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
254 "CF successfully set"
257 my @txns = map [$_->OldValue, $_->NewValue], grep $_->Type eq 'CustomField',
258 @{ $ticket->Transactions->ItemsArrayRef };
259 is_deeply(\@txns, [['very', undef], [undef, 'a bit'], ['explosive', undef], ['a bit', undef]]);
263 $m->post("$baseurl/REST/1.0/ticket/new", [
269 my $text = $m->content;
270 my @lines = $text =~ m{.*}g;
271 shift @lines; # header
272 push @lines, "CF.{single}: this";
273 $text = join "\n", @lines;
275 $m->post("$baseurl/REST/1.0/ticket/edit", [
280 ], Content_Type => 'form-data');
282 my ($id) = $m->content =~ /Ticket (\d+) created/;
283 ok($id, "got ticket #$id");
285 my $ticket = RT::Ticket->new(RT->SystemUser);
287 is($ticket->Id, $id, "loaded the REST-created ticket");
289 [sort map $_->Content, @{ $ticket->CustomFieldValues("single")->ItemsArrayRef }],
291 "CF successfully set"
295 "$baseurl/REST/1.0/ticket/show",
304 $text =~ s{CF\.\{single\}:.*\n}{}img;
305 $text .= "CF.{single}: that\n";
307 "$baseurl/REST/1.0/ticket/edit",
313 Content_Type => 'form-data'
315 $m->content =~ /Ticket ($id) updated/;
319 [sort map $_->Content, @{ $ticket->CustomFieldValues("single")->ItemsArrayRef }],
321 "CF successfully set"
324 my @txns = map [$_->OldValue, $_->NewValue], grep $_->Type eq 'CustomField',
325 @{ $ticket->Transactions->ItemsArrayRef };
326 is_deeply(\@txns, [['this', 'that']]);