-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INNO] - New endpoint for deleting subscriptions (#2602)
* - Renaming the tsx to ts * - Adding new Rest endpoint for deleting subscriptions * - Fixing snapshot * - Updating the request to get the installationId from params instead of body - Added a frontend API service for hitting this new endpoint * - Review changes * - Correction in API * - Fixing snapshot * - Review changes * - Review changes * - Updated snapshot * - Review changes
- Loading branch information
1 parent
6540f42
commit 247f143
Showing
9 changed files
with
243 additions
and
96 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { axiosRest } from "../axiosInstance"; | ||
import { axiosRest, axiosRestWithGitHubToken } from "../axiosInstance"; | ||
|
||
export default { | ||
getSubscriptions: () => axiosRest.get("/rest/subscriptions") | ||
getSubscriptions: () => axiosRest.get("/rest/subscriptions"), | ||
deleteSubscription: (subscriptionId: number) => | ||
axiosRestWithGitHubToken.delete("/rest/app/cloud/subscription/:subscriptionId", { params: { subscriptionId } }) | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Installation } from "models/installation"; | ||
import { Subscription } from "models/subscription"; | ||
import { encodeSymmetric } from "atlassian-jwt"; | ||
import { getFrontendApp } from "~/src/app"; | ||
import supertest from "supertest"; | ||
import { envVars } from "config/env"; | ||
|
||
describe("Subscription", () => { | ||
const testSharedSecret = "test-secret"; | ||
const gitHubInstallationId = 15; | ||
const getToken = ({ | ||
secret = testSharedSecret, | ||
iss = "jira-client-key", | ||
exp = Date.now() / 1000 + 10000, | ||
qsh = "context-qsh" } = {}): string => encodeSymmetric({ | ||
qsh, | ||
iss, | ||
exp | ||
}, secret); | ||
let app, subscription; | ||
beforeEach(async () => { | ||
app = getFrontendApp(); | ||
await Installation.install({ | ||
clientKey: "jira-client-key", | ||
host: jiraHost, | ||
sharedSecret: testSharedSecret | ||
}); | ||
subscription = await Subscription.create({ | ||
gitHubInstallationId, | ||
jiraHost | ||
}); | ||
}); | ||
|
||
it("Should return 400 for invalid delete subscription route", async () => { | ||
const resp = await supertest(app) | ||
.delete("/rest/subscriptions/" + gitHubInstallationId) | ||
.set("authorization", `${getToken()}`); | ||
|
||
expect(resp.status).toBe(404); | ||
}); | ||
|
||
it("Should return 401 for valid delete subscription route when missing githubToken", async () => { | ||
const resp = await supertest(app) | ||
.delete("/rest/app/cloud/subscriptions/" + gitHubInstallationId); | ||
|
||
expect(resp.status).toBe(401); | ||
expect(await Subscription.count()).toEqual(1); | ||
}); | ||
|
||
it("Should return 404 for valid delete subscription route when no valid subscriptionId is passed", async () => { | ||
const resp = await supertest(app) | ||
.delete("/rest/app/cloud/subscriptions/random-installation-id") | ||
.set("authorization", `${getToken()}`); | ||
|
||
expect(resp.status).toBe(404); | ||
expect(await Subscription.count()).toEqual(1); | ||
}); | ||
|
||
it("Should return 404 for valid delete subscription route when a different subscriptionId is passed", async () => { | ||
const resp = await supertest(app) | ||
.delete("/rest/app/cloud/subscriptions/12") | ||
.set("authorization", `${getToken()}`); | ||
|
||
expect(resp.status).toBe(404); | ||
expect(await Subscription.count()).toEqual(1); | ||
}); | ||
|
||
it("Should return 204 for valid delete subscription route when subscription is deleted", async () => { | ||
jiraNock | ||
.delete("/rest/devinfo/0.10/bulkByProperties") | ||
.query({ installationId: subscription.gitHubInstallationId }) | ||
.reply(200, "OK"); | ||
|
||
jiraNock | ||
.delete("/rest/builds/0.1/bulkByProperties") | ||
.query({ gitHubInstallationId: subscription.gitHubInstallationId }) | ||
.reply(200, "OK"); | ||
|
||
jiraNock | ||
.delete("/rest/deployments/0.1/bulkByProperties") | ||
.query({ gitHubInstallationId: subscription.gitHubInstallationId }) | ||
.reply(200, "OK"); | ||
|
||
jiraNock | ||
.put(`/rest/atlassian-connect/latest/addons/${envVars.APP_KEY}/properties/is-configured`, { | ||
isConfigured: false | ||
}) | ||
.reply(200); | ||
|
||
const resp = await supertest(app) | ||
.delete("/rest/app/cloud/subscriptions/" + subscription.id) | ||
.set("authorization", `${getToken()}`); | ||
|
||
expect(resp.status).toBe(204); | ||
expect(await Subscription.count()).toEqual(0); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Router, Request, Response } from "express"; | ||
import { errorWrapper } from "../../helper"; | ||
import { getAllSubscriptions } from "./service"; | ||
import { Installation } from "models/installation"; | ||
import { removeSubscription } from "utils/jira-utils"; | ||
import { GitHubServerApp } from "models/github-server-app"; | ||
import { InvalidArgumentError } from "config/errors"; | ||
|
||
export const SubscriptionsRouter = Router({ mergeParams: true }); | ||
|
||
SubscriptionsRouter.get("/", errorWrapper("SubscriptionsGet", async (req: Request, res: Response) => { | ||
const { jiraHost, installation } = res.locals; | ||
const { ghCloudSubscriptions, ghEnterpriseServers } = await getAllSubscriptions(jiraHost as string, (installation as Installation).id, req); | ||
|
||
res.status(200).json({ | ||
ghCloudSubscriptions, | ||
ghEnterpriseServers | ||
}); | ||
})); | ||
|
||
/** | ||
* This delete endpoint only handles Github cloud subscriptions | ||
*/ | ||
SubscriptionsRouter.delete("/", errorWrapper("SubscriptionDelete", async (req: Request, res: Response) => { | ||
const subscriptionId: number = Number(req.params.subscriptionId); | ||
const { installation } = res.locals as { installation: Installation; }; | ||
|
||
const cloudOrUUID = req.params.cloudOrUUID; | ||
if (!cloudOrUUID) { | ||
throw new InvalidArgumentError("Invalid route, couldn't determine if its cloud or enterprise!"); | ||
} | ||
|
||
// TODO: Check and add test cases for GHE later | ||
const gitHubAppId = cloudOrUUID === "cloud" ? undefined : | ||
(await GitHubServerApp.getForUuidAndInstallationId(cloudOrUUID, installation.id))?.appId; //TODO: validate the uuid regex | ||
|
||
await removeSubscription(installation, undefined, gitHubAppId, req.log, subscriptionId); | ||
|
||
res.sendStatus(204); | ||
})); |
This file was deleted.
Oops, something went wrong.
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.