Fix all server side test

This commit is contained in:
Sébastien Moratinos 2014-01-15 20:36:59 +01:00
parent 53f47bd4f5
commit e5ef0b707c
10 changed files with 147 additions and 55 deletions

View file

@ -40,7 +40,7 @@ describe '/file', ->
expect(res.statusCode).toBe(422)
done()
loginAdmin (joe) ->
loginAdmin ->
request.post(options, func)
it 'can be created by an admin', (done) ->

View file

@ -21,11 +21,11 @@ describe '/db/article', ->
loginAdmin ->
request.post {uri:url, json:article}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.slug).not.toBeUndefined()
expect(body.body).not.toBeUndefined()
expect(body.name).not.toBeUndefined()
expect(body.original).not.toBeUndefined()
expect(body.creator).not.toBeUndefined()
expect(body.slug).toBeDefined()
expect(body.body).toBeDefined()
expect(body.name).toBeDefined()
expect(body.original).toBeDefined()
expect(body.creator).toBeDefined()
articles[0] = body
done()
@ -38,7 +38,7 @@ describe '/db/article', ->
expect(body.version.minor).toBe(1)
expect(body._id).not.toBe(articles[0]._id)
expect(body.parent).toBe(articles[0]._id)
expect(body.creator).not.toBeUndefined()
expect(body.creator).toBeDefined()
articles[1] = body
done()

View file

