feat(api): get exams endpoint (#56727)

This commit is contained in:
Sem Bauke
2024-10-18 20:43:28 +02:00
committed by GitHub
parent ed2b372ced
commit 27e8bf9da0
4 changed files with 110 additions and 0 deletions
@@ -24,12 +24,14 @@ describe('/exam-environment/', () => {
setupServer();
describe('Authenticated user with exam environment authorization token', () => {
let superPost: ReturnType<typeof createSuperRequest>;
let superGet: ReturnType<typeof createSuperRequest>;
let examEnvironmentAuthorizationToken: string;
// Authenticate user
beforeAll(async () => {
const setCookies = await devLogin();
superPost = createSuperRequest({ method: 'POST', setCookies });
superGet = createSuperRequest({ method: 'GET', setCookies });
await mock.seedEnvExam();
// Add exam environment authorization token
const res = await superPost('/user/exam-environment/token');
@@ -532,15 +534,43 @@ describe('/exam-environment/', () => {
});
xdescribe('POST /exam-environment/screenshot', () => {});
describe('GET /exam-environment/exams', () => {
it('should return 200', async () => {
const res = await superGet('/exam-environment/exams').set(
'exam-environment-authorization-token',
examEnvironmentAuthorizationToken
);
expect(res.status).toBe(200);
expect(res.body).toStrictEqual({
data: {
exams: [
{
canTake: true,
config: {
name: mock.exam.config.name,
note: mock.exam.config.note,
totalTimeInMS: mock.exam.config.totalTimeInMS
},
id: mock.examId
}
]
}
});
});
});
});
describe('Authenticated user without exam environment authorization token', () => {
let superPost: ReturnType<typeof createSuperRequest>;
let superGet: ReturnType<typeof createSuperRequest>;
// Authenticate user
beforeAll(async () => {
const setCookies = await devLogin();
superPost = createSuperRequest({ method: 'POST', setCookies });
superGet = createSuperRequest({ method: 'GET', setCookies });
await mock.seedEnvExam();
});
describe('POST /exam-environment/exam/attempt', () => {
@@ -598,5 +628,16 @@ describe('/exam-environment/', () => {
});
});
});
describe('GET /exam-environment/exams', () => {
it('should return 403', async () => {
const res = await superGet('/exam-environment/exams').set(
'exam-environment-authorization-token',
'invalid-token'
);
expect(res.status).toBe(403);
});
});
});
});
@@ -23,6 +23,13 @@ import { ERRORS } from '../utils/errors';
*/
export const examEnvironmentValidatedTokenRoutes: FastifyPluginCallbackTypebox =
(fastify, _options, done) => {
fastify.get(
'/exam-environment/exams',
{
schema: schemas.examEnvironmentExams
},
getExams
);
fastify.post(
'/exam-environment/exam/generated-exam',
{
@@ -565,3 +572,37 @@ async function postScreenshotHandler(
) {
return reply.code(418);
}
async function getExams(
this: FastifyInstance,
req: UpdateReqType<typeof schemas.examEnvironmentExams>,
reply: FastifyReply
) {
const user = req.user!;
const exams = await this.prisma.envExam.findMany({
select: {
id: true,
config: true
}
});
const availableExams = exams.map(exam => {
const isExamPrerequisitesMet = checkPrerequisites(user, true);
return {
id: exam.id,
config: {
name: exam.config.name,
note: exam.config.note,
totalTimeInMS: exam.config.totalTimeInMS
},
canTake: isExamPrerequisitesMet
};
});
return reply.send({
data: {
exams: availableExams
}
});
}
+27
View File
@@ -0,0 +1,27 @@
import { Type } from '@fastify/type-provider-typebox';
import { STANDARD_ERROR } from '../utils/errors';
export const examEnvironmentExams = {
headers: Type.Object({
'exam-environment-authorization-token': Type.String()
}),
response: {
200: Type.Union([
Type.Object({
data: Type.Object({
exams: Type.Array(
Type.Object({
id: Type.String(),
config: Type.Object({
name: Type.String(),
note: Type.String(),
totalTimeInMS: Type.Number()
}),
canTake: Type.Boolean()
})
)
})
}),
STANDARD_ERROR
])
}
};
@@ -2,3 +2,4 @@ export { examEnvironmentPostExamAttempt } from './exam-attempt';
export { examEnvironmentPostExamGeneratedExam } from './exam-generated-exam';
export { examEnvironmentPostScreenshot } from './screenshot';
export { examEnvironmentTokenVerify } from './token-verify';
export { examEnvironmentExams } from './exams';