fix: .js + sendStatusMock in backend controller

This commit is contained in:
Gabriellvl 2025-03-30 22:58:08 +02:00
parent 44c242fc57
commit 82c2197950
8 changed files with 68 additions and 62 deletions

View file

@ -11,7 +11,7 @@ import {
getStudentSubmissions,
} from '../services/students.js';
import { StudentDTO } from '../interfaces/student.js';
import {requireFields} from "./error-helper";
import {requireFields} from "./error-helper.js";
export async function getAllStudentsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';

View file

@ -12,7 +12,7 @@ import { ClassDTO } from '../interfaces/class.js';
import { StudentDTO } from '../interfaces/student.js';
import { QuestionDTO, QuestionId } from '../interfaces/question.js';
import { TeacherDTO } from '../interfaces/teacher.js';
import {requireFields} from "./error-helper";
import {requireFields} from "./error-helper.js";
export async function getAllTeachersHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true';
@ -93,7 +93,7 @@ export async function getStudentJoinRequestHandler(req: Request, res: Response)
export async function updateStudentJoinRequestHandler(req: Request, res: Response) {
const studentUsername = req.query.studentUsername as string;
const classId = req.params.classId;
const accepted = req.body.accepted !== 'false'; // default = true
const accepted = req.body.accepted !== 'false'; // Default = true
requireFields({ studentUsername, classId });
await updateClassJoinRequestStatus(studentUsername, classId, accepted);

View file

@ -12,18 +12,18 @@ import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js
import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js';
import { getAllAssignments } from './assignments.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question';
import {mapToStudentRequest, mapToStudentRequestDTO} from "../interfaces/student-request";
import {Student} from "../entities/users/student.entity";
import {NotFoundException} from "../exceptions/not-found-exception";
import {fetchClass} from "./classes";
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import {mapToStudentRequest, mapToStudentRequestDTO} from "../interfaces/student-request.js";
import {Student} from "../entities/users/student.entity.js";
import {NotFoundException} from "../exceptions/not-found-exception.js";
import {fetchClass} from "./classes.js";
export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> {
const studentRepository = getStudentRepository();
const users = await studentRepository.findAll();
if (full)
return users.map(mapToStudentDTO);
{return users.map(mapToStudentDTO);}
return users.map((user) => user.username);
}
@ -54,7 +54,7 @@ export async function createStudent(userData: StudentDTO): Promise<void> {
export async function deleteStudent(username: string): Promise<void> {
const studentRepository = getStudentRepository();
await fetchStudent(username); // throws error if it does not exist
await fetchStudent(username); // Throws error if it does not exist
await studentRepository.deleteByUsername(username);
}
@ -116,16 +116,15 @@ export async function getStudentQuestions(username: string, full: boolean): Prom
const questionsDTO = questions.map(mapToQuestionDTO);
if (full)
return questionsDTO;
{return questionsDTO;}
return questionsDTO.map(mapToQuestionId);
}
export async function createClassJoinRequest(studentUsername: string, classId: string) {
const classRepo = getClassRepository();
const requestRepo = getClassJoinRequestRepository();
const student = await fetchStudent(studentUsername); // throws error if student not found
const student = await fetchStudent(studentUsername); // Throws error if student not found
const cls = await fetchClass(classId);
const request = mapToStudentRequest(student, cls);

View file

@ -1,4 +1,4 @@
import { getGroupRepository, getSubmissionRepository } from '../data/repositories.js';
import { getSubmissionRepository } from '../data/repositories.js';
import { Language } from '../entities/content/language.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { mapToSubmission, mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js';

View file

@ -8,22 +8,22 @@ import {
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from '../interfaces/question.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
import {Teacher} from "../entities/users/teacher.entity";
import {fetchStudent} from "./students";
import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity";
import {mapToStudentRequestDTO, StudentRequestDTO} from "../interfaces/student-request";
import {TeacherRepository} from "../data/users/teacher-repository";
import {ClassRepository} from "../data/classes/class-repository";
import {Class} from "../entities/classes/class.entity";
import {StudentDTO} from "../interfaces/student";
import {LearningObjectRepository} from "../data/content/learning-object-repository";
import {LearningObject} from "../entities/content/learning-object.entity";
import {QuestionRepository} from "../data/questions/question-repository";
import {Question} from "../entities/questions/question.entity";
import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository";
import {Student} from "../entities/users/student.entity";
import {NotFoundException} from "../exceptions/not-found-exception";
import {getClassStudents} from "./classes";
import {Teacher} from "../entities/users/teacher.entity.js";
import {fetchStudent} from "./students.js";
import {ClassJoinRequest, ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity.js";
import {mapToStudentRequestDTO, StudentRequestDTO} from "../interfaces/student-request.js";
import {TeacherRepository} from "../data/users/teacher-repository.js";
import {ClassRepository} from "../data/classes/class-repository.js";
import {Class} from "../entities/classes/class.entity.js";
import {StudentDTO} from "../interfaces/student.js";
import {LearningObjectRepository} from "../data/content/learning-object-repository.js";
import {LearningObject} from "../entities/content/learning-object.entity.js";
import {QuestionRepository} from "../data/questions/question-repository.js";
import {Question} from "../entities/questions/question.entity.js";
import {ClassJoinRequestRepository} from "../data/classes/class-join-request-repository.js";
import {Student} from "../entities/users/student.entity.js";
import {NotFoundException} from "../exceptions/not-found-exception.js";
import {getClassStudents} from "./classes.js";
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository: TeacherRepository = getTeacherRepository();
@ -61,7 +61,7 @@ export async function createTeacher(userData: TeacherDTO): Promise<void> {
export async function deleteTeacher(username: string): Promise<void> {
const teacherRepository: TeacherRepository = getTeacherRepository();
await fetchTeacher(username); // throws error if it does not exist
await fetchTeacher(username); // Throws error if it does not exist
await teacherRepository.deleteByUsername(username);
}
@ -106,7 +106,7 @@ export async function getTeacherQuestions(username: string, full: boolean): Prom
const learningObjectRepository: LearningObjectRepository = getLearningObjectRepository();
const learningObjects: LearningObject[] = await learningObjectRepository.findAllByTeacher(teacher);
// console.log(learningObjects)
// Console.log(learningObjects)
// TODO returns empty
if (!learningObjects || learningObjects.length === 0){

View file

@ -14,11 +14,12 @@ import {
getStudentRequestHandler,
deleteClassJoinRequestHandler
} from '../../src/controllers/students.js';
import {TEST_STUDENTS} from "../test_assets/users/students.testdata";
import {NotFoundException} from "../../src/exceptions/not-found-exception";
import {BadRequestException} from "../../src/exceptions/bad-request-exception";
import {ConflictException} from "../../src/exceptions/conflict-exception";
import {EntityAlreadyExistsException} from "../../src/exceptions/entity-already-exists-exception";
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 "../../src/interfaces/student.js";
describe('Student controllers', () => {
let req: Partial<Request>;
@ -26,6 +27,7 @@ describe('Student controllers', () => {
let jsonMock: Mock;
let statusMock: Mock;
let sendStatusMock: Mock;
beforeAll(async () => {
await setupTestApp();
@ -34,9 +36,11 @@ describe('Student controllers', () => {
beforeEach(() => {
jsonMock = vi.fn();
statusMock = vi.fn().mockReturnThis();
sendStatusMock = vi.fn().mockReturnThis();
res = {
json: jsonMock,
status: statusMock,
sendStatus: sendStatusMock,
};
});
@ -75,13 +79,13 @@ describe('Student controllers', () => {
await createStudentHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(201);
expect(sendStatusMock).toHaveBeenCalledWith(201);
req = { params: { username: 'coolstudent' } };
await deleteStudentHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(200);
expect(sendStatusMock).toHaveBeenCalledWith(200);
});
@ -116,11 +120,11 @@ describe('Student controllers', () => {
const result = jsonMock.mock.lastCall?.[0];
// check is DireStraits is part of the student list
const studentUsernames = result.students.map((s: any) => s.username);
// 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
// Check length, +1 because of create
expect(result.students).toHaveLength(TEST_STUDENTS.length);
});
@ -192,7 +196,7 @@ describe('Student controllers', () => {
);
const result = jsonMock.mock.lastCall?.[0];
// console.log('[JOIN REQUESTS]', result.requests);
// Console.log('[JOIN REQUESTS]', result.requests);
expect(result.requests.length).toBeGreaterThan(0);
});
@ -204,7 +208,7 @@ describe('Student controllers', () => {
await createStudentRequestHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(201);
expect(sendStatusMock).toHaveBeenCalledWith(201);
});
it('Create join request duplicate', async () => {
@ -226,7 +230,7 @@ describe('Student controllers', () => {
await deleteClassJoinRequestHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(204);
expect(sendStatusMock).toHaveBeenCalledWith(204);
await expect(() => deleteClassJoinRequestHandler(req as Request, res as Response))
.rejects

View file

@ -1,16 +1,17 @@
import {beforeAll, beforeEach, describe, expect, it, Mock, vi} from "vitest";
import {Request, Response} from "express";
import {setupTestApp} from "../setup-tests";
import {NotFoundException} from "../../src/exceptions/not-found-exception";
import {setupTestApp} from "../setup-tests.js";
import {NotFoundException} from "../../src/exceptions/not-found-exception.js";
import {
createTeacherHandler,
deleteTeacherHandler,
getAllTeachersHandler, getStudentJoinRequestHandler, getTeacherClassHandler,
getTeacherHandler, getTeacherQuestionHandler, getTeacherStudentHandler, updateStudentJoinRequestHandler
} from "../../src/controllers/teachers";
import {BadRequestException} from "../../src/exceptions/bad-request-exception";
import {EntityAlreadyExistsException} from "../../src/exceptions/entity-already-exists-exception";
import {getStudentRequestHandler} from "../../src/controllers/students";
} 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 {getStudentRequestHandler} from "../../src/controllers/students.js";
import {TeacherDTO} from "../../src/interfaces/teacher.js";
describe('Teacher controllers', () => {
let req: Partial<Request>;
@ -18,6 +19,7 @@ describe('Teacher controllers', () => {
let jsonMock: Mock;
let statusMock: Mock;
let sendStatusMock: Mock;
beforeAll(async () => {
await setupTestApp();
@ -26,9 +28,11 @@ describe('Teacher controllers', () => {
beforeEach(() => {
jsonMock = vi.fn();
statusMock = vi.fn().mockReturnThis();
sendStatusMock = vi.fn().mockReturnThis();
res = {
json: jsonMock,
status: statusMock,
sendStatus: sendStatusMock,
};
});
@ -67,13 +71,13 @@ describe('Teacher controllers', () => {
await createTeacherHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(201);
expect(sendStatusMock).toHaveBeenCalledWith(201);
req = { params: { username: 'coolteacher' } };
await deleteTeacherHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(200);
expect(sendStatusMock).toHaveBeenCalledWith(200);
});
it('Create duplicate student', async () => {
@ -107,7 +111,7 @@ describe('Teacher controllers', () => {
const result = jsonMock.mock.lastCall?.[0];
const teacherUsernames = result.teachers.map((s: any) => s.username);
const teacherUsernames = result.teachers.map((s: TeacherDTO) => s.username);
expect(teacherUsernames).toContain('FooFighters');
expect(result.teachers).toHaveLength(4);
@ -133,7 +137,7 @@ describe('Teacher controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ classes: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// console.log('[TEACHER CLASSES]', result);
// Console.log('[TEACHER CLASSES]', result);
expect(result.classes.length).toBeGreaterThan(0);
});
@ -148,13 +152,13 @@ describe('Teacher controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ students: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// console.log('[TEACHER STUDENTS]', result.students);
// Console.log('[TEACHER STUDENTS]', result.students);
expect(result.students.length).toBeGreaterThan(0);
});
/*
it('Get teacher questions', async () => {
It('Get teacher questions', async () => {
req = {
params: { username: 'FooFighters' },
query: { full: 'true' },
@ -184,7 +188,7 @@ describe('Teacher controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ joinRequests: expect.anything() }));
const result = jsonMock.mock.lastCall?.[0];
// console.log('[JOIN REQUESTS FOR CLASS]', result.joinRequests);
// Console.log('[JOIN REQUESTS FOR CLASS]', result.joinRequests);
expect(result.joinRequests.length).toBeGreaterThan(0);
});
@ -197,7 +201,7 @@ describe('Teacher controllers', () => {
await updateStudentJoinRequestHandler(req as Request, res as Response);
expect(statusMock).toHaveBeenCalledWith(200);
expect(sendStatusMock).toHaveBeenCalledWith(200);
req = {
params: { username: 'PinkFloyd' },
@ -205,8 +209,7 @@ describe('Teacher controllers', () => {
await getStudentRequestHandler(req as Request, res as Response);
const result = jsonMock.mock.lastCall?.[0];
const status = result.requests[0].status
const status: boolean = jsonMock.mock.lastCall?.[0].requests[0].status
expect(status).toBeTruthy;
});

View file

@ -5,7 +5,7 @@ const controller = new TeacherController();
describe('TeacherController', () => {
const newTeacher = {
username: 'testteacher',
username: 'testteacher3',
firstName: 'Testy',
lastName: 'McTestface',
};