Merge remote-tracking branch 'origin/dev' into fix/class-join-request

# Conflicts:
#	backend/src/controllers/classes.ts
#	backend/src/data/questions/question-repository.ts
#	backend/tests/controllers/students.test.ts
#	backend/tests/controllers/teachers.test.ts
This commit is contained in:
Gabriellvl 2025-04-09 12:13:42 +02:00
commit 0a0857deb9
58 changed files with 2058 additions and 277 deletions

View file

@ -0,0 +1,87 @@
import { Request, Response } from 'express';
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { setupTestApp } from '../setup-tests';
import { Language } from '@dwengo-1/common/util/language';
import { getAllAnswersHandler, getAnswerHandler, updateAnswerHandler } from '../../src/controllers/answers';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
describe('Questions controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(() => {
jsonMock = vi.fn();
res = {
json: jsonMock,
};
});
it('Get answers list', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '2' },
query: { lang: Language.English, full: 'true' },
};
await getAllAnswersHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answers: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.answers);
expect(result.answers).to.have.length.greaterThan(1);
});
it('Get answer', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' },
query: { lang: Language.English, full: 'true' },
};
await getAnswerHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answer: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.answer);
});
it('Get answer hruid does not exist', async () => {
req = {
params: { hruid: 'id_not_exist' },
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('Get answer no hruid given', async () => {
req = {
params: {},
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
});
it('Update question', async () => {
const newContent = 'updated question';
req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' },
query: { lang: Language.English },
body: { content: newContent },
};
await updateAnswerHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ answer: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
expect(result.answer.content).to.eq(newContent);
});
});

View file

@ -0,0 +1,117 @@
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { Request, Response } from 'express';
import { setupTestApp } from '../setup-tests';
import { getAllQuestionsHandler, getQuestionHandler, updateQuestionHandler } from '../../src/controllers/questions';
import { Language } from '@dwengo-1/common/util/language';
import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
describe('Questions controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
let jsonMock: Mock;
beforeAll(async () => {
await setupTestApp();
});
beforeEach(() => {
jsonMock = vi.fn();
res = {
json: jsonMock,
};
});
it('Get question list', async () => {
req = {
params: { hruid: 'id05', version: '1' },
query: { lang: Language.English, full: 'true' },
};
await getAllQuestionsHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ questions: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.questions);
expect(result.questions).to.have.length.greaterThan(1);
});
it('Get question', async () => {
req = {
params: { hruid: 'id05', version: '1', seq: '1' },
query: { lang: Language.English, full: 'true' },
};
await getQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
});
it('Get question with fallback sequence number and version', async () => {
req = {
params: { hruid: 'id05' },
query: { lang: Language.English, full: 'true' },
};
await getQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
// Const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
});
it('Get question hruid does not exist', async () => {
req = {
params: { hruid: 'id_not_exist' },
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
});
it('Get question no hruid given', async () => {
req = {
params: {},
query: { lang: Language.English, full: 'true' },
};
await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
});
/*
It('Create and delete question', async() => {
req = {
params: { hruid: 'id05', version: '1', seq: '2'},
query: { lang: Language.English },
};
await deleteQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
console.log(result.question);
});
*/
it('Update question', async () => {
const newContent = 'updated question';
req = {
params: { hruid: 'id05', version: '1', seq: '1' },
query: { lang: Language.English },
body: { content: newContent },
};
await updateQuestionHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ question: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// Console.log(result.question);
expect(result.question.content).to.eq(newContent);
});
});

View file

