Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 1 addition & 82 deletions src/server/test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,20 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file exports two useful items: testDB, which provides the method .getConnection,
* returning a connection to the testing database, and recreateDB, which destroys the
* database and creates a new schema there.
*/
//This file exports a pre-configured mocha, chai, and expect for use in tests.

const mocha = require('mocha');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const chaiHttp = require('chai-http');
const bcrypt = require('bcryptjs');
const expect = chai.expect;
const { log, LogLevel } = require('../log');

// TODO: Move logging controls to a better place.
// During testing do not show errors since many tests generate them.
log.level = LogLevel.SILENT;
// If tests are failing then turning on the console messages can help. Commenting the line
// above and uncommenting the next line will do that.
// log.level = LogLevel.DEBUG;
// During testing do not do emails. Since DB wiped and not set up for emails this would not do anything.
log.emailLevel = LogLevel.SILENT;
// Logging to the DB during testing has issues. First, the test DB is wiped before each test so it does not actually
// stay around. Second, if one test finishes
// quickly after the DB logging starts (async) then the next test starts and the beforeEach wipes the DB.
// If the attempt to put the log msg in the DB
// happens before it is initialized again then the log table is missing and causes an error.
log.logToDb = false;

const User = require('../models/User');
const { getDB, createSchema, stopDB } = require('../models/database');
const { swapConnection, dropConnection } = require('../db');
const app = require('../app');

// Configure Chai to use the required plugins
chai.use(chaiAsPromised);
chai.use(chaiHttp);

// Global database connection object, whose members are accessible to DB-touching tests.
// For instance, call:
// conn = testDB.getConnection();
// doSomething(conn)
let testDB = {
_connection: null,
config: null,
getConnection: function () {
return this._connection;
}
};

function connectTestDB() {
const testDBConfig = {
user: process.env.OED_DB_TEST_USER || process.env.OED_DB_USER,
database: process.env.OED_DB_TEST_DATABASE,
password: process.env.OED_DB_TEST_PASSWORD || process.env.OED_DB_PASSWORD,
host: process.env.OED_DB_TEST_HOST || process.env.OED_DB_HOST,
port: process.env.OED_DB_TEST_PORT || process.env.OED_DB_PORT
};

stopDB();
testDB._connection = getDB(testDBConfig);
testDB.config = testDBConfig;
swapConnection(testDB.config, testDB._connection);
}

// The user for use by tests.
const testUser = new User(undefined, 'test@example.invalid', bcrypt.hashSync('password', 10), User.role.ADMIN);
testUser.password = 'password';

async function recreateDB() {
const conn = testDB.getConnection();
// This should drop all database objects, as long as they were all created by the current database user
// They should be, since they were all created during a previous test.
await conn.none('DROP OWNED BY current_user;');
await createSchema(conn);
await testUser.insert(conn);
}

mocha.before(() => {
connectTestDB();
});

mocha.beforeEach(async () => {
await recreateDB();
});

mocha.after(() => {
dropConnection();
});

module.exports = {
chai,
mocha,
expect,
app,
testUser,
recreateDB,
testDB
};
91 changes: 91 additions & 0 deletions src/server/test/commonTestDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file exports two useful items: testDB, which provides the method .getConnection,
* returning a connection to the testing database, and recreateDB, which destroys the
* database and creates a new schema there.
*/

const { log, LogLevel } = require('../log');

// TODO: Move logging controls to a better place.
// During testing do not show errors since many tests generate them.
log.level = LogLevel.SILENT;
// If tests are failing then turning on the console messages can help. Commenting the line
// above and uncommenting the next line will do that.
// log.level = LogLevel.DEBUG;
// During testing do not do emails. Since DB wiped and not set up for emails this would not do anything.
log.emailLevel = LogLevel.SILENT;
// Logging to the DB during testing has issues. First, the test DB is wiped before each test so it does not actually
// stay around. Second, if one test finishes
// quickly after the DB logging starts (async) then the next test starts and the beforeEach wipes the DB.
// If the attempt to put the log msg in the DB
// happens before it is initialized again then the log table is missing and causes an error.
log.logToDb = false;

const User = require('../models/User');
const { getDB, createSchema, stopDB } = require('../models/database');
const { swapConnection, dropConnection } = require('../db');
const app = require('../app');
const bcrypt = require('bcryptjs');
const { mocha, chai, expect } = require('./common');

// Global database connection object, whose members are accessible to DB-touching tests.
// For instance, call:
// conn = testDB.getConnection();
// doSomething(conn)
let testDB = {
_connection: null,
config: null,
getConnection: function () {
return this._connection;
}
};

