Skip to content

Commit

Permalink
feat: add new siteUrl API endpoint to get the production URL of site (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dcshzj authored Oct 20, 2022
1 parent 177891e commit 90eb48e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/routes/v2/authenticated/__tests__/Sites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("Sites Router", () => {
getSites: jest.fn(),
getLastUpdated: jest.fn(),
getStagingUrl: jest.fn(),
getSiteUrl: jest.fn(),
getSiteInfo: jest.fn(),
}

Expand All @@ -43,6 +44,10 @@ describe("Sites Router", () => {
"/:siteName/stagingUrl",
attachReadRouteHandlerWrapper(router.getStagingUrl)
)
subrouter.get(
"/:siteName/siteUrl",
attachReadRouteHandlerWrapper(router.getSiteUrl)
)
subrouter.get(
"/:siteName/info",
attachReadRouteHandlerWrapper(router.getSiteInfo)
Expand Down Expand Up @@ -92,13 +97,29 @@ describe("Sites Router", () => {
.get(`/${mockSiteName}/stagingUrl`)
.expect(200)

expect(resp.body).toStrictEqual({ possibleStagingUrl: stagingUrl })
expect(resp.body).toStrictEqual({ stagingUrl })
expect(mockSitesService.getStagingUrl).toHaveBeenCalledWith(
mockUserWithSiteSessionData
)
})
})

describe("getSiteUrl", () => {
it("returns the site's site URL", async () => {
const siteUrl = "prod-url"
mockSitesService.getSiteUrl.mockResolvedValueOnce(siteUrl)

const resp = await request(app)
.get(`/${mockSiteName}/siteUrl`)
.expect(200)

expect(resp.body).toStrictEqual({ siteUrl })
expect(mockSitesService.getSiteUrl).toHaveBeenCalledWith(
mockUserWithSiteSessionData
)
})
})

describe("getSiteInfo", () => {
it("returns the site's info", async () => {
const siteInfo = {
Expand Down
26 changes: 25 additions & 1 deletion src/routes/v2/authenticated/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,26 @@ export class SitesRouter {
if (possibleStagingUrl instanceof BaseIsomerError) {
return res.status(404).json({ message: possibleStagingUrl.message })
}
return res.status(200).json({ possibleStagingUrl })
return res.status(200).json({ stagingUrl: possibleStagingUrl })
}

getSiteUrl: RequestHandler<
{ siteName: string },
unknown,
never,
never,
{ userWithSiteSessionData: UserWithSiteSessionData }
> = async (req, res) => {
const { userWithSiteSessionData } = res.locals
const possibleSiteUrl = await this.sitesService.getSiteUrl(
userWithSiteSessionData
)

// Check for error and throw
if (possibleSiteUrl instanceof BaseIsomerError) {
return res.status(404).json({ message: possibleSiteUrl.message })
}
return res.status(200).json({ siteUrl: possibleSiteUrl })
}

getSiteInfo: RequestHandler<
Expand Down Expand Up @@ -108,6 +127,11 @@ export class SitesRouter {
attachSiteHandler,
attachReadRouteHandlerWrapper(this.getStagingUrl)
)
router.get(
"/:siteName/siteUrl",
attachSiteHandler,
attachReadRouteHandlerWrapper(this.getSiteUrl)
)
router.get(
"/:siteName/info",
attachSiteHandler,
Expand Down
15 changes: 15 additions & 0 deletions src/services/identity/SitesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ class SitesService {
return staging
}

async getSiteUrl(
sessionData: UserWithSiteSessionData
): Promise<string | NotFoundError> {
const siteUrls = await this.getUrlsOfSite(sessionData)
if (siteUrls instanceof NotFoundError) {
return new NotFoundError(
`${sessionData.siteName} does not have a site url`
)
}

const { prod } = siteUrls

return prod
}

async create(
createParams: Partial<Site> & {
name: Site["name"]
Expand Down
45 changes: 45 additions & 0 deletions src/services/identity/__tests__/SitesService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,51 @@ 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 },
}

MockRepository.findOne.mockResolvedValueOnce(mockSiteWithDeployment)

// Act
const actual = await SitesService.getSiteUrl(
mockSessionDataEmailUserWithSite
)

// Assert
expect(actual).toEqual(productionUrl)
expect(MockRepository.findOne).toHaveBeenCalled()
})

it("should return an error when the site url for a repo is not found", async () => {
// Arrange
MockRepository.findOne.mockResolvedValueOnce(null)
MockConfigYmlService.read.mockResolvedValueOnce({
content: {},
})
MockGithubService.getRepoInfo.mockResolvedValueOnce({
description: "",
})

// Act
await expect(
SitesService.getSiteUrl(mockUserWithSiteSessionData)
).resolves.toBeInstanceOf(NotFoundError)

// Assert
expect(MockRepository.findOne).toHaveBeenCalled()
expect(MockConfigYmlService.read).toHaveBeenCalled()
expect(MockGithubService.getRepoInfo).toHaveBeenCalled()
})
})

describe("getSiteInfo", () => {
const stagingUrl = "https://repo-staging.netlify.app"
const productionUrl = "https://repo-prod.netlify.app"
Expand Down

0 comments on commit 90eb48e

Please sign in to comment.