[Bps-public-commit] rt-extension-rest2 branch, dev, updated. c7e1b4932e33fcd8820e5587a40737fafd2838c9

Wallace Reis wreis at bestpractical.com
Fri Feb 13 12:22:46 EST 2015


The branch, dev has been updated
       via  c7e1b4932e33fcd8820e5587a40737fafd2838c9 (commit)
       via  70fa060ca2b47bba0676c86c3253951a21581881 (commit)
       via  5a110da85209db83fb427da4a4ba47dd8d744c7a (commit)
      from  409245a6ed745abe4760733dc8874c0b10c8fcb4 (commit)

Summary of changes:
 lib/RT/Extension/REST2/Dispatcher.pm       |  6 +--
 lib/RT/Extension/REST2/Resource/Tickets.pm | 12 ++++-
 t/acceptance/not_found.t                   | 35 ++++++++++++--
 t/acceptance/tickets.t                     | 73 ++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+), 8 deletions(-)
 create mode 100644 t/acceptance/tickets.t

- Log -----------------------------------------------------------------
commit 5a110da85209db83fb427da4a4ba47dd8d744c7a
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Fri Feb 13 14:00:48 2015 -0200

    Fix ticket listing dispatch and no rows response

diff --git a/lib/RT/Extension/REST2/Dispatcher.pm b/lib/RT/Extension/REST2/Dispatcher.pm
index cc78a99..b844570 100644
--- a/lib/RT/Extension/REST2/Dispatcher.pm
+++ b/lib/RT/Extension/REST2/Dispatcher.pm
@@ -9,11 +9,11 @@ use Web::Dispatch::HTTPMethods;
 
 sub dispatch_request {
     my ($self) = @_;
-    sub (/*/*) {
-        my $resource_name = ucfirst lc $_[1];
+    sub (/**) {
+        my ($resource_name) = ucfirst(lc $_[1]) =~ /(\w+)\/?/;
         my $resource = "RT::Extension::REST2::Resource::${resource_name}";
         if ( $resource->require ) {
-            Web::Machine->new(
+            return Web::Machine->new(
                 resource => $resource,
             )->to_app;
         }
diff --git a/lib/RT/Extension/REST2/Resource/Tickets.pm b/lib/RT/Extension/REST2/Resource/Tickets.pm
index 5f1f5b7..39ff184 100644
--- a/lib/RT/Extension/REST2/Resource/Tickets.pm
+++ b/lib/RT/Extension/REST2/Resource/Tickets.pm
@@ -41,8 +41,16 @@ sub allowed_methods {
 
 sub limit_collection {
     my $self = shift;
-    my ($ok, $msg) = $self->collection->FromSQL( $self->query );
-    return error_as_json( $self->response, ($ok ? 1 : 0), $msg );
+    my $collection = $self->collection;
+    my ($ok, $msg) = $collection->FromSQL( $self->query );
+    if ($ok) {
+        unless ($collection->Count) {
+            ($ok, $msg) = (0, 'No tickets found');
+        }
+    }
+    return error_as_json(
+        $self->response, $ok ? 1 : 0, $msg
+    );
 }
 
 __PACKAGE__->meta->make_immutable;
diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
new file mode 100644
index 0000000..918723d
--- /dev/null
+++ b/t/acceptance/tickets.t
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use lib 't/lib';
+use RT::Extension::REST2::Test tests => undef;
+use JSON;
+
+my $mech = RT::Extension::REST2::Test->mech;
+
+my $auth = RT::Extension::REST2::Test->authorization_header;
+my $rest_base_path = '/REST/2.0';
+my $json = JSON->new->utf8;
+
+{
+    ok(my $res = $mech->get(
+        $rest_base_path . '/tickets?query=id>0', 'Authorization' => $auth
+    ));
+    is($res->code, 400, 'DB empty, so no tickets found');
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = $json->decode($res->content));
+    is($data->{'message'}, 'No tickets found');
+}
+
+done_testing;

commit 70fa060ca2b47bba0676c86c3253951a21581881
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Fri Feb 13 14:57:53 2015 -0200

    Add tests for ticket creation

diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
index 918723d..1cf28e2 100644
--- a/t/acceptance/tickets.t
+++ b/t/acceptance/tickets.t
@@ -20,4 +20,54 @@ my $json = JSON->new->utf8;
     is($data->{'message'}, 'No tickets found');
 }
 
+TODO : {
+    local $TODO = 'Missing param validation';
+    ok(my $res = $mech->post(
+        $rest_base_path . '/ticket', {}, 'Authorization' => $auth
+    ));
+    is($res->code, 400);
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = $json->decode($res->content));
+    is($data->{'message'}, 'Missing required params');
+}
+
+TODO : {
+    local $TODO = 'Invalid input params should respond 400';
+    my $payload = $json->encode({
+        Subject => 'Ticket creation using REST',
+        From => 'wallace at reis.me',
+    });
+    ok(my $res = $mech->post(
+        $rest_base_path . '/ticket',
+        Content => $payload,
+        'Content-Type' => 'application/json; charset=utf-8',
+        'Authorization' => $auth
+    ));
+    is($res->code, 400);
+    like($res->header('content-type'), qr{application/json});
+    ok(my $data = $json->decode($res->content));
+    is($data->{'message'}, 'Could not create ticket. Queue not set');
+}
+
+TODO : {
+    local $TODO = 'Fix response Location URL';
+    my $payload = $json->encode({
+        Subject => 'Ticket creation using REST',
+        From => 'wallace at reis.me',
+        To => 'rt at localhost',
+        Queue => 'General',
+        Content => 'Testing ticket creation using REST API.',
+    });
+    ok(my $res = $mech->post(
+        $rest_base_path . '/ticket',
+        Content => $payload,
+        'Content-Type' => 'application/json; charset=utf-8',
+        'Authorization' => $auth
+    ));
+    is($res->code, 201);
+    like($res->header('content-type'), qr{application/json});
+    my $new_ticket_url = $res->header('location');
+    like($new_ticket_url, qr[/tickets/\d+]);
+}
+
 done_testing;

commit c7e1b4932e33fcd8820e5587a40737fafd2838c9
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Fri Feb 13 15:21:08 2015 -0200

    Extend tests for unsupported requests

diff --git a/lib/RT/Extension/REST2/Dispatcher.pm b/lib/RT/Extension/REST2/Dispatcher.pm
index b844570..9c5eaad 100644
--- a/lib/RT/Extension/REST2/Dispatcher.pm
+++ b/lib/RT/Extension/REST2/Dispatcher.pm
@@ -10,7 +10,7 @@ use Web::Dispatch::HTTPMethods;
 sub dispatch_request {
     my ($self) = @_;
     sub (/**) {
-        my ($resource_name) = ucfirst(lc $_[1]) =~ /(\w+)\/?/;
+        my ($resource_name) = ucfirst(lc $_[1]) =~ /([^\/]+)\/?/;
         my $resource = "RT::Extension::REST2::Resource::${resource_name}";
         if ( $resource->require ) {
             return Web::Machine->new(
diff --git a/t/acceptance/not_found.t b/t/acceptance/not_found.t
index a9eb01f..cd68392 100644
--- a/t/acceptance/not_found.t
+++ b/t/acceptance/not_found.t
@@ -2,23 +2,52 @@ use strict;
 use warnings;
 use lib 't/lib';
 use RT::Extension::REST2::Test tests => undef;
+use JSON;
+use Try::Tiny;
 
 my $mech = RT::Extension::REST2::Test->mech;
 
 my $auth = RT::Extension::REST2::Test->authorization_header;
 my $rest_base_path = '/REST/2.0';
+my $json = JSON->new->utf8;
+
+sub check_404 {
+    my $res = shift;
+    is($res->code, 404);
+    TODO : {
+        local $TODO = 'Error response in JSON format';
+        like($res->header('content-type'), qr{application/json});
+        ok(my $data = try { $json->decode($res->content) });
+        is($data->{'message'}, 'Not found');
+    }
+}
 
 {
-    for (qw[/foobar /foo /ticket /queue /index.html /ticket.do/1 /1/1]) {
+    for (qw[/foobar /foo /index.html /ticket.do/1 /1/1]) {
+        my $path = $rest_base_path . $_;
+        ok(my $res = $mech->get($path, 'Authorization' => $auth),
+           "GET $path");
+        check_404($res);
+
+        ok($res = $mech->post(
+            $path, { param => 'value' }, 'Authorization' => $auth
+        ), "POST $path");
+        check_404($res);
+    }
+}
+
+TODO : {
+    local $TODO = 'Merge endpoints';
+    for (qw[/ticket /queue /user]) { # should be changed to the plural form
         my $path = $rest_base_path . $_;
         ok(my $res = $mech->get($path, 'Authorization' => $auth),
            "GET $path");
-        is($res->code, 404);
+        check_404($res);
 
         ok($res = $mech->post(
             $path, { param => 'value' }, 'Authorization' => $auth
         ), "POST $path");
-        is($res->code, 404);
+        check_404($res);
     }
 }
 

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


More information about the Bps-public-commit mailing list