Skip to content

Commit

Permalink
chore: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaksheeAtl committed Dec 22, 2023
1 parent 8d3de23 commit 124b236
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
120 changes: 120 additions & 0 deletions src/rest/routes/enterprise/delete-ghe-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { getFrontendApp } from "~/src/app";
import { Installation } from "models/installation";
import { Subscription } from "models/subscription";
import express, { Express } from "express";
import { RootRouter } from "routes/router";
import supertest from "supertest";
import { encodeSymmetric } from "atlassian-jwt";
import { GitHubServerApp } from "models/github-server-app";
import { v4 as newUUID } from "uuid";

jest.mock("~/src/sqs/queues");
jest.mock("config/feature-flags");

describe("Checking the sync request parsing route", () => {
let app: Express;
let installation: Installation;
// let installation1: Installation;
const installationIdForCloud = 1;
const installationIdForServer = 2;
const uuid = newUUID();
let gitHubServerApp: GitHubServerApp;
// let gitHubServerApp1: GitHubServerApp;
const testSharedSecret = "test-secret";
const clientKey = "jira-client-key";
const getToken = ({
secret = testSharedSecret,
iss = clientKey,
exp = Date.now() / 1000 + 10000,
qsh = "context-qsh",
sub = "myAccount"
} = {}): string => {
return encodeSymmetric(
{
qsh,
iss,
exp,
sub
},
secret
);
};
beforeEach(async () => {
app = getFrontendApp();
installation = await Installation.install({
host: jiraHost,
sharedSecret: testSharedSecret,
clientKey: clientKey
});
// installation1 = await Installation.install({
// host: jiraHost,
// sharedSecret: testSharedSecret+"1",
// clientKey: clientKey+"1"
// });
await Subscription.install({
installationId: installationIdForCloud,
host: jiraHost,
hashedClientKey: installation.clientKey,
gitHubAppId: undefined
});
gitHubServerApp = await GitHubServerApp.install(
{
uuid: uuid,
appId: 123,
gitHubAppName: "My GitHub Server App",
gitHubBaseUrl: gheUrl,
gitHubClientId: "lvl.1234",
gitHubClientSecret: "myghsecret",
webhookSecret: "mywebhooksecret",
privateKey: "myprivatekey",
installationId: installation.id
},
jiraHost
);
// gitHubServerApp1 = await GitHubServerApp.install(
// {
// uuid: uuid,
// appId: 123,
// gitHubAppName: "My GitHub Server App",
// gitHubBaseUrl: gheUrl,
// gitHubClientId: "lvl.1234",
// gitHubClientSecret: "myghsecret",
// webhookSecret: "mywebhooksecret",
// privateKey: "myprivatekey",
// installationId: installation1.id
// },
// jiraHost
// );
await Subscription.install({
installationId: installationIdForServer,
host: jiraHost,
hashedClientKey: installation.clientKey,
gitHubAppId: gitHubServerApp.id
});
app = express();
app.use(RootRouter);
});

describe("cloud", () => {
it("should throw 401 error when no github token is passed", async () => {
const resp = await supertest(app).delete(
`/rest/app/${gitHubServerApp.uuid}`
);
expect(resp.status).toEqual(401);
});

it("should return 400 on no uuid", async () => {
const resp = await supertest(app)
.delete(`/rest/app/cloud`)
.set("authorization", `${getToken()}`);
expect(resp.status).toEqual(400);
});

it("should return 200 on correct uuid", async () => {
const resp = await supertest(app)
.delete(`/rest/app/${gitHubServerApp.uuid}`)
.set("authorization", `${getToken()}`);
expect(resp.status).toEqual(200);
});
});
});
5 changes: 3 additions & 2 deletions src/rest/routes/enterprise/delete-ghe-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const deleteEnterpriseServer = async (
const { installation } = res.locals;

const cloudOrUUID = req.params.cloudOrUUID;
if (!cloudOrUUID) {
const gheUUID = cloudOrUUID === "cloud" ? undefined : cloudOrUUID; //TODO: validate the uuid regex

if (!gheUUID) {
throw new InvalidArgumentError(
"Invalid route, couldn't determine UUID of enterprise server!"
);
}

// TODO: Check and add test cases for GHE later
const gitHubApp = await GitHubServerApp.getForUuidAndInstallationId(
cloudOrUUID,
installation.id
Expand Down
2 changes: 1 addition & 1 deletion src/rest/routes/subscriptions/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DatabaseStateCreator } from "~/test/utils/database-state-creator";
jest.mock("~/src/sqs/queues");
jest.mock("config/feature-flags");

describe("Checking the deferred request parsing route", () => {
describe("Checking the sync request parsing route", () => {
let app: Express;
let installation: Installation;
const installationIdForCloud = 1;
Expand Down

0 comments on commit 124b236

Please sign in to comment.