Merge remote-tracking branch 'origin/dev' into feat/endpoints-finaliseren-tests-backend-adriaan
# Conflicts: # backend/src/controllers/assignments.ts # backend/src/controllers/classes.ts # backend/src/controllers/groups.ts # backend/src/controllers/questions.ts # backend/src/controllers/submissions.ts # backend/src/data/assignments/submission-repository.ts # backend/src/data/users/student-repository.ts # backend/src/services/classes.ts # backend/src/services/submissions.ts # backend/tests/controllers/classes.test.ts
This commit is contained in:
		
						commit
						ded1a5908e
					
				
					 375 changed files with 23425 additions and 7033 deletions
				
			
		
							
								
								
									
										87
									
								
								backend/tests/controllers/answers.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								backend/tests/controllers/answers.test.ts
									
										
									
									
									
										Normal 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); | ||||
|     }); | ||||
| }); | ||||
|  | @ -1,10 +1,7 @@ | |||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; | ||||
| import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; | ||||
| import { createClassHandler, deleteClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { getAllClasses } from '../../src/services/class.js'; | ||||
| import { checkReturnList, checkReturn404 } from './qol.js'; | ||||
| 
 | ||||
| describe('Class controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|     let res: Partial<Response>; | ||||
|  | @ -15,8 +12,8 @@ describe('Class controllers', () => { | |||
|     beforeAll(async () => { | ||||
|         await setupTestApp(); | ||||
|     }); | ||||
|    | ||||
|     beforeEach(async () =>  { | ||||
| 
 | ||||
|     beforeEach(async () => { | ||||
|         jsonMock = vi.fn(); | ||||
|         statusMock = vi.fn().mockReturnThis(); | ||||
| 
 | ||||
|  | @ -26,13 +23,34 @@ describe('Class controllers', () => { | |||
|         }; | ||||
|     }); | ||||
| 
 | ||||
|     it('create and delete class', async () => { | ||||
|         req = { | ||||
|             body: { displayName: 'coole_nieuwe_klas' }, | ||||
|         }; | ||||
| 
 | ||||
|         await createClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log('class', result.class);
 | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() })); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { id: result.class.id }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 and error if class is not found', async () => { | ||||
|         req = { | ||||
|             params: { id: 'doesnotexist'}, | ||||
|         } | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
|      | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(404); | ||||
|         expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); | ||||
|     }); | ||||
|  | @ -43,7 +61,7 @@ describe('Class controllers', () => { | |||
|         } | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
|      | ||||
| 
 | ||||
|         // status can either not be called or called with code 200
 | ||||
|         expect( | ||||
|             statusMock.mock.calls.length === 0 || statusMock.mock.calls.some(([arg]) => arg === 200) | ||||
|  | @ -54,13 +72,13 @@ describe('Class controllers', () => { | |||
|         req = { | ||||
|             body: { displayName: 'coolenieuweklas' }, | ||||
|         }; | ||||
|          | ||||
| 
 | ||||
|         await createClassHandler(req as Request, res as Response); | ||||
|          | ||||
| 
 | ||||
|         expect(statusMock).toHaveBeenCalledWith(201); | ||||
|         // TODO: return json should be a classDTO and not named (fixed in #130)
 | ||||
|         //expect(jsonMock).toHaveBeenCalledWith();
 | ||||
|          | ||||
| 
 | ||||
|         // TODO: check if class is actually added to db
 | ||||
|     }); | ||||
|     it.todo('return json should be a classDTO and not named (fixed in #130)') | ||||
|  | @ -85,7 +103,7 @@ describe('Class controllers', () => { | |||
| 
 | ||||
|         await getClassStudentsHandler(req as Request, res as Response); | ||||
| 
 | ||||
| 		checkReturnList(jsonMock, 'students'); | ||||
|         checkReturnList(jsonMock, 'students'); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found when calling getClassStudentsHandler on a non-existent class', async () => { | ||||
|  | @ -109,12 +127,12 @@ describe('Class controllers', () => { | |||
|         await getTeacherInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith({"invitations": [ | ||||
|             { | ||||
|             "class": "id01", | ||||
|             "receiver": "LimpBizkit", | ||||
|             "sender": "FooFighters", | ||||
|             } | ||||
|         ]}); | ||||
|                 { | ||||
|                     "class": "id01", | ||||
|                     "receiver": "LimpBizkit", | ||||
|                     "sender": "FooFighters", | ||||
|                 } | ||||
|             ]}); | ||||
|     }); | ||||
| 
 | ||||
|     it('should return 404 not found when calling teacher-invitations on a non-existent class', async () => { | ||||
|  | @ -142,4 +160,5 @@ describe('Class controllers', () => { | |||
| 
 | ||||
|         expect("classes" in result).toBeTruthy(); | ||||
|     }) | ||||
| }) | ||||
| 
 | ||||
| }); | ||||
|  |  | |||
							
								
								
									
										117
									
								
								backend/tests/controllers/questions.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								backend/tests/controllers/questions.test.ts
									
										
									
									
									
										Normal 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); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										240
									
								
								backend/tests/controllers/students.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										240
									
								
								backend/tests/controllers/students.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,240 @@ | |||
| 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, | ||||
|     createStudentRequestHandler, | ||||
|     getStudentRequestsHandler, | ||||
|     deleteClassJoinRequestHandler, | ||||
|     getStudentRequestHandler, | ||||
| } from '../../src/controllers/students.js'; | ||||
| import { TEST_STUDENTS } from '../test_assets/users/students.testdata.js'; | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception.js'; | ||||
| import { BadRequestException } from '../../src/exceptions/bad-request-exception.js'; | ||||
| import { ConflictException } from '../../src/exceptions/conflict-exception.js'; | ||||
| import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js'; | ||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||
| import { getClass02 } from '../test_assets/classes/classes.testdata'; | ||||
| 
 | ||||
