-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce a new site info API endpoint (#513)
* ref(fixtures): convert repoInfo to typescript * ref(services): migrate SitesService to typescript * tests: update unit and integration tests for SitesService * ref(sites): migrate sites router to typescript * fix: revert back to using SessionData * fix: remove use of Bluebird and unused getSiteToken function * fix: use more accurate type * chore: remove unused variable * refactor(tests): migrate generic axios instance to __mocks__ * feat: introduce function to obtain latest commit details * feat: add function for obtaining a User by ID * feat: introduce a new site info API endpoint * tests: add partial tests for SitesService * tests: use mockAxios directly instead of preparing an instance * tests: fix SitesService unit tests to pass * chore: adjust constants to use SCREAMING_SNAKE_CASE * fix: add authorizationMiddleware to ensure user is member of site * chore: combine sessionData unpacking * fix: insert try-catch to handle errors from JSON.parse * chore: remove unnecessary check for undefined site * chore: return instead of throwing NotFoundError * fix: add assertion to ensure integrity of GitHubCommitData * fix: remove need for adding site name to sessionData * refactor: convert routes Sites.spec.js to TypeScript * refactor: redesign getUrlsOfSite to increase readability * fix: use correct endpoint to get latest commit data * test: add unit tests for GitHubService getLatestCommitOfBranch * fix: add stub for obtaining merge author details * fix: return a well-formatted response for known exceptions * test: enhance GitHubService test for all other error statuses * chore: rename isType function and return boolean directly * fix: create new siteUrls object instead of changing in-place * fix: handle case of null or undefined user email * chore: improve code style * tests: fix output of getStagingUrl
- Loading branch information
Showing
13 changed files
with
899 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ import { Attributes } from "sequelize/types" | |
|
||
import { User, SiteMember } from "@database/models" | ||
|
||
import { mockIsomerUserId } from "./sessionData" | ||
|
||
export const mockRecipient = "[email protected]" | ||
export const mockSubject = "mock subject" | ||
export const mockBody = "somebody" | ||
|
@@ -138,3 +140,25 @@ export const mockSiteOrmResponseWithNoCollaborators = { | |
id: 1, | ||
site_members: "", | ||
} | ||
|
||
export const MOCK_COMMIT_MESSAGE_ONE = "Update file: Example.md" | ||
export const MOCK_COMMIT_FILENAME_ONE = "Example.md" | ||
export const MOCK_GITHUB_NAME_ONE = "testuser" | ||
export const MOCK_GITHUB_EMAIL_ADDRESS_ONE = "[email protected]" | ||
export const MOCK_GITHUB_DATE_ONE = "2022-09-22T04:07:53Z" | ||
export const MOCK_COMMIT_MESSAGE_OBJECT_ONE = { | ||
message: MOCK_COMMIT_MESSAGE_ONE, | ||
fileName: MOCK_COMMIT_FILENAME_ONE, | ||
userId: mockIsomerUserId, | ||
} | ||
|
||
export const MOCK_COMMIT_MESSAGE_TWO = "Update file: Test.md" | ||
export const MOCK_COMMIT_FILENAME_TWO = "Test.md" | ||
export const MOCK_GITHUB_NAME_TWO = "testuser2" | ||
export const MOCK_GITHUB_EMAIL_ADDRESS_TWO = "[email protected]" | ||
export const MOCK_GITHUB_DATE_TWO = "2022-09-28T06:25:14Z" | ||
export const MOCK_COMMIT_MESSAGE_OBJECT_TWO = { | ||
message: MOCK_COMMIT_MESSAGE_TWO, | ||
fileName: MOCK_COMMIT_FILENAME_TWO, | ||
userId: mockIsomerUserId, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,40 @@ | ||
const express = require("express") | ||
const request = require("supertest") | ||
import express from "express" | ||
import request from "supertest" | ||
|
||
const { attachReadRouteHandlerWrapper } = require("@middleware/routeHandler") | ||
import type { AuthorizationMiddleware } from "@middleware/authorization" | ||
import { attachReadRouteHandlerWrapper } from "@middleware/routeHandler" | ||
|
||
const { generateRouter } = require("@fixtures/app") | ||
const { | ||
import { generateRouter } from "@fixtures/app" | ||
import { | ||
mockSiteName, | ||
mockUserSessionData, | ||
mockUserWithSiteSessionData, | ||
} = require("@fixtures/sessionData") | ||
} from "@fixtures/sessionData" | ||
import type SitesService from "@services/identity/SitesService" | ||
|
||
const { SitesRouter } = require("../sites") | ||
import { SitesRouter } from "../sites" | ||
|
||
describe("Sites Router", () => { | ||
const mockSitesService = { | ||
getSites: jest.fn(), | ||
checkHasAccess: jest.fn(), | ||
getLastUpdated: jest.fn(), | ||
getStagingUrl: jest.fn(), | ||
getSiteInfo: jest.fn(), | ||
} | ||
|
||
const mockAuthorizationMiddleware = { | ||
verifySiteMember: jest.fn(), | ||
} | ||
|
||
const router = new SitesRouter({ | ||
sitesService: mockSitesService, | ||
sitesService: (mockSitesService as unknown) as SitesService, | ||
authorizationMiddleware: (mockAuthorizationMiddleware as unknown) as AuthorizationMiddleware, | ||
}) | ||
|
||
const subrouter = express() | ||
|
||
// We can use read route handler here because we don't need to lock the repo | ||
subrouter.get("/", attachReadRouteHandlerWrapper(router.getSites)) | ||
subrouter.get( | ||
"/:siteName", | ||
attachReadRouteHandlerWrapper(router.checkHasAccess) | ||
) | ||
subrouter.get( | ||
"/:siteName/lastUpdated", | ||
attachReadRouteHandlerWrapper(router.getLastUpdated) | ||
|
@@ -40,6 +43,10 @@ describe("Sites Router", () => { | |
"/:siteName/stagingUrl", | ||
attachReadRouteHandlerWrapper(router.getStagingUrl) | ||
) | ||
subrouter.get( | ||
"/:siteName/info", | ||
attachReadRouteHandlerWrapper(router.getSiteInfo) | ||
) | ||
const app = generateRouter(subrouter) | ||
|
||
beforeEach(() => { | ||
|
@@ -77,18 +84,39 @@ describe("Sites Router", () => { | |
}) | ||
|
||
describe("getStagingUrl", () => { | ||
it("returns the last updated time", async () => { | ||
it("returns the site's staging URL", async () => { | ||
const stagingUrl = "staging-url" | ||
mockSitesService.getStagingUrl.mockResolvedValueOnce(stagingUrl) | ||
|
||
const resp = await request(app) | ||
.get(`/${mockSiteName}/stagingUrl`) | ||
.expect(200) | ||
|
||
expect(resp.body).toStrictEqual({ stagingUrl }) | ||
expect(resp.body).toStrictEqual({ possibleStagingUrl: stagingUrl }) | ||
expect(mockSitesService.getStagingUrl).toHaveBeenCalledWith( | ||
mockUserWithSiteSessionData | ||
) | ||
}) | ||
}) | ||
|
||
describe("getSiteInfo", () => { | ||
it("returns the site's info", async () => { | ||
const siteInfo = { | ||
savedAt: 12345678, | ||
savedBy: "[email protected]", | ||
publishedAt: 23456789, | ||
publishedBy: "[email protected]", | ||
stagingUrl: "staging-url", | ||
siteUrl: "prod-url", | ||
} | ||
mockSitesService.getSiteInfo.mockResolvedValueOnce(siteInfo) | ||
|
||
const resp = await request(app).get(`/${mockSiteName}/info`).expect(200) | ||
|
||
expect(resp.body).toStrictEqual(siteInfo) | ||
expect(mockSitesService.getSiteInfo).toHaveBeenCalledWith( | ||
mockUserWithSiteSessionData | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.