[Bps-public-commit] rt-extension-rest2 branch, rest2-craig, created. c15a681f3a6c28a8be25d4498ce4b1a9133d62ec

Craig Kaiser craig at bestpractical.com
Tue Oct 31 14:53:09 EDT 2017


The branch, rest2-craig has been created
        at  c15a681f3a6c28a8be25d4498ce4b1a9133d62ec (commit)

- Log -----------------------------------------------------------------
commit b019b53d7ff8506c2d8e7daf3303b11edbd66c95
Author: craig <craig at bestpractical.com>
Date:   Mon Oct 30 15:59:42 2017 -0400

    Add examples in doc and test for TicketSQL REST2

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index fdecd03..fe7ad18 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -251,9 +251,12 @@ Wherever possible please consider using C<_hyperlinks> hypermedia
 controls available in response bodies rather than hardcoding URLs.
 
 =head3 Tickets
-
+    Need to use urlencoding for complex commands
     GET /tickets?query=<TicketSQL>
         search for tickets using TicketSQL
+        Ex. /REST/2.0/tickets?query=Status="open"'
+        Ex. /REST/2.0/tickets?query=CF.{MyCustomField}="True"'
+        Ex. curl -G --data-urlencode "query=id=1 AND Status='new'" -u 'root:password' 'myRT...REST/2.0/tickets'
 
     GET /tickets?simple=1;query=<simple search query>
         search for tickets using simple search syntax
diff --git a/t/tickets.t b/t/tickets.t
index ae151c1..797b985 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -32,6 +32,7 @@ my $user = RT::Extension::REST2::Test->user;
     is($mech->json_response->{message}, 'Could not create ticket. Queue not set');
 }
 
+my $queue = RT::Test->load_or_create_queue( Name => "General" );
 # Ticket Creation
 my ($ticket_url, $ticket_id);
 {
@@ -39,7 +40,7 @@ my ($ticket_url, $ticket_id);
         Subject => 'Ticket creation using REST',
         From    => 'test at bestpractical.com',
         To      => 'rt at localhost',
-        Queue   => 'General',
+        Queue   => $queue->Id,
         Content => 'Testing ticket creation using REST API.',
     };
 
@@ -357,4 +358,106 @@ my ($ticket_url, $ticket_id);
     is($content->{ContentType}, 'text/html');
 }
 
