[Bps-public-commit] RT-Extension-NHD branch, master, updated. c63935899a24042600e86adae090bc8269427811

Ruslan Zakirov ruz at bestpractical.com
Sun Sep 25 06:37:36 EDT 2011


The branch, master has been updated
       via  c63935899a24042600e86adae090bc8269427811 (commit)
       via  3b16a698ac486c2f616092a2fd44872cc2262a91 (commit)
       via  e4dc4f0c5d923bb6d68e118cc04ab25ed682425e (commit)
       via  4ffa11e1c47dd9dc564a2d69b9ce1704b6612531 (commit)
       via  8c95b81b22c531cc8f9dcac8ce724c4307160298 (commit)
      from  d05de3da0fc9504c2dc170d916179907af5b19b0 (commit)

Summary of changes:
 etc/NHD_Config.pm           |   21 ++++++++
 etc/schema.mysql            |    1 +
 lib/RT/NHD/Agreement.pm     |  111 ++++++++++++++++++++++++++++++++++++++++++-
 spec_question.txt           |    3 +
 t/api/agreement.t           |   59 +++++++++++++++++++++--
 t/{web => rest}/agreement.t |    6 +-
 6 files changed, 191 insertions(+), 10 deletions(-)
 create mode 100644 etc/NHD_Config.pm
 rename t/{web => rest}/agreement.t (91%)

- Log -----------------------------------------------------------------
commit 8c95b81b22c531cc8f9dcac8ce724c4307160298
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sun Sep 18 22:32:13 2011 +0400

    update spec's errata

diff --git a/spec_question.txt b/spec_question.txt
index 5ebf6c1..ee186ad 100644
--- a/spec_question.txt
+++ b/spec_question.txt
@@ -13,3 +13,6 @@
 * in section "Retrieving a Ticket Sharing Agreement"
   code for "Method Not Allowed" HTTP status is wrong.
   It should be 405, but it's 406.
+
+* There are two sections "Retrieving a Ticket Sharing Agreement"
+  in the spec. Second one should be renamed - "Retrieving a Ticket".

commit 4ffa11e1c47dd9dc564a2d69b9ce1704b6612531
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Mon Sep 19 14:38:51 2011 +0400

    it's web test, it's rest, rename it

diff --git a/t/web/agreement.t b/t/rest/agreement.t
similarity index 100%
rename from t/web/agreement.t
rename to t/rest/agreement.t

commit e4dc4f0c5d923bb6d68e118cc04ab25ed682425e
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Tue Sep 20 17:12:52 2011 +0400

    sender or receiver should be our

diff --git a/etc/NHD_Config.pm b/etc/NHD_Config.pm
new file mode 100644
index 0000000..2a53dce
--- /dev/null
+++ b/etc/NHD_Config.pm
@@ -0,0 +1,21 @@
+=head1 Networked Help Desk configuration file
+
+=cut
+
+package RT;
+
+=head2 Basics
+
+=over 4
+
+=item C<$NHD_WebURL>
+
+=cut
+
+Set( $NHD_WebURL, RT->Config->Get('WebURL') . 'NoAuth/NHD/1.0' );
+
+=back
+
+=cut
+
+1;
diff --git a/lib/RT/NHD/Agreement.pm b/lib/RT/NHD/Agreement.pm
index 2af08c9..381c1f3 100644
--- a/lib/RT/NHD/Agreement.pm
+++ b/lib/RT/NHD/Agreement.pm
@@ -23,10 +23,13 @@ sub Create {
     my $self = shift;
     my %args = @_;
 
-    my $status = $args{'Status'};
-    unless ( ($status||'') eq 'pending' ) {
+    unless ( ($args{'Status'}||'') eq 'pending' ) {
         return (undef, "New agreement must have 'pending' status");
     }
+    my $our_url = RT->Config->Get('NHD_WebURL');
+    if ( ($args{'Sender'}||'') ne $our_url && ($args{'Receiver'}||'') ne $our_url ) {
+        return (undef, "Either sender or receiver should be '$our_url'");
+    }
     return $self->SUPER::Create( %args );
 }
 
diff --git a/t/api/agreement.t b/t/api/agreement.t
index 22de433..c827224 100644
--- a/t/api/agreement.t
+++ b/t/api/agreement.t
@@ -23,7 +23,7 @@ my $i = 0;
         Name => 'Test Company',
         Status => 'pending',
         Sender => 'http://hoster.example.com/sharing',
-        Receiver => 'http://rt.example.com/sharing',
+        Receiver => RT->Config->Get('NHD_WebURL'),
         AccessKey => sha1_hex( ''. ++$i ),
     );
     ok($id, "Created an agreement $uuid");
@@ -36,7 +36,7 @@ my $i = 0;
     is( $agreement->Name, 'Test Company', 'correct value' );
     is( $agreement->Status, 'pending', 'correct value' );
     is( $agreement->Sender, 'http://hoster.example.com/sharing', 'correct value' );
-    is( $agreement->Receiver, 'http://rt.example.com/sharing', 'correct value' );
+    is( $agreement->Receiver, RT->Config->Get('NHD_WebURL'), 'correct value' );
     like( $agreement->AccessKey, qr{^[0-9a-f]{40}$}i, 'correct value' );
 }
 
@@ -49,7 +49,7 @@ my $i = 0;
         Name => 'Test Company',
         Status => 'booo',
         Sender => 'http://hoster.example.com/sharing',
-        Receiver => 'http://rt.example.com/sharing',
+        Receiver => RT->Config->Get('NHD_WebURL'),
         AccessKey => sha1_hex( ''. ++$i ),
     );
     ok(!$id, "Couldn't create an agreement $uuid: $msg");
@@ -64,7 +64,7 @@ my $i = 0;
         Name => 'Test Company',
         Status => 'accepted',
         Sender => 'http://hoster.example.com/sharing',
-        Receiver => 'http://rt.example.com/sharing',
+        Receiver => RT->Config->Get('NHD_WebURL'),
         AccessKey => sha1_hex( ''. ++$i ),
     );
     ok(!$id, "Couldn't create an agreement $uuid: $msg");
diff --git a/t/rest/agreement.t b/t/rest/agreement.t
index 4d89e7d..9f0bb43 100644
--- a/t/rest/agreement.t
+++ b/t/rest/agreement.t
@@ -25,7 +25,7 @@ my $i = 0;
             name => 'Test Company',
             status => 'pending',
             sender_url => 'http://hoster.example.com/sharing',
-            receiver_url => 'http://rt.example.com/sharing',
+            receiver_url => RT->Config->Get('NHD_WebURL'),
             access_key => $access_key,
         },
     );
@@ -40,7 +40,7 @@ my $i = 0;
     is( $agreement->Name, 'Test Company', 'correct value' );
     is( $agreement->Status, 'pending', 'correct value' );
     is( $agreement->Sender, 'http://hoster.example.com/sharing', 'correct value' );
-    is( $agreement->Receiver, 'http://rt.example.com/sharing', 'correct value' );
+    is( $agreement->Receiver, RT->Config->Get('NHD_WebURL'), 'correct value' );
     like( $agreement->AccessKey, qr{^[0-9a-f]{40}$}i, 'correct value' );
 
     $response = $m->json_request( GET => '/agreements/'. $uuid );
@@ -65,7 +65,7 @@ my $i = 0;
             name => 'Test Company',
             status => 'pending',
             sender_url => 'http://hoster.example.com/sharing',
-            receiver_url => 'http://rt.example.com/sharing',
+            receiver_url => RT->Config->Get('NHD_WebURL'),
             access_key => $access_key,
         },
         'correct agreement',

commit 3b16a698ac486c2f616092a2fd44872cc2262a91
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sun Sep 25 14:34:21 2011 +0400

    add DeactivatedBy column

diff --git a/etc/schema.mysql b/etc/schema.mysql
index cf772fc..cff4c36 100644
--- a/etc/schema.mysql
+++ b/etc/schema.mysql
@@ -6,6 +6,7 @@ CREATE TABLE NHDAgreements (
   Sender varchar(240) CHARACTER SET ascii NOT NULL,
   Receiver varchar(240) CHARACTER SET ascii NOT NULL,
   AccessKey varchar(40) CHARACTER SET ascii NOT NULL,
+  DeactivatedBy varchar(10) CHARACTER SET ascii NULL DEFAULT NULL,
 
   PRIMARY KEY (id)
 ) ENGINE=InnoDB CHARACTER SET utf8;
