Skip to content

Commit

Permalink
fix(tests): Let the server run after approval tests (#488)
Browse files Browse the repository at this point in the history
## Objectives

To make the live-coding presentation smoother:
- by allowing to do manual tests from the browser between two executions of approval tests;
- by cleaning up the code of approval tests.
  • Loading branch information
adrienjoly committed Oct 9, 2021
1 parent 70c4aeb commit 3e59421
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test-reset": "node test/reset-test-db.js",
"test-api": "npx mocha test/api/*.js --serial --exit",
"test-unit": "npx mocha test/unit/*.js --exit",
"test-approval": "START_WITH_ENV_FILE='./env-vars-testing.conf' ava test/approval.tests.js $@",
"test-approval": "npx kill-port --port 8080 >/dev/null; DONT_KILL=1 START_WITH_ENV_FILE='./env-vars-testing.conf' ava test/approval.tests.js $@",
"test-approval-debug": "npm run test-approval -- --fail-fast --serial",
"test:cypress:dev": "node_modules/.bin/cypress open",
"test:cypress": "node_modules/.bin/cypress run",
Expand Down
29 changes: 28 additions & 1 deletion test/db-helpers.js → test/approval-tests-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ async function insertTestData(url, docsPerCollection) {
await mongoClient.close();
}

async function getCleanedPageBody(body) {
try {
return JSON.parse(body);
} catch (err) {
return body
.replace(/(src|href)="(.*\.[a-z]{2,3})\?\d+\.\d+\.\d+"/g, '$1="$2"') // remove openwhyd version from paths to html resources, to reduce noise in diff
.replace(/>[a-zA-Z]+ \d{4}/g, '>(age)') // remove date of posts, because it depends on the time when tests are run
.replace(/>\d+ (day|month|year)s?( ago)?/g, '>(age)'); // remove age of posts, because it depends on the time when tests are run
}
}

const errPrinter = ((blocklist) => {
return (chunk) => {
const message = chunk.toString();
Expand All @@ -51,7 +62,7 @@ const errPrinter = ((blocklist) => {
'please install graphicsmagick',
]);

async function startOpenwhydServer(env) {
async function startOpenwhydServerWith(env) {
const serverProcess = childProcess.fork(
'./app.js',
['--fakeEmail', '--digestInterval', '-1'],
Expand All @@ -67,10 +78,26 @@ async function refreshOpenwhydCache(urlPrefix = 'http://localhost:8080') {
await promisify(request.post)(urlPrefix + '/testing/refresh');
}

async function startOpenwhydServer(envFileForProgamaticStart) {
if (envFileForProgamaticStart) {
const env = {
...(await loadEnvVars(envFileForProgamaticStart)),
MONGODB_PORT: '27117', // port exposed by docker container
TZ: 'UTC',
};
process.env.WHYD_GENUINE_SIGNUP_SECRET = env.WHYD_GENUINE_SIGNUP_SECRET; // required by ./api-client.js
return await startOpenwhydServerWith(env);
} else {
process.env.WHYD_GENUINE_SIGNUP_SECRET = 'whatever'; // required by ./api-client.js
await refreshOpenwhydCache();
}
}

module.exports = {
loadEnvVars,
readMongoDocuments,
insertTestData,
getCleanedPageBody,
startOpenwhydServer,
refreshOpenwhydCache,
};
48 changes: 14 additions & 34 deletions test/approval.tests.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
// Before running these tests, make sure that:
// - Openwhyd is running on port 8080 (`$ docker-compose up --build`)
// - Its database is empty but initialized
// Run with: $ npm run test-approval

const test = require('ava');
const { promisify } = require('util');
const {
loadEnvVars,
startOpenwhydServer,
refreshOpenwhydCache,
readMongoDocuments,
insertTestData,
} = require('./db-helpers');
getCleanedPageBody,
startOpenwhydServer,
} = require('./approval-tests-helpers');

const START_WITH_ENV_FILE = process.env.START_WITH_ENV_FILE;
const { DONT_KILL, START_WITH_ENV_FILE } = process.env;

const MONGODB_URL =
process.env.MONGODB_URL || 'mongodb://localhost:27117/openwhyd_test';

async function getCleanedPageBody(body) {
try {
return JSON.parse(body);
} catch (err) {
return body
.replace(/(src|href)="(.*\.[a-z]{2,3})\?\d+\.\d+\.\d+"/g, '$1="$2"') // remove openwhyd version from paths to html resources, to reduce noise in diff
.replace(/>[a-zA-Z]+ \d{4}/g, '>(age)') // remove date of posts, because it depends on the time when tests are run
.replace(/>\d+ (day|month|year)s?( ago)?/g, '>(age)'); // remove age of posts, because it depends on the time when tests are run
}
}

test.before(async (t) => {
const testDataCollections = {
user: await readMongoDocuments(__dirname + '/approval.users.json'),
Expand All @@ -36,26 +22,14 @@ test.before(async (t) => {
};
await insertTestData(MONGODB_URL, testDataCollections);

if (START_WITH_ENV_FILE) {
const env = {
...(await loadEnvVars(START_WITH_ENV_FILE)),
MONGODB_PORT: '27117', // port exposed by docker container
TZ: 'UTC',
};
process.env.WHYD_GENUINE_SIGNUP_SECRET = env.WHYD_GENUINE_SIGNUP_SECRET; // required by ./api-client.js
t.context.serverProcess = await startOpenwhydServer(env);
} else {
process.env.WHYD_GENUINE_SIGNUP_SECRET = 'whatever'; // required by ./api-client.js
await refreshOpenwhydCache();
}

t.context.serverProcess = await startOpenwhydServer(START_WITH_ENV_FILE);
t.context.openwhyd = require('./api-client');
t.context.getUser = (id) =>
testDataCollections.user.find(({ _id }) => id === _id.toString());
});

test.after((t) => {
if (t.context.serverProcess) {
if (t.context.serverProcess && !DONT_KILL) {
t.context.serverProcess.kill('SIGINT');
}
});
Expand All @@ -65,7 +39,9 @@ const personas = [
{ label: 'User: Adrien', userId: '4d94501d1f78ac091dbc9b4d' },
{ label: 'User: A New User', userId: '000000000000000000000003' },
];

const formats = ['HTML', 'JSON'];

const routes = [
{ label: 'Home, page 1', path: '/' },
{ label: 'Home, page 2', path: '/?after=601d160ea7db502dd31d204e' },
Expand Down Expand Up @@ -134,12 +110,16 @@ formats.forEach((format) => {
personas.forEach((persona) => {
test(`${persona.label}, ${route.label}, ${format}`, async (t) => {
const { openwhyd } = t.context;
// 1. login (or logout) and make sure that it worked as expected
const { userId } = persona;
const { jar, loggedIn, response } =
persona.label === 'Visitor'
? await promisify(openwhyd.logout)(null)
: await promisify(openwhyd.loginAs)(t.context.getUser(userId));
if (userId && !loggedIn) t.fail(`login failed: ${response.body}`); // just to make sure that login worked as expected
if (userId && !loggedIn) {
t.fail(`login failed: ${response.body}`);
}
// 2. test that the response is still the same
const path =
format === 'JSON'
? route.jsonPath ||
Expand Down

0 comments on commit 3e59421

Please sign in to comment.