From 5490bd6b869cda49b455d0afd4203b5f01d029ae Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Mon, 24 Mar 2025 11:36:05 +0100 Subject: [PATCH] feat: test controller student --- backend/src/controllers/students.ts | 1 + backend/src/services/students.ts | 8 +- backend/tests/controllers/student.test.ts | 144 ++++++++++++++++++ .../test_assets/users/students.testdata.ts | 59 ++----- 4 files changed, 165 insertions(+), 47 deletions(-) create mode 100644 backend/tests/controllers/student.test.ts diff --git a/backend/src/controllers/students.ts b/backend/src/controllers/students.ts index cde6b5ef..8dbf3da9 100644 --- a/backend/src/controllers/students.ts +++ b/backend/src/controllers/students.ts @@ -141,6 +141,7 @@ export async function getStudentSubmissionsHandler(req: Request, res: Response): } export async function getStudentQuestionsHandler(req: Request, res: Response): Promise { + const full = req.query.full === 'true'; const username = req.params.username; if (!username) { diff --git a/backend/src/services/students.ts b/backend/src/services/students.ts index 70bed549..63339874 100644 --- a/backend/src/services/students.ts +++ b/backend/src/services/students.ts @@ -138,8 +138,10 @@ export async function getStudentQuestions(username: string, full: boolean): Prom const questionRepository = getQuestionRepository(); const questions = await questionRepository.findAllByAuthor(student); - if (full) - return questions.map(mapToQuestionDTO) + const questionsDTO = questions.map(mapToQuestionDTO); - return questions.map(mapToQuestionId); + if (full) + return questionsDTO; + + return questionsDTO.map(mapToQuestionId); } diff --git a/backend/tests/controllers/student.test.ts b/backend/tests/controllers/student.test.ts new file mode 100644 index 00000000..d3df83c5 --- /dev/null +++ b/backend/tests/controllers/student.test.ts @@ -0,0 +1,144 @@ +import { setupTestApp } from '../setup-tests.js'; +import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; +import { Request, Response } from 'express'; +import { + getAllStudentsHandler, + getStudentHandler, + createStudentHandler, + deleteStudentHandler, + getStudentClassesHandler, + getStudentGroupsHandler, + getStudentSubmissionsHandler, + getStudentQuestionsHandler +} from '../../src/controllers/students.js'; +import {TEST_STUDENTS} from "../test_assets/users/students.testdata"; + +describe('Student controllers', () => { + let req: Partial; + let res: Partial; + + let jsonMock: Mock; + let statusMock: Mock; + + beforeAll(async () => { + await setupTestApp(); + }); + + beforeEach(() => { + jsonMock = vi.fn(); + statusMock = vi.fn().mockReturnThis(); + res = { + json: jsonMock, + status: statusMock, + }; + }); + + it('Student not found 404', async () => { + req = { params: { username: 'doesnotexist' } }; + + await getStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalled(); + }); + + it('No username 400', async () => { + req = { params: {} }; + + await getStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(400); + expect(jsonMock).toHaveBeenCalled(); + }); + + it('Create student', async () => { + req = { + body: { + username: 'coolstudent', + firstName: 'Cool', + lastName: 'Student' + } + }; + + await createStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(201); + expect(jsonMock).toHaveBeenCalled(); + }); + + it('Create student no body 400', async () => { + req = { body: {} }; + + await createStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(400); + expect(jsonMock).toHaveBeenCalled(); + }); + + it('Student list', async () => { + req = { query: { full: 'true' } }; + + await getAllStudentsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() })); + + const result = jsonMock.mock.lastCall?.[0]; + + // check is DireStraits is part of the student list + const studentUsernames = result.students.map((s: any) => s.username); + expect(studentUsernames).toContain('DireStraits'); + + // check length, +1 because of create + expect(result.students).toHaveLength(TEST_STUDENTS.length + 1); + }); + + it('Student classes', async () => { + req = { params: { username: 'DireStraits' }, query: {} }; + + await getStudentClassesHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() })); + }); + + it('Student groups', async () => { + req = { params: { username: 'DireStraits' }, query: {} }; + + await getStudentGroupsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ groups: expect.anything() })); + }); + + it('Student submissions', async () => { + req = { params: { username: 'DireStraits' } }; + + await getStudentSubmissionsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() })); + }); + + it('Student questions', async () => { + req = { params: { username: 'DireStraits' }, query: { full: 'true' } }; + + await getStudentQuestionsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ questions: expect.anything() })); + }); + + it('Delete student', async () => { + req = { params: { username: 'coolstudent' } }; + + await deleteStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(200); + expect(jsonMock).toHaveBeenCalled(); + }); + + it('Deleting non-existent student 404', async () => { + req = { params: { username: 'doesnotexist' } }; + + await deleteStudentHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalled(); + }); +}); diff --git a/backend/tests/test_assets/users/students.testdata.ts b/backend/tests/test_assets/users/students.testdata.ts index 61e0b590..b86ef0d0 100644 --- a/backend/tests/test_assets/users/students.testdata.ts +++ b/backend/tests/test_assets/users/students.testdata.ts @@ -1,49 +1,20 @@ import { Connection, EntityManager, IDatabaseDriver } from '@mikro-orm/core'; import { Student } from '../../../src/entities/users/student.entity'; -export function makeTestStudents(em: EntityManager>): Array { - const student01 = em.create(Student, { - username: 'Noordkaap', - firstName: 'Stijn', - lastName: 'Meuris', - }); +// 🔓 Ruwe testdata array — herbruikbaar in assertions +export const TEST_STUDENTS = [ + { username: 'Noordkaap', firstName: 'Stijn', lastName: 'Meuris' }, + { username: 'DireStraits', firstName: 'Mark', lastName: 'Knopfler' }, + { username: 'Tool', firstName: 'Maynard', lastName: 'Keenan' }, + { username: 'SmashingPumpkins', firstName: 'Billy', lastName: 'Corgan' }, + { username: 'PinkFloyd', firstName: 'David', lastName: 'Gilmoure' }, + { username: 'TheDoors', firstName: 'Jim', lastName: 'Morisson' }, + // ⚠️ Deze mag niet gebruikt worden in elke test! + { username: 'Nirvana', firstName: 'Kurt', lastName: 'Cobain' }, +]; - const student02 = em.create(Student, { - username: 'DireStraits', - firstName: 'Mark', - lastName: 'Knopfler', - }); - - const student03 = em.create(Student, { - username: 'Tool', - firstName: 'Maynard', - lastName: 'Keenan', - }); - - const student04 = em.create(Student, { - username: 'SmashingPumpkins', - firstName: 'Billy', - lastName: 'Corgan', - }); - - const student05 = em.create(Student, { - username: 'PinkFloyd', - firstName: 'David', - lastName: 'Gilmoure', - }); - - const student06 = em.create(Student, { - username: 'TheDoors', - firstName: 'Jim', - lastName: 'Morisson', - }); - - // Do not use for any tests, gets deleted in a unit test - const student07 = em.create(Student, { - username: 'Nirvana', - firstName: 'Kurt', - lastName: 'Cobain', - }); - - return [student01, student02, student03, student04, student05, student06, student07]; +// 🏗️ Functie die ORM entities maakt uit de data array +export function makeTestStudents(em: EntityManager>): Student[] { + return TEST_STUDENTS.map(data => em.create(Student, data)); } +