function connectTestDB() {
const testDBConfig = {
user: process.env.OED_DB_TEST_USER || process.env.OED_DB_USER,
database: process.env.OED_DB_TEST_DATABASE,
password: process.env.OED_DB_TEST_PASSWORD || process.env.OED_DB_PASSWORD,
host: process.env.OED_DB_TEST_HOST || process.env.OED_DB_HOST,
port: process.env.OED_DB_TEST_PORT || process.env.OED_DB_PORT
};

stopDB();
testDB._connection = getDB(testDBConfig);
testDB.config = testDBConfig;
swapConnection(testDB.config, testDB._connection);
}

// The user for use by tests.
const testUser = new User(undefined, 'test@example.invalid', bcrypt.hashSync('password', 10), User.role.ADMIN);
testUser.password = 'password';

async function recreateDB() {
const conn = testDB.getConnection();
// This should drop all database objects, as long as they were all created by the current database user
// They should be, since they were all created during a previous test.
await conn.none('DROP OWNED BY current_user;');
await createSchema(conn);
await testUser.insert(conn);
}

mocha.before(() => {
connectTestDB();
});

mocha.beforeEach(async () => {
await recreateDB();
});

mocha.after(() => {
dropConnection();
});

module.exports = {
app,
testUser,
recreateDB,
testDB
};
3 changes: 2 additions & 1 deletion src/server/test/db/baselineTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Baseline = require('../../models/Baseline');
const Meter = require('../../models/Meter');
const Reading = require('../../models/Reading');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/compareTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const moment = require('moment');
const Meter = require('../../models/Meter');
const Reading = require('../../models/Reading');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/configfileTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const md5 = require('md5');
const moment = require('moment');
const Configfile = require('../../models/obvius/Configfile');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/conversionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Conversion = require('../../models/Conversion');
const Unit = require('../../models/Unit');

Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/cyclicGroupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Group = require('../../models/Group');

/*
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/groupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Group = require('../../models/Group');
const Meter = require('../../models/Meter');
const Point = require('../../models/Point');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/loadArrayInputTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const moment = require('moment');
const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Reading = require('../../models/Reading');
const Meter = require('../../models/Meter');
const loadArrayInput = require('../../services/pipeline-in-progress/loadArrayInput');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/loadCsvInputTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const moment = require('moment');
const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const readCsv = require('../../services/pipeline-in-progress/readCsv');
const Reading = require('../../models/Reading');
const Meter = require('../../models/Meter');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/loadLogfileToReadingsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const Meter = require('../../models/Meter');
const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const demuxCsvWithSingleColumnTimestamps = require('../../services/obvius/csvDemux');
const loadLogfileToReadings = require('../../services/obvius/loadLogfileToReadings');

Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/logEmailTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const LogEmail = require('../../models/LogEmail');

mocha.describe('Log Email', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/mapTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const { Map } = require('../../models/Map');
const Point = require('../../models/Point');
const moment = require('moment');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');

const Migration = require('../../models/Migration');
const { migrateAll } = require('../../migrations/migrateDatabase');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/momentPatchTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const moment = require('moment');

mocha.describe('MomentJS patching', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/pointPatchTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Point = require('../../models/Point');

mocha.describe('Point patching', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/readingTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* This class is for testing meter readings.
*/
const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const moment = require('moment');
const Meter = require('../../models/Meter');
const Reading = require('../../models/Reading');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/unitReadingsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

const moment = require('moment');
const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Meter = require('../../models/Meter');
const Reading = require('../../models/Reading');
const Group = require('../../models/Group');
Expand Down
3 changes: 2 additions & 1 deletion src/server/test/db/unitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');
const Unit = require('../../models/Unit');
const { expectUnitToBeEquivalent, expectArrayOfUnitsToBeEquivalent } = require('../../util/compareUnits');

Expand Down
3 changes: 2 additions & 1 deletion src/server/test/migration/migrateDatabaseInvalidTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB } = require('../common');
const { mocha, expect } = require('../common');
const { testDB } = require('../commonTestDB');

const Migration = require('../../models/Migration');
const { migrateAll } = require('../../migrations/migrateDatabase');
Expand Down
4 changes: 2 additions & 2 deletions src/server/test/routes/authenticatorParamsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { expect } = require('chai');
const { chai, mocha, app } = require('../common');
const { chai, mocha, expect } = require('../common');
const { app } = require('../commonTestDB');
const { validateString, testInvalidField, validateNoExtraFields } = require('../util/validationHelpers');
const { HTTP_CODES } = require('../../util/httpCodes');
const { PASSWORD_MAX_LENGTH, PASSWORD_MIN_LENGTH, TOKEN_MAX_LENGTH, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH }
Expand Down
4 changes: 2 additions & 2 deletions src/server/test/routes/baselineParamsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

const { expect } = require('chai');
const { chai, mocha, app } = require('../common');
const { chai, mocha, expect } = require('../common');
const { app } = require('../commonTestDB');
const { HTTP_CODES } = require('../../util/httpCodes');

mocha.describe('Baseline Parameter Validation', () => {
Expand Down
Loading
Loading