Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
5001CEM/unitTests/test-accounts.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
210 lines (194 sloc)
5.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import test from 'ava' | |
import Accounts from '../modules/accounts.js' | |
test('REGISTER : register and log in with a valid account', async test => { | |
test.plan(1) | |
const account = await new Accounts() // no database specified so runs in-memory | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
const login = await account.login('doej', 'password') | |
test.is(login, true, 'unable to log in') | |
} catch(err) { | |
test.fail('error thrown') | |
} finally { | |
account.close() | |
} | |
}) | |
test('REGISTER : register a duplicate username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.register('doej', 'password', 'doej@gmail.com') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "doej" already in use', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('REGISTER : error if blank username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('', 'password', 'doej@gmail.com') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'missing field', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('REGISTER : error if blank password', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', '', 'doej@gmail.com') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'missing field', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('REGISTER : error if blank email', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', '') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'missing field', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('REGISTER : error if duplicate email', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.register('bloggsj', 'newpassword', 'doej@gmail.com') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'email address "doej@gmail.com" is already in use', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('LOGIN : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.login('roej', 'password') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('LOGIN : invalid password', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.login('doej', 'bad') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'invalid password for account "doej"', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('GET SURVEYS DONE : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.getSurveysDone('roej') | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('GET SURVEYS DONE : valid results', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
const value = await account.getSurveysDone('doej') | |
test.deepEqual(value, [-1]) | |
} catch(err) { | |
test.fail('error thrown') | |
} finally { | |
account.close() | |
} | |
}) | |
test('GET SURVEY SCORE : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.getSurveyScore('roej', -1) | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('GET SURVEY SCORE : survey not completed', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.getSurveyScore('doej', 2) | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'survey "2" has not been completed', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('GET UNANSWERED POSITION : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.updateScore('roej', 1) | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('UPDATE SCORE : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.updateScore('roej', 1, 2) | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) | |
test('UPDATE SURVEYS DONE : invalid username', async test => { | |
test.plan(1) | |
const account = await new Accounts() | |
try { | |
await account.register('doej', 'password', 'doej@gmail.com') | |
await account.updateSurveysDone('roej', 1) | |
test.fail('error not thrown') | |
} catch(err) { | |
test.is(err.message, 'username "roej" not found', 'incorrect error message') | |
} finally { | |
account.close() | |
} | |
}) |