add server auth test (reset password)

This commit is contained in:
Sébastien Moratinos 2014-02-03 00:02:47 +01:00
parent 8895f12aff
commit f27db34976
2 changed files with 74 additions and 8 deletions

View file

@ -74,7 +74,7 @@ module.exports.setupRoutes = (app) ->
else else
return res.end() return res.end()
else else
console.log 'new password is', user.get('passwordReset') res.send user.get('passwordReset')
return res.end() return res.end()
) )
) )

View file

@ -1,4 +1,8 @@
require './common' require './common'
request = require 'request'
urlLogin = getURL('/auth/login')
urlReset = getURL('/auth/reset')
describe '/auth/whoami', -> describe '/auth/whoami', ->
http = require 'http' http = require 'http'
@ -10,8 +14,6 @@ describe '/auth/whoami', ->
) )
describe '/auth/login', -> describe '/auth/login', ->
url = getURL('/auth/login')
request = require 'request'
it 'clears Users first', (done) -> it 'clears Users first', (done) ->
User.remove {}, (err) -> User.remove {}, (err) ->
@ -19,7 +21,7 @@ describe '/auth/login', ->
done() done()
it 'finds no user', (done) -> it 'finds no user', (done) ->
req = request.post(url, (error, response) -> req = request.post(urlLogin, (error, response) ->
expect(response).toBeDefined() expect(response).toBeDefined()
expect(response.statusCode).toBe(401) expect(response.statusCode).toBe(401)
done() done()
@ -40,7 +42,7 @@ describe '/auth/login', ->
form.append('password', 'nada') form.append('password', 'nada')
it 'finds that created user', (done) -> it 'finds that created user', (done) ->
req = request.post(url, (error, response) -> req = request.post(urlLogin, (error, response) ->
expect(response).toBeDefined() expect(response).toBeDefined()
expect(response.statusCode).toBe(200) expect(response.statusCode).toBe(200)
done() done()
@ -50,7 +52,7 @@ describe '/auth/login', ->
form.append('password', 'nada') form.append('password', 'nada')
it 'rejects wrong passwords', (done) -> it 'rejects wrong passwords', (done) ->
req = request.post(url, (error, response) -> req = request.post(urlLogin, (error, response) ->
expect(response.statusCode).toBe(401) expect(response.statusCode).toBe(401)
expect(response.body.indexOf("wrong, wrong")).toBeGreaterThan(-1) expect(response.body.indexOf("wrong, wrong")).toBeGreaterThan(-1)
done() done()
@ -60,10 +62,74 @@ describe '/auth/login', ->
form.append('password', 'blahblah') form.append('password', 'blahblah')
it 'is completely case insensitive', (done) -> it 'is completely case insensitive', (done) ->
req = request.post(url, (error, response) -> req = request.post(urlLogin, (error, response) ->
expect(response.statusCode).toBe(200) expect(response.statusCode).toBe(200)
done() done()
) )
form = req.form() form = req.form()
form.append('username', 'scoTT@gmaIL.com') form.append('username', 'scoTT@gmaIL.com')
form.append('password', 'NaDa') form.append('password', 'NaDa')
describe '/auth/reset', ->
passwordReset = ''
it 'emails require', (done) ->
req = request.post(urlReset, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(422)
done()
)
form = req.form()
form.append('username', 'scott@gmail.com')
it 'can\'t reset an unknow user', (done) ->
req = request.post(urlReset, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(404)
done()
)
form = req.form()
form.append('email', 'unknow')
it 'reset user password', (done) ->
req = request.post(urlReset, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(200)
expect(response.body).toBeDefined()
passwordReset = response.body
done()
)
form = req.form()
form.append('email', 'scott@gmail.com')
it 'can login after resetting', (done) ->
req = request.post(urlLogin, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(200)
done()
)
form = req.form()
form.append('username', 'scott@gmail.com')
form.append('password', passwordReset)
it 'resetting password is not permanent', (done) ->
req = request.post(urlLogin, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(401)
done()
)
form = req.form()
form.append('username', 'scott@gmail.com')
form.append('password', passwordReset)
it 'can still login with old password', (done) ->
req = request.post(urlLogin, (error, response) ->
expect(response).toBeDefined()
expect(response.statusCode).toBe(200)
done()
)
form = req.form()
form.append('username', 'scott@gmail.com')
form.append('password', 'nada')