From b3704666bc2d63d28ae8683b0ea220a76629c523 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Wed, 17 Jan 2018 09:46:16 +0100 Subject: [PATCH] Add sign-out route #288 --- lib/routes/admin-api/user.js | 7 ++++ lib/routes/admin-api/user.test.js | 57 +++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 65 insertions(+) create mode 100644 lib/routes/admin-api/user.test.js diff --git a/lib/routes/admin-api/user.js b/lib/routes/admin-api/user.js index a958795d18a1..ec32f57e6eb1 100644 --- a/lib/routes/admin-api/user.js +++ b/lib/routes/admin-api/user.js @@ -16,5 +16,12 @@ exports.router = function() { } }); + router.get('/logout', (req, res) => { + if (req.session) { + req.session = null; + } + res.redirect('/'); + }); + return router; }; diff --git a/lib/routes/admin-api/user.test.js b/lib/routes/admin-api/user.test.js new file mode 100644 index 000000000000..a436ec549580 --- /dev/null +++ b/lib/routes/admin-api/user.test.js @@ -0,0 +1,57 @@ +'use strict'; + +const { test } = require('ava'); +const store = require('./../../../test/fixtures/store'); +const supertest = require('supertest'); +const getApp = require('../../app'); +const User = require('../../user'); + +const { EventEmitter } = require('events'); +const eventBus = new EventEmitter(); + +const currentUser = new User({ email: 'test@mail.com' }); + +function getSetup() { + const base = `/random${Math.round(Math.random() * 1000)}`; + const stores = store.createStores(); + const app = getApp({ + baseUriPath: base, + stores, + eventBus, + preHook: a => { + a.use((req, res, next) => { + req.user = currentUser; + next(); + }); + }, + }); + + return { + base, + strategyStore: stores.strategyStore, + request: supertest(app), + }; +} + +test('should return current user', t => { + t.plan(1); + const { request, base } = getSetup(); + + return request + .get(`${base}/api/admin/user`) + .expect(200) + .expect('Content-Type', /json/) + .expect(res => { + t.true(res.body.email === currentUser.email); + }); +}); + +test('should logout and redirect', t => { + t.plan(0); + const { request, base } = getSetup(); + + return request + .get(`${base}/api/admin/user/logout`) + .expect(302) + .expect('Location', '/'); +}); diff --git a/package.json b/package.json index 5e725bc6d6f4..25952e34b175 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "scripts": { "start": "node server.js", + "start:google": "node examples/google-auth-unleash.js", "start:dev": "NODE_ENV=development supervisor --ignore ./node_modules/ server.js", "start:dev:pg": "pg_virtualenv npm run start:dev:pg-chain", "start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev",