|
| 1 | +import { ux, cliux, configHandler } from "@contentstack/cli-utilities" |
| 2 | +import { expect, test } from "@oclif/test"; |
| 3 | + |
| 4 | +import * as mock from "../../mock/common.mock.json" |
| 5 | + |
| 6 | +import config from "../../../../src/config" |
| 7 | +import messages, {$t} from "../../../../src/messages"; |
| 8 | + |
| 9 | +const region: { cma: string, cda: string, name: string } = configHandler.get("region"); |
| 10 | +const developerHubBaseUrl = (config.developerHubUrls as Record<string, any>)[region.cma]; |
| 11 | + |
| 12 | +describe("app:uninstall", () => { |
| 13 | + describe("Uninstall an app from organization", () => { |
| 14 | + test |
| 15 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 16 | + .stub(ux.action, "stop", () => {}) |
| 17 | + .stub(ux.action, "start", () => {}) |
| 18 | + .stub(cliux, "inquire", async (...args: any) => { |
| 19 | + const [prompt]: any = args; |
| 20 | + const cases = { |
| 21 | + App: mock.apps[1].name, |
| 22 | + Organization: mock.organizations[0].name, |
| 23 | + }; |
| 24 | + |
| 25 | + return (cases as Record<string, any>)[prompt.name]; |
| 26 | + }) |
| 27 | + .nock(region.cma, (api) => |
| 28 | + api |
| 29 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 30 | + .reply(200, { organizations: mock.organizations }) |
| 31 | + ) |
| 32 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 33 | + api |
| 34 | + .get("/manifests?limit=50&asc=name&include_count=true&skip=0") |
| 35 | + .reply(200, { |
| 36 | + data: mock.apps, |
| 37 | + }) |
| 38 | + ) |
| 39 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 40 | + api |
| 41 | + .get(`/manifests/${mock.apps[1].uid}/installations`) |
| 42 | + .reply(200, { |
| 43 | + data: mock.installations, |
| 44 | + }) |
| 45 | + ) |
| 46 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 47 | + api |
| 48 | + .delete(`/installations/${mock.installations[1].uid}`) |
| 49 | + .reply(200, { |
| 50 | + data: {}, |
| 51 | + }) |
| 52 | + ) |
| 53 | + .command([ |
| 54 | + "app:uninstall" |
| 55 | + ]) |
| 56 | + .do(({stdout}) => { |
| 57 | + expect(stdout).to.contain($t(messages.APP_UNINSTALLED, { |
| 58 | + app: mock.apps[1].name, |
| 59 | + })) |
| 60 | + }) |
| 61 | + .it("should uninstall an organization app") |
| 62 | + }) |
| 63 | + describe("Uninstall an app from a stack", () => { |
| 64 | + test |
| 65 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 66 | + .stub(ux.action, "stop", () => {}) |
| 67 | + .stub(ux.action, "start", () => {}) |
| 68 | + .stub(cliux, "inquire", async (...args: any) => { |
| 69 | + const [prompt]: any = args; |
| 70 | + const cases = { |
| 71 | + App: mock.apps[0].name, |
| 72 | + Organization: mock.organizations[0].name, |
| 73 | + appInstallation: mock.installations[0].uid |
| 74 | + }; |
| 75 | + |
| 76 | + return (cases as Record<string, any>)[prompt.name]; |
| 77 | + }) |
| 78 | + .nock(region.cma, (api) => |
| 79 | + api |
| 80 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 81 | + .reply(200, { organizations: mock.organizations }) |
| 82 | + ) |
| 83 | + .nock(region.cma, (api) => |
| 84 | + api |
| 85 | + .get(`/v3/organizations/${mock.organizations[0].uid}/stacks?limit=100&asc=name&include_count=true&skip=0`) |
| 86 | + .reply(200, { stacks: mock.stacks }) |
| 87 | + ) |
| 88 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 89 | + api |
| 90 | + .get("/manifests?limit=50&asc=name&include_count=true&skip=0") |
| 91 | + .reply(200, { |
| 92 | + data: mock.apps, |
| 93 | + }) |
| 94 | + ) |
| 95 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 96 | + api |
| 97 | + .get(`/manifests/${mock.apps[0].uid}/installations`) |
| 98 | + .reply(200, { |
| 99 | + data: mock.installations, |
| 100 | + }) |
| 101 | + ) |
| 102 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 103 | + api |
| 104 | + .delete(`/installations/${mock.installations[0].uid}`) |
| 105 | + .reply(200, { |
| 106 | + data: {}, |
| 107 | + }) |
| 108 | + ) |
| 109 | + .command([ |
| 110 | + "app:uninstall" |
| 111 | + ]) |
| 112 | + .do(({stdout}) => { |
| 113 | + expect(stdout).to.contain($t(messages.APP_UNINSTALLED, { |
| 114 | + app: mock.apps[0].name, |
| 115 | + })) |
| 116 | + }) |
| 117 | + .it("should uninstall a stack app") |
| 118 | + }) |
| 119 | + describe("Fail to uninstall an app from a stack", () => { |
| 120 | + test |
| 121 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 122 | + .stub(ux.action, "stop", () => {}) |
| 123 | + .stub(ux.action, "start", () => {}) |
| 124 | + .stub(cliux, "inquire", async (...args: any) => { |
| 125 | + const [prompt]: any = args; |
| 126 | + const cases = { |
| 127 | + App: mock.apps[0].name, |
| 128 | + Organization: mock.organizations[0].name, |
| 129 | + appInstallation: mock.installations[0].uid |
| 130 | + }; |
| 131 | + |
| 132 | + return (cases as Record<string, any>)[prompt.name]; |
| 133 | + }) |
| 134 | + .nock(region.cma, (api) => |
| 135 | + api |
| 136 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 137 | + .reply(200, { organizations: mock.organizations }) |
| 138 | + ) |
| 139 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 140 | + api |
| 141 | + .get("/manifests?limit=50&asc=name&include_count=true&skip=0") |
| 142 | + .reply(200, { |
| 143 | + data: mock.apps, |
| 144 | + }) |
| 145 | + ) |
| 146 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 147 | + api |
| 148 | + .delete(`/installations/wrong-uid`) |
| 149 | + .replyWithError({ |
| 150 | + "status": 404, |
| 151 | + "message": "App with id wrong-uid not installed", |
| 152 | + "error": "Not Found" |
| 153 | + }) |
| 154 | + ) |
| 155 | + .command([ |
| 156 | + "app:uninstall", "--installation-uid", "wrong-uid" |
| 157 | + ]) |
| 158 | + .exit(1) |
| 159 | + .do(({stdout}) => { |
| 160 | + expect(stdout).to.contain("App with id wrong-uid not installed") |
| 161 | + }) |
| 162 | + .it("should fail with an error") |
| 163 | + }); |
| 164 | +}) |
0 commit comments