Skip to content

Commit

Permalink
Fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderleegs committed Jan 5, 2023
1 parent 2ba4cd4 commit 893d255
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/integration/NotificationOnEditHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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,
Expand Down Expand Up @@ -143,7 +144,7 @@ describe("Notifications Router", () => {
})
await Site.create({
id: mockAdditionalSiteId,
name: mockSiteName,
name: "mockSite2",
apiTokenName: "token",
jobStatus: "READY",
siteStatus: "LAUNCHED",
Expand Down
3 changes: 2 additions & 1 deletion src/integration/Notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/integration/Reviews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SitesRouter as _SitesRouter } from "@routes/v2/authenticated/sites"

import {
IsomerAdmin,
Notification,
Repo,
Reviewer,
ReviewMeta,
Expand Down Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -110,7 +112,8 @@ const ReviewsRouter = new _ReviewsRouter(
reviewRequestService,
usersService,
sitesService,
collaboratorsService
collaboratorsService,
notificationsService
)
const reviewsSubrouter = ReviewsRouter.getRouter()
const subrouter = express()
Expand Down
50 changes: 47 additions & 3 deletions src/routes/v2/authenticated/__tests__/review.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -291,6 +305,7 @@ describe("Review Requests Router", () => {
expect(
mockReviewRequestService.createReviewRequest
).not.toHaveBeenCalled()
expect(mockNotificationsService.create).not.toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -570,6 +585,7 @@ describe("Review Requests Router", () => {
SiteMember: {
role: CollaboratorRoles.Admin,
},
id: mockUserId,
},
])
mockReviewRequestService.updateReviewRequest.mockResolvedValueOnce(
Expand Down Expand Up @@ -773,13 +789,23 @@ 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 = "[email protected]"
mockSitesService.getBySiteName.mockResolvedValueOnce("site")
mockReviewRequestService.getReviewRequest.mockResolvedValueOnce(
mockReviewRequest
)
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`)
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -846,6 +875,7 @@ describe("Review Requests Router", () => {
expect(
mockReviewRequestService.approveReviewRequest
).not.toHaveBeenCalled()
expect(mockNotificationsService.create).not.toHaveBeenCalled()
})
})

Expand Down Expand Up @@ -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 = "[email protected]"
mockSitesService.getBySiteName.mockResolvedValueOnce("site")
mockReviewRequestService.getReviewRequest.mockResolvedValueOnce(
mockReviewRequest
Expand All @@ -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`)
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -1179,6 +1222,7 @@ describe("Review Requests Router", () => {
expect(
mockReviewRequestService.deleteAllReviewRequestViews
).not.toHaveBeenCalled()
expect(mockNotificationsService.create).not.toHaveBeenCalled()
})
})

Expand Down

0 comments on commit 893d255

Please sign in to comment.