[Bps-public-commit] rt-extension-rest2 branch, asset-fix-add-roles, created. 1.07-3-g3ddad3e

Aaron Trevena ast at bestpractical.com
Wed Mar 25 12:23:41 EDT 2020


The branch, asset-fix-add-roles has been created
        at  3ddad3ef42ff048768a13f7b5d42445103f2dd94 (commit)

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

    Fix setting HeldBy/Contact variables on Assets

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 3ddad3ef42ff048768a13f7b5d42445103f2dd94
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..9986df2
--- /dev/null
+++ b/xt/asset-watchers.t
@@ -0,0 +1,245 @@
+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/;
+
+# Create and view asset with no owner
+{
+    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');
+}
+
+# Modify single user allowed roles.
+{
+    my $payload = {
+        Name    => 'Asset for modifying owner 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);
+
+    cmp_deeply($mech->json_response->{Owner}, {
+        type => 'user',
+        id   => 'Nobody',
+        _url => re(qr{$rest_base_path/user/Nobody$}),
+    }, 'Owner is Nobody');
+
+    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);
+    # I have submitted a pull request to RT to allow adding HeldBy and
+    # Contact by name (as the documentation says is possible), when that
+    # is merged, we can switch this if to a versioned test. In the mean
+    # time, we need to create the asset and modify it to set HeldBy and
+    # Contact. Pull request is here:
+    #   https://github.com/bestpractical/rt/pull/278
+    #if (RT::Handle::cmp_version($RT::VERSION, '4.4.0') >= 0) {
+    if (0) {
+        my $payload = {
+            Name    => 'Asset for modifying owner using REST',
+            Catalog => 'General assets',
+            Content => 'Testing asset creation using REST API.',
+            HeldBy  => 'Nobody',
+            Contact => 'Nobody',
+        };
+
+        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+)]);
+    } else {
+        my $payload = {
+            Name    => 'Asset for modifying owner 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($asset_url = $res->header('location'));
+        ok(($asset_id) = $asset_url =~ qr[/asset/(\d+)]);
+
+        $payload = {
+            'HeldBy'  => 'Nobody',
+            'Contact' => 'Nobody',
+        };
+
+        $res = $mech->put_json($asset_url,
+            $payload,
+            'Authorization' => $auth,
+        );
+        is_deeply($mech->json_response, ["Member added: Nobody", 'Member added: Nobody'], "Set HeldBy and Contact to initial values");
+    }
+
+    my $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   => 'Nobody',
+            _url => re(qr{$rest_base_path/user/Nobody$}),
+        }], "$field is Nobody");
+        }
+
+    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;
+

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


More information about the Bps-public-commit mailing list