From 585a5631f0b61ab664bc2e49f27e4348ad8de0e6 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Tue, 18 Oct 2022 14:26:27 +0800 Subject: [PATCH] refactor: remove all usages of TokenStore and tests (#533) --- src/integration/Sites.spec.ts | 3 -- src/server.js | 11 +---- src/services/identity/SitesService.ts | 7 --- src/services/identity/TokenStore.ts | 37 -------------- .../identity/__tests__/SitesService.spec.ts | 6 --- .../identity/__tests__/TokenStore.spec.ts | 48 ------------------- 6 files changed, 1 insertion(+), 111 deletions(-) delete mode 100644 src/services/identity/TokenStore.ts delete mode 100644 src/services/identity/__tests__/TokenStore.spec.ts diff --git a/src/integration/Sites.spec.ts b/src/integration/Sites.spec.ts index f48873a7e..941bf365c 100644 --- a/src/integration/Sites.spec.ts +++ b/src/integration/Sites.spec.ts @@ -19,7 +19,6 @@ import { GitHubService } from "@root/services/db/GitHubService" import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/ConfigYmlService" import IsomerAdminsService from "@root/services/identity/IsomerAdminsService" import SitesService from "@root/services/identity/SitesService" -import TokenStore from "@root/services/identity/TokenStore" import { getIdentityAuthService, getUsersService } from "@services/identity" import CollaboratorsService from "@services/identity/CollaboratorsService" import { sequelize } from "@tests/database" @@ -35,7 +34,6 @@ const gitHubService = new GitHubService({ axiosInstance: mockAxios.create() }) const configYmlService = new ConfigYmlService({ gitHubService }) const usersService = getUsersService(sequelize) const isomerAdminsService = new IsomerAdminsService({ repository: IsomerAdmin }) -const tokenStore = new TokenStore() const identityAuthService = getIdentityAuthService(gitHubService) const sitesService = new SitesService({ siteRepository: Site, @@ -43,7 +41,6 @@ const sitesService = new SitesService({ configYmlService, usersService, isomerAdminsService, - tokenStore, }) const collaboratorsService = new CollaboratorsService({ siteRepository: Site, diff --git a/src/server.js b/src/server.js index 2e1feda84..bede11a80 100644 --- a/src/server.js +++ b/src/server.js @@ -58,15 +58,7 @@ const helmet = require("helmet") const createError = require("http-errors") // Env vars -const { FRONTEND_URL, NODE_ENV, LOCAL_SITE_ACCESS_TOKEN } = process.env -const IS_LOCAL_DEV = NODE_ENV === "LOCAL_DEV" - -const tokenStore = IS_LOCAL_DEV - ? { - getToken: (_apiTokenName) => LOCAL_SITE_ACCESS_TOKEN, - } - : new TokenStore() - +const { FRONTEND_URL } = process.env // Import middleware // Import routes @@ -96,7 +88,6 @@ const sitesService = new SitesService({ configYmlService, usersService, isomerAdminsService, - tokenStore, }) const infraService = new InfraService({ sitesService, diff --git a/src/services/identity/SitesService.ts b/src/services/identity/SitesService.ts index d2d59717b..c5a6cb11c 100644 --- a/src/services/identity/SitesService.ts +++ b/src/services/identity/SitesService.ts @@ -22,15 +22,12 @@ import { ConfigYmlService } from "@services/fileServices/YmlFileServices/ConfigY import IsomerAdminsService from "@services/identity/IsomerAdminsService" import UsersService from "@services/identity/UsersService" -import TokenStore from "./TokenStore" - interface SitesServiceProps { siteRepository: ModelStatic gitHubService: GitHubService configYmlService: ConfigYmlService usersService: UsersService isomerAdminsService: IsomerAdminsService - tokenStore: TokenStore } type SiteUrlTypes = "staging" | "prod" @@ -49,22 +46,18 @@ class SitesService { private readonly isomerAdminsService: SitesServiceProps["isomerAdminsService"] - private readonly tokenStore: SitesServiceProps["tokenStore"] - constructor({ siteRepository, gitHubService, configYmlService, usersService, isomerAdminsService, - tokenStore, }: SitesServiceProps) { this.siteRepository = siteRepository this.gitHubService = gitHubService this.configYmlService = configYmlService this.usersService = usersService this.isomerAdminsService = isomerAdminsService - this.tokenStore = tokenStore } isGitHubCommitData(commit: any): commit is GitHubCommitData { diff --git a/src/services/identity/TokenStore.ts b/src/services/identity/TokenStore.ts deleted file mode 100644 index 8ce3e2e20..000000000 --- a/src/services/identity/TokenStore.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { - SecretsManagerClient, - GetSecretValueCommand, - SecretsManagerClientConfig, -} from "@aws-sdk/client-secrets-manager" - -class TokenStore { - private readonly secretsClient: SecretsManagerClient - - constructor() { - this.secretsClient = this.createClient() - } - - private createClient() { - const { AWS_REGION, AWS_ENDPOINT } = process.env - const config: SecretsManagerClientConfig = { - region: AWS_REGION || "ap-southeast-1", - } - - // Use an alternate AWS endpoint if provided. For testing with localstack - if (AWS_ENDPOINT) config.endpoint = AWS_ENDPOINT - - return new SecretsManagerClient(config) - } - - // NOTE: This is currently stricter than required. - // We can relax the constraint so that it can be undefined in the future. - async getToken(apiTokenName: string): Promise { - const command = new GetSecretValueCommand({ - SecretId: apiTokenName, - }) - const { SecretString: apiToken } = await this.secretsClient.send(command) - return apiToken - } -} - -export default TokenStore diff --git a/src/services/identity/__tests__/SitesService.spec.ts b/src/services/identity/__tests__/SitesService.spec.ts index acf8eed6b..829cb941a 100644 --- a/src/services/identity/__tests__/SitesService.spec.ts +++ b/src/services/identity/__tests__/SitesService.spec.ts @@ -37,7 +37,6 @@ import { GitHubService } from "@services/db/GitHubService" import { ConfigYmlService } from "@services/fileServices/YmlFileServices/ConfigYmlService" import IsomerAdminsService from "@services/identity/IsomerAdminsService" import _SitesService from "@services/identity/SitesService" -import TokenStore from "@services/identity/TokenStore" import UsersService from "@services/identity/UsersService" const MockRepository = { @@ -63,17 +62,12 @@ const MockIsomerAdminsService = { getByUserId: jest.fn(), } -const MockTokenStore = { - getToken: jest.fn(), -} - const SitesService = new _SitesService({ siteRepository: (MockRepository as unknown) as ModelStatic, gitHubService: (MockGithubService as unknown) as GitHubService, configYmlService: (MockConfigYmlService as unknown) as ConfigYmlService, usersService: (MockUsersService as unknown) as UsersService, isomerAdminsService: (MockIsomerAdminsService as unknown) as IsomerAdminsService, - tokenStore: (MockTokenStore as unknown) as TokenStore, }) const mockSiteName = "some site name" diff --git a/src/services/identity/__tests__/TokenStore.spec.ts b/src/services/identity/__tests__/TokenStore.spec.ts deleted file mode 100644 index c522ae542..000000000 --- a/src/services/identity/__tests__/TokenStore.spec.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { - secretsManagerClient as mockSecretsManager, - GetSecretValueCommand, -} from "@mocks/@aws-sdk/client-secrets-manager" - -import _TokenStore from "../TokenStore" - -const apiTokenName = "some token" -const TokenStore = new _TokenStore() - -describe("Token Store", () => { - beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - mockSecretsManager.send.mockClear() - }) - - it("should return the api token when the parameters are valid", async () => { - // Arrange - const expected = "some api" - const awsCommand = new GetSecretValueCommand({ - SecretId: apiTokenName, - }) - mockSecretsManager.send.mockResolvedValueOnce({ SecretString: expected }) - - // Act - const actual = await TokenStore.getToken(apiTokenName) - - // Assert - expect(actual).toBe(expected) - expect(mockSecretsManager.send).toHaveBeenCalledWith(awsCommand) - }) - - it("should return the error when the secrets client fails to retrieve credentials", () => { - // Arrange - const expected = Error("oh noes") - const awsCommand = new GetSecretValueCommand({ - SecretId: apiTokenName, - }) - mockSecretsManager.send.mockRejectedValueOnce(expected) - - // Act - const actual = TokenStore.getToken(apiTokenName) - - // Assert - expect(actual).rejects.toBe(expected) - expect(mockSecretsManager.send).toHaveBeenCalledWith(awsCommand) - }) -})