| describe('Student 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 student', async () => { | ||||
|         req = { params: { username: 'DireStraits' } }; | ||||
| 
 | ||||
|         await getStudentHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ student: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Student not found', async () => { | ||||
|         req = { params: { username: 'doesnotexist' } }; | ||||
| 
 | ||||
|         await expect(async () => getStudentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('No username', async () => { | ||||
|         req = { params: {} }; | ||||
| 
 | ||||
|         await expect(async () => getStudentHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create and delete student', async () => { | ||||
|         const student = { | ||||
|             id: 'coolstudent', | ||||
|             username: 'coolstudent', | ||||
|             firstName: 'New', | ||||
|             lastName: 'Student', | ||||
|         } as StudentDTO; | ||||
|         req = { | ||||
|             body: student, | ||||
|         }; | ||||
| 
 | ||||
|         await createStudentHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ student: expect.objectContaining(student) })); | ||||
| 
 | ||||
|         req = { params: { username: 'coolstudent' } }; | ||||
| 
 | ||||
|         await deleteStudentHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ student: expect.objectContaining(student) })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create duplicate student', async () => { | ||||
|         req = { | ||||
|             body: { | ||||
|                 username: 'DireStraits', | ||||
|                 firstName: 'dupe', | ||||
|                 lastName: 'dupe', | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createStudentHandler(req as Request, res as Response)).rejects.toThrowError(EntityAlreadyExistsException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create student no body', async () => { | ||||
|         req = { body: {} }; | ||||
| 
 | ||||
|         await expect(async () => createStudentHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     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: StudentDTO) => s.username); | ||||
|         expect(studentUsernames).toContain('DireStraits'); | ||||
| 
 | ||||
|         // Check length, +1 because of create
 | ||||
|         expect(result.students).toHaveLength(TEST_STUDENTS.length); | ||||
|     }); | ||||
| 
 | ||||
|     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() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.classes).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     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() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.groups).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Student submissions', async () => { | ||||
|         req = { params: { username: 'DireStraits' }, query: { full: 'true' } }; | ||||
| 
 | ||||
|         await getStudentSubmissionsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ submissions: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.submissions).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     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() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.questions).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Deleting non-existent student', async () => { | ||||
|         req = { params: { username: 'doesnotexist' } }; | ||||
| 
 | ||||
