Permalink
Browse files

Added name, ctime, mtime and creator to initial document metadata

  • Loading branch information...
1 parent d0dd190 commit efed4db7d480de9f462d2ad48b1197bbaa003d6a @josephg committed Jan 26, 2012
Showing with 48 additions and 5 deletions.
  1. +10 −0 src/server/useragent.coffee
  2. +38 −5 test/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
View
@@ -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()

0 comments on commit efed4db

Please sign in to comment.