fix(api): delete user's exam attempts with account (#57079)

This commit is contained in:
Oliver Eyton-Williams
2024-11-11 17:04:10 +01:00
committed by GitHub
parent 85fc9c3ac9
commit 88e2ff6eab
4 changed files with 40 additions and 4 deletions

View File

@@ -343,9 +343,7 @@ export const exam: EnvExam = {
};
export async function seedEnvExam() {
await fastifyTestInstance.prisma.envExamAttempt.deleteMany({});
await fastifyTestInstance.prisma.envGeneratedExam.deleteMany({});
await fastifyTestInstance.prisma.envExam.deleteMany({});
await clearEnvExam();
await fastifyTestInstance.prisma.envExam.create({
data: exam
@@ -369,3 +367,15 @@ export async function seedEnvExam() {
// }
// }
}
export async function clearEnvExam() {
await fastifyTestInstance.prisma.envExamAttempt.deleteMany({});
await fastifyTestInstance.prisma.envGeneratedExam.deleteMany({});
await fastifyTestInstance.prisma.envExam.deleteMany({});
}
export async function seedEnvExamAttempt() {
await fastifyTestInstance.prisma.envExamAttempt.create({
data: examAttempt
});
}

View File

@@ -277,7 +277,7 @@ model EnvExamAttempt {
needsRetake Boolean
// Relations
user user @relation(fields: [userId], references: [id])
user user @relation(fields: [userId], references: [id], onDelete: Cascade)
exam EnvExam @relation(fields: [examId], references: [id])
generatedExam EnvGeneratedExam @relation(fields: [generatedExamId], references: [id])
}

View File

@@ -42,6 +42,10 @@ describe('/exam-environment/', () => {
res.body.examEnvironmentAuthorizationToken;
});
afterAll(async () => {
await mock.clearEnvExam();
});
describe('POST /exam-environment/exam/attempt', () => {
afterEach(async () => {
await fastifyTestInstance.prisma.envExamAttempt.deleteMany();

View File

@@ -16,6 +16,11 @@ import {
createSuperRequest
} from '../../../jest.utils';
import { JWT_SECRET } from '../../utils/env';
import {
clearEnvExam,
seedEnvExam,
seedEnvExamAttempt
} from '../../../__mocks__/env-exam';
import { getMsTranscriptApiUrl } from './user';
const mockedFetch = jest.fn();
@@ -336,6 +341,10 @@ describe('userRoutes', () => {
});
describe('/account/delete', () => {
beforeEach(async () => {
await seedEnvExam();
await seedEnvExamAttempt();
});
afterEach(async () => {
await fastifyTestInstance.prisma.userToken.deleteMany({
where: { OR: [{ userId: defaultUserId }, { userId: otherUserId }] }
@@ -343,6 +352,7 @@ describe('userRoutes', () => {
await fastifyTestInstance.prisma.msUsername.deleteMany({
where: { OR: [{ userId: defaultUserId }, { userId: otherUserId }] }
});
await clearEnvExam();
});
test('POST returns 200 status code with empty object', async () => {
@@ -398,6 +408,18 @@ describe('userRoutes', () => {
);
expect(setCookie).toHaveLength(3);
});
test("POST deletes all the user's exam attempts", async () => {
const examAttempts =
await fastifyTestInstance.prisma.envExamAttempt.findMany();
expect(examAttempts).toHaveLength(1);
await superPost('/account/delete');
const examAttemptsAfter =
await fastifyTestInstance.prisma.envExamAttempt.findMany();
expect(examAttemptsAfter).toHaveLength(0);
});
});
describe('/account/reset-progress', () => {