|         await expect(async () => deleteStudentHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get join requests by student', async () => { | ||||
|         req = { | ||||
|             params: { username: 'PinkFloyd' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentRequestsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith( | ||||
|             expect.objectContaining({ | ||||
|                 requests: expect.anything(), | ||||
|             }) | ||||
|         ); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log('[JOIN REQUESTS]', result.requests);
 | ||||
|         expect(result.requests.length).toBeGreaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get join request by student and class', async () => { | ||||
|         req = { | ||||
|             params: { username: 'PinkFloyd', classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith( | ||||
|             expect.objectContaining({ | ||||
|                 request: expect.anything(), | ||||
|             }) | ||||
|         ); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create and delete join request', async () => { | ||||
|         req = { | ||||
|             params: { username: 'TheDoors' }, | ||||
|             body: { classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await createStudentRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { username: 'TheDoors', classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteClassJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create join request student already in class error', async () => { | ||||
|         req = { | ||||
|             params: { username: 'Noordkaap' }, | ||||
|             body: { classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create join request duplicate', async () => { | ||||
|         req = { | ||||
|             params: { username: 'Tool' }, | ||||
|             body: { classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										124
									
								
								backend/tests/controllers/teacher-invitations.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								backend/tests/controllers/teacher-invitations.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,124 @@ | |||
| import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { | ||||
|     createInvitationHandler, | ||||
|     deleteInvitationHandler, | ||||
|     getAllInvitationsHandler, | ||||
|     getInvitationHandler, | ||||
|     updateInvitationHandler, | ||||
| } from '../../src/controllers/teacher-invitations'; | ||||
| import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invitation'; | ||||
| import { getClassHandler } from '../../src/controllers/classes'; | ||||
| import { BadRequestException } from '../../src/exceptions/bad-request-exception'; | ||||
| import { ClassStatus } from '@dwengo-1/common/util/class-join-request'; | ||||
| import { getClass02 } from '../test_assets/classes/classes.testdata'; | ||||
| 
 | ||||
| describe('Teacher 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 teacher invitations by', async () => { | ||||
|         req = { params: { username: 'LimpBizkit' }, query: { sent: 'true' } }; | ||||
| 
 | ||||
|         await getAllInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log(result.invitations);
 | ||||
|         expect(result.invitations).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get teacher invitations for', async () => { | ||||
|         req = { params: { username: 'FooFighters' }, query: { by: 'false' } }; | ||||
| 
 | ||||
|         await getAllInvitationsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitations: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.invitations).to.have.length.greaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create and delete invitation', async () => { | ||||
|         const body = { | ||||
|             sender: 'LimpBizkit', | ||||
|             receiver: 'testleerkracht1', | ||||
|             class: getClass02().classId, | ||||
|         } as TeacherInvitationData; | ||||
|         req = { body }; | ||||
| 
 | ||||
|         await createInvitationHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { | ||||
|                 sender: 'LimpBizkit', | ||||
|                 receiver: 'testleerkracht1', | ||||
|                 classId: getClass02().classId, | ||||
|             }, | ||||
|             body: { accepted: 'false' }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteInvitationHandler(req as Request, res as Response); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get invitation', async () => { | ||||
|         req = { | ||||
|             params: { | ||||
|                 sender: 'LimpBizkit', | ||||
|                 receiver: 'FooFighters', | ||||
|                 classId: getClass02().classId, | ||||
|             }, | ||||
|         }; | ||||
|         await getInvitationHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ invitation: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get invitation error', async () => { | ||||
|         req = { | ||||
|             params: { no: 'no params' }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => getInvitationHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Accept invitation', async () => { | ||||
|         const body = { | ||||
|             sender: 'LimpBizkit', | ||||
|             receiver: 'FooFighters', | ||||
|             class: getClass02().classId, | ||||
|         } as TeacherInvitationData; | ||||
|         req = { body }; | ||||
| 
 | ||||
|         await updateInvitationHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         const result1 = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result1.invitation.status).toEqual(ClassStatus.Accepted); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { | ||||
|                 id: getClass02().classId, | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         expect(result.class.teachers).toContain('FooFighters'); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										212
									
								
								backend/tests/controllers/teachers.test.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										212
									
								
								backend/tests/controllers/teachers.test.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,212 @@ | |||
| import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||||
| import { Request, Response } from 'express'; | ||||
| import { setupTestApp } from '../setup-tests.js'; | ||||
| import { NotFoundException } from '../../src/exceptions/not-found-exception.js'; | ||||
| import { | ||||
|     createTeacherHandler, | ||||
|     deleteTeacherHandler, | ||||
|     getAllTeachersHandler, | ||||
|     getStudentJoinRequestHandler, | ||||
|     getTeacherClassHandler, | ||||
|     getTeacherHandler, | ||||
|     getTeacherStudentHandler, | ||||
|     updateStudentJoinRequestHandler, | ||||
| } from '../../src/controllers/teachers.js'; | ||||
| import { BadRequestException } from '../../src/exceptions/bad-request-exception.js'; | ||||
| import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js'; | ||||
| import { getStudentRequestsHandler } from '../../src/controllers/students.js'; | ||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { getClassHandler } from '../../src/controllers/classes'; | ||||
| import { getClass02 } from '../test_assets/classes/classes.testdata'; | ||||
| 
 | ||||
| describe('Teacher 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 teacher', async () => { | ||||
|         req = { params: { username: 'FooFighters' } }; | ||||
| 
 | ||||
|         await getTeacherHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.anything() })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Teacher not found', async () => { | ||||
|         req = { params: { username: 'doesnotexist' } }; | ||||
| 
 | ||||
|         await expect(async () => getTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('No username', async () => { | ||||
|         req = { params: {} }; | ||||
| 
 | ||||
|         await expect(async () => getTeacherHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create and delete teacher', async () => { | ||||
|         const teacher = { | ||||
|             id: 'coolteacher', | ||||
|             username: 'coolteacher', | ||||
|             firstName: 'New', | ||||
|             lastName: 'Teacher', | ||||
|         }; | ||||
|         req = { | ||||
|             body: teacher, | ||||
|         }; | ||||
| 
 | ||||
|         await createTeacherHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) })); | ||||
| 
 | ||||
|         req = { params: { username: 'coolteacher' } }; | ||||
| 
 | ||||
|         await deleteTeacherHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teacher: expect.objectContaining(teacher) })); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create duplicate student', async () => { | ||||
|         req = { | ||||
|             body: { | ||||
|                 username: 'FooFighters', | ||||
|                 firstName: 'Dave', | ||||
|                 lastName: 'Grohl', | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createTeacherHandler(req as Request, res as Response)).rejects.toThrowError(EntityAlreadyExistsException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create teacher no body', async () => { | ||||
|         req = { body: {} }; | ||||
| 
 | ||||
|         await expect(async () => createTeacherHandler(req as Request, res as Response)).rejects.toThrowError(BadRequestException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Teacher list', async () => { | ||||
|         req = { query: { full: 'true' } }; | ||||
| 
 | ||||
|         await getAllTeachersHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ teachers: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
| 
 | ||||
|         const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username); | ||||
|         expect(teacherUsernames).toContain('testleerkracht1'); | ||||
| 
 | ||||
|         expect(result.teachers).toHaveLength(5); | ||||
|     }); | ||||
| 
 | ||||
|     it('Deleting non-existent student', async () => { | ||||
|         req = { params: { username: 'doesnotexist' } }; | ||||
| 
 | ||||
|         await expect(async () => deleteTeacherHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get teacher classes', async () => { | ||||
|         req = { | ||||
|             params: { username: 'testleerkracht1' }, | ||||
|             query: { full: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherClassHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log('[TEACHER CLASSES]', result);
 | ||||
|         expect(result.classes.length).toBeGreaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Get teacher students', async () => { | ||||
|         req = { | ||||
|             params: { username: 'testleerkracht1' }, | ||||
|             query: { full: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherStudentHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log('[TEACHER STUDENTS]', result.students);
 | ||||
|         expect(result.students.length).toBeGreaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     /* | ||||
| 
 | ||||
|     It('Get teacher questions', async () => { | ||||
|         req = { | ||||
|             params: { username: 'FooFighters' }, | ||||
|             query: { full: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getTeacherQuestionHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ questions: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // console.log('[TEACHER QUESTIONS]', result.questions);
 | ||||
|         expect(result.questions.length).toBeGreaterThan(0); | ||||
| 
 | ||||
|         // TODO fix
 | ||||
|     }); | ||||
| 
 | ||||
|      */ | ||||
| 
 | ||||
|     it('Get join requests by class', async () => { | ||||
|         req = { | ||||
|             params: { classId: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ joinRequests: expect.anything() })); | ||||
| 
 | ||||
|         const result = jsonMock.mock.lastCall?.[0]; | ||||
|         // Console.log('[JOIN REQUESTS FOR CLASS]', result.joinRequests);
 | ||||
|         expect(result.joinRequests.length).toBeGreaterThan(0); | ||||
|     }); | ||||
| 
 | ||||
|     it('Update join request status', async () => { | ||||
|         req = { | ||||
|             params: { classId: getClass02().classId, studentUsername: 'PinkFloyd' }, | ||||
|             body: { accepted: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|         await updateStudentJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { username: 'PinkFloyd' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getStudentRequestsHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         const status: boolean = jsonMock.mock.lastCall?.[0].requests[0].status; | ||||
|         expect(status).toBeTruthy(); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { id: getClass02().classId }, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
|         const students: string[] = jsonMock.mock.lastCall?.[0].class.students; | ||||
|         expect(students).contains('PinkFloyd'); | ||||
|     }); | ||||
| }); | ||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl