2013-02-20 18:35:21 +00:00
|
|
|
assert = require 'assert'
|
|
|
|
|
{BrowserModel: Model} = require 'racer/test/util/model'
|
|
|
|
|
derby = require 'derby'
|
2013-02-21 19:18:53 +00:00
|
|
|
racer = require 'racer'
|
2013-02-20 18:35:21 +00:00
|
|
|
_ = require 'underscore'
|
|
|
|
|
moment = require 'moment'
|
2013-02-21 19:18:53 +00:00
|
|
|
request = require 'superagent'
|
2013-02-21 01:04:09 +00:00
|
|
|
qs = require 'querystring'
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-22 01:17:23 +00:00
|
|
|
racer.use require 'racer-db-mongo'
|
2013-02-21 19:18:53 +00:00
|
|
|
|
2013-02-22 01:17:23 +00:00
|
|
|
store = racer.createStore
|
|
|
|
|
db:
|
|
|
|
|
type: 'Mongo'
|
|
|
|
|
uri: process.env.NODE_DB_URI
|
2013-02-21 19:18:53 +00:00
|
|
|
|
2013-02-20 18:35:21 +00:00
|
|
|
# Custom modules
|
|
|
|
|
scoring = require '../src/app/scoring'
|
|
|
|
|
character = require '../src/app/character'
|
2013-02-21 01:04:09 +00:00
|
|
|
config = require './config'
|
2013-02-20 18:35:21 +00:00
|
|
|
|
|
|
|
|
###### Helpers & Variables ######
|
|
|
|
|
|
|
|
|
|
model = null
|
|
|
|
|
uuid = null
|
|
|
|
|
taskPath = null
|
2013-02-21 01:04:09 +00:00
|
|
|
baseURL = 'http://localhost:3000/api/v1'
|
|
|
|
|
UID_AND_TOKEN =
|
|
|
|
|
uid: config.uid
|
|
|
|
|
token: config.token
|
2013-02-20 18:35:21 +00:00
|
|
|
|
|
|
|
|
## Helper which clones the content at a path so tests can compare before/after values
|
|
|
|
|
# Otherwise, using model.get(path) will give the same object before as after
|
|
|
|
|
pathSnapshots = (paths) ->
|
|
|
|
|
if _.isString(paths)
|
|
|
|
|
return clone(model.get(paths))
|
|
|
|
|
_.map paths, (path) -> clone(model.get(path))
|
|
|
|
|
statsTask = -> pathSnapshots(['_user.stats', taskPath]) # quick snapshot of user.stats & task
|
|
|
|
|
|
|
|
|
|
cleanUserObj = ->
|
|
|
|
|
userObj = character.newUserObject()
|
|
|
|
|
userObj.tasks = {}
|
|
|
|
|
userObj.habitIds = []
|
|
|
|
|
userObj.dailyIds = []
|
|
|
|
|
userObj.todoIds = []
|
|
|
|
|
userObj.rewardIds = []
|
|
|
|
|
return userObj
|
|
|
|
|
resetUser = -> model.set '_user', cleanUserObj()
|
|
|
|
|
|
|
|
|
|
freshTask = (taskObj) ->
|
|
|
|
|
resetUser()
|
|
|
|
|
# create a test task
|
|
|
|
|
uuid = derby.uuid()
|
|
|
|
|
taskPath = "_user.tasks.#{uuid}"
|
|
|
|
|
{type} = taskObj
|
|
|
|
|
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
|
|
|
|
[taskObj.id, taskObj.value] = [uuid, 0]
|
|
|
|
|
model.at("_#{type}List").push taskObj
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
|
Helper function to determine if stats updates are numerically correct based on scoring
|
|
|
|
|
@direction: 'up' or 'down'
|
|
|
|
|
@options: The user stats modifiers and times to run, defaults to {times:1, modifiers:{lvl:1, weapon:0, armor:0}}
|
|
|
|
|
###
|
|
|
|
|
modificationsLookup = (direction, options = {}) ->
|
|
|
|
|
merged = _.defaults options, {times:1, lvl:1, weapon:0, armor:0}
|
|
|
|
|
{times, lvl, armor, weapon} = merged
|
|
|
|
|
userObj = cleanUserObj()
|
|
|
|
|
value = 0
|
|
|
|
|
_.times times, (n) ->
|
|
|
|
|
delta = scoring.taskDeltaFormula(value, direction)
|
|
|
|
|
value += delta
|
|
|
|
|
if direction=='up'
|
|
|
|
|
gain = scoring.expModifier(delta, options)
|
|
|
|
|
userObj.stats.exp += gain
|
|
|
|
|
userObj.stats.money += gain
|
|
|
|
|
else
|
|
|
|
|
loss = scoring.hpModifier(delta, options)
|
|
|
|
|
userObj.stats.hp += loss
|
|
|
|
|
return {user:userObj, value:value}
|
|
|
|
|
|
|
|
|
|
###### Specs ######
|
|
|
|
|
|
|
|
|
|
describe 'API', ->
|
|
|
|
|
model = null
|
2013-02-21 19:18:53 +00:00
|
|
|
user = null
|
|
|
|
|
params = null
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-21 01:04:09 +00:00
|
|
|
describe 'Without token or user id', ->
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-22 18:56:24 +00:00
|
|
|
it '/api/v1/status', (done) ->
|
|
|
|
|
request.get("#{baseURL}/status")
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.end (res) ->
|
|
|
|
|
assert.equal res.statusCode, 200
|
|
|
|
|
assert.equal res.body.status, 'up'
|
|
|
|
|
done()
|
|
|
|
|
|
2013-02-21 01:04:09 +00:00
|
|
|
it '/api/v1/user', (done) ->
|
2013-02-22 01:17:23 +00:00
|
|
|
request.get("#{baseURL}/user")
|
2013-02-21 19:18:53 +00:00
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.end (res) ->
|
|
|
|
|
assert.equal res.statusCode, 500
|
|
|
|
|
assert.ok JSON.parse(res.text).err
|
|
|
|
|
done()
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-21 01:04:09 +00:00
|
|
|
describe 'With token and user id', ->
|
2013-02-21 19:18:53 +00:00
|
|
|
before (done) ->
|
|
|
|
|
model = store.createModel()
|
2013-02-22 01:17:23 +00:00
|
|
|
#store.flush()
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-22 01:17:23 +00:00
|
|
|
model.set '_userId', uid = model.id()
|
2013-02-21 19:18:53 +00:00
|
|
|
user = character.newUserObject()
|
|
|
|
|
user.apiToken = derby.uuid()
|
|
|
|
|
model.set "users.#{uid}", user
|
|
|
|
|
user = model.get("users.#{uid}")
|
|
|
|
|
|
|
|
|
|
params =
|
|
|
|
|
uid: user.id
|
|
|
|
|
token: user.apiToken
|
2013-02-22 18:54:35 +00:00
|
|
|
done()
|
2013-02-20 18:35:21 +00:00
|
|
|
|
2013-02-22 01:38:12 +00:00
|
|
|
###
|
|
|
|
|
test '/api/v1/user', (done) ->
|
2013-02-22 01:17:23 +00:00
|
|
|
console.log "#{baseURL}/user?#{qs.stringify(params)}"
|
2013-02-22 01:38:12 +00:00
|
|
|
_.defer ->
|
|
|
|
|
request.get("#{baseURL}/user")
|
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
.query(params)
|
|
|
|
|
.on('error', (err) ->
|
|
|
|
|
console.log 'err', err
|
|
|
|
|
)
|
|
|
|
|
.end (res) ->
|
|
|
|
|
assert.ok !res.body.err
|
|
|
|
|
assert.equal res.statusCode, 200
|
|
|
|
|
assert.ok res.body.tasks
|
|
|
|
|
done()
|
|
|
|
|
###
|
|
|
|
|
it '/api/v1/task', (done) ->
|
|
|
|
|
request.post("#{baseURL}/task")
|
2013-02-21 19:18:53 +00:00
|
|
|
.set('Accept', 'application/json')
|
2013-02-22 01:38:12 +00:00
|
|
|
.send(params)
|
2013-02-21 19:18:53 +00:00
|
|
|
.on('error', (err) ->
|
|
|
|
|
console.log 'err', err
|
|
|
|
|
)
|
|
|
|
|
.end (res) ->
|
2013-02-22 01:38:12 +00:00
|
|
|
#console.log 'task', res.body
|
2013-02-21 19:18:53 +00:00
|
|
|
assert.ok !res.body.err
|
|
|
|
|
assert.equal res.statusCode, 200
|
2013-02-22 01:38:12 +00:00
|
|
|
assert.ok res.body.tasks
|
2013-02-21 19:18:53 +00:00
|
|
|
done()
|