Skip to content

Commit

Permalink
Added name, ctime, mtime and creator to initial document metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Jan 26, 2012
1 parent d0dd190 commit efed4db
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/server/useragent.coffee
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 38 additions & 5 deletions test/useragent.coffee
Expand Up @@ -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'

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit efed4db

Please sign in to comment.