From 9406cdd31b5a9231d9364d2602d95bcd9005d7d0 Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Fri, 13 Oct 2023 16:48:03 +0800 Subject: [PATCH] fix(tests): fix failing tests --- .../db/__tests__/GitFileSystemService.spec.ts | 254 +++++++++++++----- .../db/__tests__/GitHubService.spec.ts | 79 +++++- src/services/db/__tests__/RepoService.spec.ts | 82 +++--- 3 files changed, 311 insertions(+), 104 deletions(-) diff --git a/src/services/db/__tests__/GitFileSystemService.spec.ts b/src/services/db/__tests__/GitFileSystemService.spec.ts index 92e3ad221..32baf22f7 100644 --- a/src/services/db/__tests__/GitFileSystemService.spec.ts +++ b/src/services/db/__tests__/GitFileSystemService.spec.ts @@ -33,22 +33,26 @@ const GitFileSystemService = new _GitFileSystemService( ) const BRANCH_REF = config.get("github.branchRef") +const DEFAULT_BRANCH = "staging" + +const dirTree = { + "fake-repo": { + "fake-dir": { + "fake-file": "fake content", + }, + "another-fake-dir": { + "fake-file": "duplicate fake file", + }, + "fake-empty-dir": {}, + "another-fake-file": "Another fake content", + }, +} describe("GitFileSystemService", () => { beforeEach(() => { mockFs({ - [config.get("aws.efs.volPath")]: { - "fake-repo": { - "fake-dir": { - "fake-file": "fake content", - }, - "another-fake-dir": { - "fake-file": "duplicate fake file", - }, - "fake-empty-dir": {}, - "another-fake-file": "Another fake content", - }, - }, + [config.get("aws.efs.volPathStaging")]: dirTree, + [config.get("aws.efs.volPathStagingLite")]: dirTree, }) }) @@ -104,7 +108,8 @@ describe("GitFileSystemService", () => { const result = await GitFileSystemService.listDirectoryContents( "fake-repo", - "" + "", + DEFAULT_BRANCH ) const actual = result ._unsafeUnwrap() @@ -149,7 +154,8 @@ describe("GitFileSystemService", () => { const result = await GitFileSystemService.listDirectoryContents( "fake-repo", - "" + "", + DEFAULT_BRANCH ) const actual = result @@ -175,7 +181,8 @@ describe("GitFileSystemService", () => { const actual = await GitFileSystemService.listDirectoryContents( "fake-repo", - "" + "", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toHaveLength(0) @@ -184,7 +191,8 @@ describe("GitFileSystemService", () => { it("should return an empty result if the directory is empty", async () => { const actual = await GitFileSystemService.listDirectoryContents( "fake-repo", - "fake-empty-dir" + "fake-empty-dir", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toHaveLength(0) @@ -193,7 +201,8 @@ describe("GitFileSystemService", () => { it("should return a GitFileSystemError if the path is not a directory", async () => { const result = await GitFileSystemService.listDirectoryContents( "fake-repo", - "fake-dir/fake-file" + "fake-dir/fake-file", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -202,7 +211,8 @@ describe("GitFileSystemService", () => { it("should return a NotFoundError if the path does not exist", async () => { const result = await GitFileSystemService.listDirectoryContents( "fake-repo", - "non-existent-dir" + "non-existent-dir", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(NotFoundError) @@ -235,7 +245,10 @@ describe("GitFileSystemService", () => { checkIsRepo: jest.fn().mockRejectedValueOnce(new GitError()), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) }) @@ -300,7 +313,10 @@ describe("GitFileSystemService", () => { ), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrap()).toBeTrue() }) @@ -313,7 +329,10 @@ describe("GitFileSystemService", () => { remote: jest.fn().mockResolvedValueOnce(null), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrap()).toBeFalse() }) @@ -330,7 +349,10 @@ describe("GitFileSystemService", () => { ), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrap()).toBeFalse() }) @@ -340,13 +362,19 @@ describe("GitFileSystemService", () => { checkIsRepo: jest.fn().mockResolvedValueOnce(false), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrap()).toBeFalse() }) it("should mark a non-existent folder as invalid", async () => { - const result = await GitFileSystemService.isValidGitRepo("non-existent") + const result = await GitFileSystemService.isValidGitRepo( + "non-existent", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrap()).toBeFalse() }) @@ -356,7 +384,10 @@ describe("GitFileSystemService", () => { checkIsRepo: jest.fn().mockRejectedValueOnce(new GitError()), }) - const result = await GitFileSystemService.isValidGitRepo("fake-repo") + const result = await GitFileSystemService.isValidGitRepo( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) }) @@ -485,7 +516,8 @@ describe("GitFileSystemService", () => { it("should return the filesystem stats for a valid file", async () => { const result = await GitFileSystemService.getFilePathStats( "fake-repo", - "fake-dir/fake-file" + "fake-dir/fake-file", + DEFAULT_BRANCH === "staging" ) expect(result._unsafeUnwrap().isFile()).toBeTrue() @@ -494,7 +526,8 @@ describe("GitFileSystemService", () => { it("should return the filesystem stats for a valid directory", async () => { const result = await GitFileSystemService.getFilePathStats( "fake-repo", - "fake-empty-dir" + "fake-empty-dir", + DEFAULT_BRANCH === "staging" ) expect(result._unsafeUnwrap().isDirectory()).toBeTrue() @@ -503,7 +536,8 @@ describe("GitFileSystemService", () => { it("should return a NotFoundError for a non-existent path", async () => { const result = await GitFileSystemService.getFilePathStats( "fake-repo", - "non-existent" + "non-existent", + DEFAULT_BRANCH === "staging" ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(NotFoundError) @@ -579,13 +613,17 @@ describe("GitFileSystemService", () => { checkout: jest.fn().mockResolvedValueOnce(undefined), }), }) - + MockSimpleGit.clone.mockReturnValueOnce({ + cwd: jest.fn().mockResolvedValueOnce({}), + }) const result = await GitFileSystemService.clone("new-fake-repo") - expect(result.isOk()).toBeTrue() }) it("should do nothing if a valid Git repo already exists", async () => { + MockSimpleGit.cwd.mockReturnValueOnce({ + checkIsRepo: jest.fn().mockResolvedValueOnce(true), + }) MockSimpleGit.cwd.mockReturnValueOnce({ checkIsRepo: jest.fn().mockResolvedValueOnce(true), }) @@ -596,6 +634,13 @@ describe("GitFileSystemService", () => { `git@github.com:${ISOMER_GITHUB_ORG_NAME}/fake-repo.git` ), }) + MockSimpleGit.cwd.mockReturnValueOnce({ + remote: jest + .fn() + .mockResolvedValueOnce( + `git@github.com:${ISOMER_GITHUB_ORG_NAME}/fake-repo.git` + ), + }) const result = await GitFileSystemService.clone("fake-repo") @@ -603,6 +648,9 @@ describe("GitFileSystemService", () => { }) it("should return a GitFileSystemError if an existing folder exists but does not have a valid remote", async () => { + MockSimpleGit.cwd.mockReturnValueOnce({ + checkIsRepo: jest.fn().mockResolvedValueOnce(true), + }) MockSimpleGit.cwd.mockReturnValueOnce({ checkIsRepo: jest.fn().mockResolvedValueOnce(true), }) @@ -613,6 +661,13 @@ describe("GitFileSystemService", () => { `git@github.com:${ISOMER_GITHUB_ORG_NAME}/another-fake-repo.git` ), }) + MockSimpleGit.cwd.mockReturnValueOnce({ + remote: jest + .fn() + .mockResolvedValueOnce( + `git@github.com:${ISOMER_GITHUB_ORG_NAME}/another-fake-repo.git` + ), + }) const result = await GitFileSystemService.clone("fake-repo") @@ -623,6 +678,9 @@ describe("GitFileSystemService", () => { MockSimpleGit.cwd.mockReturnValueOnce({ checkIsRepo: jest.fn().mockResolvedValueOnce(false), }) + MockSimpleGit.cwd.mockReturnValueOnce({ + checkIsRepo: jest.fn().mockResolvedValueOnce(false), + }) const result = await GitFileSystemService.clone("fake-repo") @@ -635,6 +693,9 @@ describe("GitFileSystemService", () => { checkout: jest.fn().mockRejectedValueOnce(new GitError()), }), }) + MockSimpleGit.clone.mockReturnValueOnce({ + cwd: jest.fn().mockRejectedValueOnce(new GitError()), + }) const result = await GitFileSystemService.clone("anothers-fake-repo") @@ -661,7 +722,10 @@ describe("GitFileSystemService", () => { pull: jest.fn().mockResolvedValueOnce(undefined), }) - const result = await GitFileSystemService.pull("fake-repo") + const result = await GitFileSystemService.pull( + "fake-repo", + DEFAULT_BRANCH + ) expect(result.isOk()).toBeTrue() }) @@ -684,7 +748,10 @@ describe("GitFileSystemService", () => { pull: jest.fn().mockRejectedValueOnce(new GitError()), }) - const result = await GitFileSystemService.pull("fake-repo") + const result = await GitFileSystemService.pull( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) }) @@ -694,7 +761,10 @@ describe("GitFileSystemService", () => { checkIsRepo: jest.fn().mockResolvedValueOnce(false), }) - const result = await GitFileSystemService.pull("fake-repo") + const result = await GitFileSystemService.pull( + "fake-repo", + DEFAULT_BRANCH + ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) }) @@ -851,7 +921,8 @@ describe("GitFileSystemService", () => { "fake-repo", [fakePath], MOCK_USER_ID_ONE.toString(), - MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_ONE + MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_ONE, + DEFAULT_BRANCH ) expect(result._unsafeUnwrap()).toBe(mockCommitSha) @@ -892,6 +963,7 @@ describe("GitFileSystemService", () => { [fakePath], MOCK_USER_ID_ONE.toString(), MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_ONE, + DEFAULT_BRANCH, true ) @@ -925,7 +997,8 @@ describe("GitFileSystemService", () => { "fake-repo", ["fake-dir/fake-file"], "fake-hash", - "fake message" + "fake message", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf( @@ -955,7 +1028,8 @@ describe("GitFileSystemService", () => { "fake-repo", ["fake-dir/fake-file"], "fake-hash", - "fake message" + "fake message", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf( @@ -979,7 +1053,8 @@ describe("GitFileSystemService", () => { "fake-repo", [], "fake-hash", - "fake message" + "fake message", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -1001,7 +1076,8 @@ describe("GitFileSystemService", () => { "fake-repo", ["one", "two", "fake-dir/three"], "fake-hash", - "fake message" + "fake message", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -1016,7 +1092,8 @@ describe("GitFileSystemService", () => { "fake-repo", ["one", "two", "fake-dir/three"], "fake-hash", - "fake message" + "fake message", + DEFAULT_BRANCH ) expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -1070,7 +1147,8 @@ describe("GitFileSystemService", () => { "fake content", "fake-dir", "create-file", - "utf-8" + "utf-8", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual(expected) @@ -1122,7 +1200,8 @@ describe("GitFileSystemService", () => { "fake content", "fake-dir", "create-media-file", - "base64" + "base64", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual(expected) @@ -1173,7 +1252,8 @@ describe("GitFileSystemService", () => { "fake content", "fake-create-dir", "create-file", - "utf-8" + "utf-8", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual(expected) @@ -1197,7 +1277,8 @@ describe("GitFileSystemService", () => { "fake content", "fake-dir", "fake-file", - "utf-8" + "utf-8", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(ConflictError) @@ -1250,11 +1331,16 @@ describe("GitFileSystemService", () => { "fake content", "fake-dir", "create-file-rollback", - "utf-8" + "utf-8", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "test-commit-sha") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "test-commit-sha", + "staging" + ) }) }) @@ -1344,7 +1430,8 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake new content", "fake-old-hash", - "fake-user-id" + "fake-user-id", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual("fake-new-hash") @@ -1396,11 +1483,16 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake new content", "fake-old-hash", - "fake-user-id" + "fake-user-id", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "test-commit-sha") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "test-commit-sha", + "staging" + ) }) it("should return ConflictError if the old SHA provided does not match the current SHA", async () => { @@ -1424,7 +1516,8 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake new content", "fake-some-other-hash", - "fake-user-id" + "fake-user-id", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(ConflictError) @@ -1448,7 +1541,8 @@ describe("GitFileSystemService", () => { "fake-dir", "fake new content", "fake-old-hash", - "fake-user-id" + "fake-user-id", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -1472,7 +1566,8 @@ describe("GitFileSystemService", () => { "fake-dir/non-existent-file", "fake new content", "fake-old-hash", - "fake-user-id" + "fake-user-id", + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(NotFoundError) @@ -1519,6 +1614,7 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake-dir/fake-file-renamed", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) @@ -1568,6 +1664,7 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-dir-renamed", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) @@ -1619,11 +1716,16 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-dir-renamed", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "fake-hash") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "fake-hash", + "staging" + ) }) it("should rollback changes if an error occurred when moving the file", async () => { @@ -1654,11 +1756,16 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-dir-renamed", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "fake-hash") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "fake-hash", + "staging" + ) }) it("should return ConflictError if newPath is already an existing file/directory", async () => { @@ -1679,6 +1786,7 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-empty-dir", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) @@ -1703,6 +1811,7 @@ describe("GitFileSystemService", () => { "fake-nonexistent-dir", "fake-new-dir", "fake-user-id", + DEFAULT_BRANCH, "fake-message" ) @@ -1749,6 +1858,7 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-user-id", ["another-fake-file"], + DEFAULT_BRANCH, "fake-message" ) @@ -1793,6 +1903,7 @@ describe("GitFileSystemService", () => { "fake-new-dir", "fake-user-id", ["another-fake-file"], + DEFAULT_BRANCH, "fake-message" ) @@ -1843,11 +1954,16 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-user-id", ["another-fake-file"], + DEFAULT_BRANCH, "fake-message" ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "fake-hash") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "fake-hash", + "staging" + ) }) it("should return ConflictError if newPath is already an existing file/directory", async () => { @@ -1869,6 +1985,7 @@ describe("GitFileSystemService", () => { "another-fake-dir", "fake-user-id", ["fake-file"], + DEFAULT_BRANCH, "fake-message" ) @@ -1894,6 +2011,7 @@ describe("GitFileSystemService", () => { "another-fake-dir", "fake-user-id", ["fake-file"], + DEFAULT_BRANCH, "fake-message" ) @@ -1919,6 +2037,7 @@ describe("GitFileSystemService", () => { "fake-new-dir", "fake-user-id", ["fake-file"], + DEFAULT_BRANCH, "fake-message" ) @@ -2041,7 +2160,8 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake-old-hash", "fake-user-id", - false + false, + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) }) @@ -2101,7 +2221,8 @@ describe("GitFileSystemService", () => { "fake-dir/fake-file", "fake-old-hash", "fake-user-id", - false + false, + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual("fake-new-hash") @@ -2136,7 +2257,8 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-old-hash", "fake-user-id", - false + false, + DEFAULT_BRANCH ) expect(spyGetFilePathStats).toBeCalledTimes(1) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -2176,7 +2298,8 @@ describe("GitFileSystemService", () => { "fake-dir", "fake-old-hash", "fake-user-id", - false + false, + DEFAULT_BRANCH ) expect(spyGetGitBlobHash).toBeCalledTimes(1) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(ConflictError) @@ -2231,7 +2354,8 @@ describe("GitFileSystemService", () => { "fake-dir", "", "fake-user-id", - true + true, + DEFAULT_BRANCH ) expect(actual._unsafeUnwrap()).toEqual("fake-new-hash") @@ -2266,7 +2390,8 @@ describe("GitFileSystemService", () => { "fake-dir", "", "fake-user-id", - true + true, + DEFAULT_BRANCH ) expect(spyGetFilePathStats).toBeCalledTimes(1) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) @@ -2328,11 +2453,16 @@ describe("GitFileSystemService", () => { "fake-dir", "fake new content", "fake-user-id", - true + true, + DEFAULT_BRANCH ) expect(actual._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError) - expect(spyRollback).toHaveBeenCalledWith("fake-repo", "test-commit-sha") + expect(spyRollback).toHaveBeenCalledWith( + "fake-repo", + "test-commit-sha", + "staging" + ) }) }) diff --git a/src/services/db/__tests__/GitHubService.spec.ts b/src/services/db/__tests__/GitHubService.spec.ts index de2abde88..e5a129413 100644 --- a/src/services/db/__tests__/GitHubService.spec.ts +++ b/src/services/db/__tests__/GitHubService.spec.ts @@ -1,3 +1,5 @@ +import { AxiosCacheInstance } from "axios-cache-interceptor" + import { ConflictError } from "@errors/ConflictError" import { NotFoundError } from "@errors/NotFoundError" import { UnprocessableError } from "@errors/UnprocessableError" @@ -14,10 +16,9 @@ import { mockGithubSessionData, mockIsomerUserId, } from "@fixtures/sessionData" -import { ForbiddenError } from "@root/errors/ForbiddenError" import { indexHtmlContent } from "@root/fixtures/markdown-fixtures" import { collectionYmlContent } from "@root/fixtures/yaml-fixtures" -import { GitHubService } from "@services/db/GitHubService" +import GitHubService from "@services/db/GitHubService" // using es6 gives some error const { Base64 } = require("js-base64") @@ -57,9 +58,13 @@ describe("Github Service", () => { post: jest.fn(), patch: jest.fn(), } - const service = new GitHubService({ - axiosInstance: mockAxiosInstance, + /** + * type casting here as it we only really need to mock the + * functions that we use + do not need to maintain a full + * list of axios functions + */ + axiosInstance: (mockAxiosInstance as Partial) as AxiosCacheInstance, }) beforeEach(() => { @@ -167,6 +172,8 @@ describe("Github Service", () => { content, fileName, directoryName, + isMedia: false, + branchName: BRANCH_REF, }) ).resolves.toMatchObject({ sha, @@ -192,6 +199,7 @@ describe("Github Service", () => { content: collectionYmlContent, fileName: topLevelDirectoryFileName, directoryName, + isMedia: false, }) ).resolves.toMatchObject({ sha, @@ -218,6 +226,7 @@ describe("Github Service", () => { content: indexHtmlContent, fileName: resourceCategoryFileName, directoryName, + isMedia: false, }) ).resolves.toMatchObject({ sha, @@ -267,6 +276,7 @@ describe("Github Service", () => { response: { status: 422, }, + isAxiosError: true, } throw error }) @@ -275,6 +285,7 @@ describe("Github Service", () => { content, fileName, directoryName, + isMedia: false, }) ).rejects.toThrowError(ConflictError) expect(mockAxiosInstance.put).toHaveBeenCalledWith( @@ -293,6 +304,7 @@ describe("Github Service", () => { content, fileName, directoryName, + isMedia: false, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(folderParentEndpoint, { @@ -314,6 +326,7 @@ describe("Github Service", () => { content, fileName: subDirectoryFileName, directoryName: subDirectoryName, + isMedia: false, }) ).rejects.toThrowError() expect(mockAxiosInstance.get).toHaveBeenCalledWith(fileParentEndpoint, { @@ -334,6 +347,7 @@ describe("Github Service", () => { content, fileName, directoryName: `${resourceCategoryName}/_posts`, + isMedia: false, }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(resourceRoomEndpoint, { @@ -380,6 +394,7 @@ describe("Github Service", () => { it("should throw the correct error if file cannot be found", async () => { const resp = { status: 404, + isAxiosError: true, } mockAxiosInstance.get.mockResolvedValueOnce(resp) await expect( @@ -396,6 +411,54 @@ describe("Github Service", () => { }) }) + describe("ReadMedia", () => { + const endpoint = `${siteName}/git/blobs/${sha}` + const params = { + ref: BRANCH_REF, + } + + it("should read a media file works correctly", async () => { + const resp = { + data: { + content, + sha, + }, + } + mockAxiosInstance.get.mockResolvedValueOnce(resp) + await expect( + service.readMedia(sessionData, { + fileSha: sha, + }) + ).resolves.toMatchObject({ + content, + sha, + }) + expect(mockAxiosInstance.get).toHaveBeenCalledWith(endpoint, { + validateStatus, + params, + headers: authHeader.headers, + }) + }) + + it("should throw the correct error if file cannot be found", async () => { + const resp = { + status: 404, + isAxiosError: true, + } + mockAxiosInstance.get.mockResolvedValueOnce(resp) + await expect( + service.readMedia(sessionData, { + fileSha: sha, + }) + ).rejects.toThrowError(NotFoundError) + expect(mockAxiosInstance.get).toHaveBeenCalledWith(endpoint, { + validateStatus, + params, + headers: authHeader.headers, + }) + }) + }) + describe("ReadDirectory", () => { const endpoint = `${siteName}/contents/${directoryName}` const params = { @@ -487,6 +550,7 @@ describe("Github Service", () => { response: { status: 404, }, + isAxiosError: true, } throw err }) @@ -530,7 +594,7 @@ describe("Github Service", () => { fileName, directoryName, fileContent: content, - sha: undefined, + sha: "", }) ).resolves.toMatchObject({ newSha: sha, @@ -561,7 +625,7 @@ describe("Github Service", () => { fileName, directoryName, fileContent: content, - sha: undefined, + sha: "", }) ).rejects.toThrowError(NotFoundError) expect(mockAxiosInstance.get).toHaveBeenCalledWith(endpoint, { @@ -603,6 +667,7 @@ describe("Github Service", () => { response: { status: 404, }, + isAxiosError: true, } throw err }) @@ -708,6 +773,7 @@ describe("Github Service", () => { response: { status: 422, }, + isAxiosError: true, } throw err }) @@ -738,6 +804,7 @@ describe("Github Service", () => { const params = { ref: BRANCH_REF, + recursive: false, } const headers = { diff --git a/src/services/db/__tests__/RepoService.spec.ts b/src/services/db/__tests__/RepoService.spec.ts index 380f095c8..2c94821d9 100644 --- a/src/services/db/__tests__/RepoService.spec.ts +++ b/src/services/db/__tests__/RepoService.spec.ts @@ -21,7 +21,9 @@ import * as mediaUtils from "@root/utils/media-utils" import GitFileSystemService from "@services/db/GitFileSystemService" import _RepoService from "@services/db/RepoService" -import { GitHubService } from "../GitHubService" +import CommitServiceGitFile from "../CommitServiceGitFile" +import CommitServiceGitHub from "../CommitServiceGithub" +import GitHubService from "../GitHubService" const BRANCH_REF = config.get("github.branchRef") @@ -47,10 +49,20 @@ const MockGitFileSystemService = { updateRepoState: jest.fn(), } -const RepoService = new _RepoService( - (MockAxiosInstance as unknown) as AxiosCacheInstance, - (MockGitFileSystemService as unknown) as GitFileSystemService -) +const MockCommitServiceGitFile = { + create: jest.fn(), +} + +const MockCommitServiceGitHub = { + create: jest.fn(), +} + +const RepoService = new _RepoService({ + isomerRepoAxiosInstance: (MockAxiosInstance as unknown) as AxiosCacheInstance, + gitFileSystemService: (MockGitFileSystemService as unknown) as GitFileSystemService, + commitServiceGitFile: (MockCommitServiceGitFile as unknown) as CommitServiceGitFile, + commitServiceGitHub: (MockCommitServiceGitHub as unknown) as CommitServiceGitHub, +}) describe("RepoService", () => { // Prevent inter-test pollution of mocks @@ -106,33 +118,32 @@ describe("RepoService", () => { const mockFileName = "test.md" const mockDirectoryName = "" const createOutput = { - newSha: returnedSha, + sha: returnedSha, } const expected = { sha: returnedSha, } - MockGitFileSystemService.create.mockResolvedValueOnce( - okAsync(createOutput) - ) - + MockCommitServiceGitFile.create.mockResolvedValueOnce(createOutput) + const isMedia = false const actual = await RepoService.create( mockUserWithSiteSessionDataAndGrowthBook, { content: mockContent, fileName: mockFileName, directoryName: mockDirectoryName, - isMedia: false, + isMedia, } ) expect(actual).toEqual(expected) - expect(MockGitFileSystemService.create).toHaveBeenCalledWith( - mockUserWithSiteSessionDataAndGrowthBook.siteName, - mockUserWithSiteSessionDataAndGrowthBook.isomerUserId, - mockContent, - mockDirectoryName, - mockFileName, - "utf-8" + expect(MockCommitServiceGitFile.create).toHaveBeenCalledWith( + mockUserWithSiteSessionDataAndGrowthBook, + { + content: mockContent, + fileName: mockFileName, + directoryName: mockDirectoryName, + isMedia, + } ) }) @@ -142,14 +153,12 @@ describe("RepoService", () => { const mockFileName = "test.md" const mockDirectoryName = "" const createOutput = { - newSha: returnedSha, + sha: returnedSha, } const expected = { sha: returnedSha, } - MockGitFileSystemService.create.mockResolvedValueOnce( - okAsync(createOutput) - ) + MockCommitServiceGitFile.create.mockResolvedValueOnce(createOutput) const actual = await RepoService.create( mockUserWithSiteSessionDataAndGrowthBook, @@ -162,13 +171,14 @@ describe("RepoService", () => { ) expect(actual).toEqual(expected) - expect(MockGitFileSystemService.create).toHaveBeenCalledWith( - mockUserWithSiteSessionDataAndGrowthBook.siteName, - mockUserWithSiteSessionDataAndGrowthBook.isomerUserId, - mockContent, - mockDirectoryName, - mockFileName, - "base64" + expect(MockCommitServiceGitFile.create).toHaveBeenCalledWith( + mockUserWithSiteSessionDataAndGrowthBook, + { + content: mockContent, + fileName: mockFileName, + directoryName: mockDirectoryName, + isMedia: true, + } ) }) @@ -187,8 +197,7 @@ describe("RepoService", () => { const expected = { sha: "test-sha", } - const gitHubServiceCreate = jest.spyOn(GitHubService.prototype, "create") - gitHubServiceCreate.mockResolvedValueOnce(expected) + MockCommitServiceGitHub.create.mockResolvedValueOnce(expected) const actual = await RepoService.create(sessionData, { content: "content", @@ -198,10 +207,10 @@ describe("RepoService", () => { }) expect(actual).toEqual(expected) - expect(gitHubServiceCreate).toHaveBeenCalledWith(sessionData, { - content: mockContent, - fileName: mockFileName, - directoryName: mockDirectoryName, + expect(MockCommitServiceGitHub.create).toHaveBeenCalledWith(sessionData, { + content: "content", + fileName: "test.md", + directoryName: "", isMedia, }) }) @@ -580,7 +589,8 @@ describe("RepoService", () => { "pages/test.md", "fake-original-sha", mockUserWithSiteSessionDataAndGrowthBook.isomerUserId, - false + false, + "staging" ) expect(MockGitFileSystemService.push).toBeCalledTimes(1) expect(MockGitFileSystemService.push).toBeCalledWith(