codecombat/spec/server/functional/level_system.spec.coffee

139 lines
4.8 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
require '../common'
Level = require '../../../server/models/Level'
LevelSystem = require '../../../server/models/LevelSystem'
request = require '../request'
2014-01-03 13:32:13 -05:00
describe 'LevelSystem', ->
2014-01-15 14:36:59 -05:00
system =
2014-06-30 22:16:26 -04:00
name: 'Bashing'
description: 'Performs Thang bashing updates for Bashes Thangs.'
2014-01-03 13:32:13 -05:00
code: """class Bashing extends System
constructor: (world) ->
super world
"""
codeLanguage: 'coffeescript'
2014-06-30 22:16:26 -04:00
permissions: simplePermissions
dependencies: []
2014-01-03 13:32:13 -05:00
systems = {}
url = getURL('/db/level.system')
2014-01-15 14:36:59 -05:00
it 'preparing test : deletes all LevelSystem first', (done) ->
2014-01-03 13:32:13 -05:00
clearModels [Level, LevelSystem], (err) ->
expect(err).toBeNull()
done()
2014-01-15 14:36:59 -05:00
it 'can\'t be created by ordinary users.', (done) ->
loginJoe ->
2014-06-30 22:16:26 -04:00
request.post {uri: url, json: system}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(403)
done()
it 'can be created by an admin.', (done) ->
loginAdmin ->
2014-06-30 22:16:26 -04:00
request.post {uri: url, json: system}, (err, res, body) ->
2014-01-03 13:32:13 -05:00
expect(res.statusCode).toBe(200)
2014-01-15 14:36:59 -05:00
expect(body._id).toBeDefined()
expect(body.name).toBe(system.name)
expect(body.description).toBe(system.description)
expect(body.code).toBe(system.code)
expect(body.codeLanguage).toBe(system.codeLanguage)
2014-01-15 14:36:59 -05:00
expect(body.__v).toBe(0)
expect(body.creator).toBeDefined()
expect(body.original).toBeDefined()
expect(body.created).toBeDefined()
expect(body.version).toBeDefined()
expect(body.permissions).toBeDefined()
2014-01-03 13:32:13 -05:00
systems[0] = body
done()
2014-01-15 14:36:59 -05:00
it 'have a unique name.', (done) ->
loginAdmin ->
2014-06-30 22:16:26 -04:00
request.post {uri: url, json: system}, (err, res, body) ->
expect(res.statusCode).toBe(409)
2014-01-15 14:36:59 -05:00
done()
it 'can be read by an admin.', (done) ->
loginAdmin ->
2014-06-30 22:16:26 -04:00
request.get {uri: url+'/'+systems[0]._id}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
done()
it 'can be read by ordinary users.', (done) ->
loginJoe ->
2014-06-30 22:16:26 -04:00
request.get {uri: url+'/'+systems[0]._id}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
expect(body.name).toBe(systems[0].name)
expect(body.slug).toBeDefined()
expect(body.description).toBe(systems[0].description)
expect(body.code).toBe(systems[0].code)
expect(body.codeLanguage).toBe(systems[0].codeLanguage)
2014-01-15 14:36:59 -05:00
expect(body.__v).toBe(0)
expect(body.creator).toBeDefined()
expect(body.original).toBeDefined()
expect(body.created).toBeDefined()
expect(body.dependencies).toBeDefined()
expect(body.version.isLatestMajor).toBe(true)
expect(body.version.isLatestMinor).toBe(true)
expect(body.permissions).toBeDefined()
done()
it 'is unofficial by default', (done) ->
loginJoe ->
2014-06-30 22:16:26 -04:00
request.get {uri: url+'/'+systems[0]._id}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
2014-08-29 19:28:07 -04:00
expect(body.official).toBeUndefined()
2014-01-15 14:36:59 -05:00
done()
it 'official property isn\'t editable by an ordinary user.', (done) ->
2014-01-03 13:32:13 -05:00
systems[0].official = true
2014-01-15 14:36:59 -05:00
loginJoe ->
2014-06-30 22:16:26 -04:00
request.post {uri: url, json: systems[0]}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(403)
2014-01-03 13:32:13 -05:00
done()
2014-01-15 14:36:59 -05:00
it 'official property is editable by an admin.', (done) ->
systems[0].official = true
loginAdmin ->
2014-06-30 22:16:26 -04:00
request.post {uri: url, json: systems[0]}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(200)
expect(body.official).toBe(true)
expect(body.original).toBe(systems[0].original)
expect(body.version.isLatestMinor).toBe(true)
expect(body.version.isLatestMajor).toBe(true)
systems[1] = body
2014-06-30 22:16:26 -04:00
request.get {uri: url+'/'+systems[0]._id}, (err, res, body) ->
2014-01-15 14:36:59 -05:00
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
2014-08-29 19:28:07 -04:00
expect(body.official).toBeUndefined()
2014-01-15 14:36:59 -05:00
expect(body.version.isLatestMinor).toBe(false)
expect(body.version.isLatestMajor).toBe(false)
done()
it ' can\'t be requested with HTTP HEAD method', (done) ->
2014-06-30 22:16:26 -04:00
request.head {uri: url+'/'+systems[0]._id}, (err, res) ->
expect(res.statusCode).toBe(405)
2014-01-15 14:36:59 -05:00
done()
it ' can\'t be requested with HTTP DEL method', (done) ->
2014-06-30 22:16:26 -04:00
request.del {uri: url+'/'+systems[0]._id}, (err, res) ->
expect(res.statusCode).toBe(405)
2014-01-15 14:36:59 -05:00
done()
it 'get schema', (done) ->
2014-06-30 22:16:26 -04:00
request.get {uri: url+'/schema'}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body.type).toBeDefined()
done()