|
@@ -4,6 +4,7 @@ should = chai.should()
|
|
|
modulePath = "../../../../app/js/RedisManager.js"
|
|
modulePath = "../../../../app/js/RedisManager.js"
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
Errors = require "../../../../app/js/Errors"
|
|
Errors = require "../../../../app/js/Errors"
|
|
|
|
|
+crypto = require "crypto"
|
|
|
|
|
|
|
|
describe "RedisManager", ->
|
|
describe "RedisManager", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
@@ -19,6 +20,7 @@ describe "RedisManager", ->
|
|
|
docLines: ({doc_id}) -> "doclines:#{doc_id}"
|
|
docLines: ({doc_id}) -> "doclines:#{doc_id}"
|
|
|
docOps: ({doc_id}) -> "DocOps:#{doc_id}"
|
|
docOps: ({doc_id}) -> "DocOps:#{doc_id}"
|
|
|
docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
|
|
docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
|
|
|
|
|
+ docHash: ({doc_id}) -> "DocHash:#{doc_id}"
|
|
|
projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
|
|
projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
|
|
|
pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
|
|
pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
|
|
|
docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
|
|
docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
|
|
@@ -38,10 +40,11 @@ describe "RedisManager", ->
|
|
|
@lines = ["one", "two", "three"]
|
|
@lines = ["one", "two", "three"]
|
|
|
@jsonlines = JSON.stringify @lines
|
|
@jsonlines = JSON.stringify @lines
|
|
|
@version = 42
|
|
@version = 42
|
|
|
|
|
+ @hash = crypto.createHash('sha1').update(@jsonlines).digest('hex')
|
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
|
@json_ranges = JSON.stringify @ranges
|
|
@json_ranges = JSON.stringify @ranges
|
|
|
@rclient.get = sinon.stub()
|
|
@rclient.get = sinon.stub()
|
|
|
- @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @project_id, @json_ranges])
|
|
|
|
|
|
|
+ @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @hash, @project_id, @json_ranges])
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
describe "successfully", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
@@ -56,7 +59,12 @@ describe "RedisManager", ->
|
|
|
@rclient.get
|
|
@rclient.get
|
|
|
.calledWith("DocVersion:#{@doc_id}")
|
|
.calledWith("DocVersion:#{@doc_id}")
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ it 'should get the hash', ->
|
|
|
|
|
+ @rclient.get
|
|
|
|
|
+ .calledWith("DocHash:#{@doc_id}")
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
+
|
|
|
it "should get the ranges", ->
|
|
it "should get the ranges", ->
|
|
|
@rclient.get
|
|
@rclient.get
|
|
|
.calledWith("Ranges:#{@doc_id}")
|
|
.calledWith("Ranges:#{@doc_id}")
|
|
@@ -67,6 +75,26 @@ describe "RedisManager", ->
|
|
|
.calledWith(null, @lines, @version, @ranges)
|
|
.calledWith(null, @lines, @version, @ranges)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
|
|
|
|
|
+ it 'should not log any errors', ->
|
|
|
|
|
+ @logger.error.calledWith()
|
|
|
|
|
+ .should.equal false
|
|
|
|
|
+
|
|
|
|
|
+ describe "with a corrupted document", ->
|
|
|
|
|
+ beforeEach ->
|
|
|
|
|
+ @badHash = "INVALID-HASH-VALUE"
|
|
|
|
|
+ @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonlines, @version, @badHash, @project_id, @json_ranges])
|
|
|
|
|
+ @RedisManager.getDoc @project_id, @doc_id, @callback
|
|
|
|
|
+
|
|
|
|
|
+ it 'should log a hash error', ->
|
|
|
|
|
+ @logger.error.calledWith()
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
+
|
|
|
|
|
+ it 'should return the document', ->
|
|
|
|
|
+ @callback
|
|
|
|
|
+ .calledWith(null, @lines, @version, @ranges)
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
describe "getDoc with an invalid project id", ->
|
|
describe "getDoc with an invalid project id", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
@another_project_id = "project-id-456"
|
|
@another_project_id = "project-id-456"
|
|
@@ -174,6 +202,7 @@ describe "RedisManager", ->
|
|
|
@lines = ["one", "two", "three"]
|
|
@lines = ["one", "two", "three"]
|
|
|
@ops = [{ op: [{ i: "foo", p: 4 }] },{ op: [{ i: "bar", p: 8 }] }]
|
|
@ops = [{ op: [{ i: "foo", p: 4 }] },{ op: [{ i: "bar", p: 8 }] }]
|
|
|
@version = 42
|
|
@version = 42
|
|
|
|
|
+ @hash = crypto.createHash('sha1').update(JSON.stringify(@lines)).digest('hex')
|
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
|
|
|
|
|
|
@rclient.exec = sinon.stub().callsArg(0)
|
|
@rclient.exec = sinon.stub().callsArg(0)
|
|
@@ -197,6 +226,11 @@ describe "RedisManager", ->
|
|
|
@rclient.set
|
|
@rclient.set
|
|
|
.calledWith("DocVersion:#{@doc_id}", @version)
|
|
.calledWith("DocVersion:#{@doc_id}", @version)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
+
|
|
|
|
|
+ it "should set the hash", ->
|
|
|
|
|
+ @rclient.set
|
|
|
|
|
+ .calledWith("DocHash:#{@doc_id}", @hash)
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
|
|
|
it "should set the ranges", ->
|
|
it "should set the ranges", ->
|
|
|
@rclient.set
|
|
@rclient.set
|
|
@@ -272,6 +306,7 @@ describe "RedisManager", ->
|
|
|
@rclient.exec.yields()
|
|
@rclient.exec.yields()
|
|
|
@lines = ["one", "two", "three"]
|
|
@lines = ["one", "two", "three"]
|
|
|
@version = 42
|
|
@version = 42
|
|
|
|
|
+ @hash = crypto.createHash('sha1').update(JSON.stringify(@lines)).digest('hex')
|
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
|
|
|
|
|
|
describe "with non-empty ranges", ->
|
|
describe "with non-empty ranges", ->
|
|
@@ -287,6 +322,11 @@ describe "RedisManager", ->
|
|
|
@rclient.set
|
|
@rclient.set
|
|
|
.calledWith("DocVersion:#{@doc_id}", @version)
|
|
.calledWith("DocVersion:#{@doc_id}", @version)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
+
|
|
|
|
|
+ it "should set the hash", ->
|
|
|
|
|
+ @rclient.set
|
|
|
|
|
+ .calledWith("DocHash:#{@doc_id}", @hash)
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
|
|
|
it "should set the ranges", ->
|
|
it "should set the ranges", ->
|
|
|
@rclient.set
|
|
@rclient.set
|
|
@@ -333,6 +373,11 @@ describe "RedisManager", ->
|
|
|
@rclient.del
|
|
@rclient.del
|
|
|
.calledWith("DocVersion:#{@doc_id}")
|
|
.calledWith("DocVersion:#{@doc_id}")
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
+
|
|
|
|
|
+ it "should delete the hash", ->
|
|
|
|
|
+ @rclient.del
|
|
|
|
|
+ .calledWith("DocHash:#{@doc_id}")
|
|
|
|
|
+ .should.equal true
|
|
|
|
|
|
|
|
it "should delete the project_id for the doc", ->
|
|
it "should delete the project_id for the doc", ->
|
|
|
@rclient.del
|
|
@rclient.del
|