fix: backend controller tests

This commit is contained in:
laurejablonski 2025-04-27 14:24:26 +02:00
parent bf6f10c5cb
commit f97aab21fc
5 changed files with 136 additions and 65 deletions

View file

@ -5,6 +5,8 @@ import { Language } from '@dwengo-1/common/util/language';
import { getAllAnswersHandler, getAnswerHandler, updateAnswerHandler } from '../../src/controllers/answers'; import { getAllAnswersHandler, getAnswerHandler, updateAnswerHandler } from '../../src/controllers/answers';
import { BadRequestException } from '../../src/exceptions/bad-request-exception'; import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { NotFoundException } from '../../src/exceptions/not-found-exception'; import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { getQuestion02 } from '../test_assets/questions/questions.testdata';
import { getAnswer02 } from '../test_assets/questions/answers.testdata';
describe('Questions controllers', () => { describe('Questions controllers', () => {
let req: Partial<Request>; let req: Partial<Request>;
@ -24,9 +26,14 @@ describe('Questions controllers', () => {
}); });
it('Get answers list', async () => { it('Get answers list', async () => {
const a = getAnswer02();
req = { req = {
params: { hruid: 'id05', version: '1', seq: '2' }, params: {
query: { lang: Language.English, full: 'true' }, hruid: a.toQuestion.learningObjectHruid,
version: a.toQuestion.learningObjectVersion.toString(),
seq: a.toQuestion.sequenceNumber!.toString(),
},
query: { lang: a.toQuestion.learningObjectLanguage, full: 'true' },
}; };
await getAllAnswersHandler(req as Request, res as Response); await getAllAnswersHandler(req as Request, res as Response);
@ -38,9 +45,15 @@ describe('Questions controllers', () => {
}); });
it('Get answer', async () => { it('Get answer', async () => {
const a = getAnswer02();
req = { req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' }, params: {
query: { lang: Language.English, full: 'true' }, hruid: a.toQuestion.learningObjectHruid,
version: a.toQuestion.learningObjectVersion.toString(),
seq: a.toQuestion.sequenceNumber!.toString(),
seqAnswer: a.sequenceNumber!.toString(),
},
query: { lang: a.toQuestion.learningObjectLanguage, full: 'true' },
}; };
await getAnswerHandler(req as Request, res as Response); await getAnswerHandler(req as Request, res as Response);
@ -68,11 +81,19 @@ describe('Questions controllers', () => {
await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException); await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException);
}); });
it('Update question', async () => { it('Update answer', async () => {
const newContent = 'updated question'; const a = getAnswer02();
const q = a.toQuestion;
const newContent = 'updated answer';
req = { req = {
params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' }, params: {
query: { lang: Language.English }, hruid: q.learningObjectHruid,
version: q.learningObjectVersion.toString(),
seq: q.sequenceNumber!.toString(),
seqAnswer: a.sequenceNumber!.toString(),
},
query: { lang: q.learningObjectLanguage },
body: { content: newContent }, body: { content: newContent },
}; };

View file

@ -5,6 +5,7 @@ import { getAllQuestionsHandler, getQuestionHandler, updateQuestionHandler } fro
import { Language } from '@dwengo-1/common/util/language'; import { Language } from '@dwengo-1/common/util/language';
import { NotFoundException } from '../../src/exceptions/not-found-exception'; import { NotFoundException } from '../../src/exceptions/not-found-exception';
import { BadRequestException } from '../../src/exceptions/bad-request-exception'; import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { getQuestion01 } from '../test_assets/questions/questions.testdata';
describe('Questions controllers', () => { describe('Questions controllers', () => {
let req: Partial<Request>; let req: Partial<Request>;
@ -24,9 +25,10 @@ describe('Questions controllers', () => {
}); });
it('Get question list', async () => { it('Get question list', async () => {
const q = getQuestion01();
req = { req = {
params: { hruid: 'id05', version: '1' }, params: { hruid: q.learningObjectHruid, version: q.learningObjectVersion.toString() },
query: { lang: Language.English, full: 'true' }, query: { lang: q.learningObjectLanguage, full: 'true' },
}; };
await getAllQuestionsHandler(req as Request, res as Response); await getAllQuestionsHandler(req as Request, res as Response);
@ -38,9 +40,10 @@ describe('Questions controllers', () => {
}); });
it('Get question', async () => { it('Get question', async () => {
const q = getQuestion01();
req = { req = {
params: { hruid: 'id05', version: '1', seq: '1' }, params: { hruid: q.learningObjectHruid, version: q.learningObjectVersion.toString(), seq: q.sequenceNumber!.toString() },
query: { lang: Language.English, full: 'true' }, query: { lang: q.learningObjectLanguage, full: 'true' },
}; };
await getQuestionHandler(req as Request, res as Response); await getQuestionHandler(req as Request, res as Response);
@ -51,8 +54,9 @@ describe('Questions controllers', () => {
}); });
it('Get question with fallback sequence number and version', async () => { it('Get question with fallback sequence number and version', async () => {
const q = getQuestion01();
req = { req = {
params: { hruid: 'id05' }, params: { hruid: q.learningObjectHruid },
query: { lang: Language.English, full: 'true' }, query: { lang: Language.English, full: 'true' },
}; };
@ -99,10 +103,11 @@ describe('Questions controllers', () => {
*/ */
it('Update question', async () => { it('Update question', async () => {
const q = getQuestion01();
const newContent = 'updated question'; const newContent = 'updated question';
req = { req = {
params: { hruid: 'id05', version: '1', seq: '1' }, params: { hruid: q.learningObjectHruid, version: q.learningObjectVersion.toString(), seq: q.sequenceNumber!.toString() },
query: { lang: Language.English }, query: { lang: q.learningObjectLanguage },
body: { content: newContent }, body: { content: newContent },
}; };

View file

@ -15,12 +15,18 @@ import {
deleteClassJoinRequestHandler, deleteClassJoinRequestHandler,
getStudentRequestHandler, getStudentRequestHandler,
} from '../../src/controllers/students.js'; } from '../../src/controllers/students.js';
import { TEST_STUDENTS } from '../test_assets/users/students.testdata.js'; import { getDireStraits, getNoordkaap, getPinkFloyd, getTheDoors, TEST_STUDENTS } from '../test_assets/users/students.testdata.js';
import { NotFoundException } from '../../src/exceptions/not-found-exception.js'; import { NotFoundException } from '../../src/exceptions/not-found-exception.js';
import { BadRequestException } from '../../src/exceptions/bad-request-exception.js'; import { BadRequestException } from '../../src/exceptions/bad-request-exception.js';
import { ConflictException } from '../../src/exceptions/conflict-exception.js'; import { ConflictException } from '../../src/exceptions/conflict-exception.js';
import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js'; import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js';
import { StudentDTO } from '@dwengo-1/common/interfaces/student'; import { StudentDTO } from '@dwengo-1/common/interfaces/student';
import { getClass02 } from '../test_assets/classes/classes.testdata.js';
import { getClassJoinRequest02 } from '../test_assets/classes/class-join-requests.testdata.js';
import { getTestGroup01 } from '../test_assets/assignments/groups.testdata.js';
import { getSubmission01 } from '../test_assets/assignments/submission.testdata.js';
import { getQuestion } from '../../src/services/questions.js';
import { getQuestion01 } from '../test_assets/questions/questions.testdata.js';
describe('Student controllers', () => { describe('Student controllers', () => {
let req: Partial<Request>; let req: Partial<Request>;
@ -40,7 +46,8 @@ describe('Student controllers', () => {
}); });
it('Get student', async () => { it('Get student', async () => {
req = { params: { username: 'DireStraits' } }; const student = getDireStraits();
req = { params: { username: student.username } };
await getStudentHandler(req as Request, res as Response); await getStudentHandler(req as Request, res as Response);
@ -82,11 +89,12 @@ describe('Student controllers', () => {
}); });
it('Create duplicate student', async () => { it('Create duplicate student', async () => {
const student = getDireStraits();
req = { req = {
body: { body: {
username: 'DireStraits', username: student.username,
firstName: 'dupe', firstName: student.firstName,
lastName: 'dupe', lastName: student.lastName,
}, },
}; };
@ -110,14 +118,17 @@ describe('Student controllers', () => {
// Check is DireStraits is part of the student list // Check is DireStraits is part of the student list
const studentUsernames = result.students.map((s: StudentDTO) => s.username); const studentUsernames = result.students.map((s: StudentDTO) => s.username);
expect(studentUsernames).toContain('DireStraits');
expect(studentUsernames).toContain(TEST_STUDENTS[0].username);
// Check length, +1 because of create // Check length, +1 because of create
expect(result.students).toHaveLength(TEST_STUDENTS.length); expect(result.students).toHaveLength(TEST_STUDENTS.length);
}); });
it('Student classes', async () => { it('Student classes', async () => {
req = { params: { username: 'DireStraits' }, query: {} }; const class_ = getClass02();
const member = class_.students[0];
req = { params: { username: member.username }, query: {} };
await getStudentClassesHandler(req as Request, res as Response); await getStudentClassesHandler(req as Request, res as Response);
@ -128,7 +139,9 @@ describe('Student controllers', () => {
}); });
it('Student groups', async () => { it('Student groups', async () => {
req = { params: { username: 'DireStraits' }, query: {} }; const group = getTestGroup01();
const member = group.members[0];
req = { params: { username: member.username }, query: {} };
await getStudentGroupsHandler(req as Request, res as Response); await getStudentGroupsHandler(req as Request, res as Response);
@ -139,7 +152,8 @@ describe('Student controllers', () => {
}); });
it('Student submissions', async () => { it('Student submissions', async () => {
req = { params: { username: 'DireStraits' }, query: { full: 'true' } }; const submission = getSubmission01();
req = { params: { username: submission.submitter.username }, query: { full: 'true' } };
await getStudentSubmissionsHandler(req as Request, res as Response); await getStudentSubmissionsHandler(req as Request, res as Response);
@ -150,7 +164,8 @@ describe('Student controllers', () => {
}); });
it('Student questions', async () => { it('Student questions', async () => {
req = { params: { username: 'DireStraits' }, query: { full: 'true' } }; const question = getQuestion01();
req = { params: { username: question.author.username }, query: { full: 'true' } };
await getStudentQuestionsHandler(req as Request, res as Response); await getStudentQuestionsHandler(req as Request, res as Response);
@ -167,8 +182,9 @@ describe('Student controllers', () => {
}); });
it('Get join requests by student', async () => { it('Get join requests by student', async () => {
const jr = getClassJoinRequest02();
req = { req = {
params: { username: 'PinkFloyd' }, params: { username: jr.requester.username },
}; };
await getStudentRequestsHandler(req as Request, res as Response); await getStudentRequestsHandler(req as Request, res as Response);
@ -185,8 +201,9 @@ describe('Student controllers', () => {
}); });
it('Get join request by student and class', async () => { it('Get join request by student and class', async () => {
const jr = getClassJoinRequest02();
req = { req = {
params: { username: 'PinkFloyd', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, params: { username: jr.requester.username, classId: jr.class.classId! },
}; };
await getStudentRequestHandler(req as Request, res as Response); await getStudentRequestHandler(req as Request, res as Response);
@ -199,9 +216,11 @@ describe('Student controllers', () => {
}); });
it('Create and delete join request', async () => { it('Create and delete join request', async () => {
const student = getTheDoors();
const class_ = getClass02();
req = { req = {
params: { username: 'TheDoors' }, params: { username: student.username },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, body: { classId: class_.classId! },
}; };
await createStudentRequestHandler(req as Request, res as Response); await createStudentRequestHandler(req as Request, res as Response);
@ -209,7 +228,7 @@ describe('Student controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
req = { req = {
params: { username: 'TheDoors', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, params: { username: student.username, classId: class_.classId! },
}; };
await deleteClassJoinRequestHandler(req as Request, res as Response); await deleteClassJoinRequestHandler(req as Request, res as Response);
@ -220,18 +239,21 @@ describe('Student controllers', () => {
}); });
it('Create join request student already in class error', async () => { it('Create join request student already in class error', async () => {
const student = getNoordkaap();
const class_ = getClass02();
req = { req = {
params: { username: 'Noordkaap' }, params: { username: student.username },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, body: { classId: class_.classId! },
}; };
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);
}); });
it('Create join request duplicate', async () => { it('Create join request duplicate', async () => {
const jr = getClassJoinRequest02();
req = { req = {
params: { username: 'Tool' }, params: { username: jr.requester.username },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, body: { classId: jr.class.classId! },
}; };
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);

View file

@ -12,6 +12,10 @@ import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invit
import { getClassHandler } from '../../src/controllers/classes'; import { getClassHandler } from '../../src/controllers/classes';
import { BadRequestException } from '../../src/exceptions/bad-request-exception'; import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { ClassStatus } from '@dwengo-1/common/util/class-join-request'; import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
import { getTeacherInvitation01 } from '../test_assets/classes/teacher-invitations.testdata.js';
import { getLimpBizkit, getTestleerkracht1 } from '../test_assets/users/teachers.testdata.js';
import { getTestGroup01 } from '../test_assets/assignments/groups.testdata.js';
import { getClass02 } from '../test_assets/classes/classes.testdata.js';
describe('Teacher controllers', () => { describe('Teacher controllers', () => {
let req: Partial<Request>; let req: Partial<Request>;
@ -31,7 +35,8 @@ describe('Teacher controllers', () => {
}); });
it('Get teacher invitations by', async () => { it('Get teacher invitations by', async () => {
req = { params: { username: 'LimpBizkit' }, query: { sent: 'true' } }; const ti = getTeacherInvitation01();
req = { params: { username: ti.sender.username }, query: { sent: 'true' } };
await getAllInvitationsHandler(req as Request, res as Response); await getAllInvitationsHandler(req as Request, res as Response);
@ -43,7 +48,8 @@ describe('Teacher controllers', () => {
}); });
it('Get teacher invitations for', async () => { it('Get teacher invitations for', async () => {
req = { params: { username: 'FooFighters' }, query: { by: 'false' } }; const ti = getTeacherInvitation01();
req = { params: { username: ti.receiver.username }, query: { by: 'false' } };
await getAllInvitationsHandler(req as Request, res as Response); await getAllInvitationsHandler(req as Request, res as Response);
@ -54,10 +60,13 @@ describe('Teacher controllers', () => {
}); });
it('Create and delete invitation', async () => { it('Create and delete invitation', async () => {
const sender = getLimpBizkit();
const receiver = getTestleerkracht1();
const class_ = getClass02();
const body = { const body = {
sender: 'LimpBizkit', sender: sender.username,
receiver: 'testleerkracht1', receiver: receiver.username,
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', class: class_.classId,
} as TeacherInvitationData; } as TeacherInvitationData;
req = { body }; req = { body };
@ -65,9 +74,9 @@ describe('Teacher controllers', () => {
req = { req = {
params: { params: {
sender: 'LimpBizkit', sender: sender.username,
receiver: 'testleerkracht1', receiver: receiver.username,
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', classId: class_.classId!,
}, },
body: { accepted: 'false' }, body: { accepted: 'false' },
}; };
@ -76,11 +85,12 @@ describe('Teacher controllers', () => {
}); });
it('Get invitation', async () => { it('Get invitation', async () => {
const ti = getTeacherInvitation01();
req = { req = {
params: { params: {
sender: 'LimpBizkit', sender: ti.sender.username,
receiver: 'FooFighters', receiver: ti.receiver.username,
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', classId: ti.class.classId!,
}, },
}; };
await getInvitationHandler(req as Request, res as Response); await getInvitationHandler(req as Request, res as Response);
@ -97,10 +107,11 @@ describe('Teacher controllers', () => {
}); });
it('Accept invitation', async () => { it('Accept invitation', async () => {
const ti = getTeacherInvitation01();
const body = { const body = {
sender: 'LimpBizkit', sender: ti.sender.username,
receiver: 'FooFighters', receiver: ti.receiver.username,
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', class: ti.class.classId,
} as TeacherInvitationData; } as TeacherInvitationData;
req = { body }; req = { body };
@ -111,13 +122,13 @@ describe('Teacher controllers', () => {
req = { req = {
params: { params: {
id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', id: ti.class.classId!,
}, },
}; };
await getClassHandler(req as Request, res as Response); await getClassHandler(req as Request, res as Response);
const result = jsonMock.mock.lastCall?.[0]; const result = jsonMock.mock.lastCall?.[0];
expect(result.class.teachers).toContain('FooFighters'); expect(result.class.teachers).toContain(ti.receiver.username);
}); });
}); });

View file

@ -17,6 +17,10 @@ import { EntityAlreadyExistsException } from '../../src/exceptions/entity-alread
import { getStudentRequestsHandler } from '../../src/controllers/students.js'; import { getStudentRequestsHandler } from '../../src/controllers/students.js';
import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher';
import { getClassHandler } from '../../src/controllers/classes'; import { getClassHandler } from '../../src/controllers/classes';
import { getFooFighters, getTestleerkracht1 } from '../test_assets/users/teachers.testdata.js';
import { getClass02 } from '../test_assets/classes/classes.testdata.js';
import { getPinkFloyd, TEST_STUDENTS } from '../test_assets/users/students.testdata.js';
import { getClassJoinRequest01, getClassJoinRequest02 } from '../test_assets/classes/class-join-requests.testdata.js';
describe('Teacher controllers', () => { describe('Teacher controllers', () => {
let req: Partial<Request>; let req: Partial<Request>;
@ -36,7 +40,8 @@ describe('Teacher controllers', () => {
}); });
it('Get teacher', async () => { it('Get teacher', async () => {
req = { params: { username: 'FooFighters' } }; const teacher = getFooFighters();
req = { params: { username: teacher.username } };
await getTeacherHandler(req as Request, res as Response); await getTeacherHandler(req as Request, res as Response);
@ -77,12 +82,13 @@ describe('Teacher controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) })); expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) }));
}); });
it('Create duplicate student', async () => { it('Create duplicate teacher', async () => {
const teacher = getFooFighters();
req = { req = {
body: { body: {
username: 'FooFighters', username: teacher.username,
firstName: 'Dave', firstName: teacher.firstName,
lastName: 'Grohl', lastName: teacher.lastName,
}, },
}; };
@ -105,20 +111,23 @@ describe('Teacher controllers', () => {
const result = jsonMock.mock.lastCall?.[0]; const result = jsonMock.mock.lastCall?.[0];
const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username); const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username);
expect(teacherUsernames).toContain('testleerkracht1');
const teacher = getTestleerkracht1();
expect(teacherUsernames).toContain(teacher.username);
expect(result.teachers).toHaveLength(5); expect(result.teachers).toHaveLength(5);
}); });
it('Deleting non-existent student', async () => { it('Deleting non-existent teacher', async () => {
req = { params: { username: 'doesnotexist' } }; req = { params: { username: 'doesnotexist' } };
await expect(async () => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); await expect(async () => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException);
}); });
it('Get teacher classes', async () => { it('Get teacher classes', async () => {
const class_ = getClass02();
req = { req = {
params: { username: 'testleerkracht1' }, params: { username: class_.teachers[0].username },
query: { full: 'true' }, query: { full: 'true' },
}; };
@ -131,9 +140,10 @@ describe('Teacher controllers', () => {
expect(result.classes.length).toBeGreaterThan(0); expect(result.classes.length).toBeGreaterThan(0);
}); });
it('Get teacher students', async () => { it('Get teacher teachers', async () => {
const teacher = getTestleerkracht1();
req = { req = {
params: { username: 'testleerkracht1' }, params: { username: teacher.username },
query: { full: 'true' }, query: { full: 'true' },
}; };
@ -168,8 +178,9 @@ describe('Teacher controllers', () => {
*/ */
it('Get join requests by class', async () => { it('Get join requests by class', async () => {
const jr = getClassJoinRequest01();
req = { req = {
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, params: { classId: jr.class.classId! },
}; };
await getStudentJoinRequestHandler(req as Request, res as Response); await getStudentJoinRequestHandler(req as Request, res as Response);
@ -182,8 +193,9 @@ describe('Teacher controllers', () => {
}); });
it('Update join request status', async () => { it('Update join request status', async () => {
const jr = getClassJoinRequest01();
req = { req = {
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', studentUsername: 'PinkFloyd' }, params: { classId: jr.class.classId!, studentUsername: jr.requester.username },
body: { accepted: 'true' }, body: { accepted: 'true' },
}; };
@ -192,7 +204,7 @@ describe('Teacher controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
req = { req = {
params: { username: 'PinkFloyd' }, params: { username: jr.requester.username },
}; };
await getStudentRequestsHandler(req as Request, res as Response); await getStudentRequestsHandler(req as Request, res as Response);
@ -201,11 +213,11 @@ describe('Teacher controllers', () => {
expect(status).toBeTruthy(); expect(status).toBeTruthy();
req = { req = {
params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, params: { id: jr.class.classId! },
}; };
await getClassHandler(req as Request, res as Response); await getClassHandler(req as Request, res as Response);
const students: string[] = jsonMock.mock.lastCall?.[0].class.students; const students: string[] = jsonMock.mock.lastCall?.[0].class.students;
expect(students).contains('PinkFloyd'); expect(students).contains(jr.requester.username);
}); });
}); });