@ -186,7 +186,7 @@ describe('Student controllers', () => {
it('Get join request by student and class', async () => {
req = {
params: { username: 'PinkFloyd', classId: 'id02' },
params: { username: 'PinkFloyd', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await getStudentRequestHandler(req as Request, res as Response);
@ -201,7 +201,7 @@ describe('Student controllers', () => {
it('Create and delete join request', async () => {
req = {
params: { username: 'TheDoors' },
body: { classId: 'id02' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await createStudentRequestHandler(req as Request, res as Response);
@ -209,7 +209,7 @@ describe('Student controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
req = {
params: { username: 'TheDoors', classId: 'id02' },
params: { username: 'TheDoors', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await deleteClassJoinRequestHandler(req as Request, res as Response);
@ -231,7 +231,7 @@ describe('Student controllers', () => {
it('Create join request duplicate', async () => {
req = {
params: { username: 'Tool' },
body: { classId: 'id02' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);

View file

@ -105,9 +105,9 @@ describe('Teacher controllers', () => {
const result = jsonMock.mock.lastCall?.[0];
const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username);
expect(teacherUsernames).toContain('FooFighters');
expect(teacherUsernames).toContain('testleerkracht1');
expect(result.teachers).toHaveLength(4);
expect(result.teachers).toHaveLength(5);
});
it('Deleting non-existent student', async () => {
@ -118,7 +118,7 @@ describe('Teacher controllers', () => {
it('Get teacher classes', async () => {
req = {
params: { username: 'FooFighters' },
params: { username: 'testleerkracht1' },
query: { full: 'true' },
};
@ -133,7 +133,7 @@ describe('Teacher controllers', () => {
it('Get teacher students', async () => {
req = {
params: { username: 'FooFighters' },
params: { username: 'testleerkracht1' },
query: { full: 'true' },
};
@ -169,7 +169,7 @@ describe('Teacher controllers', () => {
it('Get join requests by class', async () => {
req = {
params: { classId: 'id02' },
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await getStudentJoinRequestHandler(req as Request, res as Response);
@ -183,7 +183,7 @@ describe('Teacher controllers', () => {
it('Update join request status', async () => {
req = {
params: { classId: 'id02', studentUsername: 'PinkFloyd' },
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
body: { accepted: 'true' },
};
@ -201,7 +201,7 @@ describe('Teacher controllers', () => {
expect(status).toBeTruthy();
req = {
params: { id: 'id02' },
params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
};
await getClassHandler(req as Request, res as Response);

View file

@ -15,7 +15,7 @@ describe('AssignmentRepository', () => {
});
it('should return the requested assignment', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignment = await assignmentRepository.findByClassAndId(class_!, 2);
expect(assignment).toBeTruthy();
@ -23,7 +23,7 @@ describe('AssignmentRepository', () => {
});
it('should return all assignments for a class', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignments = await assignmentRepository.findAllAssignmentsInClass(class_!);
expect(assignments).toBeTruthy();

View file

@ -18,7 +18,7 @@ describe('GroupRepository', () => {
});
it('should return the requested group', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment!, 1);
@ -27,7 +27,7 @@ describe('GroupRepository', () => {
});
it('should return all groups for assignment', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const groups = await groupRepository.findAllGroupsForAssignment(assignment!);
@ -37,7 +37,7 @@ describe('GroupRepository', () => {
});
it('should not find removed group', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const assignment = await assignmentRepository.findByClassAndId(class_!, 2);
await groupRepository.deleteByAssignmentAndGroupNumber(assignment!, 1);

View file

@ -50,7 +50,7 @@ describe('SubmissionRepository', () => {
it('should find the most recent submission for a group', async () => {
const id = new LearningObjectIdentifier('id03', Language.English, 1);
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const assignment = await assignmentRepository.findByClassAndId(class_!, 1);
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment!, 1);
const submission = await submissionRepository.findMostRecentSubmissionForGroup(id, group!);

View file

@ -26,7 +26,7 @@ describe('ClassJoinRequestRepository', () => {
});
it('should list all requests to a single class', async () => {
const class_ = await cassRepository.findById('id02');
const class_ = await cassRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const requests = await classJoinRequestRepository.findAllOpenRequestsTo(class_!);
expect(requests).toBeTruthy();
@ -35,7 +35,7 @@ describe('ClassJoinRequestRepository', () => {
it('should not find a removed request', async () => {
const student = await studentRepository.findByUsername('SmashingPumpkins');
const class_ = await cassRepository.findById('id03');
const class_ = await cassRepository.findById('80dcc3e0-1811-4091-9361-42c0eee91cfa');
await classJoinRequestRepository.deleteBy(student!, class_!);
const request = await classJoinRequestRepository.findAllRequestsBy(student!);

View file

@ -18,16 +18,16 @@ describe('ClassRepository', () => {
});
it('should return requested class', async () => {
const classVar = await classRepository.findById('id01');
const classVar = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
expect(classVar).toBeTruthy();
expect(classVar?.displayName).toBe('class01');
});
it('class should be gone after deletion', async () => {
await classRepository.deleteById('id04');
await classRepository.deleteById('33d03536-83b8-4880-9982-9bbf2f908ddf');
const classVar = await classRepository.findById('id04');
const classVar = await classRepository.findById('33d03536-83b8-4880-9982-9bbf2f908ddf');
expect(classVar).toBeNull();
});

View file

@ -34,7 +34,7 @@ describe('ClassRepository', () => {
});
it('should return all invitations for a class', async () => {
const class_ = await classRepository.findById('id02');
const class_ = await classRepository.findById('34d484a1-295f-4e9f-bfdc-3e7a23d86a89');
const invitations = await teacherInvitationRepository.findAllInvitationsForClass(class_!);
expect(invitations).toBeTruthy();
@ -42,7 +42,7 @@ describe('ClassRepository', () => {
});
it('should not find a removed invitation', async () => {
const class_ = await classRepository.findById('id01');
const class_ = await classRepository.findById('8764b861-90a6-42e5-9732-c0d9eb2f55f9');
const sender = await teacherRepository.findByUsername('FooFighters');
const receiver = await teacherRepository.findByUsername('LimpBizkit');
await teacherInvitationRepository.deleteBy(class_!, sender!, receiver!);

View file

@ -7,11 +7,11 @@ import learningObjectService from '../../../src/services/learning-objects/learni
import { envVars, getEnvVar } from '../../../src/util/envVars';
import { LearningPath } from '../../../src/entities/content/learning-path.entity';
import learningPathExample from '../../test-assets/learning-paths/pn-werking-example';
import { LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
import { Language } from '@dwengo-1/common/util/language';
const EXPECTED_DWENGO_LEARNING_OBJECT_TITLE = 'Werken met notebooks';
const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifier = {
const DWENGO_TEST_LEARNING_OBJECT_ID: LearningObjectIdentifierDTO = {
hruid: 'pn_werkingnotebooks',
language: Language.Dutch,
version: 3,

View file

@ -4,11 +4,11 @@ import { Student } from '../../../src/entities/users/student.entity';
import { Teacher } from '../../../src/entities/users/teacher.entity';
export function makeTestClasses(em: EntityManager, students: Student[], teachers: Teacher[]): Class[] {
const studentsClass01 = students.slice(0, 7);
const teacherClass01: Teacher[] = teachers.slice(0, 1);
const studentsClass01 = students.slice(0, 8);
const teacherClass01: Teacher[] = teachers.slice(4, 5);
const class01 = em.create(Class, {
classId: 'id01',
classId: '8764b861-90a6-42e5-9732-c0d9eb2f55f9',
displayName: 'class01',
teachers: teacherClass01,
students: studentsClass01,
@ -18,7 +18,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass02: Teacher[] = teachers.slice(1, 2);
const class02 = em.create(Class, {
classId: 'id02',
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
displayName: 'class02',
teachers: teacherClass02,
students: studentsClass02,
@ -28,7 +28,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass03: Teacher[] = teachers.slice(2, 3);
const class03 = em.create(Class, {
classId: 'id03',
classId: '80dcc3e0-1811-4091-9361-42c0eee91cfa',
displayName: 'class03',
teachers: teacherClass03,
students: studentsClass03,
@ -38,7 +38,7 @@ export function makeTestClasses(em: EntityManager, students: Student[], teachers
const teacherClass04: Teacher[] = teachers.slice(2, 3);
const class04 = em.create(Class, {
classId: 'id04',
classId: '33d03536-83b8-4880-9982-9bbf2f908ddf',
displayName: 'class04',
teachers: teacherClass04,
students: studentsClass04,

View file

@ -11,6 +11,8 @@ export const TEST_STUDENTS = [
{ username: 'TheDoors', firstName: 'Jim', lastName: 'Morisson' },
// ⚠️ Deze mag niet gebruikt worden in elke test!
{ username: 'Nirvana', firstName: 'Kurt', lastName: 'Cobain' },
// Makes sure when logged in as leerling1, there exists a corresponding user
{ username: 'testleerling1', firstName: 'Gerald', lastName: 'Schmittinger' },
];
// 🏗️ Functie die ORM entities maakt uit de data array

View file

@ -27,5 +27,12 @@ export function makeTestTeachers(em: EntityManager): Teacher[] {
lastName: 'Cappelle',
});
return [teacher01, teacher02, teacher03, teacher04];
// Makes sure when logged in as testleerkracht1, there exists a corresponding user
const teacher05 = em.create(Teacher, {
username: 'testleerkracht1',
firstName: 'Bob',
lastName: 'Dylan',
});
return [teacher01, teacher02, teacher03, teacher04, teacher05];
}