codecombat/test/server/functional/achievement.spec.coffee

125 lines
3.6 KiB
CoffeeScript
Raw Normal View History

require '../common'
2014-06-10 14:05:32 -04:00
unlockable =
name: 'Dungeon Arena Started'
description: 'Started playing Dungeon Arena.'
worth: 3
collection: 'level.session'
query: "{\"level.original\":\"dungeon-arena\"}"
userField: 'creator'
2014-06-10 14:05:32 -04:00
repeatable =
name: 'Simulated'
description: 'Simulated Games.'
worth: 1
collection: 'User'
query: "{\"simulatedBy\":{\"$gt\":\"0\"}}"
userField: '_id'
proportionalTo: 'simulatedBy'
2014-06-10 14:05:32 -04:00
url = getURL('/db/achievement')
2014-06-10 14:05:32 -04:00
describe 'Achievement', ->
allowHeader = 'GET, POST, PUT, PATCH'
it 'preparing test: deleting all Achievements first', (done) ->
2014-06-10 14:05:32 -04:00
clearModels [Achievement, EarnedAchievement, LevelSession, User], (err) ->
expect(err).toBeNull()
done()
it 'can\'t be created by ordinary users', (done) ->
loginJoe ->
request.post {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'can\'t be updated by ordinary users', (done) ->
loginJoe ->
request.put {uri: url, json:unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(403)
request {method: 'patch', uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'can be created by admins', (done) ->
loginAdmin ->
request.post {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
unlockable._id = body._id
2014-06-10 14:05:32 -04:00
request.post {uri: url, json: repeatable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
repeatable._id = body._id
2014-06-13 12:04:25 -04:00
Achievement.find {}, (err, docs) ->
expect(docs.length).toBe(2)
2014-06-10 14:05:32 -04:00
done()
it 'can get all for ordinary users', (done) ->
loginJoe ->
request.get {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
2014-06-10 14:05:32 -04:00
expect(body.length).toBe(2)
done()
it 'can be read by ordinary users', (done) ->
loginJoe ->
request.get {uri: url + '/' + unlockable._id, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.name).toBe(unlockable.name)
done()
it 'can\'t be requested with HTTP HEAD method', (done) ->
loginJoe ->
request.head {uri: url + '/' + unlockable._id}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP DEL method', (done) ->
loginJoe ->
request.del {uri: url + '/' + unlockable._id}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'get schema', (done) ->
request.get {uri:url + '/schema'}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body.type).toBeDefined()
done()
2014-06-10 14:05:32 -04:00
describe 'Achieving Achievements', ->
it 'allows users to unlock one-time Achievements', (done) ->
loginJoe (joe) ->
2014-06-13 12:04:25 -04:00
session = new LevelSession(
permissions: simplePermissions
2014-06-10 14:05:32 -04:00
creator: joe._id
level: original: 'dungeon-arena'
2014-06-13 12:04:25 -04:00
)
2014-06-10 14:05:32 -04:00
2014-06-13 12:04:25 -04:00
session.save (err, doc) ->
expect(err).toBeNull()
expect(doc).toBeDefined()
expect(doc.creator).toBe(session.creator)
2014-06-10 14:05:32 -04:00
2014-06-13 12:04:25 -04:00
EarnedAchievement.find {}, (err, docs) ->
expect(err).toBeNull()
console.log docs
expect(docs.length).toBe(1)
done()
2014-06-10 14:05:32 -04:00
2014-06-13 12:04:25 -04:00
it 'cleaning up test: deleting all Achievements and relates', (done) ->
2014-06-10 14:05:32 -04:00
clearModels [Achievement, EarnedAchievement, LevelSession], (err) ->
expect(err).toBeNull()
done()
2014-06-10 14:05:32 -04:00
2014-06-13 12:04:25 -04:00