-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement delete review service method
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 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
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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import { UserRepository } from '@src/core/ports/user.repository'; | |
import { ReviewService } from '@src/core/services/review/review.service'; | ||
import { | ||
CreateReviewDto, | ||
DeleteReviewDto, | ||
GetReviewsDto, | ||
UpdateReviewDto | ||
} from '@src/core/services/review/types'; | ||
|
@@ -597,4 +598,127 @@ describe('Test review service', () => { | |
expect(reviewRepository.update).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('Test delete review', () => { | ||
const userId = 'randomId'; | ||
const nickname = 'randomNickname'; | ||
const tag = '#TAGG'; | ||
const idp = new IdpEnum(Idp.GOOGLE); | ||
const email = '[email protected]'; | ||
const accessLevel = new AccessLevelEnum(AccessLevel.USER); | ||
const requesterIdToken = new AppIdToken( | ||
userId, | ||
nickname, | ||
tag, | ||
idp, | ||
email, | ||
accessLevel | ||
); | ||
const reviewId = 0; | ||
const title = 'randomTitle'; | ||
const movieName = 'randomMovie'; | ||
const content = 'randomContent'; | ||
const createdAt = new Date(); | ||
|
||
const userFound = new User( | ||
userId, | ||
nickname, | ||
tag, | ||
idp, | ||
email, | ||
accessLevel, | ||
createdAt, | ||
createdAt | ||
); | ||
const reviewFound = new Review( | ||
reviewId, | ||
userId, | ||
title, | ||
movieName, | ||
content, | ||
0, | ||
createdAt, | ||
createdAt | ||
); | ||
|
||
const userFindById = jest.fn(() => Promise.resolve(userFound)) as jest.Mock; | ||
const reviewFindById = jest.fn(() => | ||
Promise.resolve(reviewFound) | ||
) as jest.Mock; | ||
const reviewDeleteById = jest.fn(() => Promise.resolve()) as jest.Mock; | ||
|
||
beforeAll(() => { | ||
prismaMock.$transaction.mockImplementation((callback) => | ||
callback(prismaMock) | ||
); | ||
userRepository = new PostgresqlUserRepository(prismaMock); | ||
reviewRepository = new PostgresqlReviewRepository(prismaMock); | ||
replyRepository = new PostgresqlReplyRepository(prismaMock); | ||
txManager = new PrismaTransactionManager(prismaMock); | ||
userRepository.findById = userFindById; | ||
reviewRepository.findById = reviewFindById; | ||
reviewRepository.deleteById = reviewDeleteById; | ||
}); | ||
|
||
it('should success when valid', async () => { | ||
const givenDto: DeleteReviewDto = { | ||
requesterIdToken, | ||
reviewId | ||
}; | ||
await new ReviewService( | ||
userRepository, | ||
reviewRepository, | ||
replyRepository, | ||
txManager | ||
).deleteReview(givenDto); | ||
|
||
expect(reviewRepository.findById).toBeCalledTimes(1); | ||
const reviewFindByIdArgs = reviewFindById.mock.calls[0][0]; | ||
expect(reviewFindByIdArgs).toEqual(givenDto.reviewId); | ||
|
||
expect(userRepository.findById).toBeCalledTimes(1); | ||
const userFindByIdArgs = userFindById.mock.calls[0][0]; | ||
expect(userFindByIdArgs).toEqual(reviewFound.userId); | ||
|
||
expect(reviewRepository.deleteById).toBeCalledTimes(1); | ||
const reviewDeleteByIdArgs = reviewDeleteById.mock.calls[0][0]; | ||
expect(reviewDeleteByIdArgs).toEqual(givenDto.reviewId); | ||
}); | ||
|
||
it('should fail when access level and user authorization are invalid', async () => { | ||
const givenRequesterIdToken = new AppIdToken( | ||
'anotherRandomId', | ||
'anotherNickname', | ||
'#GGAT', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.USER) | ||
); | ||
const givenDto: DeleteReviewDto = { | ||
requesterIdToken: givenRequesterIdToken, | ||
reviewId | ||
}; | ||
try { | ||
await new ReviewService( | ||
userRepository, | ||
reviewRepository, | ||
replyRepository, | ||
txManager | ||
).deleteReview(givenDto); | ||
} catch (error: unknown) { | ||
expect(error).toBeInstanceOf(CustomError); | ||
expect(error).toHaveProperty('code', AppErrorCode.PERMISSIION_DENIED); | ||
} | ||
|
||
expect(reviewRepository.findById).toBeCalledTimes(1); | ||
const reviewFindByIdArgs = reviewFindById.mock.calls[0][0]; | ||
expect(reviewFindByIdArgs).toEqual(givenDto.reviewId); | ||
|
||
expect(userRepository.findById).toBeCalledTimes(1); | ||
const userFindByIdArgs = userFindById.mock.calls[0][0]; | ||
expect(userFindByIdArgs).toEqual(reviewFound.userId); | ||
|
||
expect(reviewRepository.deleteById).toBeCalledTimes(0); | ||
}); | ||
}); | ||
}); |