[Bps-public-commit] rt-extension-rest2 branch, master, updated. 1.07-15-g0097143

? sunnavy sunnavy at bestpractical.com
Thu Apr 30 14:48:05 EDT 2020


The branch, master has been updated
       via  009714390440a228ba6c684c4d1c5e48fb8dd37e (commit)
       via  44c38fa9436400c847d4368cfa8fec2a649a972e (commit)
       via  3042fa7ac0b5871c5ff3e73a2408d226ca6fdf0f (commit)
      from  cc69c2078867935e7d5ea87bc96eea6deefc2dbf (commit)

Summary of changes:
 lib/RT/Extension/REST2/Resource/Record/Writable.pm |  16 +-
 xt/asset-watchers.t                                | 180 +++++++++++++++++++++
 2 files changed, 192 insertions(+), 4 deletions(-)
 create mode 100644 xt/asset-watchers.t

- Log -----------------------------------------------------------------
commit 3042fa7ac0b5871c5ff3e73a2408d226ca6fdf0f
Author: Andrew Ruthven <puck at catalystcloud.nz>
Date:   Wed Mar 25 12:11:48 2020 +0000

    Fix setting single member roles like Owner for Assets
    
    RT::Asset doesn't have AddWatcher method defined.

diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index 6d04d3b..e5443a5 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -166,10 +166,18 @@ sub _update_role_members {
                 die "Invalid value type for role $role";
             }
 
-            my ($ok, $msg) = $record->AddWatcher(
-                Type => $role,
-                User => $val,
-            );
+            my ($ok, $msg);
+            if ($record->can('AddWatcher')) {
+                ($ok, $msg) = $record->AddWatcher(
+                    Type => $role,
+                    User => $val,
+                );
+            } else {
+                ($ok, $msg) = $record->AddRoleMember(
+                    Type => $role,
+                    User => $val,
+                );
+            }
             push @results, $msg;
         }
         else {

commit 44c38fa9436400c847d4368cfa8fec2a649a972e
Author: Andrew Ruthven <puck at catalystcloud.nz>
Date:   Wed Mar 25 12:12:05 2020 +0000

    Tests Watchers/HeldBy/Contact on Assets in REST2 API

diff --git a/xt/asset-watchers.t b/xt/asset-watchers.t
new file mode 100644
index 0000000..96c9d02
--- /dev/null
+++ b/xt/asset-watchers.t
@@ -0,0 +1,180 @@
+use strict;
+use warnings;
+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;
+
+$user->PrincipalObj->GrantRight( Right => $_ )
+    for qw/CreateAsset ShowAsset ModifyAsset OwnAsset AdminUsers SeeGroup/;
+
+# Modify single user allowed roles.
+{
+    my $payload = {
+        Name    => 'Asset creation using REST',
+        Catalog => 'General assets',
+        Content => 'Testing asset creation using REST API.',
+    };
+
+    my $res = $mech->post_json("$rest_base_path/asset",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    ok(my $asset_url = $res->header('location'));
+    ok((my $asset_id) = $asset_url =~ qr[/asset/(\d+)]);
+
+    $res = $mech->get($asset_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    my $content = $mech->json_response;
+    cmp_deeply($content->{Owner}, {
+        type => 'user',
+        id   => 'Nobody',
+        _url => re(qr{$rest_base_path/user/Nobody$}),
+    }, 'Owner is Nobody');
+
+    $res = $mech->get($content->{Owner}{_url},
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    cmp_deeply($mech->json_response, superhashof({
+        id => RT->Nobody->id,
+        Name => 'Nobody',
+        RealName => 'Nobody in particular',
+    }), 'Nobody user');
+
+    for my $field ('Owner') {
+        for my $identifier ($user->id, $user->Name) {
+            $payload = {
+                $field => $identifier,
+            };
+
+            $res = $mech->put_json($asset_url,
+                $payload,
+                'Authorization' => $auth,
+            );
+            is_deeply($mech->json_response, ["$field set to test"], "updated $field with identifier $identifier");
+
+            $res = $mech->get($asset_url,
+                'Authorization' => $auth,
+            );
+            is($res->code, 200);
+
+            cmp_deeply($mech->json_response->{$field}, {
+                type => 'user',
+                id   => 'test',
+                _url => re(qr{$rest_base_path/user/test$}),
+            }, "$field has changed to test");
+
+            $payload = {
+                $field => 'Nobody',
+            };
+
+            $res = $mech->put_json($asset_url,
+                $payload,
+                'Authorization' => $auth,
+            );
+            is_deeply($mech->json_response, ["$field set to Nobody"], "updated $field");
+
+            $res = $mech->get($asset_url,
+                'Authorization' => $auth,
+            );
+            is($res->code, 200);
+
+            cmp_deeply($mech->json_response->{$field}, {
+                type => 'user',
+                id   => 'Nobody',
+                _url => re(qr{$rest_base_path/user/Nobody$}),
+            }, "$field has changed to Nobody");
+        }
+    }
+}
+
+# Modify multi-user allowed roles (HeldBy)
+{
+    my ($asset_url, $asset_id);
+    my $payload = {
+        Name    => 'Asset for modifying owner using REST',
+        Catalog => 'General assets',
+        Content => 'Testing asset creation using REST API.',
+        HeldBy  => 'root',
+        Contact => 'root',
+    };
+
+    my $res = $mech->post_json("$rest_base_path/asset",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    ok($asset_url = $res->header('location'));
+    ok(($asset_id) = $asset_url =~ qr[/asset/(\d+)]);
+
+    $res = $mech->get($asset_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+
+    # Initial sanity check.
+    for my $field ('Contact', 'HeldBy') {
+        cmp_deeply($mech->json_response->{$field}, [{
+            type => 'user',
+            id   => 'root',
+            _url => re(qr{$rest_base_path/user/root$}),
+        }], "$field is root");
+        }
+
+    for my $field ('Contact', 'HeldBy') {
+        for my $identifier ($user->id, $user->Name) {
+            my $payload = {
+                $field => $identifier,
+            };
+
+            $res = $mech->put_json($asset_url,
+                $payload,
+                'Authorization' => $auth,
+            );
+            is_deeply($mech->json_response, ["Member added: test", 'Member deleted'], "updated $field with identifier $identifier");
+
+            $res = $mech->get($asset_url,
+                'Authorization' => $auth,
+            );
+            is($res->code, 200);
+
+            cmp_deeply($mech->json_response->{$field}, [{
+                type => 'user',
+                id   => 'test',
+                _url => re(qr{$rest_base_path/user/test$}),
+            }], "$field has changed to test");
+
+            $payload = {
+                $field => 'Nobody',
+            };
+
+            $res = $mech->put_json($asset_url,
+                $payload,
+                'Authorization' => $auth,
+            );
+            is_deeply($mech->json_response, ["Member added: Nobody", 'Member deleted'], "updated $field");
+
+            $res = $mech->get($asset_url,
+                'Authorization' => $auth,
+            );
+            is($res->code, 200);
+
+            cmp_deeply($mech->json_response->{$field}, [{
+                type => 'user',
+                id   => 'Nobody',
+                _url => re(qr{$rest_base_path/user/Nobody$}),
+            }], "$field has changed to Nobody");
+        }
+    }
+}
+
+done_testing;

commit 009714390440a228ba6c684c4d1c5e48fb8dd37e
Merge: cc69c20 44c38fa
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri May 1 02:24:25 2020 +0800

    Merge branch 'asset-fix-add-roles'


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


More information about the Bps-public-commit mailing list