From 893d255f0381e849b23be6e97f8575024c57816c Mon Sep 17 00:00:00 2001 From: Alexander Lee Date: Thu, 5 Jan 2023 17:11:34 +0800 Subject: [PATCH] Fix: update tests --- .../NotificationOnEditHandler.spec.ts | 5 +- src/integration/Notifications.spec.ts | 3 +- src/integration/Reviews.spec.ts | 9 ++-- .../v2/authenticated/__tests__/review.spec.ts | 50 +++++++++++++++++-- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/integration/NotificationOnEditHandler.spec.ts b/src/integration/NotificationOnEditHandler.spec.ts index 7948f402c..ed66a041e 100644 --- a/src/integration/NotificationOnEditHandler.spec.ts +++ b/src/integration/NotificationOnEditHandler.spec.ts @@ -27,6 +27,7 @@ import CollaboratorsService from "@root/services/identity/CollaboratorsService" import IsomerAdminsService from "@root/services/identity/IsomerAdminsService" import SitesService from "@root/services/identity/SitesService" import ReviewRequestService from "@root/services/review/ReviewRequestService" +import * as ReviewApi from "@services/db/review" import { getUsersService, notificationsService } from "@services/identity" import { sequelize } from "@tests/database" @@ -39,7 +40,7 @@ const mockGithubService = { } const usersService = getUsersService(sequelize) const reviewRequestService = new ReviewRequestService( - (mockGithubService as unknown) as GitHubService, + (mockGithubService as unknown) as typeof ReviewApi, User, ReviewRequest, Reviewer, @@ -143,7 +144,7 @@ describe("Notifications Router", () => { }) await Site.create({ id: mockAdditionalSiteId, - name: mockSiteName, + name: "mockSite2", apiTokenName: "token", jobStatus: "READY", siteStatus: "LAUNCHED", diff --git a/src/integration/Notifications.spec.ts b/src/integration/Notifications.spec.ts index 2eabb919c..412270a9b 100644 --- a/src/integration/Notifications.spec.ts +++ b/src/integration/Notifications.spec.ts @@ -39,6 +39,7 @@ import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/Co import CollaboratorsService from "@root/services/identity/CollaboratorsService" import SitesService from "@root/services/identity/SitesService" import ReviewRequestService from "@root/services/review/ReviewRequestService" +import * as ReviewApi from "@services/db/review" import { getIdentityAuthService, getUsersService, @@ -58,7 +59,7 @@ const identityAuthService = getIdentityAuthService(gitHubService) const usersService = getUsersService(sequelize) const configYmlService = new ConfigYmlService({ gitHubService }) const reviewRequestService = new ReviewRequestService( - gitHubService, + (gitHubService as unknown) as typeof ReviewApi, User, ReviewRequest, Reviewer, diff --git a/src/integration/Reviews.spec.ts b/src/integration/Reviews.spec.ts index 8876b6e7e..cf5ddf44b 100644 --- a/src/integration/Reviews.spec.ts +++ b/src/integration/Reviews.spec.ts @@ -7,6 +7,7 @@ import { SitesRouter as _SitesRouter } from "@routes/v2/authenticated/sites" import { IsomerAdmin, + Notification, Repo, Reviewer, ReviewMeta, @@ -70,8 +71,9 @@ import { import { ReviewRequestStatus } from "@root/constants" import { ReviewRequestDto } from "@root/types/dto/review" import { GitHubService } from "@services/db/GitHubService" +import * as ReviewApi from "@services/db/review" import { ConfigYmlService } from "@services/fileServices/YmlFileServices/ConfigYmlService" -import { getUsersService } from "@services/identity" +import { getUsersService, notificationsService } from "@services/identity" import CollaboratorsService from "@services/identity/CollaboratorsService" import IsomerAdminsService from "@services/identity/IsomerAdminsService" import SitesService from "@services/identity/SitesService" @@ -83,7 +85,7 @@ const configYmlService = new ConfigYmlService({ gitHubService }) const usersService = getUsersService(sequelize) const isomerAdminsService = new IsomerAdminsService({ repository: IsomerAdmin }) const reviewRequestService = new ReviewRequestService( - gitHubService, + (gitHubService as unknown) as typeof ReviewApi, User, ReviewRequest, Reviewer, @@ -110,7 +112,8 @@ const ReviewsRouter = new _ReviewsRouter( reviewRequestService, usersService, sitesService, - collaboratorsService + collaboratorsService, + notificationsService ) const reviewsSubrouter = ReviewsRouter.getRouter() const subrouter = express() diff --git a/src/routes/v2/authenticated/__tests__/review.spec.ts b/src/routes/v2/authenticated/__tests__/review.spec.ts index c860e44a7..602fc9f59 100644 --- a/src/routes/v2/authenticated/__tests__/review.spec.ts +++ b/src/routes/v2/authenticated/__tests__/review.spec.ts @@ -1,15 +1,18 @@ import express from "express" import request from "supertest" +import RequestNotFoundError from "@errors/RequestNotFoundError" + import { attachReadRouteHandlerWrapper } from "@middleware/routeHandler" import { ReviewsRouter as _ReviewsRouter } from "@routes/v2/authenticated/review" import { generateRouterForDefaultUserWithSite } from "@fixtures/app" +import { mockUserId } from "@fixtures/identity" +import { MOCK_USER_EMAIL_ONE, MOCK_USER_EMAIL_TWO } from "@fixtures/users" import { CollaboratorRoles } from "@root/constants" -import RequestNotFoundError from "@root/errors/RequestNotFoundError" -import { MOCK_USER_EMAIL_ONE, MOCK_USER_EMAIL_TWO } from "@root/fixtures/users" import CollaboratorsService from "@services/identity/CollaboratorsService" +import NotificationsService from "@services/identity/NotificationsService" import SitesService from "@services/identity/SitesService" import UsersService from "@services/identity/UsersService" import ReviewRequestService from "@services/review/ReviewRequestService" @@ -48,11 +51,16 @@ describe("Review Requests Router", () => { list: jest.fn(), } + const mockNotificationsService = { + create: jest.fn(), + } + const ReviewsRouter = new _ReviewsRouter( (mockReviewRequestService as unknown) as ReviewRequestService, (mockIdentityUsersService as unknown) as UsersService, (mockSitesService as unknown) as SitesService, - (mockCollaboratorsService as unknown) as CollaboratorsService + (mockCollaboratorsService as unknown) as CollaboratorsService, + (mockNotificationsService as unknown) as NotificationsService ) const subrouter = express() @@ -169,11 +177,13 @@ describe("Review Requests Router", () => { SiteMember: { role: CollaboratorRoles.Admin, }, + id: mockUserId, }, ]) mockReviewRequestService.createReviewRequest.mockResolvedValueOnce( mockPullRequestNumber ) + mockNotificationsService.create.mockResolvedValueOnce([]) // Act const response = await request(app) @@ -196,6 +206,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.createReviewRequest ).toHaveBeenCalledTimes(1) + expect(mockNotificationsService.create).toHaveBeenCalledTimes(1) }) it("should return 404 if the site does not exist", async () => { @@ -216,6 +227,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.createReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 404 if user is not a site collaborator", async () => { @@ -237,6 +249,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.createReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 400 if no reviewers are provided", async () => { @@ -263,6 +276,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.createReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 400 if provided reviewer is not an admin", async () => { @@ -291,6 +305,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.createReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) }) @@ -570,6 +585,7 @@ describe("Review Requests Router", () => { SiteMember: { role: CollaboratorRoles.Admin, }, + id: mockUserId, }, ]) mockReviewRequestService.updateReviewRequest.mockResolvedValueOnce( @@ -773,6 +789,7 @@ describe("Review Requests Router", () => { it("should return 200 with the review request successfully marked as approved", async () => { // Arrange const mockReviewRequest = { reviewers: [{ email: MOCK_USER_EMAIL_ONE }] } + const mockReviewer = "reviewer@test.gov.sg" mockSitesService.getBySiteName.mockResolvedValueOnce("site") mockReviewRequestService.getReviewRequest.mockResolvedValueOnce( mockReviewRequest @@ -780,6 +797,15 @@ describe("Review Requests Router", () => { mockReviewRequestService.approveReviewRequest.mockResolvedValueOnce( undefined ) + mockCollaboratorsService.list.mockResolvedValueOnce([ + { + email: mockReviewer, + SiteMember: { + role: CollaboratorRoles.Admin, + }, + id: mockUserId, + }, + ]) // Act const response = await request(app).post(`/mockSite/review/12345/approve`) @@ -791,6 +817,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.approveReviewRequest ).toHaveBeenCalledTimes(1) + expect(mockNotificationsService.create).toHaveBeenCalledTimes(1) }) it("should return 404 if the site does not exist", async () => { @@ -807,6 +834,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.approveReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 404 if the review request does not exist", async () => { @@ -826,6 +854,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.approveReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 403 if the user is not a reviewer", async () => { @@ -846,6 +875,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.approveReviewRequest ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) }) @@ -1097,6 +1127,7 @@ describe("Review Requests Router", () => { it("should return 200 with the review request closed successfully", async () => { // Arrange const mockReviewRequest = { requestor: { email: MOCK_USER_EMAIL_ONE } } + const mockReviewer = "reviewer@test.gov.sg" mockSitesService.getBySiteName.mockResolvedValueOnce("site") mockReviewRequestService.getReviewRequest.mockResolvedValueOnce( mockReviewRequest @@ -1107,6 +1138,15 @@ describe("Review Requests Router", () => { mockReviewRequestService.deleteAllReviewRequestViews.mockResolvedValueOnce( undefined ) + mockCollaboratorsService.list.mockResolvedValueOnce([ + { + email: mockReviewer, + SiteMember: { + role: CollaboratorRoles.Admin, + }, + id: mockUserId, + }, + ]) // Act const response = await request(app).delete(`/mockSite/review/12345`) @@ -1121,6 +1161,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.deleteAllReviewRequestViews ).toHaveBeenCalledTimes(1) + expect(mockNotificationsService.create).toHaveBeenCalledTimes(1) }) it("should return 404 if the site does not exist", async () => { @@ -1138,6 +1179,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.deleteAllReviewRequestViews ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 404 if the review request does not exist", async () => { @@ -1158,6 +1200,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.deleteAllReviewRequestViews ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) it("should return 404 if the user is not the requestor of the review request", async () => { @@ -1179,6 +1222,7 @@ describe("Review Requests Router", () => { expect( mockReviewRequestService.deleteAllReviewRequestViews ).not.toHaveBeenCalled() + expect(mockNotificationsService.create).not.toHaveBeenCalled() }) })