tracman-server/test/auth.js

263 lines
7.2 KiB
JavaScript
Raw Permalink Normal View History

2017-12-18 23:33:55 -07:00
'use strict'
const chai = require('chai')
2018-01-19 14:23:43 -07:00
const app = require('../server')
2018-02-24 14:41:35 -07:00
const froth = require('mocha-froth')
2017-12-21 14:49:07 -07:00
const User = require('../config/models').user
2018-01-19 14:23:43 -07:00
// const superagent = require('superagent').agent()
const request = require('supertest').agent(app)
2017-12-18 23:33:55 -07:00
chai.use(
require('chai-http')
)
2018-01-19 14:23:43 -07:00
// Import test config by object destructuring
const { FAKE_EMAIL, TEST_EMAIL,
TEST_PASSWORD, BAD_PASSWORD,
FUZZED_EMAIL_TRIES, FUZZED_PASSWORD_TRIES,
} = require('../config/test.js')
2017-12-18 23:33:55 -07:00
describe('Authentication', () => {
describe('Account creation', () => {
let passwordless_user
// Make sure test user doesn't exist
before( async () => {
try {
let user = await User.findOne({'email':TEST_EMAIL})
if (!user) return
else user.remove()
} catch (err) { console.error(err) }
2018-01-19 14:23:43 -07:00
})
2017-12-18 23:33:55 -07:00
it('Fails to create an account with a fake email', async () => {
// Confirm redirect
2017-12-21 14:49:07 -07:00
chai.expect( await request.post('/signup')
2017-12-18 23:33:55 -07:00
.type('form').send({ 'email':FAKE_EMAIL })
).to.redirectTo('/login#signup')
2018-01-19 14:23:43 -07:00
/* Ensure user was deleted after email failed to send
2018-02-24 21:59:55 -07:00
* Users with bad emails are removed asynchronously and may happen after
* the response was recieved. Ensure it's happened in a kludgy way by
* waiting 2 seconds before asserting that the user doesn't exist
*/
2017-12-18 23:33:55 -07:00
setTimeout( async () => {
chai.assert.isNull( await User.findOne({
'email': FAKE_EMAIL
}), 'Account with fake email was created')
}, 2000)
})
2018-03-08 12:56:34 -07:00
it.skip(`Fails to create accounts with ${FUZZED_EMAIL_TRIES} fuzzed emails`, () => {
2018-01-19 14:23:43 -07:00
// Fuzz emails
2018-02-24 14:41:35 -07:00
froth(FUZZED_EMAIL_TRIES).forEach( async (fuzzed_email) => {
2018-01-19 14:23:43 -07:00
// Confirm redirect
2018-02-24 14:41:35 -07:00
chai.expect( await request.post('/signup')
.type('form').send({ 'email':fuzzed_email })
).to.redirectTo('/login#signup')
/* Ensure user was deleted after email failed to send
2018-02-24 21:59:55 -07:00
* Users with bad emails are removed asynchronously and may happen after
* the response was recieved. Ensure it's happened in a kludgy way by
* waiting 2 seconds before asserting that the user doesn't exist
*/
2018-03-08 12:56:34 -07:00
//TODO: This just isn't working... phony emails are showing up in the db
2018-02-24 14:41:35 -07:00
setTimeout( async () => {
chai.assert.isNull( await User.findOne({
'email': fuzzed_email
}), 'Account with fake email was created')
}, 2000)
2018-01-19 14:23:43 -07:00
2018-02-24 14:41:35 -07:00
})
2018-01-19 14:23:43 -07:00
})
2017-12-18 23:33:55 -07:00
it('Creates an account with a valid email', async () => {
// Set email address
2017-12-21 14:49:07 -07:00
chai.expect( await request.post('/signup')
2017-12-18 23:33:55 -07:00
.type('form').send({ 'email':TEST_EMAIL })
).to.redirectTo('/login')
// Assert that user was created
passwordless_user = await User.findOne({'email':TEST_EMAIL})
chai.assert.isDefined(passwordless_user, 'Failed to create account')
})
it('Loads password page', async () => {
// Load password page
2017-12-21 14:49:07 -07:00
chai.expect(await request
.get(`/account/password/${passwordless_user.auth.passToken}`)
2018-02-25 10:04:35 -07:00
).to.be.html.and.have.status(200)
2017-12-18 23:33:55 -07:00
})
it('Fails to set a weak password', async () => {
2017-12-21 14:49:07 -07:00
chai.expect( await request
.post(`/account/password/${passwordless_user.auth.passToken}`)
2017-12-18 23:33:55 -07:00
.type('form').send({ 'password':BAD_PASSWORD })
).to.redirectTo(`/account/password/${passwordless_user.auth.passToken}`)
2017-12-18 23:33:55 -07:00
})
it('Sets a strong password', async () => {
2017-12-18 23:33:55 -07:00
2018-02-25 10:04:35 -07:00
// Perform request
let res = await request
.post(`/account/password/${passwordless_user.auth.passToken}`)
.type('form').send({ 'password':TEST_PASSWORD })
// Expect redirect
chai.expect(res).to.redirectTo('/login')
2017-12-18 23:33:55 -07:00
2018-02-25 10:04:35 -07:00
// Retrieve user with password saved
let passworded_user = await User.findOne({'email':TEST_EMAIL} )
2017-12-18 23:33:55 -07:00
2018-02-25 10:04:35 -07:00
// Assert password was set
chai.assert.isString(
passworded_user.auth.password, 'Failed to correctly save password'
)
2017-12-18 23:33:55 -07:00
2018-02-25 10:04:35 -07:00
return res
2017-12-18 23:33:55 -07:00
})
2018-01-19 14:33:54 -07:00
// These tests require the test user to have been created
after( () => {
2018-02-25 11:42:37 -07:00
describe('Logged out', function() {
2018-01-19 14:33:54 -07:00
2018-02-25 11:42:37 -07:00
// Password fuzzing could take a while... give it five seconds
this.timeout(5000)
2018-01-19 14:33:54 -07:00
2018-02-24 14:41:35 -07:00
it(`Fails to log in with ${FUZZED_PASSWORD_TRIES} fuzzed passwords`, () => {
2018-01-19 14:33:54 -07:00
// Fuzz passwords
2018-02-24 14:41:35 -07:00
froth(FUZZED_PASSWORD_TRIES).forEach( async (fuzzed_password) => {
2018-01-19 14:33:54 -07:00
2018-02-24 14:41:35 -07:00
// Confirm redirect
chai.expect( await request.post('/login')
.type('form').send({
'email': TEST_EMAIL,
'password': fuzzed_password
})
).to.redirectTo('/login') // Hey! Incorrect email or password.
})
2018-01-19 14:33:54 -07:00
})
2018-02-24 21:59:55 -07:00
it('Loads forgot password page', async () => {
2018-01-20 18:41:59 -07:00
let res = await request.get('/login/forgot')
2018-02-25 10:04:35 -07:00
chai.expect(res).to.be.html.and.have.status(200)
2018-01-20 18:41:59 -07:00
})
// TODO: Test already-logged-in forgot password requests
// TODO: Test invalid and fuzzed forgot password requests
2018-01-19 14:33:54 -07:00
2018-02-25 11:42:37 -07:00
it('Sends valid forgot password request', async () => {
2018-01-19 14:33:54 -07:00
2018-01-20 18:41:59 -07:00
// Responds with 200
2018-02-25 10:04:35 -07:00
chai.expect( await request.post('/login/forgot')
2018-01-20 18:41:59 -07:00
.type('form').send({
2018-02-25 10:04:35 -07:00
'email': TEST_EMAIL,
2018-01-20 18:41:59 -07:00
})
2018-02-25 11:42:37 -07:00
).to.redirectTo('/login')
2018-01-20 18:41:59 -07:00
2018-02-25 11:42:37 -07:00
// Assert password token was set
let requesting_user = await User.findOne({'email':TEST_EMAIL} )
2018-02-25 11:42:37 -07:00
chai.expect(requesting_user.auth.passToken)
.to.be.a('string').and.to.have.lengthOf(32)
2018-01-20 18:41:59 -07:00
})
2018-01-19 14:33:54 -07:00
// TODO: Create test for changing forgetten password
2018-01-19 14:33:54 -07:00
// Finally log in successfully
after( () => {
it('Logs in with password', async () => {
let res = await request.post('/login')
.type('form').send({
email: TEST_EMAIL,
password: TEST_PASSWORD
})
chai.expect(res).to.redirectTo('/map')
// Then do tests requiring login
after( () => {
describe('Logged in', () => {
it('Logs out', async () => {
let res = request.get('/logout')
chai.expect(res).to.redirectTo('/')
})
// it('Changes email address', async () => {
// })
// it('Changes password', async () => {
// })
// it('Changes settings', async () => {
// })
// it('Connects a Google account', async () => {
// })
// it('Connects a Facebook account', async () => {
// })
// it('Connects a Twitter account', async () => {
// })
// it('Logs in with Google', async () => {
// })
// it('Logs in with Facebook', async () => {
// })
// it('Logs in with Twitter', async () => {
// })
// it('Disconnects a Google account', async () => {
// })
// it('Disconnects a Facebook account', async () => {
// })
// it('Disconnects a Twitter account', async () => {
// })
})
})
})
2018-02-24 14:41:35 -07:00
2018-01-19 14:33:54 -07:00
})
})
})
2017-12-18 23:33:55 -07:00
})
})