Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #320 from dotkom/tests/moar
Browse files Browse the repository at this point in the history
Tests/moar
  • Loading branch information
sklirg authored Feb 28, 2018
2 parents 1fd2841 + d1a4461 commit 6966297
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 29 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function coverageGlobs() {
if (!integrationTest()) {
globs.push('!server/models/**');
globs.push('!server/utils/integrationTestUtils.js');
globs.push('!server/app.js');
globs.push('!server/server.js');
}

return globs;
Expand Down
7 changes: 1 addition & 6 deletions server/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
const logger = require('../logging');
const passport = require('passport');
const { setupOIDC } = require('./oidc');
const { deserializeUser } = require('./user');

module.exports = async (app) => {
await setupOIDC();

passport.serializeUser((user, done) => {
logger.silly('Serializing user', { userId: user.id });
done(null, user.id);
});

passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(deserializeUser);
app.use(passport.initialize());
app.use(passport.session());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ Array [
]
`;

exports[`toggleCanVote emits error if update function fails 1`] = `
Array [
Array [
"action",
Object {
"data": Object {
"id": 0,
"message": "Noe gikk galt under oppdatering av stemmeberettiget",
},
"type": "ERROR",
},
],
]
`;

exports[`toggleCanVote emits error if update function fails 2`] = `Array []`;

exports[`toggleCanVote emits error when user does not have CAN_VOTE permission 1`] = `
Array [
Array [
Expand Down
10 changes: 10 additions & 0 deletions server/channels/admin/user/__tests__/toggle_vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ describe('toggleCanVote', () => {
expect(socket.emit.mock.calls).toMatchSnapshot();
expect(socket.broadcast.emit.mock.calls).toMatchSnapshot();
});

it('emits error if update function fails', async () => {
updateUserById.mockImplementation(async () => { throw new Error(); });
const socket = generateSocket();

await toggleCanVote(socket, generateData());

expect(socket.emit.mock.calls).toMatchSnapshot();
expect(socket.broadcast.emit.mock.calls).toMatchSnapshot();
});
});

describe('setUserPermissions', () => {
Expand Down
2 changes: 1 addition & 1 deletion server/channels/admin/user/toggle_vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const toggleCanVote = async (socket, data) => {
canVote: updatedUser.canVote,
});
} catch (err) {
logger.error('Retrieving user failed.', err);
logger.error('Something went wrong while updating CAN_VOTE for user', err);
emitError(socket, new Error('Noe gikk galt under oppdatering av stemmeberettiget'));
}
};
Expand Down
44 changes: 43 additions & 1 deletion server/managers/__tests__/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jest.mock('../../models/meeting.accessors');
const { getActiveGenfors } = require('../../models/meeting.accessors');
const { canEdit } = require('../meeting');
const { getActiveQuestion, updateIssue } = require('../../models/issue.accessors');
const { disableVoting, enableVoting } = require('../issue');
const { addIssue, deleteIssue, disableVoting, enableVoting, endIssue } = require('../issue');
const { generateIssue, generateManager, generateUser, generateGenfors }
= require('../../utils/generateTestData');
const { VOTING_IN_PROGRESS, VOTING_FINISHED }
Expand Down Expand Up @@ -59,3 +59,45 @@ describe('disable voting', () => {
await expect(updatedIssue).rejects.toEqual(new Error('User is not authorized to disable voting on this issue.'));
});
});

describe('endIssue', () => {
it("throws error if user doesn't have permission", async () => {
canEdit.mockImplementation(() => false);
const issue = await getActiveQuestion();

const closedIssue = endIssue(issue);

await expect(closedIssue).rejects.toEqual(new Error('permission denied'));
});
});

describe('addIssue', () => {
it('throws error if no active genfors', async () => {
getActiveGenfors.mockImplementation(async () => null);

const issue = addIssue(generateIssue());

await expect(issue).rejects.toEqual(new Error('No genfors active'));
});

it('throws error if issue is already active', async () => {
getActiveGenfors.mockImplementation(async () => generateGenfors());
getActiveQuestion.mockImplementation(() => generateIssue());

const issue = addIssue(generateIssue());

await expect(issue).rejects.toEqual(new Error("There's already an active question"));
});
});

describe('deleteIssue', () => {
it('throws error if user is not allowed to delete', async () => {
canEdit.mockImplementation(async () => false);
getActiveGenfors.mockImplementation(async () => generateGenfors());

const deletedIssue = deleteIssue(generateIssue().id);

await expect(deletedIssue)
.rejects.toEqual(new Error("You don't have permission to delete this issue"));
});
});
12 changes: 11 additions & 1 deletion server/managers/__tests__/meeting.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jest.mock('../../models/meeting.accessors');
const { endGenfors } = require('../meeting');
const { addGenfors, endGenfors } = require('../meeting');
const { getGenfors, getActiveGenfors, updateGenfors } = require('../../models/meeting.accessors');
const { generateGenfors, generateUser } = require('../../utils/generateTestData');
const permissionLevels = require('../../../common/auth/permissions');
Expand Down Expand Up @@ -28,3 +28,13 @@ describe('endGenfors', () => {
.toEqual(new Error('Brukeren har ikke riktig rettigheter'));
});
});

