codecombat/spec/server/common.coffee

204 lines
6.3 KiB
CoffeeScript
Raw Normal View History

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
2016-06-17 13:35:22 -04:00
console.log '/spec/server/common.coffee - Setting up spec globals...'
if process.env.COCO_MONGO_HOST
throw Error('Tests may not run with production environment')
require '../../server' # make lodash globally available
mongoose = require 'mongoose'
2014-06-30 22:16:26 -04:00
path = require 'path'
GLOBAL.testing = true
2014-08-29 15:58:23 -04:00
GLOBAL.tv4 = require 'tv4' # required for TreemaUtils to work
User = require '../../server/models/User'
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.unittest = {}
unittest.users = unittest.users or {}
unittest.getNormalJoe = (done, force) ->
unittest.getUser('Joe', 'normal@jo.com', 'food', done, force)
2014-01-03 13:32:13 -05:00
unittest.getOtherSam = (done, force) ->
unittest.getUser('Sam', 'other@sam.com', 'beer', done, force)
2014-01-03 13:32:13 -05:00
unittest.getAdmin = (done, force) ->
unittest.getUser('Admin', 'admin@afc.com', '80yqxpb38j', done, force)
2014-01-03 13:32:13 -05: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.post getURL('/auth/logout'), ->
request.get getURL('/auth/whoami'), ->
2016-06-17 13:35:22 -04:00
req = request.post({url: getURL('/db/user'), json: {email, password}}, (err, response, body) ->
throw err if err
2014-06-30 22:16:26 -04:00
User.findOne({email: email}).exec((err, user) ->
throw err if err
2014-06-30 22:16:26 -04:00
user.set('permissions', if password is '80yqxpb38j' then ['admin'] else [])
user.set('name', name)
user.save (err) ->
2014-01-03 13:32:13 -05:00
wrapUpGetUser(email, user, done)
)
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
nameCount = 0
GLOBAL.createName = (name) ->
name + nameCount++
GLOBAL.createCourse = (pricePerSeat, done) ->
name = createName 'course '
course = new Course
name: name
campaignID: ObjectId("55b29efd1cd6abe8ce07db0d")
concepts: ['basic_syntax', 'arguments', 'while_loops', 'strings', 'variables']
description: "Learn basic syntax, while loops, and the CodeCombat environment."
pricePerSeat: pricePerSeat
screenshot: "/images/pages/courses/101_info.png"
course.save (err, course) =>
return done(err) if err
done(err, course)
2015-09-25 13:03:44 -04:00
GLOBAL.createPrepaid = (type, maxRedeemers, months, done) ->
options = uri: GLOBAL.getURL('/db/prepaid/-/create')
options.json =
type: type
maxRedeemers: maxRedeemers
2015-09-25 13:03:44 -04:00
if months
options.json.months = months
request.post options, done
GLOBAL.fetchPrepaid = (ppc, done) ->
options = uri: GLOBAL.getURL('/db/prepaid/-/code/'+ppc)
request.get options, done
GLOBAL.purchasePrepaid = (type, properties, maxRedeemers, token, done) ->
2015-09-25 13:03:44 -04:00
options = uri: GLOBAL.getURL('/db/prepaid/-/purchase')
options.json =
type: type
maxRedeemers: maxRedeemers
stripe:
timestamp: new Date().getTime()
options.json.stripe.token = token if token?
if type is 'terminal_subscription'
options.json.months = properties.months
2015-09-25 13:03:44 -04:00
request.post options, done
GLOBAL.subscribeWithPrepaid = (ppc, done) =>
options = url: GLOBAL.getURL('/db/subscription/-/subscribe_prepaid')
options.json =
ppc: ppc
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) ->
2016-06-17 13:35:22 -04:00
json = {username: email, password}
req = request.post({url: getURL('/auth/login'), json}, (error, response) ->
2015-03-13 18:19:20 -04:00
expect(response.statusCode).toBe(200)
done(user)
)
, true
2014-01-03 13:32:13 -05:00
GLOBAL.loginJoe = (done) ->
request.post getURL('/auth/logout'), ->
unittest.getNormalJoe (user) ->
2016-06-17 13:35:22 -04:00
json = {username: 'normal@jo.com', password: 'food'}
req = request.post({url: getURL('/auth/login'), json}, (error, response) ->
2014-01-03 13:32:13 -05:00
expect(response.statusCode).toBe(200)
done(user)
)
GLOBAL.loginSam = (done) ->
request.post getURL('/auth/logout'), ->
unittest.getOtherSam (user) ->
2016-06-17 13:35:22 -04:00
json = { username: 'other@sam.com', password: 'beer'}
req = request.post({url: getURL('/auth/login'), json}, (error, response) ->
2014-01-03 13:32:13 -05:00
expect(response.statusCode).toBe(200)
done(user)
)
GLOBAL.loginAdmin = (done) ->
request.post getURL('/auth/logout'), ->
unittest.getAdmin (user) ->
2016-06-17 13:35:22 -04:00
json = { username: 'admin@afc.com', password: '80yqxpb38j' }
req = request.post({url: getURL('/auth/login'), json}, (error, response) ->
2014-01-03 13:32:13 -05:00
expect(response.statusCode).toBe(200)
done(user)
)
# 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'), ->
2016-06-17 13:35:22 -04:00
json = { username: user.get('email'), password: user.get('name') }
req = request.post({ url: getURL('/auth/login'), json}, (error, response) ->
2015-03-13 18:19:20 -04:00
expect(response.statusCode).toBe(200)
done(user)
)
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()
GLOBAL.resetUserIDCounter = (number=0) ->
User.idCounter = number
2016-06-17 13:35:22 -04:00
console.log '/spec/server/common.coffee - Done'