1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
use strict;
use warnings;
use RT::Interface::REST;
use RT::Test tests => undef;
my ($baseurl, $m) = RT::Test->started_ok;
for my $name ("severity", "fu()n:k/") {
my $cf = RT::Test->load_or_create_custom_field(
Name => $name,
Type => 'FreeformMultiple',
Queue => 'General',
);
ok($cf->Id, "created a CustomField");
is($cf->Name, $name, "correct CF name");
}
{
my $cf = RT::Test->load_or_create_custom_field(
Name => 'single',
Type => 'FreeformSingle',
Queue => 'General',
);
ok($cf->Id, "created a CustomField");
}
my $queue = RT::Test->load_or_create_queue(Name => 'General');
ok($queue->Id, "loaded the General queue");
$m->post("$baseurl/REST/1.0/ticket/new", [
user => 'root',
pass => 'password',
format => 'l',
]);
my $text = $m->content;
my @lines = $text =~ m{.*}g;
shift @lines; # header
# CFs aren't in the default ticket form
push @lines, "CF-fu()n:k/: maximum"; # old style
push @lines, "CF.{severity}: explosive"; # new style
$text = join "\n", @lines;
ok($text =~ s/Subject:\s*$/Subject: REST interface/m, "successfully replaced subject");
$m->post("$baseurl/REST/1.0/ticket/edit", [
user => 'root',
pass => 'password',
content => $text,
], Content_Type => 'form-data');
my ($id) = $m->content =~ /Ticket (\d+) created/;
ok($id, "got ticket #$id");
my $ticket = RT::Ticket->new(RT->SystemUser);
$ticket->Load($id);
is($ticket->Id, $id, "loaded the REST-created ticket");
is($ticket->Subject, "REST interface", "subject successfully set");
is($ticket->FirstCustomFieldValue("fu()n:k/"), "maximum", "CF successfully set");
$m->post("$baseurl/REST/1.0/search/ticket", [
user => 'root',
pass => 'password',
query => "id=$id",
fields => "Subject,CF-fu()n:k/,CF.{severity},Status",
]);
# the fields are interpreted server-side a hash (why?), so we can't depend
# on order
for ("id: ticket/1",
"Subject: REST interface",
"CF.{fu()n:k/}: maximum",
"CF.{severity}: explosive",
"Status: new") {
$m->content_contains($_);
}
# Create ticket 2 for testing ticket links
for (2 .. 4) {
$m->post("$baseurl/REST/1.0/ticket/edit", [
user => 'root',
pass => 'password',
content => $text,
], Content_Type => 'form-data');
$m->post(
"$baseurl/REST/1.0/ticket/1/links",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
}
diag "Add one link";
my $link_data = <<'END_LINKS';
id: ticket/1/links
DependsOn: 2
END_LINKS
$m->post(
"$baseurl/REST/1.0/ticket/1/links",
[
user => 'root',
pass => 'password',
content => $link_data,
],
Content_Type => 'form-data',
);
# See what links get reported for ticket 1.
$m->post(
"$baseurl/REST/1.0/ticket/1/links/show",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
# Verify that the link was added correctly.
my $content = form_parse($m->content);
my $depends_on = vsplit($content->[0]->[2]->{DependsOn});
@$depends_on = sort @$depends_on;
like(
$depends_on->[0], qr{/ticket/2$},
"Link to ticket 2 added.",
) or diag("'content' obtained:\n", $m->content);
diag "Add two links";
$link_data = <<'END_LINKS';
id: ticket/1/links
DependsOn: 3,4
END_LINKS
$m->post(
"$baseurl/REST/1.0/ticket/1/links",
[
user => 'root',
pass => 'password',
content => $link_data,
],
Content_Type => 'form-data',
);
# See what links get reported for ticket 1.
$m->post(
"$baseurl/REST/1.0/ticket/1/links/show",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
$content = form_parse($m->content);
$depends_on = vsplit($content->[0]->[2]->{DependsOn});
@$depends_on = sort @$depends_on;
like(
$depends_on->[0], qr{/ticket/3$},
"Link to ticket 3 found",
) or diag("'content' obtained:\n", $m->content);
like(
$depends_on->[1], qr{/ticket/4$},
"Link to ticket 4 found",
) or diag("'content' obtained:\n", $m->content);
$m->post(
"$baseurl/REST/1.0/ticket/2/links/show",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
my ($link) = $m->content =~ m|DependedOnBy:.*ticket/(\d+)|;
is($link, undef, "Link removed from ticket 2") or diag("'content' obtained:\n", $m->content);
$m->post(
"$baseurl/REST/1.0/ticket/3/links/show",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
($link) = $m->content =~ m|DependedOnBy:.*ticket/(\d+)|;
is($link, 1, "Ticket 3 has link to 1.") or diag("'content' obtained:\n", $m->content);
$m->post(
"$baseurl/REST/1.0/ticket/4/links/show",
[
user => 'root',
pass => 'password',
],
Content_Type => 'form-data',
);
($link) = $m->content =~ m|DependedOnBy:.*ticket/(\d+)|;
is($link, 1, "Ticket 4 has link to 1.") or diag("'content' obtained:\n", $m->content);
diag "Test custom fields";
{
$m->post("$baseurl/REST/1.0/ticket/new", [
user => 'root',
pass => 'password',
format => 'l',
]);
my $text = $m->content;
my @lines = $text =~ m{.*}g;
shift @lines; # header
push @lines, "CF.{severity}: explosive";
push @lines, "CF.{severity}: very";
$text = join "\n", @lines;
$m->post("$baseurl/REST/1.0/ticket/edit", [
user => 'root',
pass => 'password',
content => $text,
], Content_Type => 'form-data');
my ($id) = $m->content =~ /Ticket (\d+) created/;
ok($id, "got ticket #$id");
my $ticket = RT::Ticket->new(RT->SystemUser);
$ticket->Load($id);
is($ticket->Id, $id, "loaded the REST-created ticket");
is_deeply(
[sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
["explosive", "very"],
"CF successfully set"
);
$m->post(
"$baseurl/REST/1.0/ticket/show",
[
user => 'root',
pass => 'password',
format => 'l',
id => "ticket/$id",
]
);
$text = $m->content;
$text =~ s/.*?\n\n//;
$text =~ s/\n\n/\n/;
$text =~ s{CF\.\{severity\}:.*\n}{}img;
$text .= "CF.{severity}: explosive, a bit\n";
$m->post(
"$baseurl/REST/1.0/ticket/edit",
[
user => 'root',
pass => 'password',
content => $text,
],
Content_Type => 'form-data'
);
$m->content =~ /Ticket ($id) updated/;
$ticket->Load($id);
is_deeply(
[sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
['a bit', 'explosive'],
"CF successfully set"
);
$m->post(
"$baseurl/REST/1.0/ticket/show",
[
user => 'root',
pass => 'password',
format => 'l',
id => "ticket/$id",
]
);
$text = $m->content;
$text =~ s{CF\.\{severity\}:.*\n}{}img;
$text .= "CF.{severity}:\n";
$m->post(
"$baseurl/REST/1.0/ticket/edit",
[
user => 'root',
pass => 'password',
content => $text,
],
Content_Type => 'form-data'
);
$m->content =~ /Ticket ($id) updated/;
$ticket->Load($id);
is_deeply(
[sort map $_->Content, @{ $ticket->CustomFieldValues("severity")->ItemsArrayRef }],
[],
"CF successfully set"
);
my @txns = map [$_->OldValue, $_->NewValue], grep $_->Type eq 'CustomField',
@{ $ticket->Transactions->ItemsArrayRef };
is_deeply(\@txns, [['very', undef], [undef, 'a bit'], ['explosive', undef], ['a bit', undef]]);
}
{
$m->post("$baseurl/REST/1.0/ticket/new", [
user => 'root',
pass => 'password',
format => 'l',
]);
my $text = $m->content;
my @lines = $text =~ m{.*}g;
shift @lines; # header
push @lines, "CF.{single}: this";
$text = join "\n", @lines;
$m->post("$baseurl/REST/1.0/ticket/edit", [
user => 'root',
pass => 'password',
content => $text,
], Content_Type => 'form-data');
my ($id) = $m->content =~ /Ticket (\d+) created/;
ok($id, "got ticket #$id");
my $ticket = RT::Ticket->new(RT->SystemUser);
$ticket->Load($id);
is($ticket->Id, $id, "loaded the REST-created ticket");
is_deeply(
[sort map $_->Content, @{ $ticket->CustomFieldValues("single")->ItemsArrayRef }],
["this"],
"CF successfully set"
);
$m->post(
"$baseurl/REST/1.0/ticket/show",
[
user => 'root',
pass => 'password',
format => 'l',
id => "ticket/$id",
]
);
$text = $m->content;
$text =~ s{CF\.\{single\}:.*\n}{}img;
$text .= "CF.{single}: that\n";
$m->post(
"$baseurl/REST/1.0/ticket/edit",
[
user => 'root',
pass => 'password',
content => $text,
],
Content_Type => 'form-data'
);
$m->content =~ /Ticket ($id) updated/;
$ticket->Load($id);
is_deeply(
[sort map $_->Content, @{ $ticket->CustomFieldValues("single")->ItemsArrayRef }],
['that'],
"CF successfully set"
);
my @txns = map [$_->OldValue, $_->NewValue], grep $_->Type eq 'CustomField',
@{ $ticket->Transactions->ItemsArrayRef };
is_deeply(\@txns, [['this', 'that']]);
}
done_testing();
|