Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / api / private / fixtures / index.js
blob2440a0ee6df6d7cee093ebf5864ada554b9bacb8
1 'use strict';
3 const env = require('gitter-web-env');
4 const logger = env.logger;
5 const express = require('express');
6 const asyncHandler = require('express-async-handler');
7 const identifyRoute = require('gitter-web-env').middlewares.identifyRoute;
9 const fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
11 const router = express.Router({ caseSensitive: true, mergeParams: true });
13 router.post(
14   '/',
15   identifyRoute('fixtures'),
16   asyncHandler(async (req, res) => {
17     const fixtures = await fixtureLoader.createExpectedFixtures(req.body);
19     const fixtureResponse = Object.keys(req.body).reduce((partialResponse, fixtureKey) => {
20       partialResponse[fixtureKey] = fixtures[fixtureKey].toJSON();
22       // A hack around the serialization and our fixtures adding extra properties onto the Mongoose object
23       // See `modules/test-utils/lib/create-users.js`
24       if (fixtureKey.match(/^user/)) {
25         partialResponse[fixtureKey].accessToken = fixtures[fixtureKey].accessToken;
26       }
28       return partialResponse;
29     }, {});
31     logger.info('Fixture endpoint generated some new fixtures', fixtureResponse);
33     res.send(fixtureResponse);
34   })
37 module.exports = router;