diff --git a/lib/RT/NHD/Agreement.pm b/lib/RT/NHD/Agreement.pm
index 381c1f3..81925cf 100644
--- a/lib/RT/NHD/Agreement.pm
+++ b/lib/RT/NHD/Agreement.pm
@@ -45,6 +45,15 @@ sub ValidateStatus {
     return 1;
 }
 
+sub ValidateDeactivatedBy {
+    my $self = shift;
+    my $value = shift;
+
+    return 1 unless $value;
+    return 0 unless grep $_ eq lc $value, 'sender', 'receiver';
+    return 1;
+}
+
 sub ValidateSender { return (shift)->_ValidateURI( @_ ) }
 sub ValidateReceiver { return (shift)->_ValidateURI( @_ ) }
 
@@ -68,6 +77,7 @@ sub FromJSON {
         Sender => $args->{'sender_url'},
         Receiver => $args->{'receiver_url'},
         AccessKey => $args->{'access_key'},
+        DeactivatedBy => $args->{'deactivated_by'},
     };
 }
 
@@ -80,6 +90,7 @@ sub ForJSON {
         sender_url => $self->Sender,
         receiver_url => $self->Receiver,
         access_key => $self->AccessKey,
+        deactivated_by => $self->DeactivatedBy,
     };
 }
 
@@ -98,6 +109,8 @@ sub _CoreAccessible { return {
     {read => 1, write => 1, sql_type => 12, length => 240, is_blob => 0, is_numeric => 0, type => 'varchar(240)', default => ''},
     AccessKey =>
     {read => 1, write => 1, sql_type => 12, length => 40, is_blob => 0, is_numeric => 0, type => 'varchar(40)', default => ''},
+    DeactivatedBy =>
+    {read => 1, write => 1, sql_type => 10, length => 15, is_blob => 0, is_numeric => 0, type => 'varchar(10)', default => ''},
 } }
 
 RT::Base->_ImportOverlays();

commit c63935899a24042600e86adae090bc8269427811
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sun Sep 25 14:35:33 2011 +0400

    first implementation of Update method

diff --git a/lib/RT/NHD/Agreement.pm b/lib/RT/NHD/Agreement.pm
index 81925cf..a42c3eb 100644
--- a/lib/RT/NHD/Agreement.pm
+++ b/lib/RT/NHD/Agreement.pm
@@ -33,6 +33,79 @@ sub Create {
     return $self->SUPER::Create( %args );
 }
 
+sub Update {
+    my $self = shift;
+    my $by = shift;
+    my %args = @_;
+
+    unless ( $by eq 'Receiver' || $by eq 'Sender' ) {
+        return (0, "First argument should be either 'Sender' or 'Receiver'");
+    }
+
+    # filter out repeated values even if spec says update should only
+    # enlist new values
+    foreach my $field ( grep $_ ne 'id', keys %{ $self->TableAttributes } ) {
+        next unless exists $args{ $field };
+
+        my $cur = $self->$field();
+        my $new = $args{ $field };
+        next if (defined $new && !defined $cur)
+            || (!defined $new && defined $cur)
+            || $new ne $cur;
+
+        delete $args{ $field };
+    }
+
+    return (0, 'UUID can not be changed') if exists $args{'UUID'};
+
+    if ( $by eq 'Sender' ) {
+        return (0, 'Only receiver may change its URL')
+            if exists $args{'Receiver'};
+    } else {
+        return (0, 'Only sender may change Name')
+            if exists $args{'Name'};
+        return (0, 'Only sender may change its URL')
+            if exists $args{'Sender'};
+    }
+
+    if ( exists $args{'Status'} ) {
+        my $cur = $self->Status;
+        my $new = $args{'Status'} || '';
+
+        # XXX: not yet implemented
+    }
+
+    if ( exists $args{'DeactivatedBy'} ) {
+        return (0, 'DeactivatedBy can be changed only with Status')
+            unless exists $args{'Status'};
+
+        if ( $args{'Status'} eq 'inactive' ) {
+            return (0, "Inactivating agreement, DeactivatedBy should be \L$by")
+                unless lc $args{'DeactivatedBy'} eq lc $by;
+        }
+        elsif ( $self->Status eq 'inactive' ) {
+            return (0, "Re-activating agreement, DeactivatedBy should be set to empty")
+                if $args{'DeactivatedBy'};
+        }
+        else {
+            return (0, "Can not set DeactivatedBy when change status from '". $self->Status ."' to '$args{'Status'}'");
+        }
+    }
+
+
+    $RT::Handle->BeginTransaction;
+    foreach my $field ( grep $_ ne 'id', keys %{ $self->TableAttributes } ) {
+        next unless exists $args{ $field };
+
+        my $method = "Set$field";
+        my ($status, $msg) = $self->$method( $args{ $field } );
+        return $self->RollbackTransaction( "Couldn't update $field: $msg" )
+            unless $status;
+    }
+    $RT::Handle->Commit;
+    return (1, 'Updated');
+}
+
 sub ValidateUUID { return RT::Extension::NHD->CheckUUID( $_[1] ) }
 sub ValidateAccessKey { return RT::Extension::NHD->CheckUUID( $_[1] ) }
 
