Skip to content

Commit

Permalink
fix(tests): Clean up the database between each API test suite (#417)
Browse files Browse the repository at this point in the history
## What does this PR do / solve?

`user.api.tests.js` fails if it is not run after `auth.api.tests.js`, because the former relies on the presence of a user that was added to the db by the latter.

Proof, after resetting the db between tests: https://github.com/openwhyd/openwhyd/pull/417/checks?check_run_id=1581677648#step:10:114

## Overview of changes

Reset the db between each test suite, to prevent side effects.
  • Loading branch information
adrienjoly committed Dec 19, 2020
1 parent 6911c9f commit d7ff64e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"start": "node app.js --fakeEmail --digestInterval -1 $@",
"start:coverage": "npx nyc --silent node app.js --fakeEmail --digestInterval -1 $@",
"test-reset": "node test/reset-test-db.js",
"test-api": "npm run test-reset && npx mocha test/api/*.js --exit",
"test-api": "npx mocha test/api/*.js --serial --exit",
"test-unit": "npx mocha test/unit/*.js --exit",
"test:cypress:dev": "node_modules/.bin/cypress open",
"test:cypress": "node_modules/.bin/cypress run",
Expand Down
8 changes: 5 additions & 3 deletions test/api/auth.api.tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* global describe, it */
/* global describe, it, before */

var { promisify } = require('util');
var assert = require('assert');

var { ADMIN_USER, TEST_USER } = require('../fixtures.js');
var { ADMIN_USER, TEST_USER, cleanup } = require('../fixtures.js');
const apiClient = require('../api-client.js');

const get = promisify(apiClient.get);
Expand All @@ -21,12 +21,14 @@ const genSecureUser = (() => {
});
})();

before(cleanup);

