Finished basic functional tests for achievement

This commit is contained in:
Ruben Vereecken 2014-06-09 17:28:35 +02:00
parent aecf937722
commit b66f4984c3
9 changed files with 118 additions and 14 deletions

View file

@ -2,6 +2,7 @@ mongoose = require('mongoose')
jsonschema = require('../../app/schemas/models/achievement')
log = require 'winston'
util = require '../../app/lib/utils'
plugins = require('../plugins/plugins')
# `pre` and `post` are not called for update operations executed directly on the database,
# including `Model.update`,`.findByIdAndUpdate`,`.findOneAndUpdate`, `.findOneAndRemove`,and `.findByIdAndRemove`.order
@ -35,9 +36,7 @@ AchievementSchema.pre('save', (next) ->
next()
)
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema)
plugins = require('../plugins/plugins')
AchievementSchema.plugin(plugins.NamedPlugin)
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema)

View file

@ -17,6 +17,7 @@ module.exports = class Handler
postEditableProperties: []
jsonSchema: {}
waterfallFunctions: []
allowedMethods: ['GET', 'POST', 'PUT', 'PATCH']
# subclasses should override these methods
hasAccess: (req) -> true
@ -420,3 +421,7 @@ module.exports = class Handler
dict[document.id] = document
res.send dict
res.end()
delete: (req, res) -> @sendMethodNotAllowed res, @allowedMethods, "DELETE not allowed."
head: (req, res) -> @sendMethodNotAllowed res, @allowedMethods, "HEAD not allowed."

View file

@ -17,8 +17,9 @@ module.exports.notFound = (res, message='Not found.') ->
res.send 404, message
res.end()
module.exports.badMethod = (res, message='Method Not Allowed') ->
# TODO: The response MUST include an Allow header containing a list of valid methods for the requested resource
module.exports.badMethod = (res, allowed=['GET', 'POST', 'PUT', 'PATCH'], message='Method Not Allowed') ->
allowHeader = _.reduce allowed, ((str, current) -> str += ', ' + current)
res.set 'Allow', allowHeader # TODO not sure if these are always the case
res.send 405, message
res.end()

View file

@ -8,7 +8,7 @@ module.exports.setup = (app) ->
app.all '/file*', (req, res) ->
return fileGet(req, res) if req.route.method is 'get'
return filePost(req, res) if req.route.method is 'post'
return errors.badMethod(res)
return errors.badMethod(res, ['GET', 'POST'])
fileGet = (req, res) ->

View file

@ -30,6 +30,8 @@ models_path = [
'../../server/levels/thangs/LevelThangType'
'../../server/users/User'
'../../server/patches/Patch'
'../../server/achievements/Achievement'
'../../server/achievements/EarnedAchievement'
]
for m in models_path

View file

@ -0,0 +1,86 @@
require '../common'
describe 'Achievement', ->
unlockable =
name: 'One Time Only'
description: 'So you did the really cool thing.'
worth: 6.66
collection: 'level.session'
repeatable =
name: 'Lots of em'
description: 'Oops you did it again.'
worth: 1
collection: 'User'
proportionalTo: '_id'
url = getURL('/db/achievement')
allowHeader = 'GET, POST, PUT, PATCH'
it 'preparing test: deleting all Achievements first', (done) ->
clearModels [Achievement], (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
done()
it 'can get all for ordinary users', (done) ->
loginJoe ->
request.get {uri: url, json: unlockable}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.length).toBe(1)
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()
it 'cleaning up test: deleting all Achievements', (done) ->
clearModels [Achievement], (err) ->
expect(err).toBeNull()
done()

View file

@ -28,6 +28,8 @@ xdescribe '/file', ->
my_buffer_url: 'http://fc07.deviantart.net/fs37/f/2008/283/5/1/Chu_Chu_Pikachu_by_angelishi.gif'
}
allowHeader = 'GET, POST'
it 'preparing test : deletes all the files first', (done) ->
dropGridFS ->
done()
@ -147,19 +149,28 @@ xdescribe '/file', ->
request.post(options, func)
it ' can\'t be requested with HTTP PATCH method', (done) ->
request {method: 'patch', uri:url}, (err, res) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it ' can\'t be requested with HTTP PUT method', (done) ->
request.put {uri:url}, (err, res) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it ' can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri:url}, (err, res) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it ' can\'t be requested with HTTP DEL method', (done) ->
request.del {uri:url}, (err, res) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
# TODO: test server errors, see what they do

View file

@ -130,17 +130,17 @@ describe 'LevelComponent', ->
xit ' can\'t be requested with HTTP PUT method', (done) ->
request.put {uri:url+'/'+components[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
expect(res.statusCode).toBe(405)
done()
it ' can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri:url+'/'+components[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
expect(res.statusCode).toBe(405)
done()
it ' can\'t be requested with HTTP DEL method', (done) ->
request.del {uri:url+'/'+components[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
expect(res.statusCode).toBe(405)
done()
it 'get schema', (done) ->

View file

@ -123,12 +123,12 @@ describe 'LevelSystem', ->
it ' can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri:url+'/'+systems[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
expect(res.statusCode).toBe(405)
done()
it ' can\'t be requested with HTTP DEL method', (done) ->
request.del {uri:url+'/'+systems[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
expect(res.statusCode).toBe(405)
done()
it 'get schema', (done) ->