diff --git a/src/integration/Reviews.spec.ts b/src/integration/Reviews.spec.ts index 7fad6b8f0..4cb47120f 100644 --- a/src/integration/Reviews.spec.ts +++ b/src/integration/Reviews.spec.ts @@ -1265,6 +1265,23 @@ describe("Review Requests Integration Tests", () => { }) }) + it("should return 404 if review request is not approved", async () => { + // Arrange + const app = generateRouterForUserWithSite( + subrouter, + MOCK_USER_SESSION_DATA_ONE, + MOCK_REPO_NAME_TWO + ) + + // Act + const actual = await request(app).post( + `/${MOCK_REPO_NAME_TWO}/${MOCK_GITHUB_PULL_REQUEST_NUMBER}/merge` + ) + + // Assert + expect(actual.statusCode).toEqual(404) + }) + it("should merge the pull request successfully", async () => { // Arrange const app = generateRouterForUserWithSite( @@ -1272,6 +1289,17 @@ describe("Review Requests Integration Tests", () => { MOCK_USER_SESSION_DATA_ONE, MOCK_REPO_NAME_ONE ) + const activeReviewRequest = await ReviewRequest.findOne({ + where: { + requestorId: MOCK_USER_ID_ONE, + siteId: MOCK_SITE_ID_ONE, + }, + }) + if (!activeReviewRequest) fail("Should not reach here") + await activeReviewRequest.set({ + reviewStatus: ReviewRequestStatus.Approved, + }) + await activeReviewRequest.save() mockGenericAxios.post.mockResolvedValueOnce(null) mockGenericAxios.put.mockResolvedValueOnce(null) diff --git a/src/routes/v2/authenticated/__tests__/review.spec.ts b/src/routes/v2/authenticated/__tests__/review.spec.ts index 81a76fb20..16e56c057 100644 --- a/src/routes/v2/authenticated/__tests__/review.spec.ts +++ b/src/routes/v2/authenticated/__tests__/review.spec.ts @@ -738,9 +738,9 @@ describe("Review Requests Router", () => { // Arrange mockCollaboratorsService.getRole.mockResolvedValueOnce("role") - mockReviewRequestService.getReviewRequest.mockResolvedValueOnce( - "review request" - ) + mockReviewRequestService.getReviewRequest.mockResolvedValueOnce({ + reviewStatus: ReviewRequestStatus.Approved, + }) mockReviewRequestService.mergeReviewRequest.mockResolvedValueOnce( undefined ) diff --git a/src/routes/v2/authenticated/review.ts b/src/routes/v2/authenticated/review.ts index 0a71aedfb..934698f62 100644 --- a/src/routes/v2/authenticated/review.ts +++ b/src/routes/v2/authenticated/review.ts @@ -716,13 +716,19 @@ export class ReviewsRouter { return res.status(404).json({ message: possibleReviewRequest.message }) } - // Step 4: Merge review request + // Step 4: Check if review request is valid + if (possibleReviewRequest.reviewStatus !== ReviewRequestStatus.Approved) + return res + .status(404) + .json({ message: "Approved review request not found!" }) + + // Step 5: Merge review request // NOTE: We are not checking for existence of PR // as the underlying Github API returns 404 if // the requested review could not be found. await this.reviewRequestService.mergeReviewRequest(possibleReviewRequest) - // Step 5: Clean up the review request view records + // Step 6: Clean up the review request view records // The error is discarded as we are guaranteed to have a review request await this.reviewRequestService.deleteAllReviewRequestViews( site.value,