Skip to content
12 changes: 10 additions & 2 deletions src/server/test/routes/unitsRouteTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
const { mocha, expect, testDB, app, testUser } = require('../common');
const chai = require('chai');
const Unit = require('../../models/Unit');
const { HTTP_CODES } = require('../../util/httpCodes');

mocha.describe('Units Route', () => {
let token;

mocha.before(async () => {
// login
const res = await chai.request(app).post('/api/loginLogout/login')
.send({ username: testUser.username, password: testUser.password });
token = res.body.token;
});
mocha.after(async () => {
Comment thread
Nespina24 marked this conversation as resolved.
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.describe('Edit endpoint', () => {

Expand All @@ -28,7 +36,7 @@ mocha.describe('Units Route', () => {
name: 'New name',
identifier: unit.identifier,
});
expect(res).to.have.status(200);
expect(res).to.have.status(HTTP_CODES.OK);
const updatedUnit = await Unit.getById(unit.id, conn);
expect(updatedUnit.name).to.equal('New name');
expect(updatedUnit.note).to.equal(beforeNote);
Expand Down
26 changes: 23 additions & 3 deletions src/server/test/web/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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/. */

/* This file tests the API for retrieving meters, by artificially
* inserting meters prior to executing the test code. */
/* This file tests the API for retrieving maps, by artificially
* inserting maps prior to executing the test code. */

const { chai, mocha, expect, app, testDB, testUser } = require('../common');
const { Map } = require('../../models/Map');
Expand Down Expand Up @@ -51,6 +51,9 @@ function expectMapsToBeEquivalent(maps, length) {
mocha.describe('maps API', () => {
mocha.beforeEach(async () => {
// TODO Why is there an empty body here?

// It seems to work fine if I comment it out.
// Deleting the beforeEach() hook should be possible.
});
Comment thread
Nespina24 marked this conversation as resolved.
Outdated

mocha.it('returns nothing when no map is present', async () => {
Expand Down Expand Up @@ -80,10 +83,19 @@ mocha.describe('maps API', () => {
// Since this .before is in the middle of tests, it should not have issues as
// documented in usersTest.js.
mocha.before(async () => {
// login
let res = await chai.request(app).post('/api/loginLogout/login')
.send({ username: testUser.username, password: testUser.password });
token = res.body.token;
});
mocha.after(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it('returns all maps', async () => {
const conn = testDB.getConnection();
await new Map(undefined, 'Map 1', true, null, 'default', moment('2000-10-10'), origin, opposite, 'placeholder', 1.0, 0.1).insert(conn);
Expand Down Expand Up @@ -119,6 +131,14 @@ mocha.describe('maps API', () => {
.send({ username: unauthorizedUser.username, password: unauthorizedUser.password });
token = res.body.token;
});
mocha.afterEach(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it(`should reject requests from ${role} to create maps`, async () => {
// get maps
let res = await chai.request(app).post('/api/maps/create').set('token', token);
Expand Down Expand Up @@ -170,7 +190,7 @@ mocha.describe('maps API', () => {
expect(res.body).to.have.property('name', 'Map 2');
});

mocha.it('responds appropriately when the meter in question does not exist', async () => {
mocha.it('responds appropriately when the map in question does not exist', async () => {
const conn = testDB.getConnection();
const map = new Map(undefined, 'Map', true, null, 'default', moment('2000-10-10'), origin, opposite, 'placeholder', 0.0, 0.0);
await map.insert(conn);
Expand Down
20 changes: 18 additions & 2 deletions src/server/test/web/meters.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,20 @@ mocha.describe('meters API', () => {
// Since this .before is in the middle of tests, it should not have issues as
// documented in usersTest.js.
mocha.before(async () => {
// login
let res = await chai.request(app).post('/api/loginLogout/login')
.send({ username: testUser.username, password: testUser.password });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is supposed to test both ADMIN and CSV roles, then it looks like it doesn't access the CSV role. The test looks like it simply runs ADMIN both times instead of depending on the role being looped. If this behavior is unintended, then I can make the appropriate changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I see seems very similar to the next test for the other roles:

mocha.describe('Admin role & CSV role:', () => {
		for (const role in User.role) {
			if (User.role[role] !== User.role.OBVIUS && User.role[role] !== User.role.EXPORT) {

I see what you mean and you seem correct. I think it should login with the desired role and use that token. Thanks for offering to make that change.

Unlike the next test which should test everyone else, this one should only test these roles. Thus, I think it would be much better to check if the role is admin or csv rather than not the others. Then it will still work if more roles are added. If you agree, could you also do that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted the test to login with the same role, but I noticed that it would only pass if I changed the before hook to a beforeEach hook in src/server/test/web/meters.js on line 162. Otherwise, I kept getting the following errors for both admin and csv:

 21 passing (35s)
  2 failing

  1) meters API
       Admin role & CSV role:
         should return all meters for ADMIN:
     AssertionError: expected { id: 1, name: null, url: null, …(31) } to have property 'name' of 'Meter 1', but got null
      at expectMetersToBeEquivalent (src/server/test/web/meters.js:54:26)
      at Context.<anonymous> (src/server/test/web/meters.js:213:6)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

  2) meters API
       Admin role & CSV role:
         should return all meters for CSV:
     AssertionError: expected { id: 1, name: null, url: null, …(31) } to have property 'name' of 'Meter 1', but got null
      at expectMetersToBeEquivalent (src/server/test/web/meters.js:54:26)
      at Context.<anonymous> (src/server/test/web/meters.js:213:6)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I tested and found that the same error message is given for export and obvius roles for the same test, regardless of whether before or beforeEach is used (This might be the expected outcome, but shouldn't be for admin or csv). I also found that there's no difference between using an after or afterEach hook on line 176, but I kept it as afterEach to be consistent. I'm unsure of why I would strictly need a beforeEach for this test, but I plan on leaving it like this unless you have any input.

token = res.body.token;
});
mocha.it('returns all meters', async () => {
mocha.after(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it(`should return all meters for ${role}`, async () => {
const conn = testDB.getConnection();
await new Meter(undefined, 'Meter 1', '1.1.1.1', true, true, Meter.type.MAMAC, '+01', gps,
'Identified 1', 'notes 1', 10.0, true, true, '01:01:25', '05:05:05', 5.1, 7.3, 1, 'increasing', false,
Expand Down Expand Up @@ -217,7 +226,14 @@ mocha.describe('meters API', () => {
.send({ username: unauthorizedUser.username, password: unauthorizedUser.password });
token = res.body.token;
});

mocha.afterEach(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it('should only return visible data', async () => {
const conn = testDB.getConnection();
await new Meter(undefined, 'Meter 1', '1.1.1.1', true, true, Meter.type.MAMAC, '+01', gps,
Expand Down
67 changes: 46 additions & 21 deletions src/server/test/web/preferencesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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/. */

/* This file tests the API for retrieving meters, by artificially
* inserting meters prior to executing the test code. */
/* This file tests the API for retrieving preferences, by artificially
* inserting preferences prior to executing the test code. */

const { chai, mocha, expect, app, testDB, testUser } = require('../common');
const User = require('../../models/User');
Expand All @@ -13,24 +13,39 @@ const { HTTP_CODES } = require('../../util/httpCodes');
mocha.describe('preferences API', () => {
mocha.describe('modification api', () => {
mocha.describe('edit endpoint', () => {
mocha.it('should accept requests from Admin role', async () => {
let res = await chai.request(app).post('/api/loginLogout/login')
.send({ username: testUser.username, password: testUser.password });
expect(res).to.have.status(HTTP_CODES.OK);
const token = res.body.token;
const preferences = {
displayTitle: 'title',
defaultChartToRender: 'line',
defaultBarStacking: true,
defaultLanguage: 'en',
defaultWarningFileSize: 5,
defaultFileSizeLimit: 25,
defaultAreaNormalization: true,
defaultAreaUnit: 'meters',
defaultMeterReadingFrequency: '1:13:17'
}
res = await chai.request(app).post('/api/preferences').set('token', token).send({ preferences });
expect(res).to.have.status(HTTP_CODES.OK);
const preferences = {
displayTitle: 'title',
defaultChartToRender: 'line',
defaultBarStacking: true,
defaultLanguage: 'en',
defaultWarningFileSize: 5,
defaultFileSizeLimit: 25,
defaultAreaNormalization: true,
defaultAreaUnit: 'meters',
defaultMeterReadingFrequency: '1:13:17'
};
mocha.describe('Admin role: ', () => {
let token;
mocha.before(async () => {
// login
let res = await chai.request(app).post('/api/loginLogout/login')
.send({ username: testUser.username, password: testUser.password });
token = res.body.token;
});
mocha.after(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it('should accept requests from Admin role', async () => {
res = await chai.request(app).post('/api/preferences')
.set('token', token)
.send({ preferences });
expect(res).to.have.status(HTTP_CODES.OK);
});
});

mocha.describe('Non-Admin roles: ', () => {
Expand All @@ -51,8 +66,18 @@ mocha.describe('preferences API', () => {
.send({ username: unauthorizedUser.username, password: unauthorizedUser.password });
token = res.body.token;
});
mocha.afterEach(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it(`should reject requests from ${role}`, async () => {
let res = await chai.request(app).post('/api/preferences').set('token', token);
res = await chai.request(app).post('/api/preferences')
.set('token', token)
.send({ preferences });
expect(res).to.have.status(HTTP_CODES.FORBIDDEN);
});
}
Expand Down
23 changes: 20 additions & 3 deletions src/server/test/web/usersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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/. */

/* This file tests the API for retrieving meters, by artificially
* inserting meters prior to executing the test code. */
/* This file tests the API for retrieving users, by artificially
* inserting users prior to executing the test code. */

const { chai, mocha, expect, app, testDB, testUser, recreateDB } = require('../common');
const User = require('../../models/User');
Expand All @@ -24,6 +24,13 @@ mocha.describe('Users API', () => {
.send({ username: testUser.username, password: testUser.password });
token = res.body.token;
});
mocha.after(async () => {
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it('retrieves users', async () => {
const conn = testDB.getConnection();
const password = await bcrypt.hash('password', 10);
Expand Down Expand Up @@ -118,10 +125,20 @@ mocha.describe('Users API', () => {
.send({ username: unauthorizedUser.username, password: unauthorizedUser.password });
token = res.body.token;
});
mocha.it('should reject request to retrieve users', async () => {
mocha.afterEach(async () => {
// logout
if (token) {
await chai.request(app).post('/api/loginLogout/logout')
.set('token', token);
}
});

mocha.it(`should reject requests from ${role} to retrieve users`, async () => {
// get
const res = await chai.request(app).get('/api/users').set('token', token);
expect(res).to.have.status(HTTP_CODES.FORBIDDEN);
});

mocha.it(`should reject requests from ${role} to create users`, async () => {
// create
const res = await chai.request(app).post('/api/users/create').set('token', token);
Expand Down
Loading