+# TicketSQL AND
+diag "Got correct result for  AND TicketSQL";
+{
+    my $res = $mech->get("$rest_base_path/tickets?query=id>0 AND Queue='General'",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    my $content = $mech->json_response;
+    is($content->{count}, 1);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 1);
+    is(scalar @{$content->{items}}, 1);
+
+    my $ticket = $content->{items}->[0];
+    is($ticket->{type}, 'ticket');
+    is($ticket->{id}, 1);
+    like($ticket->{_url}, qr{$rest_base_path/ticket/1$});
+}
+
+# OR TicketSQL
+# Got correct result for "OR TicketSQL"
+{
+  my $res = $mech->get("$rest_base_path/tickets?query=id<0 OR Queue='General'",
+      'Authorization' => $auth,
+  );
+  is($res->code, 200);
+  my $content = $mech->json_response;
+  is($content->{count}, 1);
+  is($content->{page}, 1);
+  is($content->{per_page}, 20);
+  is($content->{total}, 1);
+  is(scalar @{$content->{items}}, 1);
+
+  my $ticket = $content->{items}->[0];
+  is($ticket->{type}, 'ticket');
+  is($ticket->{id}, 1);
+  like($ticket->{_url}, qr{$rest_base_path/ticket/1$});
+}
+
+# Second Queue
+$queue = RT::Test->load_or_create_queue( Name => "Two" );
+
+# Second ticket
+my ($ticket_url2, $ticket_id2);
+{
+    my $payload = {
+        Subject => 'Ticket creation using REST',
+        From    => 'test at bestpractical.com',
+        To      => 'rt at localhost',
+        Queue   => 'two',
+        Content => 'Testing ticket creation using REST API.',
+    };
+
+    # Rights Test - No CreateTicket
+    my $res = $mech->post_json("$rest_base_path/ticket",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    ok($ticket_url2 = $res->header('location'));
+    ok(($ticket_id2) = $ticket_url2 =~ qr[/ticket/(\d+)]);
+}
+
+# Search with Multiple Tickets and Queues
+diag "Got correct result for two tickets two queues";
+{
+    my $res = $mech->get("$rest_base_path/tickets?query=id>0 AND Queue='General'",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    my $content = $mech->json_response;
+    is($content->{count}, 1);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 1);
+    is(scalar @{$content->{items}}, 1);
+
+    my $ticket = $content->{items}->[0];
+    is($ticket->{type}, 'ticket');
+    is($ticket->{id}, 1);
+    like($ticket->{_url}, qr{$rest_base_path/ticket/1$});
+}
+
+# Find both queues tickets
+diag "Got correct result for two tickets two queues";
+{
+    my $res = $mech->get("$rest_base_path/tickets?query=Queue='Two' OR Queue='General'",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    my $content = $mech->json_response;
+    is($content->{count}, 2);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 2);
+    is(scalar @{$content->{items}}, 2);
+
+    my $ticket = $content->{items}->[0];
+    is($ticket->{type}, 'ticket');
+}
+
 done_testing;

commit c15a681f3a6c28a8be25d4498ce4b1a9133d62ec
Author: craig <craig at bestpractical.com>
Date:   Tue Oct 31 14:52:52 2017 -0400

    Add TicketSQL test for incorrect query, more doc examples

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index fe7ad18..26950e5 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -255,8 +255,8 @@ controls available in response bodies rather than hardcoding URLs.
     GET /tickets?query=<TicketSQL>
         search for tickets using TicketSQL
         Ex. /REST/2.0/tickets?query=Status="open"'
-        Ex. /REST/2.0/tickets?query=CF.{MyCustomField}="True"'
-        Ex. curl -G --data-urlencode "query=id=1 AND Status='new'" -u 'root:password' 'myRT...REST/2.0/tickets'
+        Ex. "/REST/2.0/tickets?query=CF.MyCustomField='True'"
+        Ex. curl -G --data-urlencode "query=(Queue='Two' OR Queue='General') AND Subject='Rest2'" -u 'root:password' 'myRT...REST/2.0/tickets'
 
     GET /tickets?simple=1;query=<simple search query>
         search for tickets using simple search syntax
@@ -564,7 +564,15 @@ handle them appropriately.
 
 # XXX TODO: API doc
 
-sub to_psgi_app { shift->to_app(@_) }
+sub to_psgi_app {
+    my $self = shift;
+    my $res = $self->to_app(@_);
+
+    return Plack::Util::response_cb($res, sub {
+        my $res = shift;
+        $self->CleanupRequest;
+    });
+}
 
 sub to_app {
     my $class = shift;
@@ -596,6 +604,24 @@ sub PSGIWrap {
     };
 }
 
+sub CleanupRequest {
+
+    if ( $RT::Handle && $RT::Handle->TransactionDepth ) {
+        $RT::Handle->ForceRollback;
+        $RT::Logger->crit(
+            "Transaction not committed. Usually indicates a software fault."
+            . "Data loss may have occurred" );
+    }
+
+    # Clean out the ACL cache. the performance impact should be marginal.
+    # Consistency is imprived, too.
+    RT::Principal->InvalidateACLCache();
+    DBIx::SearchBuilder::Record::Cachable->FlushCache
+      if ( RT->Config->Get('WebFlushDbCacheEveryRequest')
+        and UNIVERSAL::can(
+            'DBIx::SearchBuilder::Record::Cachable' => 'FlushCache' ) );
+}
+
 =head1 AUTHOR
 
 Best Practical Solutions, LLC <modules at bestpractical.com>
diff --git a/t/tickets.t b/t/tickets.t
index 797b985..3acae19 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -405,7 +405,7 @@ $queue = RT::Test->load_or_create_queue( Name => "Two" );
 my ($ticket_url2, $ticket_id2);
 {
     my $payload = {
-        Subject => 'Ticket creation using REST',
+        Subject => 'Rest2',
         From    => 'test at bestpractical.com',
         To      => 'rt at localhost',
         Queue   => 'two',
@@ -445,19 +445,34 @@ diag "Got correct result for two tickets two queues";
 # Find both queues tickets
 diag "Got correct result for two tickets two queues";
 {
-    my $res = $mech->get("$rest_base_path/tickets?query=Queue='Two' OR Queue='General'",
+    my $res = $mech->get("$rest_base_path/tickets?query=(Queue='Two' OR Queue='General') AND Subject='Rest2'",
         'Authorization' => $auth,
     );
     is($res->code, 200);
     my $content = $mech->json_response;
-    is($content->{count}, 2);
+    is($content->{count}, 1);
     is($content->{page}, 1);
     is($content->{per_page}, 20);
-    is($content->{total}, 2);
-    is(scalar @{$content->{items}}, 2);
+    is($content->{total}, 1);
+    is(scalar @{$content->{items}}, 1);
 
     my $ticket = $content->{items}->[0];
     is($ticket->{type}, 'ticket');
 }
+TODO: {
+    local $TODO = "Have incorrect ticketSQL queries return error or nothing. Currently will return all tickets";
+};
 
+# Malform query error
+diag "Got correct error response for malformed query";
+{
+  my $res = $mech->get("$rest_base_path/tickets?query=Queue=incorrect",
+      'Authorization' => $auth,
+  );
+  my $content = $mech->json_response;
+  is($content->{count}, 0);
+  is($content->{page}, 1);
+  is($content->{total}, 0);
+  is(scalar @{$content->{items}}, 0);
+}
 done_testing;

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list