Skip to content

Commit

Permalink
BC-4375 - add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Sep 18, 2023
1 parent 72b9fd2 commit ca9bb57
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ describe('submission create (api)', () => {
expect(result.id).toBeDefined();
expect(result.timestamps.createdAt).toBeDefined();
expect(result.timestamps.lastUpdatedAt).toBeDefined();
expect(result.userData.userId).toBe(studentUser.id);
expect(result.userData.firstName).toBe('John');
expect(result.userData.lastName).toBe('Mr Doe');
expect(result.userId).toBe(studentUser.id);
});

it('should actually create the submission item', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
userFactory,
} from '@shared/testing';
import { ServerTestModule } from '@src/modules/server';
import { SubmissionItemResponse } from '../dto';
import { SubmissionItemResponse, SubmissionsResponse } from '../dto';

Check failure on line 18 in apps/server/src/modules/board/controller/api-test/submission-item-lookup.api.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'SubmissionItemResponse' is defined but never used

const baseRouteName = '/board-submissions';
describe('submission item lookup (api)', () => {
Expand All @@ -38,7 +38,7 @@ describe('submission item lookup (api)', () => {
await app.close();
});

describe('with teacher of two submission containers filled with submission items of 2 students', () => {
describe('when user is teacher and we have 2 submission containers filled with submission items from 2 students', () => {
const setup = async () => {
await cleanupCollections(em);

Expand Down Expand Up @@ -110,6 +110,8 @@ describe('submission item lookup (api)', () => {
item12,
item21,
item22,
studentUser1,
studentUser2,
};
};
it('should return status 200', async () => {
Expand All @@ -123,24 +125,38 @@ describe('submission item lookup (api)', () => {
const { loggedInClient, submissionContainerNode1, item11, item12 } = await setup();

const response = await loggedInClient.get(`${submissionContainerNode1.id}`);
const body = response.body as SubmissionItemResponse[];
expect(body.length).toBe(2);
expect(body.map((item) => item.id)).toContain(item11.id);
expect(body.map((item) => item.id)).toContain(item12.id);
const body = response.body as SubmissionsResponse;
const { submissionItemsResponse } = body;
expect(submissionItemsResponse.length).toBe(2);
expect(submissionItemsResponse.map((item) => item.id)).toContain(item11.id);
expect(submissionItemsResponse.map((item) => item.id)).toContain(item12.id);
});

it('should return all items from container 2 as teacher', async () => {
const { loggedInClient, submissionContainerNode2, item21, item22 } = await setup();

const response = await loggedInClient.get(`${submissionContainerNode2.id}`);
const body = response.body as SubmissionItemResponse[];
expect(body.length).toBe(2);
expect(body.map((item) => item.id)).toContain(item21.id);
expect(body.map((item) => item.id)).toContain(item22.id);
const body = response.body as SubmissionsResponse;
const { submissionItemsResponse } = body;
expect(submissionItemsResponse.length).toBe(2);
expect(submissionItemsResponse.map((item) => item.id)).toContain(item21.id);
expect(submissionItemsResponse.map((item) => item.id)).toContain(item22.id);
});

it('should return list of students', async () => {
const { loggedInClient, submissionContainerNode1, studentUser1, studentUser2 } = await setup();

const response = await loggedInClient.get(`${submissionContainerNode1.id}`);
const body = response.body as SubmissionsResponse;
const { users } = body;
expect(users.length).toBe(2);
const userIds = users.map((user) => user.userId);
expect(userIds).toContain(studentUser1.id);
expect(userIds).toContain(studentUser2.id);
});
});

describe('with student of a submission container filled with 2 items', () => {
describe('when user is student and we have a submission container element filled with 2 submission items', () => {
const setup = async () => {
await cleanupCollections(em);

Expand Down Expand Up @@ -194,13 +210,14 @@ describe('submission item lookup (api)', () => {
const { loggedInClient, submissionContainerNode, item1 } = await setup();

const response = await loggedInClient.get(`${submissionContainerNode.id}`);
const body = response.body as SubmissionItemResponse[];
expect(body.length).toBe(1);
expect(body[0].id).toBe(item1.id);
const body = response.body as SubmissionsResponse;
const { submissionItemsResponse } = body;
expect(submissionItemsResponse.length).toBe(1);
expect(submissionItemsResponse[0].id).toBe(item1.id);
});
});

describe('with invalid user', () => {
describe('when user is invalid', () => {
const setup = async () => {
await cleanupCollections(em);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './submission-container.url.params';
export * from './create-submission-item.body.params';
export * from './submission-item.response';
export * from './submission-item.url.params';
export * from './submissions.response';
export * from './update-submission-item.body.params';
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ describe(BoardDoAuthorizableService.name, () => {
teacherId: teacher.id,
substitutionTeacherId: substitutionTeacher.id,
studentIds: students.map((s) => s.id),
teacher,
substitutionTeacher,
students,
};
};

Expand Down Expand Up @@ -131,6 +134,33 @@ describe(BoardDoAuthorizableService.name, () => {
expect(userPermissions[studentIds[2]]).toEqual([BoardRoles.READER]);
expect(userRoleEnums[studentIds[2]]).toEqual(UserRoleEnum.STUDENT);
});

it('should return the users with their names', async () => {
const { board, teacher, substitutionTeacher, students } = setup();

const boardDoAuthorizable = await service.getBoardAuthorizable(board);
const firstNames = boardDoAuthorizable.users.reduce((map, user) => {
map[user.userId] = user.firstName;
return map;
}, {});

const lastNames = boardDoAuthorizable.users.reduce((map, user) => {
map[user.userId] = user.lastName;
return map;
}, {});

expect(boardDoAuthorizable.users).toHaveLength(5);
expect(firstNames[teacher.id]).toEqual(teacher.firstName);
expect(lastNames[teacher.id]).toEqual(teacher.lastName);
expect(firstNames[substitutionTeacher.id]).toEqual(substitutionTeacher.firstName);
expect(lastNames[substitutionTeacher.id]).toEqual(substitutionTeacher.lastName);
expect(firstNames[students[0].id]).toEqual(students[0].firstName);
expect(lastNames[students[0].id]).toEqual(students[0].lastName);
expect(firstNames[students[1].id]).toEqual(students[1].firstName);
expect(lastNames[students[1].id]).toEqual(students[1].lastName);
expect(firstNames[students[2].id]).toEqual(students[2].firstName);
expect(lastNames[students[2].id]).toEqual(students[2].lastName);
});
});

describe('when trying to create a boardDoAuthorizable on a column without a columnboard as root', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class BoardDoAuthorizableService implements AuthorizationLoaderService {
userRoleEnum: UserRoleEnum.TEACHER,
};
}),
...course.getSubstitutionTeacherList().map((user) => {
...course.getSubstitutionTeachersList().map((user) => {
return {
userId: user.id,
firstName: user.firstName,
Expand Down
28 changes: 17 additions & 11 deletions apps/server/src/modules/board/uc/submission-item.uc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ObjectId } from 'bson';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { BoardDoAuthorizable, BoardRoles, UserRoleEnum } from '@shared/domain';
Expand Down Expand Up @@ -54,9 +53,6 @@ describe(SubmissionItemUc.name, () => {
authorizationService = module.get(AuthorizationService);
authorizationService.checkPermission.mockImplementation(() => {});
boardDoAuthorizableService = module.get(BoardDoAuthorizableService);
boardDoAuthorizableService.getBoardAuthorizable.mockResolvedValue(
new BoardDoAuthorizable({ users: [], id: new ObjectId().toHexString() })
);
elementService = module.get(ContentElementService);
submissionItemService = module.get(SubmissionItemService);
await setupEntities();
Expand Down Expand Up @@ -102,9 +98,14 @@ describe(SubmissionItemUc.name, () => {

it('student1 should only get their own submission item', async () => {
const { user1, submissionContainerEl, submissionItem1 } = setup();
const items = await uc.findSubmissionItems(user1.id, submissionContainerEl.id);
expect(items.length).toBe(1);
expect(items[0]).toStrictEqual(submissionItem1);
const { submissionItems } = await uc.findSubmissionItems(user1.id, submissionContainerEl.id);
expect(submissionItems.length).toBe(1);
expect(submissionItems[0]).toStrictEqual(submissionItem1);
});
it('student should not get a list of users', async () => {
const { user1, submissionContainerEl } = setup();
const { users } = await uc.findSubmissionItems(user1.id, submissionContainerEl.id);
expect(users.length).toBe(0);
});
});
describe('when user is a teacher', () => {
Expand Down Expand Up @@ -140,10 +141,15 @@ describe(SubmissionItemUc.name, () => {

it('teacher should get all submission items', async () => {
const { teacher, submissionContainerEl, submissionItem1, submissionItem2 } = setup();
const items = await uc.findSubmissionItems(teacher.id, submissionContainerEl.id);
expect(items.length).toBe(2);
expect(items.map((item) => item.id)).toContain(submissionItem1.id);
expect(items.map((item) => item.id)).toContain(submissionItem2.id);
const { submissionItems } = await uc.findSubmissionItems(teacher.id, submissionContainerEl.id);
expect(submissionItems.length).toBe(2);
expect(submissionItems.map((item) => item.id)).toContain(submissionItem1.id);
expect(submissionItems.map((item) => item.id)).toContain(submissionItem2.id);
});
it('teacher should get list of students', async () => {
const { teacher, submissionContainerEl } = setup();
const { users } = await uc.findSubmissionItems(teacher.id, submissionContainerEl.id);
expect(users.length).toBe(2);
});
});
describe('when user has not an authorized role', () => {
Expand Down
81 changes: 81 additions & 0 deletions apps/server/src/shared/domain/entity/course.entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,85 @@ describe('CourseEntity', () => {
});
});
});

describe('getStudentsList is called', () => {
const setup = () => {
const students = userFactory.buildListWithId(2);
const course = courseFactory.build({ students });
return { course, students };
};
it('should return the students of the course', () => {
const { course, students } = setup();
const [student1, student2] = students;

const result = course.getStudentsList();

expect(result.length).toEqual(2);
expect(result[0].id).toEqual(student1.id);
expect(result[0].firstName).toEqual(student1.firstName);
expect(result[0].lastName).toEqual(student1.lastName);
expect(result[1].id).toEqual(student2.id);
});
it('should return an empty array if no students are in the course', () => {
const course = courseFactory.build({ students: [] });

const result = course.getStudentsList();

expect(result.length).toEqual(0);
});
});

describe('getTeachersList is called', () => {
const setup = () => {
const teachers = userFactory.buildListWithId(2);
const course = courseFactory.build({ teachers });
return { course, teachers };
};
it('should return the students of the course', () => {
const { course, teachers } = setup();
const [teacher1, teacher2] = teachers;

const result = course.getTeachersList();

expect(result.length).toEqual(2);
expect(result[0].id).toEqual(teacher1.id);
expect(result[0].firstName).toEqual(teacher1.firstName);
expect(result[0].lastName).toEqual(teacher1.lastName);
expect(result[1].id).toEqual(teacher2.id);
});
it('should return an empty array if no teachers are in the course', () => {
const course = courseFactory.build({ teachers: [] });

const result = course.getTeachersList();

expect(result.length).toEqual(0);
});
});

describe('getSubstitutionTeacherList is called', () => {
const setup = () => {
const substitutionTeachers = userFactory.buildListWithId(2);
const course = courseFactory.build({ substitutionTeachers });
return { course, substitutionTeachers };
};
it('should return the substitutionTeachers of the course', () => {
const { course, substitutionTeachers } = setup();
const [substitutionTeacher1, substitutionTeacher2] = substitutionTeachers;

const result = course.getSubstitutionTeachersList();

expect(result.length).toEqual(2);
expect(result[0].id).toEqual(substitutionTeacher1.id);
expect(result[0].firstName).toEqual(substitutionTeacher1.firstName);
expect(result[0].lastName).toEqual(substitutionTeacher1.lastName);
expect(result[1].id).toEqual(substitutionTeacher2.id);
});
it('should return an empty array if no substitutionTeachers are in the course', () => {
const course = courseFactory.build({ substitutionTeachers: [] });

const result = course.getSubstitutionTeachersList();

expect(result.length).toEqual(0);
});
});
});
2 changes: 1 addition & 1 deletion apps/server/src/shared/domain/entity/course.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class Course
return [];
}

public getSubstitutionTeacherList(): UsersList[] {
public getSubstitutionTeachersList(): UsersList[] {
const users = this.substitutionTeachers.getItems();
if (users.length) {
const usersList = this.extractUserList(users);
Expand Down
21 changes: 21 additions & 0 deletions apps/server/src/shared/repo/course/course.repo.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,27 @@ describe('course repo', () => {
expect(foundCourse.courseGroups.isInitialized()).toEqual(true);
expect(foundCourse.courseGroups[0].id).toEqual(courseGroup.id);
});

it('should populate course teachers, substitute teachers and students', async () => {
const teacher = userFactory.buildWithId();
const substitutionTeacher = userFactory.buildWithId();
const student = userFactory.buildWithId();

const course = courseFactory.buildWithId({
teachers: [teacher],
substitutionTeachers: [substitutionTeacher],
students: [student],
});
await em.persistAndFlush([course, teacher, substitutionTeacher, student]);
em.clear();

const foundCourse = await repo.findById(course.id);
expect(foundCourse.courseGroups.isInitialized()).toEqual(true);

expect(foundCourse.teachers[0].id).toEqual(teacher.id);
expect(foundCourse.substitutionTeachers[0].id).toEqual(substitutionTeacher.id);
expect(foundCourse.students[0].id).toEqual(student.id);
});
});

describe('unset optional property', () => {
Expand Down

0 comments on commit ca9bb57

Please sign in to comment.