Skip to content

Commit

Permalink
test(service): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Nov 15, 2023
1 parent 0c1dd5c commit d824b6a
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 427 deletions.
24 changes: 13 additions & 11 deletions src/middleware/routeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ const attachRollbackRouteHandlerWrapper = (routeHandler) => async (
const {
currentCommitSha: currentStgCommitSha,
treeSha: stgTreeSha,
} = await getCommitAndTreeSha(siteName, accessToken)
} = await getCommitAndTreeSha(siteName, accessToken, STAGING_BRANCH)

const {
currentCommitSha: currentStgLiteCommitSha,
} = await getCommitAndTreeSha(siteName, accessToken)
} = await getCommitAndTreeSha(siteName, accessToken, STAGING_LITE_BRANCH)

const githubSessionData = new GithubSessionData({
currentCommitSha: currentStgCommitSha,
Expand All @@ -179,13 +179,15 @@ const attachRollbackRouteHandlerWrapper = (routeHandler) => async (
await routeHandler(req, res, next).catch(async (err) => {
try {
if (shouldUseGitFileSystem) {
await backOff(() => {
const rollbackRes = gitFileSystemService
await backOff(async () => {
const rollbackRes = await gitFileSystemService
.rollback(siteName, originalStagingCommitSha, STAGING_BRANCH)
.rollback(
siteName,
originalStagingLiteCommitSha,
STAGING_LITE_BRANCH
.andThen(() =>
gitFileSystemService.rollback(
siteName,
originalStagingLiteCommitSha,
STAGING_LITE_BRANCH
)
)
.unwrapOr(false)
if (!rollbackRes) throw new GitFileSystemError("Rollback failure")
Expand All @@ -204,14 +206,14 @@ const attachRollbackRouteHandlerWrapper = (routeHandler) => async (
if (!pushRes) throw new GitFileSystemError("Push failure")
})
} else {
await backOff(() => {
revertCommit(
await backOff(async () => {
await revertCommit(
originalStagingCommitSha,
siteName,
accessToken,
STAGING_BRANCH
)
revertCommit(
await revertCommit(
originalStagingLiteCommitSha,
siteName,
accessToken,
Expand Down
17 changes: 13 additions & 4 deletions src/services/db/GitFileCommitService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { okAsync } from "neverthrow"

import GithubSessionData from "@root/classes/GithubSessionData"
import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData"
import { STAGING_BRANCH, STAGING_LITE_BRANCH } from "@root/constants"
Expand Down Expand Up @@ -26,13 +28,20 @@ export default class GitFileCommitService {
) {
// We await the push to staging FIRST, and then push to staging-lite
// We don't want a case when staging lite updates but staging doesn't
await this.gitFileSystemService.push(sessionData.siteName, STAGING_BRANCH)
const res = this.gitFileSystemService.push(
sessionData.siteName,
STAGING_BRANCH
)

if (shouldUpdateStagingLite) {
await this.gitFileSystemService.push(
sessionData.siteName,
STAGING_LITE_BRANCH
res.andThen(() =>
this.gitFileSystemService.push(
sessionData.siteName,
STAGING_LITE_BRANCH
)
)
}
await res
}

async create(
Expand Down
13 changes: 13 additions & 0 deletions src/services/db/GitFileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,19 @@ export default class GitFileSystemService {
)
}

hi(siteName: string, originalStagingCommitSha: string) {
const rollbackRes = this.rollback(
siteName,
originalStagingCommitSha,
STAGING_BRANCH
)
.andThen(() =>
this.rollback(siteName, originalStagingCommitSha, STAGING_LITE_BRANCH)
)
.unwrapOr(false)
if (!rollbackRes) throw new GitFileSystemError("Rollback failure")
}

cloneBranch(
repoName: string,
isStaging: boolean
Expand Down
18 changes: 12 additions & 6 deletions src/services/db/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ export default class GitHubService {
pathToCheck = directoryName.split("/").slice(0, -1).join("/")
}
// this is to check if the file path still exists, else this will throw a 404
await this.readDirectory(sessionData, { directoryName: pathToCheck })
await this.readDirectory(sessionData, {
directoryName: pathToCheck,
branchName,
})
}

// Validation and sanitisation of media already done
Expand All @@ -191,7 +194,7 @@ export default class GitHubService {
content: encodedContent,
branch: branchName,
}

console.log("params", params)
const resp = await this.axiosInstance.put(endpoint, params, {
headers: {
Authorization: `token ${accessToken}`,
Expand All @@ -217,7 +220,7 @@ export default class GitHubService {
fileName,
directoryName,
branchName,
}: { fileName: any; directoryName: any; branchName?: string }
}: { fileName: any; directoryName: any; branchName: string }
) {
const { accessToken } = sessionData
const { siteName } = sessionData
Expand Down Expand Up @@ -245,7 +248,7 @@ export default class GitHubService {

async readMedia(
sessionData: UserWithSiteSessionData,
{ fileSha, branchName }: { fileSha: any; branchName?: string }
{ fileSha, branchName }: { fileSha: any; branchName: string }
) {
/**
* Files that are bigger than 1 MB needs to be retrieved
Expand Down Expand Up @@ -278,7 +281,7 @@ export default class GitHubService {

async readDirectory(
sessionData: UserWithSiteSessionData,
{ directoryName, branchName }: { directoryName: any; branchName?: string }
{ directoryName, branchName }: { directoryName: any; branchName: string }
) {
const { accessToken } = sessionData
const { siteName } = sessionData
Expand Down Expand Up @@ -321,7 +324,7 @@ export default class GitHubService {
const endpoint = this.getFilePath({ siteName, fileName, directoryName })
// this is to check if the file path still exists, else this will throw a 404. Only needed for paths outside of root
if (directoryName) {
await this.readDirectory(sessionData, { directoryName })
await this.readDirectory(sessionData, { directoryName, branchName })
}
const encodedNewContent = Base64.encode(fileContent)

Expand All @@ -330,6 +333,7 @@ export default class GitHubService {
const { sha: retrievedSha } = await this.read(sessionData, {
fileName,
directoryName,
branchName,
})
fileSha = retrievedSha
}
Expand Down Expand Up @@ -773,6 +777,8 @@ export default class GitHubService {
})
}
} else if (item.path === newPath && item.type !== "tree") {
console.log("item.path", item.path)
console.log("newPath", newPath)
throw new ConflictError("Target file already exists")
} else if (item.path === oldPath && item.type !== "tree") {
// Add file to new directory
Expand Down
4 changes: 4 additions & 0 deletions src/services/db/RepoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export default class RepoService extends GitHubService {
return super.read(sessionData, {
fileName,
directoryName,
branchName: STAGING_BRANCH,
})
}

Expand Down Expand Up @@ -263,6 +264,7 @@ export default class RepoService extends GitHubService {
const filePath = `${directoryName}/${fileName}`
const directoryData = await super.readDirectory(sessionData, {
directoryName,
branchName: STAGING_BRANCH,
})
const {
author: { date: addedTime },
Expand Down Expand Up @@ -311,6 +313,7 @@ export default class RepoService extends GitHubService {

return super.readDirectory(sessionData, {
directoryName,
branchName: defaultBranch,
})
}

Expand Down Expand Up @@ -352,6 +355,7 @@ export default class RepoService extends GitHubService {
} else {
dirContent = (await super.readDirectory(sessionData, {
directoryName,
branchName: defaultBranch,
})) as GitDirectoryItem[]
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/db/__tests__/GitFileCommitService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ describe("GitFileCommitService", () => {
// Arrange
const pushSpy = jest
.spyOn(gitFileSystemService, "push")
.mockResolvedValue(okAsync("push ok"))
.mockImplementation((siteName, branchName) =>
ResultAsync.fromPromise(
Promise.resolve("push ok"),
() => new GitFileSystemError("push failed")
)
)

// Act
await gitFileCommitService.pushToGithub(sessionData, true)
Expand Down
25 changes: 18 additions & 7 deletions src/services/db/__tests__/GitFileSystemService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ describe("GitFileSystemService", () => {
checkIsRepo: jest.fn().mockResolvedValueOnce(true),
})

const result = await GitFileSystemService.isGitInitialized("fake-repo")
const result = await GitFileSystemService.isGitInitialized(
"fake-repo",
true
)

expect(result._unsafeUnwrap()).toBeTrue()
})
Expand All @@ -242,7 +245,10 @@ describe("GitFileSystemService", () => {
checkIsRepo: jest.fn().mockResolvedValueOnce(false),
})

const result = await GitFileSystemService.isGitInitialized("fake-repo")
const result = await GitFileSystemService.isGitInitialized(
"fake-repo",
true
)

expect(result._unsafeUnwrap()).toBeFalse()
})
Expand Down Expand Up @@ -272,7 +278,8 @@ describe("GitFileSystemService", () => {
})

const result = await GitFileSystemService.isOriginRemoteCorrect(
"fake-repo"
"fake-repo",
true
)

expect(result._unsafeUnwrap()).toBeTrue()
Expand All @@ -288,7 +295,8 @@ describe("GitFileSystemService", () => {
})

const result = await GitFileSystemService.isOriginRemoteCorrect(
"fake-repo"
"fake-repo",
true
)

expect(result._unsafeUnwrap()).toBeFalse()
Expand All @@ -300,7 +308,8 @@ describe("GitFileSystemService", () => {
})

const result = await GitFileSystemService.isOriginRemoteCorrect(
"fake-repo"
"fake-repo",
true
)

expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)
Expand Down Expand Up @@ -499,7 +508,8 @@ describe("GitFileSystemService", () => {

const result = await GitFileSystemService.getGitBlobHash(
"fake-repo",
"fake-dir/fake-file"
"fake-dir/fake-file",
true
)

expect(result._unsafeUnwrap()).toBe("fake-hash")
Expand All @@ -512,7 +522,8 @@ describe("GitFileSystemService", () => {

const result = await GitFileSystemService.getGitBlobHash(
"fake-repo",
"fake-dir/fake-file"
"fake-dir/fake-file",
true
)

expect(result._unsafeUnwrapErr()).toBeInstanceOf(GitFileSystemError)
Expand Down
Loading

0 comments on commit d824b6a

Please sign in to comment.