@ -16,18 +16,18 @@ describe '/db/campaign', ->
campaign.permissions = [access: 'owner', target: user._id]
request.post {uri:url, json:campaign}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.permissions).not.toBeUndefined()
expect(body.permissions).toBeDefined()
campaigns[0] = body
done()
it 'does not allow other users access', (done) ->
loginSam (user) ->
loginSam ->
request.get {uri:url+'/'+campaigns[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'allows editing permissions.', (done) ->
loginJoe (user) ->
loginJoe ->
campaigns[0].permissions.push(access: 'read', target: 'public')
request.put {uri:url, json:campaigns[0]}, (err, res, body) ->
expect(res.statusCode).toBe(200)
@ -36,7 +36,7 @@ describe '/db/campaign', ->
done()
it 'allows anyone to access it through public permissions', (done) ->
loginSam (user) ->
loginSam ->
request.get {uri:url+'/'+campaigns[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
done()

View file

@ -15,7 +15,7 @@ describe 'Level', ->
done()
it 'can make a Level.', (done) ->
loginJoe (joe) ->
loginJoe ->
request.post {uri:url, json:level}, (err, res, body) ->
expect(res.statusCode).toBe(200)
done()

View file

@ -19,13 +19,13 @@ describe 'LevelComponent', ->
done()
it 'can\'t be created by ordinary users.', (done) ->
loginJoe (joe) ->
loginJoe ->
request.post {uri:url, json:component}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'can be created by an admin.', (done) ->
loginAdmin (joe) ->
loginAdmin ->
request.post {uri:url, json:component}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body._id).toBeDefined()
@ -43,13 +43,13 @@ describe 'LevelComponent', ->
done()
it 'have a unique name.', (done) ->
loginAdmin (joe) ->
loginAdmin ->
request.post {uri:url, json:component}, (err, res, body) ->
expect(res.statusCode).toBe(422)
done()
it 'can be read by an admin.', (done) ->
loginAdmin (joe) ->
loginAdmin ->
request.get {uri:url+'/'+components[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
@ -57,7 +57,7 @@ describe 'LevelComponent', ->
done()
it 'can be read by ordinary users.', (done) ->
loginJoe (joe) ->
loginJoe ->
request.get {uri:url+'/'+components[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
@ -81,7 +81,7 @@ describe 'LevelComponent', ->
done()
it 'is unofficial by default', (done) ->
loginJoe (joe) ->
loginJoe ->
request.get {uri:url+'/'+components[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
@ -90,7 +90,7 @@ describe 'LevelComponent', ->
done()
it 'has system ai by default', (done) ->
loginJoe (joe) ->
loginJoe ->
request.get {uri:url+'/'+components[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
@ -100,14 +100,14 @@ describe 'LevelComponent', ->
it 'official property isn\'t editable by an ordinary user.', (done) ->
components[0].official = true
loginJoe (joe) ->
loginJoe ->
request.post {uri:url, json:components[0]}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'official property is editable by an admin.', (done) ->
components[0].official = true
loginAdmin (joe) ->
loginAdmin ->
request.post {uri:url, json:components[0]}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.official).toBe(true)

View file

@ -2,7 +2,7 @@ require '../common'
describe 'LevelSystem', ->
raw =
system =
name:'Bashing'
description:'Performs Thang bashing updates for Bashes Thangs.'
code: """class Bashing extends System
@ -10,30 +10,121 @@ describe 'LevelSystem', ->
super world
"""
language: 'coffeescript'
official: true
permissions:simplePermissions
systems = {}
url = getURL('/db/level.system')
it 'clears things first', (done) ->
it 'preparing test : deletes all LevelSystem first', (done) ->
clearModels [Level, LevelSystem], (err) ->
expect(err).toBeNull()
done()
it 'can make a LevelSystem, without setting official.', (done) ->
loginJoe (joe) ->
request.post {uri:url, json:systems}, (err, res, body) ->
it 'can\'t be created by ordinary users.', (done) ->
loginJoe ->
request.post {uri:url, json:system}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'can be created by an admin.', (done) ->
loginAdmin ->
request.post {uri:url, json:system}, (err, res, body) ->
expect(res.statusCode).toBe(200)
expect(body.official).toBeUndefined()
expect(body._id).toBeDefined()
expect(body.name).toBe(system.name)
expect(body.description).toBe(system.description)
expect(body.code).toBe(system.code)
expect(body.language).toBe(system.language)
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()
systems[0] = body
done()
it 'can allows admins to edit the official property.', (done) ->
systems[0].official = true
loginAdmin (joe) ->
request.post {uri:url, json:systems[0]}, (err, res, body) ->
expect(body.official).toBe(true)
expect(res.statusCode).toBe(200)
it 'have a unique name.', (done) ->
loginAdmin ->
request.post {uri:url, json:system}, (err, res, body) ->
expect(res.statusCode).toBe(422)
done()
it 'can be read by an admin.', (done) ->
loginAdmin ->
request.get {uri:url+'/'+systems[0]._id}, (err, res, body) ->
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 ->
request.get {uri:url+'/'+systems[0]._id}, (err, res, body) ->
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.language).toBe(systems[0].language)
expect(body.__v).toBe(0)
expect(body.official).toBeDefined()
expect(body.creator).toBeDefined()
expect(body.original).toBeDefined()
expect(body.created).toBeDefined()
expect(body.configSchema).toBeDefined()
expect(body.dependencies).toBeDefined()
expect(body.propertyDocumentation).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 ->
request.get {uri:url+'/'+systems[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
expect(body.official).toBe(false)
done()
it 'official property isn\'t editable by an ordinary user.', (done) ->
systems[0].official = true
loginJoe ->
request.post {uri:url, json:systems[0]}, (err, res, body) ->
expect(res.statusCode).toBe(403)
done()
it 'official property is editable by an admin.', (done) ->
systems[0].official = true
loginAdmin ->
request.post {uri:url, json:systems[0]}, (err, res, body) ->
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
request.get {uri:url+'/'+systems[0]._id}, (err, res, body) ->
expect(res.statusCode).toBe(200)
body = JSON.parse(body)
expect(body._id).toBe(systems[0]._id)
expect(body.official).toBe(false)
expect(body.version.isLatestMinor).toBe(false)
expect(body.version.isLatestMajor).toBe(false)
done()
it ' can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri:url+'/'+systems[0]._id}, (err, res) ->
expect(res.statusCode).toBe(404)
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)
done()

View file

@ -1,8 +1,11 @@
require '../common'
request = require 'request'
urlUser = '/db/user'
describe 'POST /db/user', ->
request = require 'request'
it 'clears the db first', (done) ->
it 'preparing test : clears the db first', (done) ->
clearModels [User], (err) ->
throw err if err
done()
@ -13,13 +16,13 @@ describe 'POST /db/user', ->
expect(user.get('password')).toBeUndefined()
expect(user?.get('passwordHash')).not.toBeUndefined()
if user?.get('passwordHash')?
expect(user.get('passwordHash')[..5]).toBe('948c7e')
expect(user.get('passwordHash')[..5]).toBe('31dc3d')
expect(user.get('permissions').length).toBe(0)
done()
it 'serves the user through /db/user/id', (done) ->
unittest.getNormalJoe (user) ->
url = getURL('/db/user/'+user._id)
url = getURL(urlUser+'/'+user._id)
request.get url, (err, res, body) ->
expect(res.statusCode).toBe(200)
user = JSON.parse(body)
@ -40,7 +43,7 @@ describe 'POST /db/user', ->
loginJoe ->
unittest.getAdmin (user) ->
url = getURL('/db/user/'+user._id)
url = getURL(urlUser+'/'+user._id)
request.get url, (err, res, body) ->
expect(res.statusCode).toBe(200)
user = JSON.parse(body)
@ -55,7 +58,7 @@ describe 'PUT /db/user', ->
req = request.post getURL('/auth/logout'),
(err, res) ->
expect(res.statusCode).toBe(200)
req = request.put getURL('/db/user'),
req = request.put getURL(urlUser),
(err, res) ->
expect(res.statusCode).toBe(422)
expect(res.body).toBe('No input.')
@ -66,7 +69,7 @@ describe 'PUT /db/user', ->
it 'denies requests to edit someone who is not joe', (done) ->
unittest.getAdmin (admin) ->
req = request.put getURL('/db/user'),
req = request.put getURL(urlUser),
(err, res) ->
expect(res.statusCode).toBe(403)
done()
@ -74,7 +77,7 @@ describe 'PUT /db/user', ->
it 'denies invalid data', (done) ->
unittest.getNormalJoe (joe) ->
req = request.put getURL('/db/user'),
req = request.put getURL(urlUser),
(err, res) ->
expect(res.statusCode).toBe(422)
expect(res.body.indexOf('too long')).toBeGreaterThan(-1)
@ -87,13 +90,10 @@ ghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghl
it 'logs in as admin', (done) ->
loginAdmin -> done()
it 'denies non-existent ids', (done) ->
req = request.put getURL('/db/user'),
req = request.put getURL(urlUser),
(err, res) ->
expect(res.statusCode).toBe(404)
expect(res.body).toBe('Resource not found.')
done()
done()
form = req.form()
form.append('_id', '513108d4cb8b610000000004')
@ -102,7 +102,7 @@ ghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghl
it 'denies if the email being changed is already taken', (done) ->
unittest.getNormalJoe (joe) ->
unittest.getAdmin (admin) ->
req = request.put getURL('/db/user'), (err, res) ->
req = request.put getURL(urlUser), (err, res) ->
expect(res.statusCode).toBe(409)
expect(res.body.indexOf('already used')).toBeGreaterThan(-1)
done()
@ -112,7 +112,7 @@ ghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghl
it 'works', (done) ->
unittest.getNormalJoe (joe) ->
req = request.put getURL('/db/user'), (err, res) ->
req = request.put getURL(urlUser), (err, res) ->
expect(res.statusCode).toBe(200)
unittest.getUser('New@email.com', 'null', (joe) ->
expect(joe.get('name')).toBe('Wilhelm')
@ -124,8 +124,9 @@ ghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghlfarghlarghl
form.append('email', 'New@email.com')
form.append('name', 'Wilhelm')
describe 'GET /db/user', ->
request = require 'request'
it 'logs in as admin', (done) ->
req = request.post(getURL('/auth/login'), (error, response) ->
expect(response.statusCode).toBe(200)
@ -143,7 +144,7 @@ describe 'GET /db/user', ->
['sort', '-dateCreated']
]
options = {
url: getURL('/db/user')
url: getURL(urlUser)
qs: {
conditions: JSON.stringify(conditions)
}
@ -161,7 +162,7 @@ describe 'GET /db/user', ->
['lime', 20]
]
options = {
url: getURL('/db/user')
url: getURL(urlUser)
qs: {
conditions: JSON.stringify(conditions)
}

View file

@ -23,8 +23,8 @@ describe 'CampaignStatus', ->
.populate('campaign')
.exec (err, c) ->
expect(err).toBe(null)
expect(c.user.get('name')).not.toBeUndefined()
expect(c.campaign.get('name')).not.toBeUndefined()
expect(c.user.get('name')).toBeDefined()
expect(c.campaign.get('name')).toBeDefined()
done()
it 'rejects duplicates', (done) ->

View file

@ -27,5 +27,5 @@ describe 'LevelDraft', ->
throw err if err
LevelDraft.findOne {_id:draft._id}, (err, fetched) ->
expect(fetched.level.original).not.toBeUndefined()
expect(fetched.level.original).toBeDefined()
done()

View file

@ -259,7 +259,7 @@ describe 'VersionedPlugin', ->
expect(results.length).toBe(3)
expect(results[2].slug).toBeUndefined()
expect(results[1].slug).toBeUndefined()
expect(results[0].slug).not.toBeUndefined()
expect(results[0].slug).toBeDefined()
done()