2015-12-16 20:09:22 -05:00
|
|
|
async = require 'async'
|
|
|
|
utils = require '../../server/lib/utils'
|
|
|
|
co = require 'co'
|
2016-02-22 18:08:58 -05:00
|
|
|
Promise = require 'bluebird'
|
2016-04-06 13:56:06 -04:00
|
|
|
User = require '../../server/models/User'
|
2016-05-04 15:05:28 -04:00
|
|
|
Level = require '../../server/models/Level'
|
|
|
|
Achievement = require '../../server/models/Achievement'
|
|
|
|
Campaign = require '../../server/models/Campaign'
|
|
|
|
campaignSchema = require '../../app/schemas/models/campaign.schema'
|
|
|
|
campaignLevelProperties = _.keys(campaignSchema.properties.levels.additionalProperties.properties)
|
|
|
|
campaignAdjacentCampaignProperties = _.keys(campaignSchema.properties.adjacentCampaigns.additionalProperties.properties)
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports = mw =
|
|
|
|
getURL: (path) -> 'http://localhost:3001' + path
|
|
|
|
|
2016-02-22 18:08:58 -05:00
|
|
|
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 = {}
|
2016-03-03 17:22:50 -05:00
|
|
|
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
|
2016-03-03 17:22:50 -05:00
|
|
|
}, options)
|
|
|
|
user = new User(options)
|
2016-02-22 18:08:58 -05:00
|
|
|
promise = user.save()
|
|
|
|
return promise
|
2015-12-16 20:09:22 -05:00
|
|
|
|
2016-05-04 15:05:28 -04:00
|
|
|
loginUser: Promise.promisify (user, options={}, done) ->
|
2016-03-30 15:00:12 -04:00
|
|
|
if _.isFunction(options)
|
|
|
|
done = options
|
|
|
|
options = {}
|
2015-12-16 20:09:22 -05:00
|
|
|
form = {
|
|
|
|
username: user.get('email')
|
|
|
|
password: 'password'
|
|
|
|
}
|
2016-03-30 15:00:12 -04:00
|
|
|
(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)
|
2016-05-04 15:05:28 -04:00
|
|
|
mw.lastLogin = user
|
2015-12-16 20:09:22 -05:00
|
|
|
done(err, user)
|
|
|
|
|
2016-02-22 18:08:58 -05:00
|
|
|
initAdmin: (options) ->
|
2015-12-16 20:09:22 -05:00
|
|
|
if _.isFunction(options)
|
|
|
|
done = options
|
|
|
|
options = {}
|
|
|
|
options = _.extend({permissions: ['admin']}, options)
|
2016-02-22 18:08:58 -05:00
|
|
|
return @initUser(options)
|
2015-12-16 20:09:22 -05:00
|
|
|
|
2016-02-22 18:08:58 -05:00
|
|
|
initArtisan: (options) ->
|
2015-12-16 20:09:22 -05:00
|
|
|
if _.isFunction(options)
|
|
|
|
done = options
|
|
|
|
options = {}
|
|
|
|
options = _.extend({permissions: ['artisan']}, options)
|
2016-02-22 18:08:58 -05:00
|
|
|
return @initUser(options)
|
2015-12-16 20:09:22 -05:00
|
|
|
|
2016-03-30 19:20:37 -04:00
|
|
|
becomeAnonymous: Promise.promisify (done) ->
|
|
|
|
request.post mw.getURL('/auth/logout'), ->
|
|
|
|
request.get mw.getURL('/auth/whoami'), done
|
|
|
|
|
2016-02-22 18:08:58 -05:00
|
|
|
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)
|
2016-05-04 15:05:28 -04:00
|
|
|
|
|
|
|
makeLevel: Promise.promisify (data, sources, done) ->
|
|
|
|
args = Array.from(arguments)
|
|
|
|
[done, [data, sources]] = [args.pop(), args]
|
|
|
|
|
|
|
|
data = _.extend({}, {
|
|
|
|
name: _.uniqueId('Level ')
|
|
|
|
permissions: [{target: mw.lastLogin.id, access: 'owner'}]
|
|
|
|
}, data)
|
|
|
|
|
|
|
|
request.post { uri: getURL('/db/level'), json: data }, (err, res) ->
|
|
|
|
return done(err) if err
|
|
|
|
Level.findById(res.body._id).exec done
|
|
|
|
|
|
|
|
makeAchievement: Promise.promisify (data, sources, done) ->
|
|
|
|
args = Array.from(arguments)
|
|
|
|
[done, [data, sources]] = [args.pop(), args]
|
|
|
|
|
|
|
|
data = _.extend({}, {
|
|
|
|
name: _.uniqueId('Achievement ')
|
|
|
|
}, data)
|
|
|
|
if sources.related and not data.related
|
|
|
|
related = sources.related
|
|
|
|
data.related = (related.get('original') or related._id).valueOf()
|
|
|
|
|
|
|
|
request.post { uri: getURL('/db/achievement'), json: data }, (err, res) ->
|
|
|
|
return done(err) if err
|
|
|
|
Achievement.findById(res.body._id).exec done
|
|
|
|
|
|
|
|
makeCampaign: Promise.promisify (data, sources, done) ->
|
|
|
|
args = Array.from(arguments)
|
|
|
|
[done, [data, sources]] = [args.pop(), args]
|
|
|
|
|
|
|
|
data = _.extend({}, {
|
|
|
|
name: _.uniqueId('Campaign ')
|
|
|
|
}, data)
|
|
|
|
if not data.levels
|
|
|
|
data.levels = {}
|
|
|
|
for level in sources?.levels or []
|
|
|
|
data.levels[level.get('original').valueOf()] = _.pick level.toObject(), campaignLevelProperties
|
|
|
|
|
|
|
|
if not data.adjacentCampaigns
|
|
|
|
data.adjacentCampaigns = {}
|
|
|
|
for campaign in sources?.adjacentCampaigns or []
|
|
|
|
data.adjacentCampaigns[campaign.id] = _.pick campaign.toObject(), campaignAdjacentCampaignProperties
|
|
|
|
|
|
|
|
request.post { uri: getURL('/db/campaign'), json: data }, (err, res) ->
|
|
|
|
return done(err) if err
|
|
|
|
Campaign.findById(res.body._id).exec done
|