test for Account.authenticate

This commit is contained in:
jysperm
2014-10-24 12:08:23 +08:00
parent 0666d0b796
commit 62528b5b58
2 changed files with 38 additions and 0 deletions

View File

@@ -177,6 +177,22 @@ Account.statics.generateToken = (callback) ->
else
callback token
# @param callback(Token, Account)
Account.statics.authenticate = (token, callback) ->
unless token
return callback()
@findOneAndUpdate
'tokens.token': token
,
$set:
'tokens.$.updated_at': new Date()
, (err, account) ->
matched_token = _.findWhere account?.tokens,
token: token
callback matched_token, account
# @param callback(err, Token)
Account.methods.createToken = (type, payload, callback) ->
models.Account.generateToken (token) =>

View File

@@ -115,6 +115,28 @@ describe 'model/Account', ->
err.should.be.exist
done()
describe 'authenticate', ->
it 'should success', (done) ->
Account.authenticate util.token, (token, account) ->
token.token.should.equal util.token
account.id.should.equal util.account.id
done()
it 'should fail with no token', (done) ->
Account.authenticate '', (token, account) ->
expect(token).to.not.exist
expect(account).to.not.exist
done()
it 'should fail with invalid token', (done) ->
Account.authenticate 'invalid token', (token, account) ->
expect(token).to.not.exist
expect(account).to.not.exist
done()
describe 'matchPassword', ->
it 'should be matched', ->
util.account.matchPassword(util.password).should.be.ok