From 0321ed29925cc0c334eeb3ded19f3343addff56e Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Thu, 24 Jan 2019 18:55:12 +0000 Subject: [PATCH] updated lab sheet --- 01 Setup.md | 4 ++-- exercises/10_auth/simple/index.js | 36 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 exercises/10_auth/simple/index.js diff --git a/01 Setup.md b/01 Setup.md index 7986b5c..34ee822 100644 --- a/01 Setup.md +++ b/01 Setup.md @@ -139,12 +139,12 @@ Once the web server is up and running you access the page by pointing the browse As we develop more complex web apps it becomes more and more difficult to fully test our app every time we change the code. By not testing everything at regular intervals there is an increasing chance that our new code could break the existing code in our system. Since regular testing is a chore, programmers have developed ways to automate this process. -In this section you will be given a sneak preview of how an automated test suite works. We will be using a testing framework developed by Facebook, called [Jest](https://jestjs.io) and will be using a second tool called [Supertest](https://github.com/visionmedia/supertest#readme) which allows us to interact with our code using http. +In this section you will be given a sneak preview of how an automated test suite works. We will be using a testing framework developed by Facebook, called [Jest](https://jestjs.io) and will be using a second tool called [Supertest](https://github.com/visionmedia/supertest#readme) which allows us to interact with our code using http. Rather than using the status code numbers in our tests we will use a module called [http-status-codes](https://www.npmjs.com/package/http-status-codes) to display these as human-readable strings. The process requires you to install both these packages and then run the `jest` command: ```shell -$ npm install jest supertest +$ npm install jest supertest http-status-codes $ ./node_modules/.bin/jest PASS .test/index.test.js GET / diff --git a/exercises/10_auth/simple/index.js b/exercises/10_auth/simple/index.js new file mode 100644 index 0000000..47b3381 --- /dev/null +++ b/exercises/10_auth/simple/index.js @@ -0,0 +1,36 @@ + +'use strict' + +const Router = require('koa-router') +const auth = require('koa-simple-auth') +const koaBody = require('koa-body')() +const router = module.exports = new Router() + +const catch_api_error = async ctx => { + try{ + yield next + } catch(err){ + this.body = JSON.stringify({ 'error': err.message }) + } +} + +router.post('/login', + catch_api_error, + koaBody, + auth.login, + function *() { + this.body = JSON.stringify({ authenticated: true }) + } +) + +router.post('/register', catch_api_error, koaBody, auth.register, function *() { + this.body = JSON.stringify({ authenticated: true }) +}) + +router.get('/unregister', catch_api_error, koaBody, auth.unregister, function *() { + this.body = JSON.stringify({ authenticated: false }) +}) + +router.get('/logout', auth.logout, function *() { + this.body = JSON.stringify({ authenticated: false }) +})