Skip to content

Commit

Permalink
Prevent mutating state reset template (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty authored Jan 25, 2024
1 parent 00c47f2 commit 5753b33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';

import { importMetaUrl } from './importMetaUtils';
import { ChatModel, defaultChatModel } from './models/chat';
import { LevelState, levelsInitialState } from './models/level';
import { LevelState, getInitialLevelStates } from './models/level';
import { router } from './router';

dotenv.config();
Expand Down Expand Up @@ -55,7 +55,7 @@ app.use((req, _res, next) => {
// initialise session variables first time
if (!req.session.initialised) {
req.session.chatModel = defaultChatModel;
req.session.levelState = levelsInitialState;
req.session.levelState = getInitialLevelStates();
req.session.initialised = true;
}
next();
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controller/resetController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request, Response } from 'express';

import { levelsInitialState } from '@src/models/level';
import { getInitialLevelStates } from '@src/models/level';

function handleResetProgress(req: Request, res: Response) {
console.debug('Resetting progress for all levels', req.session.levelState);
req.session.levelState = levelsInitialState;
req.session.levelState = getInitialLevelStates();
res.send(req.session.levelState);
}

Expand Down
23 changes: 14 additions & 9 deletions backend/src/models/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ interface LevelState {
sentEmails: EmailInfo[];
}

const levelsInitialState = Object.values(LEVEL_NAMES)
.filter((value) => Number.isNaN(Number(value)))
.map((value) => ({
level: value as LEVEL_NAMES,
chatHistory: [],
defences: defaultDefences,
sentEmails: [],
}));
function getInitialLevelStates() {
return Object.values(LEVEL_NAMES)
.filter((value) => Number.isNaN(Number(value)))
.map(
(value) =>
({
level: value as LEVEL_NAMES,
chatHistory: [],
defences: defaultDefences,
sentEmails: [],
} as LevelState)
);
}

export { LEVEL_NAMES, levelsInitialState };
export { LEVEL_NAMES, getInitialLevelStates };
export type { LevelState };
12 changes: 8 additions & 4 deletions backend/test/unit/controller/resetController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '@src/models/chat';
import { DEFENCE_ID, Defence, DefenceConfigItem } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES, LevelState, levelsInitialState } from '@src/models/level';
import {
LEVEL_NAMES,
LevelState,
getInitialLevelStates,
} from '@src/models/level';

declare module 'express-session' {
interface Session {
Expand Down Expand Up @@ -69,7 +73,7 @@ describe('handleResetProgress unit tests', () => {

const res = responseMock();
handleResetProgress(reqWithChatHistory, res);
expect(res.send).toHaveBeenCalledWith(levelsInitialState);
expect(res.send).toHaveBeenCalledWith(getInitialLevelStates());
});

test('GIVEN sent emails THEN should reset emails for all levels', () => {
Expand All @@ -84,7 +88,7 @@ describe('handleResetProgress unit tests', () => {

const res = responseMock();
handleResetProgress(reqWithSentEmails, res);
expect(res.send).toHaveBeenCalledWith(levelsInitialState);
expect(res.send).toHaveBeenCalledWith(getInitialLevelStates());
});

test('GIVEN defences THEN should reset defences for levels', () => {
Expand Down Expand Up @@ -116,6 +120,6 @@ describe('handleResetProgress unit tests', () => {

const res = responseMock();
handleResetProgress(reqWithDefences, res);
expect(res.send).toHaveBeenCalledWith(levelsInitialState);
expect(res.send).toHaveBeenCalledWith(getInitialLevelStates());
});
});

0 comments on commit 5753b33

Please sign in to comment.