From 24828f6fdd23850dbf6dad2166d689798f46e5a3 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Thu, 10 Nov 2022 15:55:19 +0800 Subject: [PATCH] tests: add remaining tests for identity features in SitesService (#566) * chore: define more specific constants for source of URLs * chore: move siteUrls type to repoInfo * tests: add remaining tests for identity features * tests: add spy method to check if extractAuthorEmail was called --- src/fixtures/identity.ts | 2 + src/fixtures/repoInfo.ts | 13 +- src/services/identity/SitesService.ts | 9 +- .../identity/__tests__/SitesService.spec.ts | 674 +++++++++++++++++- src/types/repoInfo.ts | 3 + 5 files changed, 668 insertions(+), 33 deletions(-) diff --git a/src/fixtures/identity.ts b/src/fixtures/identity.ts index ab1d7ae7b..bdccf50d3 100644 --- a/src/fixtures/identity.ts +++ b/src/fixtures/identity.ts @@ -162,3 +162,5 @@ export const MOCK_COMMIT_MESSAGE_OBJECT_TWO = { fileName: MOCK_COMMIT_FILENAME_TWO, userId: mockIsomerUserId, } + +export const MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME = "isomergithub1" diff --git a/src/fixtures/repoInfo.ts b/src/fixtures/repoInfo.ts index 6c1390d3b..49b7474dc 100644 --- a/src/fixtures/repoInfo.ts +++ b/src/fixtures/repoInfo.ts @@ -1,10 +1,19 @@ import { GitHubRepositoryData } from "@root/types/repoInfo" +export const MOCK_STAGING_URL_GITHUB = "https://repo-staging.netlify.app" +export const MOCK_STAGING_URL_CONFIGYML = + "https://repo-staging-configyml.netlify.app" +export const MOCK_STAGING_URL_DB = "https://repo-staging-db.netlify.app" + +export const MOCK_PRODUCTION_URL_GITHUB = "https://repo-prod.netlify.app" +export const MOCK_PRODUCTION_URL_CONFIGYML = + "https://repo-prod-configyml.netlify.app" +export const MOCK_PRODUCTION_URL_DB = "https://repo-prod-db.netlify.app" + export const repoInfo: GitHubRepositoryData = { name: "repo", private: false, - description: - "Staging: https://repo-staging.netlify.app | Production: https://repo-prod.netlify.app", + description: `Staging: ${MOCK_STAGING_URL_GITHUB} | Production: ${MOCK_PRODUCTION_URL_GITHUB}`, pushed_at: "2021-09-09T02:41:37Z", permissions: { admin: true, diff --git a/src/services/identity/SitesService.ts b/src/services/identity/SitesService.ts index 885799c2e..5e34a5a92 100644 --- a/src/services/identity/SitesService.ts +++ b/src/services/identity/SitesService.ts @@ -16,7 +16,11 @@ import { UnprocessableError } from "@root/errors/UnprocessableError" import { genericGitHubAxiosInstance } from "@root/services/api/AxiosInstance" import { GitHubCommitData } from "@root/types/commitData" import { ConfigYmlData } from "@root/types/configYml" -import type { GitHubRepositoryData, RepositoryData } from "@root/types/repoInfo" +import type { + GitHubRepositoryData, + RepositoryData, + SiteUrls, +} from "@root/types/repoInfo" import { SiteInfo } from "@root/types/siteInfo" import { GitHubService } from "@services/db/GitHubService" import { ConfigYmlService } from "@services/fileServices/YmlFileServices/ConfigYmlService" @@ -33,9 +37,6 @@ interface SitesServiceProps { reviewRequestService: ReviewRequestService } -type SiteUrlTypes = "staging" | "prod" -type SiteUrls = { [key in SiteUrlTypes]: string } - class SitesService { // NOTE: Explicitly specifying using keyed properties to ensure // that the types are synced. diff --git a/src/services/identity/__tests__/SitesService.spec.ts b/src/services/identity/__tests__/SitesService.spec.ts index dac332aac..c9d0037dc 100644 --- a/src/services/identity/__tests__/SitesService.spec.ts +++ b/src/services/identity/__tests__/SitesService.spec.ts @@ -12,12 +12,19 @@ import { MOCK_GITHUB_DATE_TWO, MOCK_COMMIT_MESSAGE_ONE, MOCK_COMMIT_MESSAGE_TWO, + MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME, } from "@fixtures/identity" import { repoInfo, repoInfo2, adminRepo, noAccessRepo, + MOCK_STAGING_URL_CONFIGYML, + MOCK_PRODUCTION_URL_CONFIGYML, + MOCK_PRODUCTION_URL_DB, + MOCK_STAGING_URL_DB, + MOCK_STAGING_URL_GITHUB, + MOCK_PRODUCTION_URL_GITHUB, } from "@fixtures/repoInfo" import { mockUserWithSiteSessionData, @@ -28,11 +35,12 @@ import { } from "@fixtures/sessionData" import mockAxios from "@mocks/axios" import { NotFoundError } from "@root/errors/NotFoundError" +import RequestNotFoundError from "@root/errors/RequestNotFoundError" import { UnprocessableError } from "@root/errors/UnprocessableError" import ReviewRequestService from "@root/services/review/ReviewRequestService" import { GitHubCommitData } from "@root/types/commitData" import { ConfigYmlData } from "@root/types/configYml" -import type { RepositoryData } from "@root/types/repoInfo" +import type { RepositoryData, SiteUrls } from "@root/types/repoInfo" import { SiteInfo } from "@root/types/siteInfo" import { GitHubService } from "@services/db/GitHubService" import { ConfigYmlService } from "@services/fileServices/YmlFileServices/ConfigYmlService" @@ -76,6 +84,10 @@ const SitesService = new _SitesService({ reviewRequestService: (MockReviewRequestService as unknown) as ReviewRequestService, }) +const SpySitesService = { + extractAuthorEmail: jest.spyOn(SitesService, "extractAuthorEmail"), +} + const mockSiteName = "some site name" const mockSite = ({ name: "i m a site", @@ -87,6 +99,431 @@ describe("SitesService", () => { // Prevent inter-test pollution of mocks afterEach(() => jest.clearAllMocks()) + describe("extractAuthorEmail", () => { + it("should return the email address of the author of the commit", () => { + // Arrange + const expected = MOCK_GITHUB_EMAIL_ADDRESS_ONE + const commitData: GitHubCommitData = { + author: { + name: MOCK_GITHUB_NAME_ONE, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + + // Act + const actual = SitesService.extractAuthorEmail(commitData) + + // Assert + expect(actual).toEqual(expected) + }) + }) + + describe("insertUrlsFromConfigYml", () => { + it("should insert URLs if both are not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_CONFIGYML, + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const configYmlResponse = { + content: { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_CONFIGYML, + }, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + + it("should only insert staging URL if it is not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_DB, + } + const initial: SiteUrls = { + staging: "", + prod: MOCK_PRODUCTION_URL_DB, + } + const configYmlResponse = { + content: { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_CONFIGYML, + }, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + + it("should only insert production URL if it is not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_CONFIGYML, + } + const initial: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: "", + } + const configYmlResponse = { + content: { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_CONFIGYML, + }, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + + it("should not insert URLs if both are already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_DB, + } + const initial: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_DB, + } + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).not.toHaveBeenCalled() + }) + + it("should not insert staging URL if it does not exist in config.yml", async () => { + // Arrange + const expected: SiteUrls = { + staging: "", + prod: MOCK_PRODUCTION_URL_CONFIGYML, + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const configYmlResponse = { + content: { + prod: MOCK_PRODUCTION_URL_CONFIGYML, + }, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + + it("should not insert production URL if it does not exist in config.yml", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: "", + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const configYmlResponse = { + content: { + staging: MOCK_STAGING_URL_CONFIGYML, + }, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + + it("should not insert URLs if config.yml does not contain both staging and production URLs", async () => { + // Arrange + const expected: SiteUrls = { + staging: "", + prod: "", + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const configYmlResponse = { + content: {}, + sha: "abc", + } + MockConfigYmlService.read.mockResolvedValueOnce(configYmlResponse) + + // Act + const actual = await SitesService.insertUrlsFromConfigYml( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockConfigYmlService.read).toHaveBeenCalled() + }) + }) + + describe("insertUrlsFromGitHubDescription", () => { + it("should insert URLs if both are not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_GITHUB, + prod: MOCK_PRODUCTION_URL_GITHUB, + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + MockGithubService.getRepoInfo.mockResolvedValueOnce(repoInfo) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should only insert staging URL if it is not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_GITHUB, + prod: MOCK_PRODUCTION_URL_DB, + } + const initial: SiteUrls = { + staging: "", + prod: MOCK_PRODUCTION_URL_DB, + } + MockGithubService.getRepoInfo.mockResolvedValueOnce(repoInfo) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should only insert production URL if it is not already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_GITHUB, + } + const initial: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: "", + } + MockGithubService.getRepoInfo.mockResolvedValueOnce(repoInfo) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should not insert URLs if both are already present", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_DB, + } + const initial: SiteUrls = { + staging: MOCK_STAGING_URL_DB, + prod: MOCK_PRODUCTION_URL_DB, + } + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).not.toHaveBeenCalled() + }) + + it("should not insert staging URL if it does not exist in the description", async () => { + // Arrange + const expected: SiteUrls = { + staging: "", + prod: MOCK_PRODUCTION_URL_CONFIGYML, + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const repoInfoWithoutStagingUrl = { + description: `Production: ${MOCK_PRODUCTION_URL_CONFIGYML}`, + } + MockGithubService.getRepoInfo.mockResolvedValueOnce( + repoInfoWithoutStagingUrl + ) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should not insert production URL if it does not exist in the description", async () => { + // Arrange + const expected: SiteUrls = { + staging: MOCK_STAGING_URL_CONFIGYML, + prod: "", + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const repoInfoWithoutProductionUrl = { + description: `Staging: ${MOCK_STAGING_URL_CONFIGYML}`, + } + MockGithubService.getRepoInfo.mockResolvedValueOnce( + repoInfoWithoutProductionUrl + ) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should not insert URLs if description is empty", async () => { + // Arrange + const expected: SiteUrls = { + staging: "", + prod: "", + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const repoInfoWithoutDescription = { + description: "", + } + MockGithubService.getRepoInfo.mockResolvedValueOnce( + repoInfoWithoutDescription + ) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + + it("should not insert URLs if description is some gibberish", async () => { + // Arrange + const expected: SiteUrls = { + staging: "", + prod: "", + } + const initial: SiteUrls = { + staging: "", + prod: "", + } + const repoInfoWithGibberishDescription = { + description: "abcdefghijklmnopqrstuvwxyz-staging and-prod", + } + MockGithubService.getRepoInfo.mockResolvedValueOnce( + repoInfoWithGibberishDescription + ) + + // Act + const actual = await SitesService.insertUrlsFromGitHubDescription( + initial, + mockSessionDataEmailUserWithSite + ) + + // Assert + expect(actual).toEqual(expected) + expect(MockGithubService.getRepoInfo).toHaveBeenCalled() + }) + }) + describe("getBySiteName", () => { it("should call the findOne method of the db model to get the siteName", async () => { // Arrange @@ -106,6 +543,34 @@ describe("SitesService", () => { }) }) + describe("getSitesForEmailUser", () => { + it("should call the findSitesByUserId method of UsersService to get the sites for the user", async () => { + // Arrange + const expected = [mockSiteName] + const mockUserWithSites = { + site_members: [ + { + repo: { + name: mockSiteName, + }, + }, + ], + } + MockUsersService.findSitesByUserId.mockResolvedValueOnce( + mockUserWithSites + ) + + // Act + const actual = await SitesService.getSitesForEmailUser(mockIsomerUserId) + + // Assert + expect(actual).toEqual(expected) + expect(MockUsersService.findSitesByUserId).toBeCalledWith( + mockIsomerUserId + ) + }) + }) + describe("getCommitAuthorEmail", () => { it("should return the email of the commit author who is an email login user", async () => { // Arrange @@ -126,6 +591,7 @@ describe("SitesService", () => { // Assert expect(actual).toBe(expected) expect(MockUsersService.findById).toHaveBeenCalledWith(mockIsomerUserId) + expect(SpySitesService.extractAuthorEmail).not.toHaveBeenCalled() }) it("should return the email of the commit author who is a GitHub login user", async () => { @@ -146,29 +612,185 @@ describe("SitesService", () => { // Assert expect(actual).toBe(expected) expect(MockUsersService.findById).not.toHaveBeenCalled() + expect(SpySitesService.extractAuthorEmail).toHaveBeenCalled() + }) + }) + + describe("getMergeAuthorEmail", () => { + it("should return the email of the merge commit author if it was not performed using the common access token", async () => { + // Arrange + const expected = MOCK_GITHUB_EMAIL_ADDRESS_ONE + const commit: GitHubCommitData = { + author: { + name: MOCK_GITHUB_NAME_ONE, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + + // Act + const actual = await SitesService.getMergeAuthorEmail( + commit, + mockUserWithSiteSessionData + ) + + // Assert + expect(actual).toEqual(expected) + expect( + MockReviewRequestService.getLatestMergedReviewRequest + ).not.toHaveBeenCalled() + expect(SpySitesService.extractAuthorEmail).toHaveBeenCalled() + }) + + it("should return the email of the merge commit author if the site cannot be found", async () => { + // Arrange + const expected = MOCK_GITHUB_EMAIL_ADDRESS_ONE + const commit: GitHubCommitData = { + author: { + name: MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + MockRepository.findOne.mockResolvedValueOnce(null) + + // Act + const actual = await SitesService.getMergeAuthorEmail( + commit, + mockUserWithSiteSessionData + ) + + // Assert + expect(actual).toEqual(expected) + expect( + MockReviewRequestService.getLatestMergedReviewRequest + ).not.toHaveBeenCalled() + expect(SpySitesService.extractAuthorEmail).toHaveBeenCalled() + }) + + it("should return the email of the merge commit author if there are no merged review requests", async () => { + // Arrange + const expected = MOCK_GITHUB_EMAIL_ADDRESS_ONE + const commit: GitHubCommitData = { + author: { + name: MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + MockRepository.findOne.mockResolvedValueOnce(mockSite) + MockReviewRequestService.getLatestMergedReviewRequest.mockResolvedValueOnce( + new RequestNotFoundError() + ) + + // Act + const actual = await SitesService.getMergeAuthorEmail( + commit, + mockUserWithSiteSessionData + ) + + // Assert + expect(actual).toEqual(expected) + expect( + MockReviewRequestService.getLatestMergedReviewRequest + ).toHaveBeenCalledWith(mockSite) + expect(SpySitesService.extractAuthorEmail).toHaveBeenCalled() + }) + + it("should return the email of the requestor for the latest merged review request", async () => { + // Arrange + const expected = mockEmail + const commit: GitHubCommitData = { + author: { + name: MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + const mockReviewRequest = { + requestor: { + email: mockEmail, + }, + } + MockRepository.findOne.mockResolvedValueOnce(mockSite) + MockReviewRequestService.getLatestMergedReviewRequest.mockResolvedValueOnce( + mockReviewRequest + ) + + // Act + const actual = await SitesService.getMergeAuthorEmail( + commit, + mockUserWithSiteSessionData + ) + + // Assert + expect(actual).toEqual(expected) + expect( + MockReviewRequestService.getLatestMergedReviewRequest + ).toHaveBeenCalledWith(mockSite) + expect(SpySitesService.extractAuthorEmail).not.toHaveBeenCalled() + }) + + it("should return the email of the merge commit author if the requestor for the latest merged review request does not have an email", async () => { + // Arrange + const expected = MOCK_GITHUB_EMAIL_ADDRESS_ONE + const commit: GitHubCommitData = { + author: { + name: MOCK_COMMON_ACCESS_TOKEN_GITHUB_NAME, + email: MOCK_GITHUB_EMAIL_ADDRESS_ONE, + date: MOCK_GITHUB_DATE_ONE, + }, + message: MOCK_COMMIT_MESSAGE_ONE, + } + const mockReviewRequest = { + requestor: { + email: null, + }, + } + MockRepository.findOne.mockResolvedValueOnce(mockSite) + MockReviewRequestService.getLatestMergedReviewRequest.mockResolvedValueOnce( + mockReviewRequest + ) + + // Act + const actual = await SitesService.getMergeAuthorEmail( + commit, + mockUserWithSiteSessionData + ) + + // Assert + expect(actual).toEqual(expected) + expect( + MockReviewRequestService.getLatestMergedReviewRequest + ).toHaveBeenCalledWith(mockSite) + expect(SpySitesService.extractAuthorEmail).toHaveBeenCalled() }) }) describe("getUrlsOfSite", () => { const deployment: Partial = { - stagingUrl: "https://repo-deployment-staging.netlify.app", - productionUrl: "https://repo-deployment-prod.netlify.app", + stagingUrl: MOCK_STAGING_URL_DB, + productionUrl: MOCK_PRODUCTION_URL_DB, } const emptyDeployment: Partial = { stagingUrl: "", productionUrl: "", } const configYmlData: Partial = { - staging: "https://repo-configyml-staging.netlify.app", - prod: "https://repo-configyml-prod.netlify.app", + staging: MOCK_STAGING_URL_CONFIGYML, + prod: MOCK_PRODUCTION_URL_CONFIGYML, } const emptyConfigYmlData: Partial = { staging: "", prod: "", } const gitHubUrls = { - staging: "https://repo-repoinfo-staging.netlify.app", - prod: "https://repo-repoinfo-prod.netlify.app", + staging: MOCK_STAGING_URL_GITHUB, + prod: MOCK_PRODUCTION_URL_GITHUB, } const repoInfo: { description: string } = { description: `Staging: ${gitHubUrls.staging} | Production: ${gitHubUrls.prod}`, @@ -464,14 +1086,14 @@ describe("SitesService", () => { }) describe("getStagingUrl", () => { - const stagingUrl = "https://repo-staging.netlify.app" - const productionUrl = "https://repo-prod.netlify.app" - it("should return the staging URL if it is available", async () => { // Arrange const mockSiteWithDeployment = { ...mockSite, - deployment: { stagingUrl, productionUrl }, + deployment: { + stagingUrl: MOCK_STAGING_URL_DB, + productionUrl: MOCK_PRODUCTION_URL_DB, + }, } MockRepository.findOne.mockResolvedValueOnce(mockSiteWithDeployment) @@ -482,7 +1104,7 @@ describe("SitesService", () => { ) // Assert - expect(actual).toEqual(stagingUrl) + expect(actual).toEqual(MOCK_STAGING_URL_DB) expect(MockRepository.findOne).toHaveBeenCalled() }) @@ -509,14 +1131,14 @@ describe("SitesService", () => { }) describe("getSiteUrl", () => { - const stagingUrl = "https://repo-staging.netlify.app" - const productionUrl = "https://repo-prod.netlify.app" - it("should return the site URL if it is available", async () => { // Arrange const mockSiteWithDeployment = { ...mockSite, - deployment: { stagingUrl, productionUrl }, + deployment: { + stagingUrl: MOCK_STAGING_URL_DB, + productionUrl: MOCK_PRODUCTION_URL_DB, + }, } MockRepository.findOne.mockResolvedValueOnce(mockSiteWithDeployment) @@ -527,7 +1149,7 @@ describe("SitesService", () => { ) // Assert - expect(actual).toEqual(productionUrl) + expect(actual).toEqual(MOCK_PRODUCTION_URL_DB) expect(MockRepository.findOne).toHaveBeenCalled() }) @@ -554,13 +1176,11 @@ describe("SitesService", () => { }) describe("getSiteInfo", () => { - const stagingUrl = "https://repo-staging.netlify.app" - const productionUrl = "https://repo-prod.netlify.app" const mockSiteWithDeployment = { ...mockSite, deployment: { - stagingUrl, - productionUrl, + stagingUrl: MOCK_STAGING_URL_DB, + productionUrl: MOCK_PRODUCTION_URL_DB, }, } @@ -593,8 +1213,8 @@ describe("SitesService", () => { savedBy: MOCK_GITHUB_EMAIL_ADDRESS_ONE, publishedAt: new Date(MOCK_GITHUB_DATE_TWO).getTime(), publishedBy: MOCK_GITHUB_EMAIL_ADDRESS_TWO, - stagingUrl, - siteUrl: productionUrl, + stagingUrl: MOCK_STAGING_URL_DB, + siteUrl: MOCK_PRODUCTION_URL_DB, } MockRepository.findOne.mockResolvedValueOnce(mockSiteWithDeployment) @@ -644,8 +1264,8 @@ describe("SitesService", () => { savedBy: MOCK_GITHUB_EMAIL_ADDRESS_ONE, publishedAt: new Date(MOCK_GITHUB_DATE_TWO).getTime(), publishedBy: MOCK_GITHUB_EMAIL_ADDRESS_TWO, - stagingUrl, - siteUrl: productionUrl, + stagingUrl: MOCK_STAGING_URL_DB, + siteUrl: MOCK_PRODUCTION_URL_DB, } MockRepository.findOne.mockResolvedValueOnce(mockSiteWithDeployment) @@ -712,8 +1332,8 @@ describe("SitesService", () => { savedBy: "Unknown Author", publishedAt: 0, publishedBy: "Unknown Author", - stagingUrl, - siteUrl: productionUrl, + stagingUrl: MOCK_STAGING_URL_DB, + siteUrl: MOCK_PRODUCTION_URL_DB, } const mockEmptyCommit: GitHubCommitData = { diff --git a/src/types/repoInfo.ts b/src/types/repoInfo.ts index 899415785..1bd9e3ec6 100644 --- a/src/types/repoInfo.ts +++ b/src/types/repoInfo.ts @@ -18,3 +18,6 @@ export type RepositoryData = { repoName: GitHubRepositoryData["name"] isPrivate: GitHubRepositoryData["private"] } + +type SiteUrlTypes = "staging" | "prod" +export type SiteUrls = { [key in SiteUrlTypes]: string }