[Bps-public-commit] rt-extension-rest2 branch, master, updated. d48400c8d86824f0b18b0bd2d38b63db77db2dbf

Shawn Moore shawn at bestpractical.com
Wed Jun 28 16:37:51 EDT 2017


The branch, master has been updated
       via  d48400c8d86824f0b18b0bd2d38b63db77db2dbf (commit)
      from  9d62b8514679adcbcfb457f232fffa8d5034c7bb (commit)

Summary of changes:
 Makefile.PL                                        |  1 +
 .../Extension/REST2/Resource/Record/Hypermedia.pm  | 38 +++++++++-
 t/ticket-links.t                                   | 87 ++++++++++++++++++++++
 3 files changed, 124 insertions(+), 2 deletions(-)
 create mode 100644 t/ticket-links.t

- Log -----------------------------------------------------------------
commit d48400c8d86824f0b18b0bd2d38b63db77db2dbf
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Wed Jun 28 20:36:05 2017 +0000

    Include RT::Link relations as hypermedia links

diff --git a/Makefile.PL b/Makefile.PL
index 220849d..ab30f79 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -28,6 +28,7 @@ requires 'Path::Dispatcher';
 recommends 'JSON::XS';
 
 test_requires 'Try::Tiny';
+test_requires 'Test::Deep';
 
 my ($lp) = ($INC{'RT.pm'} =~ /^(.*)[\\\/]/);
 my $lib_path = join( ' ', "$RT::LocalPath/lib", $lp );
diff --git a/lib/RT/Extension/REST2/Resource/Record/Hypermedia.pm b/lib/RT/Extension/REST2/Resource/Record/Hypermedia.pm
index 486a892..e8aef68 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Hypermedia.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Hypermedia.pm
@@ -4,12 +4,12 @@ use warnings;
 
 use Moose::Role;
 use namespace::autoclean;
-use RT::Extension::REST2::Util qw(query_string);
+use RT::Extension::REST2::Util qw(expand_uid);
 use JSON qw(to_json);
 
 sub hypermedia_links {
     my $self = shift;
-    return [ $self->_self_link ];
+    return [ $self->_self_link, $self->_rtlink_links ];
 }
 
 sub _self_link {
@@ -38,5 +38,39 @@ sub _transaction_history_link {
     };
 }
 
+my %link_refs = (
+    DependsOn => 'depends-on',
+    DependedOnBy => 'depended-on-by',
+    MemberOf => 'parent',
+    Members => 'child',
+    RefersTo => 'refers-to',
+    ReferredToBy => 'referred-to-by',
+);
+
+sub _rtlink_links {
+    my $self = shift;
+    my $record = $self->record;
+    my @links;
+
+    for my $relation (keys %link_refs) {
+        my $ref = $link_refs{$relation};
+        my $mode = $RT::Link::TYPEMAP{$relation}{Mode};
+        my $type = $RT::Link::TYPEMAP{$relation}{Type};
+        my $method = $mode . "Obj";
+
+        my $links = $record->$relation;
+
+        while (my $link = $links->Next) {
+            my $entry = expand_uid($link->$method->UID);
+            push @links, {
+                %$entry,
+                ref => $ref,
+            };
+        }
+    }
+
+    return @links;
+}
+
 1;
 
diff --git a/t/ticket-links.t b/t/ticket-links.t
new file mode 100644
index 0000000..e820d63
--- /dev/null
+++ b/t/ticket-links.t
@@ -0,0 +1,87 @@
+use strict;
+use warnings;
+use lib 't/lib';
+use RT::Extension::REST2::Test tests => undef;
+use Test::Deep;
+
+my $mech = RT::Extension::REST2::Test->mech;
+
+my $auth = RT::Extension::REST2::Test->authorization_header;
+my $rest_base_path = '/REST/2.0';
+my $user = RT::Extension::REST2::Test->user;
+
+my $queue = RT::Test->load_or_create_queue( Name => "General" );
+
+my $parent = RT::Ticket->new(RT->SystemUser);
+my ($ok, undef, $msg) = $parent->Create(Queue => 'General', Subject => 'parent ticket');
+ok($ok, $msg);
+my $parent_id = $parent->Id;
+
+my $child = RT::Ticket->new(RT->SystemUser);
+($ok, undef, $msg) = $child->Create(Queue => 'General', Subject => 'child ticket');
+ok($ok, $msg);
+my $child_id = $child->Id;
+
+($ok, $msg) = $child->AddLink(Type => 'MemberOf', Target => $parent->id);
+ok($ok, $msg);
+
+$user->PrincipalObj->GrantRight( Right => 'ShowTicket' );
+
+# Inspect existing ticket links (parent)
+{
+
+    my $res = $mech->get("$rest_base_path/ticket/$parent_id",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    my %links;
+    for (@{ $content->{_hyperlinks} }) {
+        push @{ $links{$_->{ref}} }, $_;
+    }
+
+    cmp_deeply($links{'depends-on'}, undef, 'no depends-on links');
+    cmp_deeply($links{'depended-on-by'}, undef, 'no depended-on-by links');
+    cmp_deeply($links{'parent'}, undef, 'no parent links');
+    cmp_deeply($links{'refers-to'}, undef, 'no refers-to links');
+    cmp_deeply($links{'referred-to-by'}, undef, 'no referred-to-by links');
+
+    cmp_deeply($links{'child'}, [{
+        ref  => 'child',
+        type => 'ticket',
+        id   => $child->Id,
+        _url => re(qr{$rest_base_path/ticket/$child_id$}),
+    }], 'one child link');
+}
+
+# Inspect existing ticket links (child)
+{
+
+    my $res = $mech->get("$rest_base_path/ticket/$child_id",
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    my %links;
+    for (@{ $content->{_hyperlinks} }) {
+        push @{ $links{$_->{ref}} }, $_;
+    }
+
+    cmp_deeply($links{'depends-on'}, undef, 'no depends-on links');
+    cmp_deeply($links{'depended-on-by'}, undef, 'no depended-on-by links');
+    cmp_deeply($links{'child'}, undef, 'no child links');
+    cmp_deeply($links{'refers-to'}, undef, 'no refers-to links');
+    cmp_deeply($links{'referred-to-by'}, undef, 'no referred-to-by links');
+
+    cmp_deeply($links{'parent'}, [{
+        ref  => 'parent',
+        type => 'ticket',
+        id   => $parent->Id,
+        _url => re(qr{$rest_base_path/ticket/$parent_id$}),
+    }], 'one child link');
+}
+
+done_testing;
+

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


More information about the Bps-public-commit mailing list