2014-01-03 13:32:13 -05:00
|
|
|
# import this at the top of every file so we're not juggling connections
|
|
|
|
# and common libraries are available
|
|
|
|
|
2014-02-24 23:27:38 -05:00
|
|
|
console.log 'IT BEGINS'
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
require 'jasmine-spec-reporter'
|
2015-03-13 18:19:20 -04:00
|
|
|
jasmine.getEnv().defaultTimeoutInterval = 300000
|
2014-04-28 05:46:43 -04:00
|
|
|
jasmine.getEnv().reporter.subReporters_ = []
|
|
|
|
jasmine.getEnv().addReporter(new jasmine.SpecReporter({
|
|
|
|
displaySuccessfulSpec: true,
|
|
|
|
displayFailedSpec: true
|
|
|
|
}))
|
2014-06-11 16:21:11 -04:00
|
|
|
|
|
|
|
rep = new jasmine.JsApiReporter()
|
|
|
|
jasmine.getEnv().addReporter(rep)
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
GLOBAL._ = require 'lodash'
|
|
|
|
_.str = require 'underscore.string'
|
2014-01-03 13:32:13 -05:00
|
|
|
_.mixin(_.str.exports())
|
|
|
|
GLOBAL.mongoose = require 'mongoose'
|
|
|
|
mongoose.connect('mongodb://localhost/coco_unittest')
|
2014-06-30 22:16:26 -04:00
|
|
|
path = require 'path'
|
2014-04-08 22:26:19 -04:00
|
|
|
GLOBAL.testing = true
|
2014-08-29 15:58:23 -04:00
|
|
|
GLOBAL.tv4 = require 'tv4' # required for TreemaUtils to work
|
2014-01-22 17:57:41 -05:00
|
|
|
|
|
|
|
models_path = [
|
2014-12-04 15:57:57 -05:00
|
|
|
'../../server/analytics/AnalyticsUsersActive'
|
2014-01-22 17:57:41 -05:00
|
|
|
'../../server/articles/Article'
|
2014-12-16 20:46:24 -05:00
|
|
|
'../../server/campaigns/Campaign'
|
2015-04-01 14:56:48 -04:00
|
|
|
'../../server/clans/Clan'
|
2014-01-22 17:57:41 -05:00
|
|
|
'../../server/levels/Level'
|
|
|
|
'../../server/levels/components/LevelComponent'
|
|
|
|
'../../server/levels/systems/LevelSystem'
|
|
|
|
'../../server/levels/sessions/LevelSession'
|
|
|
|
'../../server/levels/thangs/LevelThangType'
|
|
|
|
'../../server/users/User'
|
2014-04-08 22:26:19 -04:00
|
|
|
'../../server/patches/Patch'
|
2014-06-09 11:28:35 -04:00
|
|
|
'../../server/achievements/Achievement'
|
|
|
|
'../../server/achievements/EarnedAchievement'
|
2014-11-11 20:40:03 -05:00
|
|
|
'../../server/payments/Payment'
|
2015-03-19 18:02:45 -04:00
|
|
|
'../../server/prepaids/Prepaid'
|
2014-01-03 13:32:13 -05:00
|
|
|
]
|
|
|
|
|
2014-01-22 17:57:41 -05:00
|
|
|
for m in models_path
|
|
|
|
model = path.basename(m)
|
|
|
|
#console.log('model=' + model)
|
|
|
|
GLOBAL[model] = require m
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
async = require 'async'
|
|
|
|
|
|
|
|
GLOBAL.clearModels = (models, done) ->
|
|
|
|
|
|
|
|
funcs = []
|
|
|
|
for model in models
|
|
|
|
if model is User
|
|
|
|
unittest.users = {}
|
|
|
|
wrapped = (m) -> (callback) ->
|
|
|
|
m.remove {}, (err) ->
|
|
|
|
callback(err, true)
|
|
|
|
funcs.push(wrapped(model))
|
|
|
|
|
|
|
|
async.parallel funcs, (err, results) ->
|
|
|
|
done(err)
|
|
|
|
|
|
|
|
GLOBAL.saveModels = (models, done) ->
|
|
|
|
|
|
|
|
funcs = []
|
|
|
|
for model in models
|
|
|
|
wrapped = (m) -> (callback) ->
|
|
|
|
m.save (err) ->
|
|
|
|
callback(err, true)
|
|
|
|
funcs.push(wrapped(model))
|
|
|
|
|
|
|
|
async.parallel funcs, (err, results) ->
|
|
|
|
done(err)
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
GLOBAL.simplePermissions = [target: 'public', access: 'owner']
|
2014-01-03 13:32:13 -05:00
|
|
|
GLOBAL.ObjectId = mongoose.Types.ObjectId
|
|
|
|
GLOBAL.request = require 'request'
|
|
|
|
|
|
|
|
GLOBAL.unittest = {}
|
|
|
|
unittest.users = unittest.users or {}
|
|
|
|
|
|
|
|
unittest.getNormalJoe = (done, force) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
unittest.getUser('Joe', 'normal@jo.com', 'food', done, force)
|
2014-01-03 13:32:13 -05:00
|
|
|
unittest.getOtherSam = (done, force) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
unittest.getUser('Sam', 'other@sam.com', 'beer', done, force)
|
2014-01-03 13:32:13 -05:00
|
|
|
unittest.getAdmin = (done, force) ->
|
2014-04-17 20:09:01 -04:00
|
|
|
unittest.getUser('Admin', 'admin@afc.com', '80yqxpb38j', done, force)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-04-17 20:09:01 -04:00
|
|
|
unittest.getUser = (name, email, password, done, force) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
# Creates the user if it doesn't already exist.
|
|
|
|
|
|
|
|
return done(unittest.users[email]) if unittest.users[email] and not force
|
|
|
|
request = require 'request'
|
|
|
|
request.post getURL('/auth/logout'), ->
|
2014-02-24 23:27:38 -05:00
|
|
|
request.get getURL('/auth/whoami'), ->
|
|
|
|
req = request.post(getURL('/db/user'), (err, response, body) ->
|
|
|
|
throw err if err
|
2014-06-30 22:16:26 -04:00
|
|
|
User.findOne({email: email}).exec((err, user) ->
|
|
|
|
user.set('permissions', if password is '80yqxpb38j' then ['admin'] else [])
|
2014-04-17 20:09:01 -04:00
|
|
|
user.set('name', name)
|
2014-04-08 17:10:50 -04:00
|
|
|
user.save (err) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
wrapUpGetUser(email, user, done)
|
2014-02-24 23:27:38 -05:00
|
|
|
)
|
2014-01-03 13:32:13 -05:00
|
|
|
)
|
2014-02-24 23:27:38 -05:00
|
|
|
form = req.form()
|
|
|
|
form.append('email', email)
|
|
|
|
form.append('password', password)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
wrapUpGetUser = (email, user, done) ->
|
|
|
|
unittest.users[email] = user
|
|
|
|
return done(unittest.users[email])
|
|
|
|
|
|
|
|
GLOBAL.getURL = (path) ->
|
|
|
|
return 'http://localhost:3001' + path
|
|
|
|
|
2015-03-19 18:02:45 -04:00
|
|
|
GLOBAL.createPrepaid = (type, done) ->
|
|
|
|
options = uri: GLOBAL.getURL('/db/prepaid/-/create')
|
|
|
|
options.json = type: type if type?
|
|
|
|
request.post options, done
|
|
|
|
|
2015-03-13 18:19:20 -04:00
|
|
|
newUserCount = 0
|
|
|
|
GLOBAL.createNewUser = (done) ->
|
|
|
|
name = password = "user#{newUserCount++}"
|
|
|
|
email = "#{name}@foo.bar"
|
|
|
|
unittest.getUser name, email, password, done, true
|
|
|
|
GLOBAL.loginNewUser = (done) ->
|
|
|
|
name = password = "user#{newUserCount++}"
|
|
|
|
email = "#{name}@me.com"
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
unittest.getUser name, email, password, (user) ->
|
|
|
|
req = request.post(getURL('/auth/login'), (error, response) ->
|
|
|
|
expect(response.statusCode).toBe(200)
|
|
|
|
done(user)
|
|
|
|
)
|
|
|
|
form = req.form()
|
|
|
|
form.append('username', email)
|
|
|
|
form.append('password', password)
|
|
|
|
, true
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
GLOBAL.loginJoe = (done) ->
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
unittest.getNormalJoe (user) ->
|
|
|
|
req = request.post(getURL('/auth/login'), (error, response) ->
|
|
|
|
expect(response.statusCode).toBe(200)
|
|
|
|
done(user)
|
|
|
|
)
|
|
|
|
form = req.form()
|
|
|
|
form.append('username', 'normal@jo.com')
|
|
|
|
form.append('password', 'food')
|
|
|
|
|
|
|
|
GLOBAL.loginSam = (done) ->
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
unittest.getOtherSam (user) ->
|
|
|
|
req = request.post(getURL('/auth/login'), (error, response) ->
|
|
|
|
expect(response.statusCode).toBe(200)
|
|
|
|
done(user)
|
|
|
|
)
|
|
|
|
form = req.form()
|
|
|
|
form.append('username', 'other@sam.com')
|
|
|
|
form.append('password', 'beer')
|
|
|
|
|
|
|
|
GLOBAL.loginAdmin = (done) ->
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
unittest.getAdmin (user) ->
|
|
|
|
req = request.post(getURL('/auth/login'), (error, response) ->
|
|
|
|
expect(response.statusCode).toBe(200)
|
|
|
|
done(user)
|
|
|
|
)
|
|
|
|
form = req.form()
|
|
|
|
form.append('username', 'admin@afc.com')
|
|
|
|
form.append('password', '80yqxpb38j')
|
|
|
|
# find some other way to make the admin object an admin... maybe directly?
|
|
|
|
|
2015-03-13 18:19:20 -04:00
|
|
|
GLOBAL.loginUser = (user, done) ->
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
req = request.post(getURL('/auth/login'), (error, response) ->
|
|
|
|
expect(response.statusCode).toBe(200)
|
|
|
|
done(user)
|
|
|
|
)
|
|
|
|
form = req.form()
|
|
|
|
form.append('username', user.get('email'))
|
|
|
|
form.append('password', user.get('name'))
|
|
|
|
|
2015-04-01 14:56:48 -04:00
|
|
|
GLOBAL.logoutUser = (done) ->
|
|
|
|
request.post getURL('/auth/logout'), ->
|
|
|
|
done()
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
GLOBAL.dropGridFS = (done) ->
|
|
|
|
if mongoose.connection.readyState is 2
|
|
|
|
mongoose.connection.once 'open', ->
|
|
|
|
_drop(done)
|
|
|
|
else
|
|
|
|
_drop(done)
|
|
|
|
|
|
|
|
_drop = (done) ->
|
|
|
|
files = mongoose.connection.db.collection('media.files')
|
|
|
|
files.remove {}, ->
|
|
|
|
chunks = mongoose.connection.db.collection('media.chunks')
|
|
|
|
chunks.remove {}, ->
|
|
|
|
done()
|
2014-06-11 16:21:11 -04:00
|
|
|
|
|
|
|
tickInterval = null
|
|
|
|
tick = ->
|
|
|
|
# When you want jasmine-node to exit after running the tests,
|
|
|
|
# you have to close the connection first.
|
|
|
|
if rep.finished
|
|
|
|
mongoose.disconnect()
|
|
|
|
clearTimeout tickInterval
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
tickInterval = setInterval tick, 1000
|