Skip to content

Login page acceptance tests #7

Merged
merged 5 commits into from Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .githooks/pre-commit
@@ -1,4 +1,9 @@
#!/bin/sh

echo "running the 'pre-commit' script"

echo "running eslint..."
./node_modules/.bin/eslint .

echo "running tests..."
npm test
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -10,7 +10,7 @@ Run `npm run build-db` to create the database structure.

To run the server, run `npm start`.

To live-reloading, run `npm run watch`.
To start live-reloading, run `npm run watch`.

## Debugging

Expand All @@ -28,4 +28,5 @@ This project is split into a few folders:
- `public/`: contains the site's front-end static pages and JavaScript.
- `utils/`: contains shared utility code.
- `views/`: contains dynamic HTML templates.
- `./`: contains code that is used directly by the Koa app.
- `tests/`: contains all tests for the project.
- `./`: contains code that is used directly by the Koa app.
14 changes: 4 additions & 10 deletions build/build_db.js
Expand Up @@ -2,19 +2,13 @@

'use strict'

const fs = require('fs')
const path = require('path')

const sqlite = require('sqlite')

sqlite.open('app.db').then(db => {
const sqlPath = path.join(__dirname, 'build_db.sql')
fs.readFile(sqlPath, 'utf8', async(err, sql) => {
if (err) {
console.error(err)
process.exit(1)
}
const { runSQLScript } = require('./utils')

await db.exec(sql)
})
sqlite.open('app.db').then(async db => {
const sqlPath = path.join(__dirname, 'build_db.sql')
await runSQLScript(db, sqlPath)
})
18 changes: 18 additions & 0 deletions build/util.js
@@ -0,0 +1,18 @@
'use strict'

const fs = require('fs')

async function runSQLScript(db, filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', async(err, sql) => {
if (err) {
return reject(err)
}
resolve(db.exec(sql))
})
})
}

module.exports = {
runSQLScript
}
27 changes: 23 additions & 4 deletions controllers/login.js
@@ -1,30 +1,49 @@
'use strict'

const Router = require('koa-router')
const koaBody = require('koa-body')
const koaBodyParser = require('koa-bodyparser')
const bcrypt = require('bcrypt')

const login = new Router({ prefix: '/login' })

login.use(koaBody())
const { EntityNotFound } = require('../utils/errors')

login.use(koaBodyParser())

login.get('/', async ctx => ctx.render('login.hbs'))

login.post('/', async ctx => {
const { username, password } = ctx.request.body
const redirect = ctx.query.refer || '/'

const user = await ctx.db.getUser(username)
const user = await getUser(ctx.db, username)
if (!user) {
return ctx.render('login.hbs', { errorMsg: 'User does not exist' })
}

if (await bcrypt.compare(password, user.hash)) {
ctx.session.authorised = true
ctx.session.userID = user.id
return ctx.redirect('back')
return ctx.redirect(redirect)
} else {
return ctx.render('login.hbs', { errorMsg: 'Password incorrect' })
}
})

async function getUser(db, username) {
try {
// try to get the user by username
const user = await db.getUser(username)
return user
} catch (error) {
// if an EntityNotFound error is thrown, return null
if (error instanceof EntityNotFound) {
return null
}

// else re-throw the error
throw error
}
}

module.exports = login
12 changes: 10 additions & 2 deletions db.js
Expand Up @@ -6,7 +6,11 @@

const sqlite = require('sqlite')

const { NotImplemented } = require('./utils/errors')
const {
NotImplemented,
EntityNotFound
} = require('./utils/errors')

const User = require('./models/user')

class DbContext {
Expand Down Expand Up @@ -61,7 +65,11 @@ class SqliteDbContext extends DbContext {
}

const user = await sqlite.get(query, id)
return user ? Object.assign(new User(), user) : null
if (!user) {
throw new EntityNotFound(`user with id ${id} not found`)
}

return Object.assign(new User(), user)
}

async getUsers() {
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -15,5 +15,6 @@ module.exports = {
testPathIgnorePatterns: [
'/node_modules/',
'/__tests__/fixtures/',
'/tests/test.js'
]
}