[Rt-commit] rt branch, 4.4/validate-asset-links, created. rt-4.4.1-426-g1ed55c9

Jim Brandt jbrandt at bestpractical.com
Thu Jun 8 13:44:08 EDT 2017


The branch, 4.4/validate-asset-links has been created
        at  1ed55c96f9ef175bebd387aacd7e4cac14a8e932 (commit)

- Log -----------------------------------------------------------------
commit 269543c508bb31830774cc6a172336d922f79b44
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Thu Jun 8 13:28:17 2017 -0400

    Add tests for linking asset ids with leading zeros
    
    Add tests cases for leading zeros, one of which causes
    an invalid link to be created.

diff --git a/t/assets/links.t b/t/assets/links.t
index a9101fe..64185f0 100644
--- a/t/assets/links.t
+++ b/t/assets/links.t
@@ -27,6 +27,7 @@ diag "RT::URI::asset";
     my %uris = (
         # URI                   => Asset Name
         "asset:1"               => { id => 1, Name => "Thinkpad T420s" },
+        "asset:01"              => { id => 1, Name => "Thinkpad T420s" },
         "asset://example.com/2" => { id => 2, Name => "Standing desk" },
         "asset:13"              => undef,
     );
@@ -103,6 +104,21 @@ diag "Linking to tickets";
     ok $ok, "Deleted link from opposite side: $msg";
 }
 
+diag "Linking to tickets, asset leading zeros";
+{
+    my $laptop = RT::Asset->new( RT->SystemUser );
+    $laptop->LoadByCols( Name => "Thinkpad T420s" );
+
+    my ($ok, $msg) = $ticket->AddLink( Type => 'RefersTo', Target => 'asset:' . '0' . $laptop->Id );
+    ok $ok, "Ticket refers to asset: $msg";
+
+    my $links = $laptop->ReferredToBy;
+    is $links->Count, 1, "Found a ReferredToBy link via asset";
+
+    ($ok, $msg) = $laptop->DeleteLink( Type => 'RefersTo', Base => $ticket->URI );
+    ok $ok, "Deleted link from opposite side: $msg";
+}
+
 diag "Links on ->Create";
 {
     my $desk = RT::Asset->new( RT->SystemUser );

commit 818f1119d1b04b9c4638f1fc2e3f131df25b2f9b
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Thu Jun 8 13:32:22 2017 -0400

    When passed an asset id, attempt to load it to validate
    
    Update asset's ParseURI method to attempt to load using
    values that appear to be an id. This confirms the linked
    asset exists and normalizes the Id including clearing
    leading zeroes.
    
    This makes handling of asset URIs consistent with the
    ticket version.

diff --git a/lib/RT/URI/asset.pm b/lib/RT/URI/asset.pm
index 15f6f30..538e648 100644
--- a/lib/RT/URI/asset.pm
+++ b/lib/RT/URI/asset.pm
@@ -138,7 +138,17 @@ sub ParseURI {
 
     # canonicalize "42" and "asset:42" -> asset://example.com/42
     if ($uri =~ /^(?:\Q$scheme\E:)?(\d+)$/i) {
-        $self->{'uri'} = $self->LocalURIPrefix . $1;
+        my $asset_obj = RT::Asset->new( $self->CurrentUser );
+        my ($ret, $msg) = $asset_obj->Load($1);
+
+        if ( $ret ) {
+            $self->{'uri'} = $asset_obj->URI;
+            $self->{'object'} = $asset_obj;
+        }
+        else {
+            RT::Logger->error("Unable to load asset for id: $1: $msg");
+            return;
+        }
     }
     else {
         $self->{'uri'} = $uri;

commit 1ed55c96f9ef175bebd387aacd7e4cac14a8e932
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Thu Jun 8 13:37:04 2017 -0400

    Update with new warning for unknown uris

diff --git a/t/assets/links.t b/t/assets/links.t
index 64185f0..540e2ab 100644
--- a/t/assets/links.t
+++ b/t/assets/links.t
@@ -49,7 +49,8 @@ diag "RT::URI::asset";
             my $parsed;
             warnings_like {
                 $parsed = $uri->FromURI($url);
-            } [qr/\Q$url\E/, qr/\Q$url\E/], "Caught warnings about unknown URI";
+            } [qr/Unable to load asset/, qr/\Q$url\E/],
+                "Caught warnings about unknown URI";
             ok !$parsed, "Failed to parse $url, as expected";
         }
     }

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


More information about the rt-commit mailing list