Repository: goldibex/targaryen
Pull request #95 (2016-12-08 17:10:47)

Fix plugin handling of timestamps
Added tests and fix timestamp argument position in database write calls


Patch:
diff --git a/plugins/chai.js b/plugins/chai.js
index 124ac16..c499997 100644
--- a/plugins/chai.js
+++ b/plugins/chai.js
@@ -82,7 +82,7 @@ function chaiTargaryen(chai, utils) {
 
     case 'write':
       newData = utils.flag(this, 'operationData');
-      result = data.write(path, newData, now);
+      result = data.write(path, newData, undefined, now);
       break;
 
     case 'patch':
diff --git a/plugins/jasmine.js b/plugins/jasmine.js
index 0923e72..45dcd14 100644
--- a/plugins/jasmine.js
+++ b/plugins/jasmine.js
@@ -54,7 +54,7 @@ exports.matchers = {
 
       const data = targaryen.util.getFirebaseData().as(auth);
 
-      const result = data.write(path, newData, now);
+      const result = data.write(path, newData, undefined, now);
 
       return {
         pass: result.allowed === true,
@@ -70,7 +70,7 @@ exports.matchers = {
 
       const data = targaryen.util.getFirebaseData().as(auth);
 
-      const result = data.write(path, newData, now);
+      const result = data.write(path, newData, undefined, now);
 
       return {
         pass: result.allowed === false,


Test patch:
diff --git a/test/jasmine/core.js b/test/jasmine/core.js
index 4aa226f..2d383c8 100644
--- a/test/jasmine/core.js
+++ b/test/jasmine/core.js
@@ -103,4 +103,31 @@ describe('the targaryen Jasmine plugin', function() {
     expect(targaryen.users.facebook).canPatch('/foo', {bar: 2, baz: 2});
   });
 
+  it('can set operation time stamp', function() {
+    targaryen.setFirebaseData({foo: 2000});
+    targaryen.setFirebaseRules({
+      rules: {
+        $key: {
+          '.read': 'data.val() > now',
+          '.write': 'newData.val() == now'
+        }
+      }
+    });
+
+    expect(null).canRead('/foo', 1000);
+    expect(null).cannotRead('/foo');
+
+    expect(null).canWrite('/foo', {'.sv': 'timestamp'}, 1000);
+    expect(null).canWrite('/foo', {'.sv': 'timestamp'});
+
+    expect(null).canWrite('/foo', 1000, 1000);
+    expect(null).cannotWrite('/foo', 1000, 2000);
+
+    expect(null).canPatch('/', {foo: {'.sv': 'timestamp'}}, 1000);
+    expect(null).canPatch('/', {foo: {'.sv': 'timestamp'}});
+
+    expect(null).canPatch('/', {foo: 1000}, 1000);
+    expect(null).cannotPatch('/', {foo: 1000}, 2000);
+  });
+
 });
diff --git a/test/spec/plugins/chai.js b/test/spec/plugins/chai.js
index 5bc7bcd..83acfd5 100644
--- a/test/spec/plugins/chai.js
+++ b/test/spec/plugins/chai.js
@@ -108,4 +108,31 @@ describe('Chai plugin', function() {
     expect(targaryen.users.facebook).can.patch({bar: 2, baz: 2}).path('/foo');
   });
 
+  it('can set operation time stamp', function() {
+    targaryen.setFirebaseData({foo: 2000});
+    targaryen.setFirebaseRules({
+      rules: {
+        $key: {
+          '.read': 'data.val() > now',
+          '.write': 'newData.val() == now'
+        }
+      }
+    });
+
+    expect(null).can.readAt(1000).path('/foo');
+    expect(null).cannot.read.path('/foo');
+
+    expect(null).can.write({'.sv': 'timestamp'}, 1000).path('/foo');
+    expect(null).can.write({'.sv': 'timestamp'}).path('/foo');
+
+    expect(null).can.write(1000, 1000).path('/foo');
+    expect(null).cannot.write(2000, 1000).path('/foo');
+
+    expect(null).can.patch({foo: {'.sv': 'timestamp'}}, 1000).path('/');
+    expect(null).can.patch({foo: {'.sv': 'timestamp'}}).path('/');
+
+    expect(null).can.patch({foo: 1000, bar: 1000}, 1000).path('/');
+    expect(null).cannot.patch({foo: 1000, bar: 1000}, 2000).path('/');
+  });
+
 });