From efed4db7d480de9f462d2ad48b1197bbaa003d6a Mon Sep 17 00:00:00 2001 From: Joseph Date: Fri, 27 Jan 2012 08:09:25 +1100 Subject: [PATCH] Added name, ctime, mtime and creator to initial document metadata --- src/server/useragent.coffee | 10 +++++++++ test/useragent.coffee | 43 ++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/server/useragent.coffee b/src/server/useragent.coffee index 4961f98c..e5bee8ce 100644 --- a/src/server/useragent.coffee +++ b/src/server/useragent.coffee @@ -24,6 +24,9 @@ module.exports = (model, options) -> # This is a map from docName -> listener function @listeners = {} + # Should be manually set by the auth function. + @name = null + # We have access to these with socket.io, but I'm not sure we can support # these properties on the REST API or sockjs, etc. #xdomain: data.xdomain @@ -74,6 +77,13 @@ module.exports = (model, options) -> # We don't check that types[type.name] == type. That might be important at some point. type = types[type] if typeof type == 'string' + # I'm not sure what client-specified metadata should be allowed in the document metadata + # object. For now, I'm going to ignore all create metadata until I know how it should work. + meta = {} + + meta.creator = @name if @name + meta.ctime = meta.mtime = Date.now() + # The action object has a 'type' property already. Hence the doc type is renamed to 'docType' @doAuth {docName, docType:type, meta}, 'create', callback, => model.create docName, type, meta, callback diff --git a/test/useragent.coffee b/test/useragent.coffee index 09393f59..6f5167d8 100644 --- a/test/useragent.coffee +++ b/test/useragent.coffee @@ -38,8 +38,7 @@ genTests = (async) -> testCase else @auth agent, action - @auth = (agent, action) -> - throw new Error "Unexpected call to @auth(#{action.name})" + @auth = (agent, action) -> action.accept() @name = 'testingdoc' @@ -201,15 +200,49 @@ genTests = (async) -> testCase @model.create = (docName, type, meta, callback) => test.strictEqual docName, @name test.strictEqual type, types.simple - test.deepEqual meta, {} callback() @agent.create @name, 'simple', {}, (error) => test.equal error, null - test.expect 10 + test.expect 9 + test.done() + + 'create sets meta.creator:agent.name if the agent has .name set': (test) -> @connect => + # Technically, this should probably be set during the initial connect, but it doesn't matter. + @agent.name = 'laura' + + @model.create = (docName, type, meta, callback) => + test.strictEqual meta.creator, 'laura' + callback() + + @agent.create @name, 'simple', {}, (error) => + test.equal error, null + test.expect 2 test.done() -# 'create sets the client id as the au + 'create does not set meta.creator if the agent does not have .name set': (test) -> @connect => + @model.create = (docName, type, meta, callback) => + test.strictEqual meta.creator, undefined + callback() + + @agent.create @name, 'simple', {}, (error) => test.done() + + 'create sets meta.ctime and mtime': (test) -> @connect => + @model.create = (docName, type, meta, callback) => + test.ok (Date.now() - meta.ctime) < 20 + test.ok (Date.now() - meta.mtime) < 20 + callback() + + @agent.create @name, 'simple', {}, (error) => test.done() + + "A client can't override ctime, mtime and creator": (test) -> @connect => + @model.create = (docName, type, meta, callback) => + test.ok (Date.now() - meta.ctime) < 20 + test.ok (Date.now() - meta.mtime) < 20 + test.strictEqual meta.creator, undefined + callback() + + @agent.create @name, 'simple', {creator:'fred', ctime:10, mtime:20}, (error) => test.done() 'create not allowed if canCreate() rejects': (test) -> @connect => @auth = (agent, action) -> action.reject()