From 23355a677126851a8c8e2923d27e27e57ea0c569 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Tue, 19 Dec 2017 06:33:55 +0000 Subject: [PATCH] #68 Added tests --- test.js | 302 -------------------------------------------------- test/auth.js | 180 ++++++++++++++++++++++++++++++ test/index.js | 51 +++++++++ test/map.js | 39 +++++++ 4 files changed, 270 insertions(+), 302 deletions(-) delete mode 100755 test.js create mode 100755 test/auth.js create mode 100644 test/index.js create mode 100644 test/map.js diff --git a/test.js b/test.js deleted file mode 100755 index f6c7e87..0000000 --- a/test.js +++ /dev/null @@ -1,302 +0,0 @@ -const chai = require('chai') -const chaiHttp = require('chai-http') -const request = require('supertest') -const server = require('./server') -<<<<<<< HEAD -chai.use(chaiHttp) - -describe('Public', function () { - it('Displays homepage', function (done) { - request(server).get('/') - .expect(200) - .end(function (err, res) { done() }) - }) - - it('Displays help page', function (done) { - request(server).get('/help') - .expect(200) - .end(function (err, res) { done() }) - }) - - it('Displays terms of service', function (done) { - request(server).get('/terms') - .expect(200) - .end(function (err, res) { done() }) - }) - - it('Displays privacy policy', function (done) { - request(server).get('/privacy') - .expect(200) - .end(function (err, res) { done() }) - }) - - it('Displays robots.txt', function (done) { - request(server).get('/robots.txt') - .expect(200) - .expect('Content-Type', /text/) - .end(function (err, res) { done() }) - }) - - it('Displays demo map', function (done) { - request(server).get('/map/keith') - .expect(200) - .end(function (err, res) { done() }) - }) -}) - -describe('User', function () { - it('Creates an account', function (done) { - request(server).post('/signup', {'email': 'test@tracman.org'}) - .expect(200) - .end(function (err, res) { done() }) - }) - - // TODO: it('Creates a password', function(done){ - - // }) - - // TODO: it('Logs in', function(done){ - - // }) - - // TODO: it('Logs out', function(done){ - - // }) - - // TODO: it('Forgets password', function(done){ - - // }) - - // TODO: it('Changes forgotten password', function(done){ - - // }) - - // TODO: it('Logs back in', function(done){ - - // }) - - // TODO: it('Changes email address', function(done){ - - // }) - - // TODO: it('Changes password', function(done){ - - // }) - - // TODO: it('Changes settings', function(done){ - - // }) - - // TODO: it('Connects a Google account', function(done){ - - // }) - - // TODO: it('Connects a Facebook account', function(done){ - - // }) - - // TODO: it('Connects a Twitter account', function(done){ - - // }) - - // TODO: it('Logs in with Google', function(done){ - - // }) - - // TODO: it('Logs in with Facebook', function(done){ - - // }) - - // TODO: it('Logs in with Twitter', function(done){ - - // }) - - // TODO: it('Disconnects a Google account', function(done){ - - // }) - - // TODO: it('Disconnects a Facebook account', function(done){ - - // }) - - // TODO: it('Disconnects a Twitter account', function(done){ - - // }) - - // TODO: it('Shows own map', function(done){ - // request(server).get('/map') - // .expect(200) - // .end(function(err,res){ done(); }) - // }) - - // TODO: it('Sets own location', function(done){ - - // }) - - // TODO: it('Tracks own location', function(done){ - - // }) - - // TODO: it('Clears own location', function(done){ - - // }) - - // TODO: it('Deletes account', function(done){ - - // }) -}) -======= -const chai.use(chaiHttp); - - -describe('Public Pages', () => { - - function getPublicPage(url,done){ - request(server.get(url) - .expect(200) - .end( (err,res)=>{ done(); } ) - } - - it('Displays homepage', (done) => { - getPublicPage ('/', done) - }) - - it('Displays help page', (done) => { - getPublicPage('/help',done) - }) - - it('Displays terms of service', (done) => { - getPublicPage ('/terms',done) - }) - - it('Displays privacy policy', (done) => { - getPublicPage ('/privacy', done) - }) - - it('Displays robots.txt', done) => { - request(server).get('/robots.txt') - .expect(200) - .expect('Content-Type', /text/) - .end( (err,res) => { done(); } ) - }) - - it('Displays demo map', (done) => { - getPublicPage ('/map/demo', done) - }) - - it('Displays help page', (done) => { - getPublicPage ('/help', done) - }) - - it('Displays contact page', (done) => { - getPublicPage('/contact', done) - }) - - - -}) - -describe('Accounts', () => { - - it('Creates an account', (done) => { - request(server).post('/signup',{"email":"test@tracman.org"}) - .expect(200) - .end( (err,res) => { done(); } ); - }); - - //TODO: it('Creates a password', (done) => { - - // }); - - //TODO: it('Logs in', (done) => { - - // }); - - //TODO: it('Logs out', (done) => { - - // }); - - //TODO: it('Forgets password', (done) => { - - // }); - - //TODO: it('Changes forgotten password', (done) => { - - // }); - - //TODO: it('Logs back in', (done) => { - - // }); - - //TODO: it('Changes email address', (done) => { - - // }); - - //TODO: it('Changes password', (done) => { - - // }); - - //TODO: it('Changes settings', (done) => { - - // }); - - //TODO: it('Connects a Google account', (done) => { - - // }); - - //TODO: it('Connects a Facebook account', (done) => { - - // }); - - //TODO: it('Connects a Twitter account', (done) => { - - // }); - - //TODO: it('Logs in with Google', (done) => { - - // }); - - //TODO: it('Logs in with Facebook', (done) => { - - // }); - - //TODO: it('Logs in with Twitter', (done) => { - - // }); - - //TODO: it('Disconnects a Google account', (done) => { - - // }); - - //TODO: it('Disconnects a Facebook account', (done) => { - - // }); - - //TODO: it('Disconnects a Twitter account', (done) => { - - // }); - - //TODO: it('Shows own map', (done) => { - // request(server).get('/map') - // .expect(200) - // .end(function(err,res){ done(); }); - // }); - - //TODO: it('Sets own location', (done) => { - - // }); - - //TODO: it('Tracks own location', (done) => { - - // }); - - //TODO: it('Clears own location', (done) => { - - // }); - - //TODO: it('Deletes account', (done) => { - - // }); - -}); ->>>>>>> 72141a31873b674ed23daf262ce14be38dda50ff diff --git a/test/auth.js b/test/auth.js new file mode 100755 index 0000000..bc1f333 --- /dev/null +++ b/test/auth.js @@ -0,0 +1,180 @@ +'use strict' + +const request = require('supertest') +const server = require('../server') +const User = require('../config/models').user +const chai = require('chai') +chai.use( + require('chai-http') +) + +// Constants for dummy accounts +const FAKE_EMAIL = 'nobody@tracman.org' +const TEST_EMAIL = 'test@tracman.org' +const TEST_PASSWORD = 'mDAQYe2VYE' +const BAD_PASSWORD = 'password123' + +// Ensure server is ready +before((done) => { server.on('ready', done) }) + +describe('Authentication', () => { + + describe('Account creation', () => { + let passwordless_user + + // Make sure test user doesn't exist + before( async () => { + let existing_test_user = await User.findOne({'email':TEST_EMAIL}) + if (existing_test_user) existing_test_user.remove() + }) + + it('Fails to create an account with a fake email', async () => { + + // Confirm redirect + chai.expect( await request(server).post('/signup') + .type('form').send({ 'email':FAKE_EMAIL }) + ).to.redirectTo('/login#signup') + + // Ensure user was deleted after email failed to send + // 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 + setTimeout( async () => { + chai.assert.isNull( await User.findOne({ + 'email': FAKE_EMAIL + }), 'Account with fake email was created') + }, 2000) + + }) + + it('Creates an account with a valid email', async () => { + + // Set email address + chai.expect( await request(server).post('/signup') + .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 + chai.expect(await request(server) + .get(`/settings/password/${passwordless_user.auth.passToken}`) + ).html.to.have.status(200) + }) + + it('Fails to set a weak password', async () => { + chai.expect( await request(server) + .post(`/settings/password/${passwordless_user.auth.passToken}`) + .type('form').send({ 'password':BAD_PASSWORD }) + ).to.redirectTo(`/settings/password/${passwordless_user.auth.passToken}`) + }) + + it('Sets a strong password', () => { + + // Set password + return request(server) + .post(`/settings/password/${passwordless_user.auth.passToken}`) + .type('form').send({ 'password':TEST_PASSWORD }) + .then( async (res) => { + + // Expect redirect + chai.expect(res).to.redirectTo('/login?next=/map?new=1') + + // Retrieve user with password saved + let passworded_user = await User.findOne({'email':TEST_EMAIL} ) + + // Assert password was set + chai.assert.isString( + passworded_user.auth.password, 'Failed to correctly save password' + ) + + }) + + }) + + }) + + describe('Account usage', () => { + + // Create account to play with + before( () => { + // Create user + // Set password + }) + + // it('Logs in', async () => { + + // }) + + // it('Logs out', async () => { + + // }) + + // it('Forgets password', async () => { + + // }) + + // it('Changes forgotten password', async () => { + + // }) + + // it('Logs back in', async () => { + + // }) + + // 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 () => { + + // }) + + }) + +}) diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..0bdbf72 --- /dev/null +++ b/test/index.js @@ -0,0 +1,51 @@ +const chai = require('chai') +const chaiHttp = require('chai-http') +const request = require('supertest') +const server = require('../server') +chai.use(chaiHttp) + +// Ensure server is ready +before((done) => { server.on('ready', done) }) + +describe('Index routes', () => { + + // Ensure server is ready + before((done) => { server.on('ready', done) }) + + it( 'Displays homepage', async () => { + let res = await request(server).get('/') + chai.expect(res).html.to.have.status(200) + }) + + it('Displays help page', async () => { + let res = await request(server).get('/help') + chai.expect(res).html.to.have.status(200) + }) + + it('Displays terms of service', async () => { + let res = await request(server).get('/terms') + chai.expect(res).html.to.have.status(200) + }) + + it('Displays privacy policy', async () => { + let res = await request(server).get('/privacy') + chai.expect(res).html.to.have.status(200) + }) + + it('Displays robots.txt', async () => { + let res = await request(server).get('/') + chai.expect(res).to.have.status(200)//.to.be.text Not sure why mocha's + // getting text/html in mocha; it's showing text/plain in chrome + }) + + it('Redirects /signup to /logn#signup', async () => { + let res = await request(server).get('/signup') + chai.expect(res).to.redirectTo('/login#signup') + }) + + it('Redirects /demo to /map/demo', async () => { + let res = await request(server).get('/demo') + chai.expect(res).to.redirectTo('/map/demo') + }) + +}) diff --git a/test/map.js b/test/map.js new file mode 100644 index 0000000..2c33c07 --- /dev/null +++ b/test/map.js @@ -0,0 +1,39 @@ +const chai = require('chai') +const chaiHttp = require('chai-http') +const request = require('supertest') +const server = require('../server') +chai.use(chaiHttp) + +// Ensure server is ready +before((done) => { server.on('ready', done) }) + +describe('Mapping', () => { + + it('Displays demo map', async () => { + let res = await request(server).get('/map/demo') + chai.expect(res).to.have.status(200).to.be.html + }) + + // TODO: it('Shows own map', async () => { + // request(server).get('/map') + // .expect(200) + // .end(function(err,res){ done(); }) + // }) + + // TODO: it('Sets own location', async () => { + + // }) + + // TODO: it('Tracks own location', async () => { + + // }) + + // TODO: it('Clears own location', async () => { + + // }) + + // TODO: it('Deletes account', async () => { + + // }) + +}) \ No newline at end of file