mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-12-02 11:58:10 -05:00
c2f17edaea
This test has been unstable, possibly because it was using one request instance to fire off requests for many users. Rewrote using generators and multiple request instances with separate cookie jars. For #3469.
66 lines
1.7 KiB
CoffeeScript
66 lines
1.7 KiB
CoffeeScript
async = require 'async'
|
|
utils = require '../../server/lib/utils'
|
|
co = require 'co'
|
|
Promise = require 'bluebird'
|
|
|
|
module.exports = mw =
|
|
getURL: (path) -> 'http://localhost:3001' + path
|
|
|
|
clearModels: Promise.promisify (models, done) ->
|
|
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: []
|
|
email: 'user'+_.uniqueId()+'@gmail.com'
|
|
password: 'password'
|
|
}, options)
|
|
user = new User(options)
|
|
promise = user.save()
|
|
return promise
|
|
|
|
loginUser: Promise.promisify (user, options={}, done) ->
|
|
if _.isFunction(options)
|
|
done = options
|
|
options = {}
|
|
form = {
|
|
username: user.get('email')
|
|
password: 'password'
|
|
}
|
|
(options.request or request).post mw.getURL('/auth/login'), { form: form }, (err, res) ->
|
|
expect(err).toBe(null)
|
|
expect(res.statusCode).toBe(200)
|
|
done(err, user)
|
|
|
|
initAdmin: (options) ->
|
|
if _.isFunction(options)
|
|
done = options
|
|
options = {}
|
|
options = _.extend({permissions: ['admin']}, options)
|
|
return @initUser(options)
|
|
|
|
initArtisan: (options) ->
|
|
if _.isFunction(options)
|
|
done = options
|
|
options = {}
|
|
options = _.extend({permissions: ['artisan']}, options)
|
|
return @initUser(options)
|
|
|
|
logout: Promise.promisify (done) ->
|
|
request.post mw.getURL('/auth/logout'), done
|
|
|
|
wrap: (gen) ->
|
|
fn = co.wrap(gen)
|
|
return (done) ->
|
|
fn.apply(@, [done]).catch (err) -> done.fail(err)
|
|
|