[Bps-public-commit] RT-Extension-NHD branch, master, updated. 40d221b84e230db3c6a558a9d6c8dae1f0c2d570
Ruslan Zakirov
ruz at bestpractical.com
Fri Sep 30 17:17:40 EDT 2011
The branch, master has been updated
via 40d221b84e230db3c6a558a9d6c8dae1f0c2d570 (commit)
from c63935899a24042600e86adae090bc8269427811 (commit)
Summary of changes:
html/NoAuth/NHD/1.0/agreements/dhandler | 36 ++++++++---
lib/RT/NHD/Agreement.pm | 106 +++++++++++++++++++++++++++++--
t/api/agreement.t | 50 +++++++++------
3 files changed, 155 insertions(+), 37 deletions(-)
- Log -----------------------------------------------------------------
commit 40d221b84e230db3c6a558a9d6c8dae1f0c2d570
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date: Fri Sep 30 22:33:21 2011 +0400
prepare us for sending data to remote server
diff --git a/html/NoAuth/NHD/1.0/agreements/dhandler b/html/NoAuth/NHD/1.0/agreements/dhandler
index 26bc21f..e03c736 100644
--- a/html/NoAuth/NHD/1.0/agreements/dhandler
+++ b/html/NoAuth/NHD/1.0/agreements/dhandler
@@ -35,19 +35,38 @@ return RT::Extension::NHD->BadWebRequest('Unauthorized')
my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
$agreement->Load( $uuid );
-return RT::Extension::NHD->BadWebRequest('Not Found')
- if !$agreement->id && $action ne 'create';
+if ( $action eq 'create' && $agreement->id ) {
+ return RT::Extension::NHD->BadWebRequest(
+ 'Unprocessable Entity',
+ "Couldn't create an agreement: record already exists"
+ );
+}
+elsif ( $action ne 'create' ) {
+ return RT::Extension::NHD->BadWebRequest('Not Found')
+ unless $agreement->id;
-if ( $token ne join ':', ($agreement->UUID || $uuid || $duuid), ($agreement->AccessKey || $access_key) ) {
- return RT::Extension::NHD->BadWebRequest('Forbidden')
+ if ( $token ne join ':', $agreement->UUID, $agreement->AccessKey ) {
+ return RT::Extension::NHD->BadWebRequest('Forbidden')
+ }
}
+my %data = %{ $agreement->FromJSON( \%ARGS ) };
+
+my ($user, $msg) = $agreement->LoadOrCreateUser( %data );
+return RT::Extension::NHD->BadWebRequest(
+ 'Unprocessable Entity',
+ "Couldn't create user: $msg"
+) unless $user;
+
+$agreement->CurrentUser( RT::CurrentUser->new( $user ) );
+
if ( $agreement->id ) {
+
if ( $action eq 'show' ) {
return RT::Extension::NHD->WebSendJSON( $agreement->ForJSON );
}
elsif ( $action eq 'update' ) {
- my ($status, $msg) = $agreement->UpdateBySender( %{ $agreement->FromJSON( \%ARGS ) } );
+ my ($status, $msg) = $agreement->Update( %data );
unless ( $status ) {
RT->Logger->error("Couldn't update NHD agreement: $msg");
return RT::Extension::NHD->BadWebRequest('Unprocessable Entity');
@@ -58,15 +77,12 @@ if ( $agreement->id ) {
return RT::Extension::NHD->BadWebRequest;
}
}
-elsif ( $action eq 'create' ) {
- my ($status, $msg) = $agreement->Create( %{ $agreement->FromJSON( \%ARGS ) } );
+else {
+ my ($status, $msg) = $agreement->Create( %data );
unless ( $status ) {
RT->Logger->error("Couldn't create NHD agreement: $msg");
return RT::Extension::NHD->BadWebRequest('Unprocessable Entity');
}
return RT::Extension::NHD->GoodWebRequest('Created');
}
-else {
- return RT::Extension::NHD->BadWebRequest;
-}
</%INIT>
\ No newline at end of file
diff --git a/lib/RT/NHD/Agreement.pm b/lib/RT/NHD/Agreement.pm
index a42c3eb..60c2603 100644
--- a/lib/RT/NHD/Agreement.pm
+++ b/lib/RT/NHD/Agreement.pm
@@ -23,25 +23,42 @@ sub Create {
my $self = shift;
my %args = @_;
+ my $we_are = $self->WhoWeAre( %args );
+ unless ( $we_are ) {
+ return (undef, "Either sender or receiver should be '". RT->Config->Get('NHD_WebURL') ."'");
+ }
+
+ my $by = $self->WhoIsCurrentUser( %args );
+ unless ( $by ) {
+ return (0, "Current user is not a sender or receiver");
+ }
+
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'");
+
+ my @rv = $self->SUPER::Create( %args );
+
+ if ( $we_are eq $by ) {
+ my ($status, $msg) = $self->SendUpdate;
+ return $self->RollbackTransaction( "Couldn't send update to remote host: $msg" )
+ unless $status;
}
- return $self->SUPER::Create( %args );
+
+ return @rv;
}
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'");
+ my $by = $self->WhoIsCurrentUser;
+ unless ( $by ) {
+ return (0, "Current user is not a sender or reciever");
}
+ my $we_are = $self->WhoWeAre;
+
# filter out repeated values even if spec says update should only
# enlist new values
foreach my $field ( grep $_ ne 'id', keys %{ $self->TableAttributes } ) {
@@ -102,10 +119,85 @@ sub Update {
return $self->RollbackTransaction( "Couldn't update $field: $msg" )
unless $status;
}
+ if ( $by eq $we_are ) {
+ my ($status, $msg) = $self->SendUpdate( Fields => [keys %args] );
+ return $self->RollbackTransaction( "Couldn't send update to remote host: $msg" )
+ unless $status;
+ }
$RT::Handle->Commit;
+
return (1, 'Updated');
}
+sub SendUpdate {
+ my $self = shift;
+ return (1, 'Updated remote host');
+}
+
+sub WhoWeAre {
+ my $self = shift;
+ my %args = @_;
+
+ my $our_url = RT->Config->Get('NHD_WebURL');
+
+ my $res;
+ if ( ($self->Sender || $args{'Sender'}) eq $our_url ) {
+ $res = 'Sender';
+ }
+ elsif ( ($self->Receiver || $args{'Receiver'} || '') eq $our_url ) {
+ $res = 'Receiver';
+ }
+ return $res;
+}
+
+sub WhoIsCurrentUser {
+ my $self = shift;
+ my %args = @_;
+
+ my $user_url = $self->CurrentUser->UserObj->Name;
+ if ( ($self->Sender || $args{'Sender'} || '') eq $user_url ) {
+ return 'Sender';
+ }
+ elsif ( ($self->Receiver || $args{'Receiver'} || '') eq $user_url ) {
+ return 'Receiver';
+ }
+ return $self->WhoWeAre( %args );
+}
+
+sub LoadOrCreateUser {
+ my $self = shift;
+ my %args = @_;
+
+ my $our_url = RT->Config->Get('NHD_WebURL');
+
+ my $url;
+ if ( ($self->Sender || $args{'Sender'}) eq $our_url ) {
+ $url = $self->Receiver || $args{'Receiver'} || '';
+ }
+ elsif ( ($self->Receiver || $args{'Receiver'} || '') eq $our_url ) {
+ $url = $self->Sender || $args{'Sender'} || '';
+ }
+ else {
+ return (undef, "Either sender or receiver should be '$our_url'");
+ }
+ unless ( $url ) {
+ return (undef, "Undefined URL");
+ }
+ my $user = RT::User->new( RT->SystemUser );
+ $user->Load( $url );
+ return ($user, 'Loaded') if $user->id;
+
+ my ($status, $msg) = $user->Create(
+ Name => $url,
+ Privileged => 0,
+ Disabled => 0,
+ Comments => "Auto created on submitting Networked HelpDesk Agreement",
+ );
+ return ($status, $msg) unless $status;
+
+ return ($user, 'Created');
+}
+
sub ValidateUUID { return RT::Extension::NHD->CheckUUID( $_[1] ) }
sub ValidateAccessKey { return RT::Extension::NHD->CheckUUID( $_[1] ) }
diff --git a/t/api/agreement.t b/t/api/agreement.t
index f4d6370..5cdbd08 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 => 23;
+use RT::Extension::NHD::Test tests => 24;
use Digest::SHA1 qw(sha1_hex);
{
@@ -12,17 +12,29 @@ use Digest::SHA1 qw(sha1_hex);
isa_ok($agreement, 'RT::Record');
}
+my $remote_url = 'http://hoster.example.com/sharing';
+my $remote_user;
+{
+ $remote_user = RT::User->new( RT->SystemUser );
+ my ($status, $msg) = $remote_user->Create(
+ Name => $remote_url,
+ Privileged => 0,
+ Disabled => 0,
+ );
+ ok $status, "created an user";
+}
+
my $i = 0;
{
my $uuid = sha1_hex( ''. ++$i );
- my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+ my $agreement = RT::NHD::Agreement->new( RT::CurrentUser->new( $remote_user ) );
my ($id, $msg) = $agreement->Create(
UUID => $uuid,
Name => 'Test Company',
Status => 'pending',
- Sender => 'http://hoster.example.com/sharing',
+ Sender => $remote_url,
Receiver => RT->Config->Get('NHD_WebURL'),
AccessKey => sha1_hex( ''. ++$i ),
);
@@ -35,20 +47,20 @@ my $i = 0;
is( $agreement->UUID, $uuid, 'correct value' );
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->Sender, $remote_url, 'correct value' );
is( $agreement->Receiver, RT->Config->Get('NHD_WebURL'), 'correct value' );
like( $agreement->AccessKey, qr{^[0-9a-f]{40}$}i, 'correct value' );
}
# bad status
{
- my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+ my $agreement = RT::NHD::Agreement->new( RT::CurrentUser->new( $remote_user ) );
my $uuid = sha1_hex( ''. ++$i );
my ($id, $msg) = $agreement->Create(
UUID => $uuid,
Name => 'Test Company',
Status => 'booo',
- Sender => 'http://hoster.example.com/sharing',
+ Sender => $remote_url,
Receiver => RT->Config->Get('NHD_WebURL'),
AccessKey => sha1_hex( ''. ++$i ),
);
@@ -57,65 +69,63 @@ my $i = 0;
# can only be created with pending status
{
- my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+ my $agreement = RT::NHD::Agreement->new( RT::CurrentUser->new( $remote_user ) );
my $uuid = sha1_hex( ''. ++$i );
my ($id, $msg) = $agreement->Create(
UUID => $uuid,
Name => 'Test Company',
Status => 'accepted',
- Sender => 'http://hoster.example.com/sharing',
+ Sender => $remote_url,
Receiver => RT->Config->Get('NHD_WebURL'),
AccessKey => sha1_hex( ''. ++$i ),
);
ok(!$id, "Couldn't create an agreement $uuid: $msg");
}
-# simple update
+# simple update by sender we are receiver
{
- my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+ my $agreement = RT::NHD::Agreement->new( RT::CurrentUser->new( $remote_user ) );
my $uuid = sha1_hex( ''. ++$i );
my ($id, $msg) = $agreement->Create(
UUID => $uuid,
Name => 'Test Company',
Status => 'pending',
- Sender => 'http://hoster.example.com/sharing',
+ Sender => $remote_url,
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',
+ (my $status, $msg) = $agreement->Update(
+ Name => 'Correct Test Company',
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->Name, 'Correct Test Company', 'correct value' );
is( $agreement->AccessKey, sha1_hex( ''. $i ), 'correct value' );
}
# update with error
{
- my $agreement = RT::NHD::Agreement->new( RT->SystemUser );
+ my $agreement = RT::NHD::Agreement->new( RT::CurrentUser->new( $remote_user ) );
my $uuid = sha1_hex( ''. ++$i );
my ($id, $msg) = $agreement->Create(
UUID => $uuid,
Name => 'Test Company',
Status => 'pending',
- Sender => 'http://hoster.example.com/sharing',
+ Sender => $remote_url,
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',
+ Name => 'Correct Test Company',
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->Name, 'Test Company', 'correct value' );
is( $agreement->AccessKey, sha1_hex( ''. $i ), 'correct value' );
}
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list