@@ -94,6 +167,24 @@ sub ForJSON {
     };
 }
 
+sub TableAttributes {
+    my $self = shift;
+    my $class = ref($self) || $self;
+    $self->_BuildTableAttributes unless $RT::Record::_TABLE_ATTR->{ $class };
+    return $RT::Record::_TABLE_ATTR->{ $class };
+}
+
+sub RollbackTransaction {
+    my $self = shift;
+    my $msg = shift;
+
+    $RT::Handle->Rollback;
+
+    $self->LoadByCols( id => $self->id );
+
+    return (0, $msg);
+}
+
 sub _CoreAccessible { return {
     id =>
     { read => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)' },
diff --git a/t/api/agreement.t b/t/api/agreement.t
index c827224..f4d6370 100644
--- a/t/api/agreement.t
+++ b/t/api/agreement.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use RT::Extension::NHD::Test tests => 25;
+use RT::Extension::NHD::Test tests => 23;
 use Digest::SHA1 qw(sha1_hex);
 
 {
@@ -70,3 +70,52 @@ my $i = 0;
     ok(!$id, "Couldn't create an agreement $uuid: $msg");
 }
 
+# simple update
+{
+    my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+    my $uuid = sha1_hex( ''. ++$i );
+    my ($id, $msg) = $agreement->Create(
+        UUID => $uuid,
+        Name => 'Test Company',
+        Status => 'pending',
+        Sender => 'http://hoster.example.com/sharing',
+        Receiver => RT->Config->Get('NHD_WebURL'),
+        AccessKey => sha1_hex( ''. ++$i ),
+    );
+    ok($id, "Created an agreement") or diag "error: $msg";
+
+    my ($status, $msg) = $agreement->Update(
+        Sender =>
+        Sender => 'http://hoster.moved.com/sharing',
+        AccessKey => sha1_hex( ''. ++$i ),
+    );
+    ok $status, 'updated URL of the sender by sender';
+    is( $agreement->Sender, 'http://hoster.moved.com/sharing', 'correct value' );
+    is( $agreement->AccessKey, sha1_hex( ''. $i ), 'correct value' );
+}
+
+# update with error
+{
+    my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+    my $uuid = sha1_hex( ''. ++$i );
+    my ($id, $msg) = $agreement->Create(
+        UUID => $uuid,
+        Name => 'Test Company',
+        Status => 'pending',
+        Sender => 'http://hoster.example.com/sharing',
+        Receiver => RT->Config->Get('NHD_WebURL'),
+        AccessKey => sha1_hex( ''. ++$i ),
+    );
+    ok($id, "Created an agreement") or diag "error: $msg";
+
+    my ($status, $msg) = $agreement->Update(
+        Sender =>
+        Sender => 'http://hoster.moved.com/sharing',
+        AccessKey => 'bad access key',
+    );
+    ok !$status, "updated failed: $msg";
+    # make sure we're transactional
+    is( $agreement->Sender, 'http://hoster.example.com/sharing', 'correct value' );
+    is( $agreement->AccessKey, sha1_hex( ''. $i ), 'correct value' );
+}
+

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



More information about the Bps-public-commit mailing list