codecombat/spec/server/utils.coffee

74 lines
1.9 KiB
CoffeeScript
Raw Normal View History

2015-12-16 20:09:22 -05:00
async = require 'async'
utils = require '../../server/lib/utils'
co = require 'co'
Promise = require 'bluebird'
User = require '../../server/models/User'
2015-12-16 20:09:22 -05:00
module.exports = mw =
getURL: (path) -> 'http://localhost:3001' + path
clearModels: Promise.promisify (models, done) ->
2015-12-16 20:09:22 -05:00
funcs = []
for model in models
wrapped = (m) ->
(callback) ->
m.remove {}, (err) ->
callback(err, true)
funcs.push(wrapped(model))
async.parallel funcs, done
initUser: (options, done) ->
if _.isFunction(options)
done = options
options = {}
options = _.extend({
permissions: []
2015-12-16 20:09:22 -05:00
email: 'user'+_.uniqueId()+'@gmail.com'
password: 'password'
2016-04-08 18:06:05 -04:00
anonymous: false
}, options)
user = new User(options)
promise = user.save()
return promise
2015-12-16 20:09:22 -05:00
loginUser: Promise.promisify (user, options={}, done) ->
if _.isFunction(options)
done = options
options = {}
2015-12-16 20:09:22 -05:00
form = {
username: user.get('email')
password: 'password'
}
(options.request or request).post mw.getURL('/auth/login'), { form: form }, (err, res) ->
2015-12-16 20:09:22 -05:00
expect(err).toBe(null)
expect(res.statusCode).toBe(200)
done(err, user)
initAdmin: (options) ->
2015-12-16 20:09:22 -05:00
if _.isFunction(options)
done = options
options = {}
options = _.extend({permissions: ['admin']}, options)
return @initUser(options)
2015-12-16 20:09:22 -05:00
initArtisan: (options) ->
2015-12-16 20:09:22 -05:00
if _.isFunction(options)
done = options
options = {}
options = _.extend({permissions: ['artisan']}, options)
return @initUser(options)
2015-12-16 20:09:22 -05:00
becomeAnonymous: Promise.promisify (done) ->
request.post mw.getURL('/auth/logout'), ->
request.get mw.getURL('/auth/whoami'), done
logout: Promise.promisify (done) ->
2015-12-16 20:09:22 -05:00
request.post mw.getURL('/auth/logout'), done
wrap: (gen) ->
fn = co.wrap(gen)
return (done) ->
fn.apply(@, [done]).catch (err) -> done.fail(err)