From 5ad2e246e56a36299e62a56afe488ddddcc423a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Mon, 25 Jun 2018 18:34:23 +0200 Subject: [PATCH] Include user information on revive Closes #327 --- lib/routes/admin-api/archive.js | 5 ++++- lib/routes/admin-api/archive.test.js | 19 +++++++++++++++++++ test/fixtures/fake-event-store.js | 15 +++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/routes/admin-api/archive.js b/lib/routes/admin-api/archive.js index 7f07ebc6d0..ef2675374b 100644 --- a/lib/routes/admin-api/archive.js +++ b/lib/routes/admin-api/archive.js @@ -6,6 +6,7 @@ const logger = require('../../logger')('/admin-api/archive.js'); const { FEATURE_REVIVED } = require('../../event-type'); const ValidationError = require('../../error/validation-error'); const validateRequest = require('../../error/validate-request'); +const extractUser = require('../../extract-user'); const handleErrors = (req, res, error) => { switch (error.constructor) { @@ -33,11 +34,13 @@ module.exports.router = function(config) { router.post('/revive/:name', (req, res) => { req.checkParams('name', 'Name is required').notEmpty(); + const userName = extractUser(req); + validateRequest(req) .then(() => eventStore.store({ type: FEATURE_REVIVED, - createdBy: req.connection.remoteAddress, + createdBy: userName, data: { name: req.params.name }, }) ) diff --git a/lib/routes/admin-api/archive.test.js b/lib/routes/admin-api/archive.test.js index 89b5102596..366371e5e7 100644 --- a/lib/routes/admin-api/archive.test.js +++ b/lib/routes/admin-api/archive.test.js @@ -20,6 +20,7 @@ function getSetup() { return { base, archiveStore: stores.featureToggleStore, + eventStore: stores.eventStore, request: supertest(app), }; } @@ -67,3 +68,21 @@ test('should revive toggle', t => { return request.post(`${base}/api/admin/archive/revive/${name}`).expect(200); }); + +test('should create event when reviving toggle', async t => { + t.plan(4); + const name = 'name1'; + const { request, base, archiveStore, eventStore } = getSetup(); + archiveStore.addArchivedFeature({ + name, + strategies: [{ name: 'default' }], + }); + + await request.post(`${base}/api/admin/archive/revive/${name}`); + + const events = await eventStore.getEvents(); + t.is(events.length, 1); + t.is(events[0].type, 'feature-revived'); + t.is(events[0].data.name, name); + t.is(events[0].createdBy, 'unknown'); +}); diff --git a/test/fixtures/fake-event-store.js b/test/fixtures/fake-event-store.js index 8c5618634e..ee1fb29d88 100644 --- a/test/fixtures/fake-event-store.js +++ b/test/fixtures/fake-event-store.js @@ -1,6 +1,13 @@ 'use strict'; -module.exports = () => ({ - store: () => Promise.resolve(), - getEvents: () => Promise.resolve([]), -}); +module.exports = () => { + const events = []; + + return { + store: event => { + events.push(event); + Promise.resolve(); + }, + getEvents: () => Promise.resolve(events), + }; +};