From 996a688236bc3841a9638aefc834835c5e05654d Mon Sep 17 00:00:00 2001 From: Luc Boissaye Date: Thu, 2 Aug 2012 15:41:38 +0200 Subject: [PATCH] All Test working well with callback --- lib/access/access.coffee | 69 ++++++++---------- lib/app.coffee | 42 +++++------ lib/auth/auth.coffee | 137 +++++++++++++++++------------------ lib/component.coffee | 26 +++++-- lib/event/event.coffee | 41 +++++------ lib/group/group.coffee | 9 +-- lib/store/local/store.coffee | 45 +++++++----- lib/store/local/token.coffee | 10 +-- lib/store/local/user.coffee | 8 +- lib/token/token.coffee | 15 ++-- test/access.coffee | 47 +++--------- test/event.coffee | 6 +- test/login.coffee | 2 +- test/store/group.coffee | 72 +++++++++--------- test/store/token.coffee | 43 ++++++----- test/store/user.coffee | 29 ++++---- 16 files changed, 290 insertions(+), 311 deletions(-) diff --git a/lib/access/access.coffee b/lib/access/access.coffee index 6cc763c..ad5ccb4 100644 --- a/lib/access/access.coffee +++ b/lib/access/access.coffee @@ -13,62 +13,57 @@ module.exports = class Access extends Component _isUserMemberOfGroup: (userId, groupName)-> _ = @ return (cb)-> - try - _.app.stores.group.findGroupByName(groupName, (group)-> - _.app.stores.group.isUserMemberOfGroupCache(userId, group.id,(res)-> + _.app.stores.group.findGroupByName(groupName, (err,group)-> + if err? + if err[0] == 'Not found' + cb(null,false) + return + else + cb(err,null) + return + _.app.stores.group.isUserMemberOfGroupCache(userId, group.id,(err,res)-> cb(null, res) ) ) - catch e - if e == 'Not found' - cb(null, false) - return - throw e - #!TODO improve this function with cache _checkToken: (token, callbackOK)-> - try - @token.getToken(token, (datas)-> - #!TODO Token read only ? - #!TODO Token time expiration - if datas.userId? - console.log "TOKEN OK" - callbackOK(datas.userId) - ) - catch e - throw e + _ = @ + @token.getToken(token, (err,datas)-> + _.checkErr(err) + #!TODO Token read only ? + #!TODO Token time expiration + if datas.userId? + callbackOK(null,datas.userId) + ) check : (token, groups, cb, action = "undefined") -> _ = @ @_checkToken(token, - (userId)-> + (err,userId)-> + _.checkErr(err) groupCheck = [] for g in groups groupCheck.push(_._isUserMemberOfGroup(userId, g)) - try - require('async').parallel(groupCheck,(err,res)-> - if true in res - cb(userId) - _.emit('access/'+action, - granted : true - token : token - userId : userId - groups : groups - ) - return + require('async').parallel(groupCheck,(err,res)-> + _.checkErr(err) + if true in res + cb(null,userId) _.emit('access/'+action, - granted : false + granted : true token : token userId : userId groups : groups ) - throw 'Access denied' return + _.emit('access/'+action, + granted : false + token : token + userId : userId + groups : groups ) - catch e - console.log "ERROR IN CHECK ?#{e}?" - console.log e - throw e + cb(['Access denied'],null) + return + ) ) _add : (groupName, token, cb)-> _ = @ diff --git a/lib/app.coffee b/lib/app.coffee index 112ed2a..5d90a5d 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -1,44 +1,39 @@ + module.exports = class App - constructor : (app,@configs)-> + constructor : (express,@configs)-> _ = @ + Log = require 'log' + @log = new Log() EventEmitter = require('eventemitter2').EventEmitter2 @_event = new EventEmitter( wildcard:true ) @_event.on('*',(infos)-> - console.log this.event - console.log infos + _.log.debug "EVENT #{@event} : #{JSON.stringify(infos)}" ) #!TODO move this function to access... @_event.once('token/new',(datas)-> console.log "Check INIT ROOT ?" - try - _.stores.group.findGroupByName('root',()->) - catch e - if e == 'Not found' - try - _.stores.group.addGroup('_root',(groupId)-> - _.stores.group.addUserToGroup(datas.userId,groupId,(res)-> + _.stores.group.findGroupByName('root',(err,group)-> + if group == null + console.log "ROOT NULL ?" + _.stores.group.addGroup('_root',(err,groupId)-> + _.stores.group.addUserToGroup(datas.userId,groupId,(err,res)-> + if !res + throw "Error root access granted..." + _.stores.group.addUserToGroupCache(datas.userId,groupId,(err,res)-> if !res throw "Error root access granted..." - _.stores.group.addUserToGroupCache(datas.userId,groupId,(res)-> - if !res - throw "Error root access granted..." - _.emit('root/new',datas) - ) + _.emit('root/new',datas) ) ) - catch e - console.log "ROOT INIT ERROR" - console.log e - else - throw e - + ) + ) ) #@log = @app.log - @app = app - @app[ "auth" or @configs.bind] = @ #App binding + @express = express + @express[ "auth" or @configs.bind] = @ #express binding #Init stores : @stores = {} @@ -82,7 +77,6 @@ module.exports = class App # Events ### emit : (args...)-> - console.log "EMIT ?" @event.emit(args...) on : (args...)-> @event.on(args...) diff --git a/lib/auth/auth.coffee b/lib/auth/auth.coffee index abcaebf..2c3d7d4 100644 --- a/lib/auth/auth.coffee +++ b/lib/auth/auth.coffee @@ -5,9 +5,58 @@ module.exports = class Auth extends Component @._everyAuth() @._routes() + addUser : (source='', id='', datas={},cb)-> + _ = @ + store = @app.stores.user + store.addUser(source,id,datas,(err,userId)-> + _.checkErr(err) + cb(null,userId) + _.emit('user/new', + userId:userId + ) + ) + + + login : (source, id, datas, user, cb)-> + _ = @ + store = @app.stores.user + #Check on user : Does the user is already logged in ? + if user != null + # Is it a new way of connection ? + #@log.debug "User Is already in, let's add a way of login !" + else + store.findUserBySourceAndId(source,id,(err,user)-> + if err? + if err[0] == 'Not found' + store.addUser(source,id,datas,(err,userId)-> + #find a better way ? + store.findUserById(userId,(err,user)-> + cb(null,user) + _.app.event.emit('user/login', + userId:user.id + ) + return + ) + ) + else + cb(err,null) + return + else + if source == 'local' + #!TODO password hash with a good method.... + if datas.password != user[source].password + cb(['Wrong Password'],null) + return + _.app.event.emit('user/login', + userId:user.id + ) + cb(null,user) + return + ) + _routes : ()-> _ = @ - @app.get('/login/*', (req, res)-> + @routeGet('/login/*', (req, res)-> if req.params? and req.params[0]? and req.params[0] != '' req.session.url = req.params[0] if req.user? @@ -16,13 +65,11 @@ module.exports = class Auth extends Component res.redirect('/auth/local')#!TODO RENDER LOGIN PAGE ) - @app.get('/redirect', (req, res)-> + @routeGet('/redirect', (req, res)-> if not req.loggedIn - res.json({}) - return res.redirect('/login/') return - tokenCallback = (token)-> + tokenCallback = (err,token)-> req.session.token = token if req.session.url? #_.app.log.debug "Redirect "+url @@ -37,68 +84,17 @@ module.exports = class Auth extends Component ) return if req.session.token? - tokenCallback(req.session.token) + tokenCallback(null,req.session.token) else _.app.token.add(req.user.id, {}, tokenCallback) ) - addUser : (source='', id='', datas={},cb)-> - _ = @ - store = @app.stores.user - store.addUser(source,id,datas,(userId)-> - cb(userId) - _.emit('user/new', - userId:userId - ) - ) - - - login : (source, id, datas, user, cb)-> - _ = @ - store = @app.stores.user - #Check on user : Does the user is already logged in ? - if user != null - # Is it a new way of connection ? - #@log.debug "User Is already in, let's add a way of login !" - else - try - store.findUserBySourceAndId(source,id,(user)-> - if source == 'local' - #!TODO password hash with a good method.... - if datas.password != user[source].password - throw 'Wrong Password' - return - _.app.event.emit('user/login', - userId:user.id - ) - cb(user) - ) - catch e - if e == 'Not found' - store.addUser(source,id,datas,(userId)-> - #find a better way ? - store.findUserById(userId,(user)-> - cb(user) - _.app.event.emit('user/login', - userId:user.id - ) - return - ) - ) - else - throw e - _everyAuth : ()-> _ = @ store = @app.stores.user @everyAuth = require 'everyauth' #@everyAuth.debug = true @everyAuth.everymodule.findUserById((id, cb)-> - try - store.findUserById(id,(user)-> - cb(null,user) - ) - catch e - cb([e],null) + store.findUserById(id,cb) ) #PASSWORD : @everyAuth @@ -109,17 +105,16 @@ module.exports = class Auth extends Component .loginView('login') .authenticate((login, password)-> promise = this.Promise() - try - _.login('local',login, - login:login - password:password - ,null#!TODO put here user from session - ,(user)-> - promise.fulfill(user) - ) - catch e - promise.fulfill([e]) - + _.login('local',login, + login:login + password:password + ,null#!TODO put here user from session + ,(err,user)-> + if err != null + promise.fulfill(err) + return + promise.fulfill(user) + ) return promise ) .getRegisterPath('/register') @@ -136,8 +131,8 @@ module.exports = class Auth extends Component for providerName, providerConfigs of _.app.configs.everyAuth @._everyAuth_Providers(providerName, providerConfigs) #Register EveryAuth - @app.app.use(@everyAuth.middleware()) - @everyAuth.helpExpress(@app.app) + @express().use(@everyAuth.middleware()) + @everyAuth.helpExpress(@express()) _everyAuth_Providers:(providerName, providerConfigs)-> _ = @ diff --git a/lib/component.coffee b/lib/component.coffee index fe73acc..170133c 100644 --- a/lib/component.coffee +++ b/lib/component.coffee @@ -17,17 +17,22 @@ module.exports = class Component res.json( error:true ) + #!TODO Make a better check error + checkErr : (err)-> + if err != null + throw err + ### # Express route encapsulation ### - route_get : (args...)-> - @app.app.get(args...) - route_post : (args...)-> - @app.app.post(args...) - route_update : (args...)-> - @app.app.update(args...) - route_delete : (args...)-> - @app.app.delete(args...) + routeGet : (args...)-> + @express().get(args...) + routePost : (args...)-> + @express().post(args...) + routeUpdate : (args...)-> + @express().update(args...) + routeDelete : (args...)-> + @express().delete(args...) ### # Events @@ -36,3 +41,8 @@ module.exports = class Component @app._event.emit(args...) on : (args...)-> @app._event.on(args...) + + log : ()-> + return @app.log + express : ()-> + return @app.express diff --git a/lib/event/event.coffee b/lib/event/event.coffee index 26c4476..c75b8f3 100644 --- a/lib/event/event.coffee +++ b/lib/event/event.coffee @@ -3,7 +3,7 @@ module.exports = class Event extends Component constructor : (app)-> @app = app @access = app.access - io = require('socket.io').listen(app.app) + io = require('socket.io').listen(@express()) @channel = io.of('/auth')#!TODO Put in configs... #@channel = io @_init_socket() @@ -15,27 +15,22 @@ module.exports = class Event extends Component socket.emit('pong',datas) ) socket.on('subscribe',(datas)-> - try - #!TODO Check on datas.event - _.app.access.check( - datas.token, - ['_event_'+datas.event,'_event_*','_root'], - (userId)-> - console.log "Event "+datas.event+" transfered to socket io" - _.on(datas.event,(datas)-> - console.log "New Event to socketIO#{@.event}" - socket.emit(this.event, datas) - ) - _.emit('event/subscribe', - token : datas.token - userId : userId - event : datas.event - ) - #!TODO Remove listener if socket close ? - ,'event/subscribe' - ) - catch e - console.log "SUBSCRIVE ERROR :"+e + #!TODO Check on datas.event + _.app.access.check( + datas.token, + ['_event_'+datas.event,'_event_*','_root'], + (err,userId)-> + _.checkErr(err) + _.on(datas.event,(datas)-> + socket.emit(this.event, datas) + ) + _.emit('event/subscribe', + token : datas.token + userId : userId + event : datas.event + ) + #!TODO Remove listener if socket close ? + ,'event/subscribe' + ) ) ) - diff --git a/lib/group/group.coffee b/lib/group/group.coffee index 28c7e64..13a987c 100644 --- a/lib/group/group.coffee +++ b/lib/group/group.coffee @@ -1,4 +1,3 @@ - Component = require '../component' module.exports = class Group extends Component constructor : (app)-> @@ -10,11 +9,11 @@ module.exports = class Group extends Component _routes : ()-> _ = @ - @.route_get('/group/:token', (req, res)-> + @.routeGet('/group/:token', (req, res)-> res.redirect('/not-implemented-yet') ) - @.route_post('/group/', (req, res)-> + @.routePost('/group/', (req, res)-> _.add( req.params.name, req.params.token, @@ -23,7 +22,7 @@ module.exports = class Group extends Component ) ) - @.route_post('/group/:name/users/', (req, res)-> + @.routePost('/group/:name/users/', (req, res)-> #!TODO Get user by token ? userId = req.params.userId _.addUserToGroup( @@ -41,8 +40,6 @@ module.exports = class Group extends Component _.app.access.check(token, ['_group_add','_root'],(userId)-> #!TODO check group existence _.store.addGroup(groupName, (groupId)-> - console.log "TEST :" - console.log cb cb(groupId) _.event.emit('group:new', groupId : groupId diff --git a/lib/store/local/store.coffee b/lib/store/local/store.coffee index 86592a8..8cecb5d 100644 --- a/lib/store/local/store.coffee +++ b/lib/store/local/store.coffee @@ -13,22 +13,24 @@ module.exports = class Store throw "INTERNAL : Id collision" datas.id = id @[field][id] = datas - cb(id) + cb(null,id) _getItem:(field,id, cb)-> if not @[field]? throw "INTERNAL : Field does not exists" if not @[field][id]? - throw "Not found" - cb(@[field][id]) + cb(['Not found'],null) + return + cb(null,@[field][id]) _deleteItem:(field,id, cb)-> if not @[field]? throw "INTERNAL : Field does not exists" if not @[field][id]? - throw "Not found" + cb(['Not found'],null) + return delete(@[field][id]) - cb(true) + cb(null,true) _findOneItemBy : (field, fieldToSearch, value, cb)-> if not @[field]? @@ -36,29 +38,33 @@ module.exports = class Store for k, v of @[field] if v[fieldToSearch]? if v[fieldToSearch] == value - cb(v) + cb(null,v) return - throw "Not found" + cb(['Not found'],null) + return _addItemToItemField : (groupId, field, item,cb)-> if not @_groups[groupId]? - throw "Group does not exists" + cb(['Group does not exists'],null) + return if not @_groups[groupId][field]? throw "INTERNAL : Field does not exists" if item in @_groups[groupId][field] - throw "Item already in field" + cb(['Item already in field'],null) + return @_groups[groupId][field].push(item) - cb(true) + cb(null,true) _isItemInItemField : (groupId,field, item, cb)-> if not @_groups[groupId]? - throw "Group does not exists" + cb(['Group does not exists'],null) + return if not @_groups[groupId][field]? throw "INTERNAL : Field does not exists" if item in @_groups[groupId][field] - cb(true) + cb(null,true) return - cb(false) + cb(null,false) _getItemsWhereItemIsInField : (field, item, cb)-> res = [] @@ -66,18 +72,21 @@ module.exports = class Store if v[field]? if item in v[field] res.push(v) - cb(res) + cb(null,res) _removeItemFromItemField : (groupId, field, item,cb)-> if not @_groups[groupId]? - throw "Group does not exists" + cb(['Group does not exists'],null) + return if not @_groups[groupId][field]? throw "INTERNAL : Field does not exists" if not item in @_groups[groupId][field] - throw "Item not in field" + cb(['Item not in field'],null) + return + index = @_groups[groupId][field].indexOf(item) if index == -1 - cb(false) + cb(null,false) return @_groups[groupId][field].splice(index, 1) - cb(true) + cb(null,true) diff --git a/lib/store/local/token.coffee b/lib/store/local/token.coffee index d9f3f37..2b2036d 100644 --- a/lib/store/local/token.coffee +++ b/lib/store/local/token.coffee @@ -6,17 +6,17 @@ module.exports = class Token extends Store addToken : (datas, cb)-> token = @._generateId() @_tokens[token] = datas - cb(token) + cb(null,token) getToken : (token,cb)-> if @_tokens[token]? - cb(@_tokens[token]) + cb(null,@_tokens[token]) return - cb(null) + cb(['Not found'],null) deleteToken : (token,cb)-> if @_tokens[token]? delete(@_tokens[token]) - cb(true) + cb(null,true) return - cb(false) + cb(null,false) diff --git a/lib/store/local/user.coffee b/lib/store/local/user.coffee index a73fb07..f17cae0 100644 --- a/lib/store/local/user.coffee +++ b/lib/store/local/user.coffee @@ -19,8 +19,10 @@ module.exports = class User extends Store findUserBySourceAndId : (source, id, cb)-> if not @_usersBySource[source]? - throw 'Not found' + cb(['Not found'],null) + return if not @_usersBySource[source][id]? - throw 'Not found' - cb(@_usersBySource[source][id]) + cb(['Not found'],null) + return + cb(null,@_usersBySource[source][id]) diff --git a/lib/token/token.coffee b/lib/token/token.coffee index 4d648ba..718f326 100644 --- a/lib/token/token.coffee +++ b/lib/token/token.coffee @@ -8,16 +8,17 @@ module.exports = class Token extends Component _routes : ()-> _ = @ - @route_get('/token/:token/:auth', (req, res)-> + #!TODO change this route... + @routeGet('/token/:token/:auth', (req, res)-> #SECURITY : You can only check your own token if req.params['token'] != req.params['auth'] _.app._error(req,res) return - userId = _.app.stores.token.getToken(req.params['token'],(datas)-> + userId = _.app.stores.token.getToken(req.params['token'],(err,datas)-> if datas == null _.app._error(req,res) return - _.app.stores.user.findUserById(datas.userId,(user)-> + _.app.stores.user.findUserById(datas.userId,(err,user)-> if user != null res.json(user) return @@ -35,13 +36,11 @@ module.exports = class Token extends Component { userId : userId }, - (token)-> - cb(token) + (err,token)-> + _.checkErr(err) + cb(err,token) _.emit('token/new', token: token userId : userId ) ) - delete : (token, cb)-> - return - diff --git a/test/access.coffee b/test/access.coffee index 4cdaa93..66ed97d 100644 --- a/test/access.coffee +++ b/test/access.coffee @@ -14,64 +14,39 @@ describe('Access', ()-> tobi.Browser.browsers = {} browser = tobi.createBrowser(3001, 'local.host') browser.userAgent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30' - app.auth.addUser('local','ombr0',{},(userId)-> + app.auth.addUser('local','ombr0',{},(err,userId)-> users.push(userId) console.log "IDI :" - console.log app.token.add - app.token.add(userId,{},(token)-> + console.log userId + app.token.add(userId,{},(err,token)-> tokens.push(token) console.log "I have a token :-D Hope I'm root ?" done() - return - setTimeout(()-> - console.log app.stores.group._groups - app.auth.addUser('local','ombr1',{},(userId)-> - users.push(userId) - app.auth.addUser('local','ombr2',{},(userId)-> - users.push(userId) - app.group.add('group0',token, (groupId)-> - groups.push(groupId) - app.group.add('group1',token, (groupId)-> - groups.push(groupId) - app.group.add('group2',token, (groupId)-> - groups.push(groupId) - app.group.add('group3',token, (groupId)-> - groups.push(groupId) - done() - ) - ) - ) - ) - ) - ) - ,500) ) ) ) after(()-> - app.app.close() + app.express.close() ) describe('Root user', ()-> it('First user should be root',(done)-> - app.access.check(tokens[0], ['_root'],(userId)-> + console.log tokens[0] + app.access.check(tokens[0], ['_root'],(err,userId)-> userId.should.eql(users[0]) done() ,'test/root') ) it('Second user should not be root',(done)-> - app.auth.addUser('local','ombr1',{},(userId)-> + app.auth.addUser('local','ombr1',{},(err,userId)-> users.push(userId) - app.token.add(userId,{},(token)-> + app.token.add(userId,{},(err,token)-> tokens.push(token) - try - app.access.check(token, ['_root'],(userId)-> - throw "Callback should not be called" - ,'test/root') - catch e - e.should.eql('Access denied') + app.access.check(token, ['_root'],(err,userId)-> + err[0].should.eql('Access denied') done() + ,'test/root') ) ) ) diff --git a/test/event.coffee b/test/event.coffee index 08730ef..1d6ddbc 100644 --- a/test/event.coffee +++ b/test/event.coffee @@ -19,7 +19,7 @@ describe('Events', ()-> ) after(()-> - app.app.close() + app.express.close() ) describe('Ping', ()-> @@ -37,8 +37,8 @@ describe('Events', ()-> describe('Subscribe', ()-> it('Should be able to subscribe to events',(done)-> - app.auth.addUser('local','ombr0',{},(userId)-> - app.token.add(userId,{},(token)-> + app.auth.addUser('local','ombr0',{},(err,userId)-> + app.token.add(userId,{},(err,token)-> socket.on('event/subscribe',(datas)-> console.log "Subscribed :-)" console.log datas diff --git a/test/login.coffee b/test/login.coffee index 1f07a94..8eccb0b 100644 --- a/test/login.coffee +++ b/test/login.coffee @@ -14,7 +14,7 @@ describe('Login Logout', ()-> ) after(()-> - app.app.close() + app.express.close() ) describe('/login', ()-> diff --git a/test/store/group.coffee b/test/store/group.coffee index b44b722..2801007 100644 --- a/test/store/group.coffee +++ b/test/store/group.coffee @@ -15,7 +15,7 @@ describe('Store Group', ()-> ) describe('addGroup', ()-> it('Should be able to add a group',(done)-> - store.addGroup('group1',(id)-> + store.addGroup('group1',(err,id)-> should.exist(id) done() ) @@ -24,9 +24,9 @@ describe('Store Group', ()-> describe('getGroup', ()-> it('Should be able to add and retrieve a group',(done)-> - store.addGroup('group2',(id)-> + store.addGroup('group2',(err,id)-> should.exist(id) - store.getGroup(id,(res)-> + store.getGroup(id,(err,res)-> res.name.should.eql('group2') done() ) @@ -35,9 +35,9 @@ describe('Store Group', ()-> ) describe('findGroupByName', ()-> it('Should be able to find an existing group',(done)-> - store.addGroup('group_name',(id)-> + store.addGroup('group_name',(err,id)-> should.exist(id) - store.findGroupByName('group_name',(res)-> + store.findGroupByName('group_name',(err,res)-> res.name.should.eql('group_name') done() ) @@ -46,21 +46,21 @@ describe('Store Group', ()-> ) describe('deleteGroup', ()-> it('Should be able to delete an existing group',(done)-> - store.addGroup('group_delete',(id)-> + store.addGroup('group_delete',(err,id)-> should.exist(id) - store.deleteGroup(id,(res)-> + store.deleteGroup(id,(err,res)-> res.should.be.true done() ) ) ) it('Should not be able to delete a non-existent group',(done)-> - store.addGroup('group_delete',(id)-> + store.addGroup('group_delete',(err,id)-> should.exist(id) - store.deleteGroup(id,(res)-> + store.deleteGroup(id,(err,res)-> res.should.be.true try - store.deleteGroup(id,(res)-> + store.deleteGroup(id,(err,res)-> trow "Callback should not be called" ) catch err @@ -73,21 +73,21 @@ describe('Store Group', ()-> describe('addUserToGroup', ()-> it('Should be able to add a user',(done)-> - store.addGroup('group_test',(groupId)-> + store.addGroup('group_test',(err,groupId)-> should.exist(groupId) - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> res.should.be.true done() ) ) ) it('Should not be able to add a user who is already in the group',(done)-> - store.findGroupByName('group_test',(group)-> + store.findGroupByName('group_test',(err,group)-> should.exist(group) should.exist(group.id) groupId = group.id try - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> throw "Callback should not be called" ) catch e @@ -96,11 +96,11 @@ describe('Store Group', ()-> ) it('Should not be able to add a other user who is not in the group',(done)-> - store.findGroupByName('group_test',(group)-> + store.findGroupByName('group_test',(err,group)-> should.exist(group) should.exist(group.id) groupId = group.id - store.addUserToGroup(users[1],groupId,(res)-> + store.addUserToGroup(users[1],groupId,(err,res)-> res.should.be.true done() ) @@ -112,13 +112,13 @@ describe('Store Group', ()-> describe('removeUserFromGroup', ()-> it('Should be able to remove a user',(done)-> - store.addGroup('group3',(groupId)-> + store.addGroup('group3',(err,groupId)-> should.exist(groupId) - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> res.should.be.true - store.removeUserFromGroup(users[0],groupId,(res)-> + store.removeUserFromGroup(users[0],groupId,(err,res)-> res.should.be.true - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> res.should.be.true done() ) @@ -128,14 +128,14 @@ describe('Store Group', ()-> ) it('Should not be able to remove user who is not in the group',(done)-> - store.addGroup('group4',(groupId)-> + store.addGroup('group4',(err,groupId)-> should.exist(groupId) - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> res.should.be.true - store.removeUserFromGroup(users[0],groupId,(res)-> + store.removeUserFromGroup(users[0],groupId,(err,res)-> res.should.be.true try - store.removeUserFromGroup(users[0],groupId,(res)-> + store.removeUserFromGroup(users[0],groupId,(err,res)-> throw "Callback should not be called" res.should.be.true ) @@ -155,17 +155,17 @@ describe('Store Group', ()-> ) describe('isUserMemberOfGroup ',()-> it('should return true if the user is directly in the group',(done)-> - store.addGroup('group5',(groupId)-> + store.addGroup('group5',(err,groupId)-> should.exist(groupId) - store.isUserMemberOfGroup(users[0],groupId,(res)-> + store.isUserMemberOfGroup(users[0],groupId,(err,res)-> res.should.be.false - store.addUserToGroup(users[0],groupId,(res)-> + store.addUserToGroup(users[0],groupId,(err,res)-> res.should.be.true - store.isUserMemberOfGroup(users[0],groupId,(res)-> + store.isUserMemberOfGroup(users[0],groupId,(err,res)-> res.should.be.true - store.removeUserFromGroup(users[0],groupId,(res)-> + store.removeUserFromGroup(users[0],groupId,(err,res)-> res.should.be.true - store.isUserMemberOfGroup(users[0],groupId,(res)-> + store.isUserMemberOfGroup(users[0],groupId,(err,res)-> res.should.be.false done() ) @@ -193,17 +193,17 @@ describe('Store Group', ()-> ) describe('getGroupsUserIsMemberOf ',()-> it('should return an array with all the groups the user is member',(done)-> - store.getGroupsUserIsMemberOf(users[2],(groups)-> + store.getGroupsUserIsMemberOf(users[2],(err,groups)-> groups.length.should.eql(0) - store.addGroup('group5',(groupId)-> - store.addUserToGroup(users[2],groupId,(res)-> + store.addGroup('group5',(err,groupId)-> + store.addUserToGroup(users[2],groupId,(err,res)-> res.should.be.true - store.getGroupsUserIsMemberOf(users[2],(groups)-> + store.getGroupsUserIsMemberOf(users[2],(err,groups)-> groups.length.should.eql(1) groups[0].name.should.eql('group5') - store.removeUserFromGroup(users[2],groupId,(res)-> + store.removeUserFromGroup(users[2],groupId,(err,res)-> res.should.be.true - store.getGroupsUserIsMemberOf(users[2],(groups)-> + store.getGroupsUserIsMemberOf(users[2],(err,groups)-> groups.length.should.eql(0) done() ) diff --git a/test/store/token.coffee b/test/store/token.coffee index 56c44fb..d974a30 100644 --- a/test/store/token.coffee +++ b/test/store/token.coffee @@ -16,7 +16,9 @@ describe('Store Token',()-> store.addToken( content: 'lalal' plus : 'yeass' - ,(token)-> + ,(err,token)-> + should.not.exist(err) + should.exist(token) done() ) ) @@ -29,8 +31,9 @@ describe('Store Token',()-> difficult: 'Oui !' store.addToken( obj, - (token)-> - store.getToken(token,(datas)-> + (err,token)-> + store.getToken(token,(err,datas)-> + should.not.exist(err) datas.should.eql(obj) done() ) @@ -47,8 +50,9 @@ describe('Store Token',()-> difficult: 'Oui !' store.addToken( obj, - (token)-> - store.getToken(token,(datas)-> + (err,token)-> + store.getToken(token,(err,datas)-> + should.not.exist(err) datas.should.eql(obj) done() ) @@ -64,12 +68,13 @@ describe('Store Token',()-> difficult: 'Oui !' store.addToken( obj, - (token)-> - store.getToken(token,(datas)-> + (err,token)-> + store.getToken(token,(err,datas)-> datas.should.eql(obj) - store.deleteToken(token,(res)-> + store.deleteToken(token,(err,res)-> res.should.be.true - store.getToken(token,(datas)-> + store.getToken(token,(err,datas)-> + err[0].should.eql('Not found') should.not.exist(datas) done() ) @@ -88,12 +93,13 @@ describe('Store Token',()-> difficult: 'Oui !' store.addToken( obj, - (token)-> - store.getToken(token,(datas)-> + (err,token)-> + store.getToken(token,(err,datas)-> datas.should.eql(obj) - store.deleteToken(token,(res)-> + store.deleteToken(token,(err,res)-> res.should.be.true - store.getToken(token,(datas)-> + store.getToken(token,(err,datas)-> + err[0].should.eql('Not found') should.not.exist(datas) done() ) @@ -110,14 +116,15 @@ describe('Store Token',()-> difficult: 'Oui !' store.addToken( obj, - (token)-> - store.getToken(token,(datas)-> + (err,token)-> + store.getToken(token,(err,datas)-> datas.should.eql(obj) - store.deleteToken(token,(res)-> + store.deleteToken(token,(err,res)-> res.should.be.true - store.getToken(token,(datas)-> + store.getToken(token,(err,datas)-> should.not.exist(datas) - store.deleteToken(token,(res)-> + store.deleteToken(token,(err,res)-> + should.not.exist(err) res.should.be.false done() ) diff --git a/test/store/user.coffee b/test/store/user.coffee index 575f05e..ad78a22 100644 --- a/test/store/user.coffee +++ b/test/store/user.coffee @@ -18,7 +18,8 @@ describe('Store User', ()-> store.addUser('local','user1', login:'user1' password: 'superpassword' - ,(userId)-> + ,(err,userId)-> + should.not.exist(err) should.exist(userId) done() ) @@ -27,19 +28,19 @@ describe('Store User', ()-> describe('findUserId', ()-> it('Should not be able to retrieve a non existing user',(done)-> - try - store.findUserById(Math.round(Math.random()*100000),(user)-> - throw "Callback should not be called" - ) - catch e - done() + store.findUserById(Math.round(Math.random()*100000),(err,userId)-> + should.exist(err) + should.not.exist(userId) + done() + ) ) it('Should be able to retrieve an existing user',(done)-> store.addUser('local','user3', login:'user3' password: 'superpassword' - ,(userId)-> - store.findUserById(userId,(user)-> + ,(err,userId)-> + store.findUserById(userId,(err,user)-> + should.not.exist(err) should.exist(user) should.exist(user.id) user['local'].login.should.eql('user3') @@ -51,19 +52,19 @@ describe('Store User', ()-> ) describe('findUserBySourceAndId', ()-> it('Should not be able to retrieve a non existing user',(done)-> - try store.findUserBySourceAndId('local','nonuser',(err,user)-> - throw "Callback should not be called" + should.exist(err) + should.not.exist(user) + done() ) - catch e - done() ) it('Should be able to retrieve an existing user',(done)-> store.addUser('local','user2', login:'user2' password: 'superpassword' ,(err,user)-> - store.findUserBySourceAndId('local','user2',(user)-> + store.findUserBySourceAndId('local','user2',(err,user)-> + should.not.exist(err) should.exist(user) user.local.login.should.equal('user2') user.local.password.should.equal('superpassword')