describe('auth api', () => {
describe('login with email', () => {
it('succeeds', async () => {
const { response, body } = await loginAs(ADMIN_USER);
const cookies = ((response.headers || {})['set-cookie'] || []).join(' ');
assert(/whydSid\=/.test(cookies));
assert(/whydSid=/.test(cookies));
assert(body.redirect);
});

Expand Down
45 changes: 22 additions & 23 deletions test/api/post.api.tests.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/* global describe, it */
/* global describe, it, before */

var assert = require('assert');

var { TEST_USER } = require('../fixtures.js');
var { ADMIN_USER, cleanup } = require('../fixtures.js');
var api = require('../api-client.js');

describe(`post api`, function () {
before(cleanup); // to prevent side effects between tests

var pId, uId;
const post = {
eId: '/yt/XdJVWSqb4Ck',
name: 'Lullaby - Jack Johnson and Matt Costa',
};

it(`should allow adding a track`, function (done) {
api.loginAs(TEST_USER, function (error, { response, body, jar }) {
api.addPost(jar, post, function (error, { response, body }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.addPost(jar, post, function (error, { body }) {
assert.ifError(error);
assert.equal(body.eId, post.eId);
assert.equal(body.name, post.name);
Expand All @@ -27,8 +29,8 @@ describe(`post api`, function () {
});

it(`should allow re-adding a track (aka "repost")`, function (done) {
api.loginAs(TEST_USER, function (error, { response, body, jar }) {
api.addPost(jar, { pId }, function (error, { response, body }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.addPost(jar, { pId }, function (error, { body }) {
assert.ifError(error);
assert(body._id);
assert.notEqual(body._id, pId);
Expand All @@ -50,8 +52,8 @@ describe(`post api`, function () {
});

it(`should allow adding a track to a playlist`, function (done) {
api.loginAs(TEST_USER, function (error, { response, body, jar }) {
api.addPost(jar, postInPlaylist, function (error, { response, body }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.addPost(jar, postInPlaylist, function (error, { body }) {
assert.ifError(error);
assert(body._id);
assert.equal(body.eId, postInPlaylist.eId);
Expand All @@ -64,8 +66,8 @@ describe(`post api`, function () {
});

it(`make sure that the playlist was created`, function (done) {
api.loginAs(TEST_USER, function (error, { jar }) {
api.getUser(jar, {}, function (error, { response, body }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.getUser(jar, {}, function (error, { body }) {
assert.equal(body.pl.length, 1);
assert.equal(body.pl[0].id, firstPlaylistIndex);
assert.equal(body.pl[0].name, postInPlaylist.pl.name);
Expand All @@ -77,11 +79,8 @@ describe(`post api`, function () {
});

it(`should find 1 track in the playlist`, function (done) {
api.loginAs(TEST_USER, function (error, { jar }) {
api.getPlaylist(jar, playlistFullId, function (
error,
{ response, body }
) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.getPlaylist(jar, playlistFullId, function (error, { body }) {
assert.ifError(error);
assert.equal(body.length, 1);
assert.equal(body[0].id, playlistFullId);
Expand All @@ -93,10 +92,10 @@ describe(`post api`, function () {
});

it(`should return 1 track in the playlist`, function (done) {
api.loginAs(TEST_USER, function (error, { jar }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.getPlaylistTracks(jar, `u/${uId}`, firstPlaylistIndex, function (
error,
{ response, body }
{ body }
) {
assert.equal(body.length, 1);
assert.equal(body[0].pl.id, firstPlaylistIndex);
Expand All @@ -107,9 +106,9 @@ describe(`post api`, function () {
});

it(`should return 1 track in the playlist, with limit=1000`, function (done) {
api.loginAs(TEST_USER, function (error, { jar }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
const url = `/u/${uId}/playlist/${firstPlaylistIndex}?format=json&limit=1000`;
api.get(jar, url, function (error, { response, body }) {
api.get(jar, url, function (error, { body }) {
assert.equal(body.length, 1);
assert.equal(body[0].pl.id, firstPlaylistIndex);
assert.equal(body[0].pl.name, postInPlaylist.pl.name);
Expand All @@ -119,10 +118,10 @@ describe(`post api`, function () {
});

it(`should return tracks if two limit parameters are provided`, function (done) {
api.loginAs(TEST_USER, function (error, { jar }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
const url = `/u/${uId}/playlist/${firstPlaylistIndex}?format=json&limit=1000&limit=20`;
// => the `limit` property will be parsed as ["1000","20"] => causing bug #89
api.get(jar, url, function (error, { response, body }) {
api.get(jar, url, function (error, { body }) {
assert.notEqual(body.length, 0);
done();
});
Expand All @@ -133,12 +132,12 @@ describe(`post api`, function () {
// TODO: delete post

it(`should return the comment data after adding it`, function (done) {
api.loginAs(TEST_USER, function (error, { response, body, jar }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
const comment = {
pId,
text: 'hello world',
};
api.addComment(jar, comment, function (error, { response, body }) {
api.addComment(jar, comment, function (error, { body }) {
assert.ifError(error);
assert.equal(body.pId, comment.pId);
assert.equal(body.text, comment.text);
Expand Down
24 changes: 12 additions & 12 deletions test/api/user.api.tests.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/* global describe, it */
/* global describe, it, before */

var assert = require('assert');

var { TEST_USER } = require('../fixtures.js');
var { ADMIN_USER, cleanup } = require('../fixtures.js');
var api = require('../api-client.js');

describe(`user api -- getting user data`, function () {
before(cleanup); // to prevent side effects between tests

it(`gets user profile data`, function (done) {
const url =
'/api/user?includeSubscr=true&isSubscr=true&countPosts=true&countLikes=true&getVersion=1';
api.loginAs(TEST_USER, function (error, { jar }) {
api.loginAs(ADMIN_USER, function (error, { jar }) {
api.get(jar, url, function (err, { body, ...res }) {
assert.ifError(err);
assert.equal(res.response.statusCode, 200);
assert(!body.error);
assert.equal(body.email, TEST_USER.email);
assert.equal(body.email, ADMIN_USER.email);
assert(body.openwhydServerVersion);
done();
});
Expand All @@ -23,17 +24,16 @@ describe(`user api -- getting user data`, function () {
});

describe(`user api -- setting user data`, function () {
before(cleanup); // to prevent side effects between tests

it(`updates the user's name`, function (done) {
api.loginAs(TEST_USER, function (error, { response, body, jar }) {
api.loginAs(ADMIN_USER, function (error, { body, jar }) {
assert.ifError(body.error);
assert(body.redirect);
api.getUser(jar, {}, function (error, { response, body }) {
assert.equal(body.name, TEST_USER.name);
api.getUser(jar, {}, function (error, { body }) {
assert.equal(body.name, ADMIN_USER.name);
const newName = 'renamed user';
api.setUser(jar, { name: newName }, function (
error,
{ response, body }
) {
api.setUser(jar, { name: newName }, function (error, { body }) {
assert.equal(body.name, newName);
done();
});
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const childProcess = require('child_process');

exports.URL_PREFIX = 'http://localhost:8080';

exports.ADMIN_USER = {
Expand All @@ -18,3 +20,10 @@ exports.TEST_USER = {
password: 'test-user', // for the /register api endpoint
md5: '42b27efc1480b4fe6d7eaa5eec47424d',
};

// Call this before each test to prevent side effects between tests
exports.cleanup = (done) => {
console.warn('🧹 Cleaning up test db...');
const process = childProcess.fork('test/reset-test-db.js');
process.on('close', () => done());
};

0 comments on commit d7ff64e

Please sign in to comment.