[Bps-public-commit] rt-extension-rest2 branch, master, updated. 22af80f2be7ee13ff99a6158a329c9dc237a5199

Shawn Moore shawn at bestpractical.com
Fri Jul 7 12:33:28 EDT 2017


The branch, master has been updated
       via  22af80f2be7ee13ff99a6158a329c9dc237a5199 (commit)
      from  9957fcaeacf3bdd863ac125af915b61456a0db86 (commit)

Summary of changes:
 t/conflict.t | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 110 insertions(+), 9 deletions(-)

- Log -----------------------------------------------------------------
commit 22af80f2be7ee13ff99a6158a329c9dc237a5199
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri Jul 7 16:33:17 2017 +0000

    Improve conflict tests

diff --git a/t/conflict.t b/t/conflict.t
index 56d3337..b424348 100644
--- a/t/conflict.t
+++ b/t/conflict.t
@@ -33,8 +33,8 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     );
     is($res->code, 200);
     is($mech->json_response->{Subject}, 'Version 1');
-    my $etag = $res->header('ETag');
-    ok($etag, "got an ETag");
+    my $first_etag = $res->header('ETag');
+    ok($first_etag, "got an ETag");
 
     $payload = {
         Subject => 'Version 2',
@@ -42,7 +42,7 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     $res = $mech->put_json($ticket_url,
         $payload,
         'Authorization' => $auth,
-        'If-Match' => $etag,
+        'If-Match' => $first_etag,
     );
     is($res->code, 200);
 
@@ -51,8 +51,8 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     );
     is($res->code, 200);
     is($mech->json_response->{Subject}, 'Version 2');
-    $etag = $res->header('ETag');
-    ok($etag, "got an ETag");
+    my $second_etag = $res->header('ETag');
+    ok($second_etag, "got an ETag");
 
     $payload = {
         Subject => 'Version 3',
@@ -60,7 +60,7 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     $res = $mech->put_json($ticket_url,
         $payload,
         'Authorization' => $auth,
-        'If-Match' => $etag,
+        'If-Match' => $second_etag,
     );
     is($res->code, 200);
 
@@ -69,11 +69,25 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     );
     is($res->code, 200);
     is($mech->json_response->{Subject}, 'Version 3');
-    $etag = $res->header('ETag');
-    ok($etag, "got an ETag");
+    my $third_etag = $res->header('ETag');
+    ok($third_etag, "got an ETag");
+
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+        'If-None-Match' => $third_etag,
+    );
+    is($res->code, 304, 'not modified');
+
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+        'If-None-Match' => $second_etag,
+    );
+    is($res->code, 200, 'has been modified');
+    is($mech->json_response->{Subject}, 'Version 3');
+    is($res->header('ETag'), $third_etag, 'ETag unchanged');
 }
 
-# Create and update a ticket with conflicts
+# Create and update a ticket with reusing old ETags
 {
     my ($ticket_url, $ticket_id);
     my $payload = {
@@ -149,6 +163,93 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     is($mech->json_response->{Subject}, 'Version 3');
     my $fourth_etag = $res->header('ETag');
     ok($fourth_etag, "got an ETag");
+}
+
+# Create and update a ticket with legitimate conflicts
+{
+    my ($ticket_url, $ticket_id);
+    my $payload = {
+        Subject => 'Version 1',
+        Queue   => 'General',
+    };
+
+    my $res = $mech->post_json("$rest_base_path/ticket",
+        $payload,
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    ok($ticket_url = $res->header('location'));
+    ok(($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)]);
+
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    is($mech->json_response->{Subject}, 'Version 1');
+    my $first_etag = $res->header('ETag');
+    ok($first_etag, "got an ETag");
+
+    $payload = {
+        Subject => 'Version 2',
+    };
+    $res = $mech->put_json($ticket_url,
+        $payload,
+        'Authorization' => $auth,
+        'If-Match' => $first_etag,
+    );
+    is($res->code, 200);
+
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    is($mech->json_response->{Subject}, 'Version 2');
+    my $second_etag = $res->header('ETag');
+    ok($second_etag, "got an ETag");
+
+    # some other user comes in to update the ticket...
+    my $ticket_obj = RT::Ticket->new(RT->SystemUser);
+    $ticket_obj->Load($ticket_id);
+    $ticket_obj->Correspond(Content => 'oops');
+
+    $payload = {
+        Subject => 'Version 3',
+    };
+    $res = $mech->put_json($ticket_url,
+        $payload,
+        'Authorization' => $auth,
+        'If-Match' => $second_etag, # <-- note old etag use
+    );
+    is($res->code, 412);
+    is($mech->json_response->{message}, 'Precondition Failed');
 
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    is($mech->json_response->{Subject}, 'Version 2');
+    my $third_etag = $res->header('ETag');
+    ok($third_etag, "got an ETag");
+    isnt($third_etag, $second_etag, "ETag is changed from the previous one");
+
+    # now at this point the REST API consumer can either prompt the user
+    # what to do, or analyze the changes to see that the other changes
+    # made don't affect the changes we want to make
+
+    $res = $mech->put_json($ticket_url,
+        $payload,
+        'Authorization' => $auth,
+        'If-Match' => $third_etag,
+    );
+    is($res->code, 200);
+
+    $res = $mech->get($ticket_url,
+        'Authorization' => $auth,
+    );
+    is($res->code, 200);
+    is($mech->json_response->{Subject}, 'Version 3');
+    my $fourth_etag = $res->header('ETag');
+    ok($fourth_etag, "got an ETag");
 }
+
 done_testing;

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


More information about the Bps-public-commit mailing list