-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/npm_and_yarn/golevelup/nestjs-rab…
…bitmq-3.7.0
- Loading branch information
Showing
30 changed files
with
1,193 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
apps/server/src/modules/board/controller/api-test/submission-item-update.api.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
import { EntityManager } from '@mikro-orm/mongodb'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BoardExternalReferenceType, SubmissionItemNode } from '@shared/domain'; | ||
import { | ||
TestApiClient, | ||
UserAndAccountTestFactory, | ||
cardNodeFactory, | ||
cleanupCollections, | ||
columnBoardNodeFactory, | ||
columnNodeFactory, | ||
courseFactory, | ||
submissionContainerElementNodeFactory, | ||
submissionItemNodeFactory, | ||
} from '@shared/testing'; | ||
import { ServerTestModule } from '@src/modules/server'; | ||
import { SubmissionItemResponse } from '../dto'; | ||
|
||
const baseRouteName = '/board-submissions'; | ||
describe('submission item update (api)', () => { | ||
let app: INestApplication; | ||
let em: EntityManager; | ||
let testApiClient: TestApiClient; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ServerTestModule], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
em = module.get(EntityManager); | ||
testApiClient = new TestApiClient(app, baseRouteName); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe('when user is a valid teacher', () => { | ||
const setup = async () => { | ||
await cleanupCollections(em); | ||
|
||
const { teacherAccount, teacherUser } = UserAndAccountTestFactory.buildTeacher(); | ||
const course = courseFactory.build({ teachers: [teacherUser] }); | ||
await em.persistAndFlush([teacherAccount, teacherUser, course]); | ||
|
||
const columnBoardNode = columnBoardNodeFactory.buildWithId({ | ||
context: { id: course.id, type: BoardExternalReferenceType.Course }, | ||
}); | ||
|
||
const columnNode = columnNodeFactory.buildWithId({ parent: columnBoardNode }); | ||
|
||
const cardNode = cardNodeFactory.buildWithId({ parent: columnNode }); | ||
|
||
const submissionContainerNode = submissionContainerElementNodeFactory.buildWithId({ parent: cardNode }); | ||
const submissionItemNode = submissionItemNodeFactory.buildWithId({ | ||
userId: 'foo', | ||
parent: submissionContainerNode, | ||
completed: true, | ||
}); | ||
|
||
await em.persistAndFlush([columnBoardNode, columnNode, cardNode, submissionContainerNode, submissionItemNode]); | ||
em.clear(); | ||
|
||
const loggedInClient = await testApiClient.login(teacherAccount); | ||
|
||
return { loggedInClient, submissionItemNode }; | ||
}; | ||
it('should return status 403', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
expect(response.status).toEqual(403); | ||
}); | ||
it('should not actually update submission item entity', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
const result = await em.findOneOrFail(SubmissionItemNode, submissionItemNode.id); | ||
expect(result.completed).toEqual(submissionItemNode.completed); | ||
}); | ||
}); | ||
|
||
describe('when user is a student trying to update his own submission item', () => { | ||
const setup = async () => { | ||
await cleanupCollections(em); | ||
|
||
const { studentAccount, studentUser } = UserAndAccountTestFactory.buildStudent(); | ||
const course = courseFactory.build({ students: [studentUser] }); | ||
await em.persistAndFlush([studentAccount, studentUser, course]); | ||
|
||
const columnBoardNode = columnBoardNodeFactory.buildWithId({ | ||
context: { id: course.id, type: BoardExternalReferenceType.Course }, | ||
}); | ||
|
||
const columnNode = columnNodeFactory.buildWithId({ parent: columnBoardNode }); | ||
|
||
const cardNode = cardNodeFactory.buildWithId({ parent: columnNode }); | ||
|
||
const submissionContainerNode = submissionContainerElementNodeFactory.buildWithId({ parent: cardNode }); | ||
|
||
const submissionItemNode = submissionItemNodeFactory.buildWithId({ | ||
userId: studentUser.id, | ||
parent: submissionContainerNode, | ||
completed: true, | ||
}); | ||
|
||
await em.persistAndFlush([columnBoardNode, columnNode, cardNode, submissionContainerNode, submissionItemNode]); | ||
em.clear(); | ||
|
||
const loggedInClient = await testApiClient.login(studentAccount); | ||
|
||
return { loggedInClient, studentUser, submissionItemNode }; | ||
}; | ||
it('should return status 204', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
expect(response.status).toEqual(204); | ||
}); | ||
|
||
it('should actually update the submission item', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
const submissionItemResponse = response.body as SubmissionItemResponse; | ||
|
||
const result = await em.findOneOrFail(SubmissionItemNode, submissionItemResponse.id); | ||
expect(result.id).toEqual(submissionItemNode.id); | ||
expect(result.completed).toEqual(false); | ||
}); | ||
|
||
it('should fail without params completed', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, {}); | ||
expect(response.status).toBe(400); | ||
}); | ||
}); | ||
|
||
describe('when user is a student from same course, and tries to update a submission item he did not create himself', () => { | ||
const setup = async () => { | ||
await cleanupCollections(em); | ||
|
||
const { studentAccount, studentUser } = UserAndAccountTestFactory.buildStudent(); | ||
const { studentAccount: studentAccount2, studentUser: studentUser2 } = UserAndAccountTestFactory.buildStudent(); | ||
const course = courseFactory.build({ students: [studentUser, studentUser2] }); | ||
await em.persistAndFlush([studentAccount, studentUser, studentAccount2, studentUser2, course]); | ||
|
||
const columnBoardNode = columnBoardNodeFactory.buildWithId({ | ||
context: { id: course.id, type: BoardExternalReferenceType.Course }, | ||
}); | ||
|
||
const columnNode = columnNodeFactory.buildWithId({ parent: columnBoardNode }); | ||
|
||
const cardNode = cardNodeFactory.buildWithId({ parent: columnNode }); | ||
|
||
const submissionContainerNode = submissionContainerElementNodeFactory.buildWithId({ parent: cardNode }); | ||
|
||
const submissionItemNode = submissionItemNodeFactory.buildWithId({ | ||
userId: studentUser.id, | ||
parent: submissionContainerNode, | ||
completed: true, | ||
}); | ||
await em.persistAndFlush([columnBoardNode, columnNode, cardNode, submissionContainerNode, submissionItemNode]); | ||
em.clear(); | ||
|
||
const loggedInClient = await testApiClient.login(studentAccount2); | ||
|
||
return { loggedInClient, submissionItemNode }; | ||
}; | ||
|
||
it('should return status 403', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
expect(response.status).toEqual(403); | ||
}); | ||
it('should not actually update submission item entity', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
const result = await em.findOneOrFail(SubmissionItemNode, submissionItemNode.id); | ||
expect(result.completed).toEqual(submissionItemNode.completed); | ||
}); | ||
}); | ||
|
||
describe('when user is a student not in course', () => { | ||
const setup = async () => { | ||
await cleanupCollections(em); | ||
|
||
const { studentAccount, studentUser } = UserAndAccountTestFactory.buildStudent(); | ||
const { studentAccount: studentAccount2, studentUser: studentUser2 } = UserAndAccountTestFactory.buildStudent(); | ||
const course = courseFactory.build({ students: [studentUser] }); | ||
await em.persistAndFlush([studentAccount, studentUser, studentAccount2, studentUser2, course]); | ||
|
||
const columnBoardNode = columnBoardNodeFactory.buildWithId({ | ||
context: { id: course.id, type: BoardExternalReferenceType.Course }, | ||
}); | ||
|
||
const columnNode = columnNodeFactory.buildWithId({ parent: columnBoardNode }); | ||
|
||
const cardNode = cardNodeFactory.buildWithId({ parent: columnNode }); | ||
|
||
const submissionContainerNode = submissionContainerElementNodeFactory.buildWithId({ parent: cardNode }); | ||
|
||
const submissionItemNode = submissionItemNodeFactory.buildWithId({ | ||
userId: studentUser.id, | ||
parent: submissionContainerNode, | ||
completed: true, | ||
}); | ||
await em.persistAndFlush([columnBoardNode, columnNode, cardNode, submissionContainerNode, submissionItemNode]); | ||
em.clear(); | ||
|
||
const loggedInClient = await testApiClient.login(studentAccount2); | ||
|
||
return { loggedInClient, submissionItemNode }; | ||
}; | ||
|
||
it('should return status 403', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
const response = await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
expect(response.status).toEqual(403); | ||
}); | ||
|
||
it('should not actually update submission item entity', async () => { | ||
const { loggedInClient, submissionItemNode } = await setup(); | ||
|
||
await loggedInClient.patch(`${submissionItemNode.id}`, { completed: false }); | ||
|
||
const result = await em.findOneOrFail(SubmissionItemNode, submissionItemNode.id); | ||
expect(result.completed).toEqual(submissionItemNode.completed); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
apps/server/src/modules/board/controller/dto/submission-item/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './board-submission-id.params'; | ||
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 './update-submission-item.body.params'; |
2 changes: 1 addition & 1 deletion
2
...ission-item/board-submission-id.params.ts → ...n-item/submission-container.url.params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
apps/server/src/modules/board/controller/dto/submission-item/submission-item.url.params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsMongoId } from 'class-validator'; | ||
|
||
export class SubmissionItemUrlParams { | ||
@IsMongoId() | ||
@ApiProperty({ | ||
description: 'The id of the submission item.', | ||
required: true, | ||
nullable: false, | ||
}) | ||
submissionItemId!: string; | ||
} |
11 changes: 11 additions & 0 deletions
11
...er/src/modules/board/controller/dto/submission-item/update-submission-item.body.params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsBoolean } from 'class-validator'; | ||
|
||
export class UpdateSubmissionItemBodyParams { | ||
@IsBoolean() | ||
@ApiProperty({ | ||
description: 'Boolean indicating whether the submission is completed.', | ||
required: true, | ||
}) | ||
completed!: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.