describe('addGenfors', () => {
it('throws error if meeting is in progress', async () => {
getActiveGenfors.mockImplementation(async () => generateGenfors());

const genfors = addGenfors(generateGenfors());

expect(genfors).rejects.toEqual(new Error('Meeting in progress, you need to close it or force new'));
});
});
36 changes: 33 additions & 3 deletions server/managers/__tests__/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
jest.mock('../../models/meeting.accessors');
jest.mock('../../models/user.accessors');
const { addUser: modelAddUser, updateUserById } = require('../../models/user.accessors');
const { addUser: modelAddUser, getAnonymousUser, getUserByUsername, updateUserById } = require('../../models/user.accessors');
const { getActiveGenfors } = require('../../models/meeting.accessors');
const { addUser, setUserPermissions } = require('../user');
const { addAnonymousUser, addUser, setUserPermissions } = require('../user');
const permissionLevels = require('../../../common/auth/permissions');
const { generateGenfors, generateUser } = require('../../utils/generateTestData');
const { generateGenfors, generateAnonymousUser, generateUser } = require('../../utils/generateTestData');

const userObj = generateUser({
name: 'Test User',
Expand Down Expand Up @@ -41,6 +41,36 @@ describe('addUser manager', () => {
expect(await addUser(userObj.name, userObj.onlinewebId, permissionLevels.IS_SUPERUSER))
.toMatchObject(Object.assign(userObj, { permissions: permissionLevels.IS_SUPERUSER }));
});

it('throws error if failing to create user', async () => {
modelAddUser.mockImplementation(async () => { throw new Error(); });

const user = addUser(generateUser());

await expect(user).rejects.toEqual(new Error());
});
});

describe('addAnonymousUser', () => {
it('throws error if trying to create new anonUser when user is already registered', async () => {
getActiveGenfors.mockImplementation(async () => generateGenfors());
getUserByUsername.mockImplementation(async () => generateUser({ completedRegistration: true }));

const anonUser = addAnonymousUser(generateAnonymousUser());

await expect(anonUser).rejects.toEqual(new Error('User is already registered'));
});

it('throws error if anonUser already exists', async () => {
getActiveGenfors.mockImplementation(async () => generateGenfors());
getUserByUsername.mockImplementation(async () =>
generateUser({ completedRegistration: false }));
getAnonymousUser.mockImplementation(async () => generateAnonymousUser());

const anonUser = addAnonymousUser(generateAnonymousUser());

await expect(anonUser).rejects.toEqual(new Error('Anonymous user aleady exists'));
});
});

describe('updating user permissions', () => {
Expand Down
11 changes: 2 additions & 9 deletions server/managers/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ async function addIssue(issueData, closeCurrentIssue) {
const activeIssue = model.getActiveQuestion(genfors.id);
if (activeIssue && activeIssue.active && !closeCurrentIssue) {
throw new Error("There's already an active question");
} else if (activeIssue && !activeIssue.active && closeCurrentIssue) {
logger.warn("There's already an active issue. Closing it and proceeding", {
issue: activeIssue.description,
// user: user,
closeCurrentIssue,
});
await model.endIssue(activeIssue);
}
// removed possible issues and proceeding to create a new one

const users = await getQualifiedUsers(genfors.id);
const meetingId = genfors.id;
const data = Object.assign(issueData, {
Expand All @@ -56,7 +49,7 @@ async function deleteIssue(issue, user) {
if (userCanEdit) {
return model.deleteIssue(issue);
}
return null;
throw new Error("You don't have permission to delete this issue");
}

const countVoteAlternatives = (alternatives, votes) => {
Expand Down
11 changes: 3 additions & 8 deletions server/managers/meeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,12 @@ async function endGenfors(genfors, user) {
}


// TODO add security function
async function addGenfors(title, date, user, force) {
// Only allow one at a time
async function addGenfors(title, date) {
// Only allow one genfors at a time, so check if one exists first
const meeting = await getActiveGenfors();
// @TODO Prompt user for confirmations and disable active genfors

if (meeting) {
if (!force) {
throw new Error('Meeting in progress, you need to close it or force new');
}
await endGenfors(meeting, user);
throw new Error('Meeting in progress, you need to close it or force new');
}
return createGenfors(title, date);
}
Expand Down

0 comments on commit 6966297

Please sign in to comment.