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

Wallace Reis wreis at bestpractical.com
Thu Feb 19 12:09:05 EST 2015


The branch, dev has been updated
       via  557ebf82074ef0bae4e266be7acd616ef447bf7d (commit)
       via  e8aa04b8627769697c85a473a452b841da6ba2f9 (commit)
       via  bea2682a72c2490e0368ed9d4b6882d59656d7a7 (commit)
       via  d13a644476daac39ea7816880620638475851007 (commit)
       via  4a0108ff1a6fd8baf9bffc1b6f744b7b922bbf34 (commit)
       via  2739444376a25cc80441260cb9124dbfca47a44e (commit)
      from  c7e1b4932e33fcd8820e5587a40737fafd2838c9 (commit)

Summary of changes:
 Makefile.PL                                        |  2 ++
 lib/RT/Extension/REST2/Resource/Record.pm          | 12 +++++++-----
 lib/RT/Extension/REST2/Resource/Record/Readable.pm |  7 ++++---
 lib/RT/Extension/REST2/Resource/Record/Writable.pm |  2 +-
 lib/RT/Extension/REST2/Util.pm                     | 14 ++++++++++++++
 t/acceptance/main.t                                |  9 +++++++++
 t/acceptance/tickets.t                             | 16 ++++++++++------
 7 files changed, 47 insertions(+), 15 deletions(-)

- Log -----------------------------------------------------------------
commit 2739444376a25cc80441260cb9124dbfca47a44e
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 08:56:31 2015 -0300

    Add tests for 405 error response at main endpoint

diff --git a/t/acceptance/main.t b/t/acceptance/main.t
index a80debc..468e27d 100644
--- a/t/acceptance/main.t
+++ b/t/acceptance/main.t
@@ -2,10 +2,13 @@ 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 $rest_base_path = '/REST/2.0';
+my $json = JSON->new->utf8;
 
 {
     ok(my $res = $mech->get($rest_base_path), "GET $rest_base_path");
@@ -42,6 +45,12 @@ my $auth = RT::Extension::REST2::Test->authorization_header;
     ), "POST $rest_base_path");
     is($res->code, 405);
     like($res->header('allow'), qr/GET|HEAD|OPTIONS/);
+    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'}, 'Method not allowed');
+    }
 }
 
 done_testing;

commit 4a0108ff1a6fd8baf9bffc1b6f744b7b922bbf34
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 09:44:12 2015 -0300

    Status code for no rows should be 404

diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
index 1cf28e2..4b16b9c 100644
--- a/t/acceptance/tickets.t
+++ b/t/acceptance/tickets.t
@@ -14,7 +14,10 @@ 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');
+    TODO : {
+        local $TODO = 'Status code for no rows';
+        is($res->code, 404, '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');

commit d13a644476daac39ea7816880620638475851007
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 09:44:24 2015 -0300

    Check that Location endpoint returns ok

diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
index 4b16b9c..e241167 100644
--- a/t/acceptance/tickets.t
+++ b/t/acceptance/tickets.t
@@ -71,6 +71,7 @@ TODO : {
     like($res->header('content-type'), qr{application/json});
     my $new_ticket_url = $res->header('location');
     like($new_ticket_url, qr[/tickets/\d+]);
+    $mech->get_ok($rest_base_path . $new_ticket_url);
 }
 
 done_testing;

commit bea2682a72c2490e0368ed9d4b6882d59656d7a7
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 10:14:11 2015 -0300

    Update test deps

diff --git a/Makefile.PL b/Makefile.PL
index 2171c7f..2b2a41e 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -27,5 +27,7 @@ requires 'Pod::POM';
 
 recommends 'JSON::XS';
 
+test_requires 'Try::Tiny';
+
 sign;
 WriteAll;

commit e8aa04b8627769697c85a473a452b841da6ba2f9
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 10:14:29 2015 -0300

    Invalid input params should respond 400

diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index e6272ba..65b5d57 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -78,7 +78,7 @@ sub create_resource {
     } else {
         return error_as_json(
             $self->response,
-            \409, $msg || "Create failed for unknown reason");
+            \400, $msg || "Create failed for unknown reason");
     }
 }
 
diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
index e241167..4448327 100644
--- a/t/acceptance/tickets.t
+++ b/t/acceptance/tickets.t
@@ -34,8 +34,7 @@ TODO : {
     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',

commit 557ebf82074ef0bae4e266be7acd616ef447bf7d
Author: Wallace Reis <wreis at bestpractical.com>
Date:   Thu Feb 19 10:33:23 2015 -0300

    Fix response Location URL

diff --git a/lib/RT/Extension/REST2/Resource/Record.pm b/lib/RT/Extension/REST2/Resource/Record.pm
index c1380bf..99a903c 100644
--- a/lib/RT/Extension/REST2/Resource/Record.pm
+++ b/lib/RT/Extension/REST2/Resource/Record.pm
@@ -7,9 +7,9 @@ use namespace::autoclean;
 
 extends 'RT::Extension::REST2::Resource';
 
-use Scalar::Util qw( blessed );
 use Web::Machine::Util qw( bind_path create_date );
 use Module::Runtime qw( require_module );
+use RT::Extension::REST2::Util qw(record_class record_type);
 
 has 'record_class' => (
     is          => 'ro',
@@ -26,9 +26,8 @@ has 'record' => (
 );
 
 sub _build_record_class {
-    my $self   = shift;
-    my ($type) = blessed($self) =~ /::(\w+)$/;
-    my $class  = "RT::$type";
+    my $self = shift;
+    my $class = record_class($self);
     require_module($class);
     return $class;
 }
@@ -42,7 +41,10 @@ sub _build_record {
 }
 
 sub base_uri {
-    $_[0]->request->base
+    my $self = shift;
+    my $base = $self->request->base;
+    my $type = record_type($self);
+    return $base . lc $type;
 }
 
 sub resource_exists {
diff --git a/lib/RT/Extension/REST2/Resource/Record/Readable.pm b/lib/RT/Extension/REST2/Resource/Record/Readable.pm
index 6003f2a..c5be583 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Readable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Readable.pm
@@ -11,14 +11,15 @@ requires 'current_user';
 requires 'base_uri';
 
 use JSON ();
-use RT::Extension::REST2::Util qw( serialize_record );
+use RT::Extension::REST2::Util qw( serialize_record record_type );
 
 sub serialize {
     my $self = shift;
-    my $data = serialize_record( $self->record );
+    my $record = $self->record;
+    my $data = serialize_record($record);
 
     # Add the resource url for this record
-    $data->{_url} = join "/", $self->base_uri, $self->record->id;
+    $data->{_url} = join "/", $self->base_uri, $record->id;
 
     return $data;
 }
diff --git a/lib/RT/Extension/REST2/Util.pm b/lib/RT/Extension/REST2/Util.pm
index 44f0329..214fda6 100644
--- a/lib/RT/Extension/REST2/Util.pm
+++ b/lib/RT/Extension/REST2/Util.pm
@@ -3,6 +3,7 @@ use strict;
 use warnings;
 
 use JSON ();
+use Scalar::Util qw( blessed );
 
 use Sub::Exporter -setup => {
     exports => [qw[
@@ -11,6 +12,8 @@ use Sub::Exporter -setup => {
         serialize_record
         deserialize_record
         error_as_json
+        record_type
+        record_class
     ]]
 };
 
@@ -157,4 +160,15 @@ sub error_as_json {
     return $return;
 }
 
+sub record_type {
+    my $object = shift;
+    my ($type) = blessed($object) =~ /::(\w+)$/;
+    return $type;
+}
+
+sub record_class {
+    my $type = record_type(shift);
+    return "RT::$type";
+}
+
 1;
diff --git a/t/acceptance/tickets.t b/t/acceptance/tickets.t
index 4448327..ef1612a 100644
--- a/t/acceptance/tickets.t
+++ b/t/acceptance/tickets.t
@@ -51,8 +51,7 @@ TODO : {
     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',
@@ -69,8 +68,10 @@ TODO : {
     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+]);
-    $mech->get_ok($rest_base_path . $new_ticket_url);
+    like($new_ticket_url, qr[/ticket/\d+]);
+    $mech->get_ok($rest_base_path . $new_ticket_url,
+        ['Authorization' => $auth]
+    );
 }
 
 done_testing;

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


More information about the Bps-public-commit mailing list