|
| 1 | +import fs from "fs"; |
| 2 | +import { join } from "path"; |
| 3 | +import { PassThrough } from "stream"; |
| 4 | +import { test, expect } from "@oclif/test"; |
| 5 | +import { cliux, configHandler, ux } from "@contentstack/cli-utilities"; |
| 6 | + |
| 7 | +import config from "../../../../src/config"; |
| 8 | +import * as mock from "../../mock/common.mock.json"; |
| 9 | +import manifestData from "../../config/manifest.json"; |
| 10 | +import messages, { $t } from "../../../../src/messages"; |
| 11 | +import * as commonUtils from "../../../../src/util/common-utils"; |
| 12 | + |
| 13 | +const region: { cma: string; name: string; cda: string } = |
| 14 | + configHandler.get("region"); |
| 15 | +const developerHubBaseUrl = (config.developerHubUrls as Record<string, any>)[ |
| 16 | + region.cma |
| 17 | +]; |
| 18 | + |
| 19 | +describe("app:get", () => { |
| 20 | + describe("Get app manifest", () => { |
| 21 | + test |
| 22 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 23 | + .stub(ux.action, "stop", () => {}) |
| 24 | + .stub(ux.action, "start", () => {}) |
| 25 | + .stub(fs, "readdirSync", () => []) |
| 26 | + .stub(fs, "writeFileSync", () => new PassThrough()) |
| 27 | + .stub(cliux, "inquire", async (...args: any) => { |
| 28 | + const [prompt]: any = args; |
| 29 | + const cases = { |
| 30 | + App: "App 1", |
| 31 | + Organization: "test org 1", |
| 32 | + }; |
| 33 | + |
| 34 | + return (cases as Record<string, any>)[prompt.name]; |
| 35 | + }) |
| 36 | + .nock(region.cma, (api) => |
| 37 | + api |
| 38 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 39 | + .reply(200, { organizations: mock.organizations }) |
| 40 | + ) |
| 41 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 42 | + api |
| 43 | + .get( |
| 44 | + "/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack" |
| 45 | + ) |
| 46 | + .reply(200, { |
| 47 | + data: mock.apps, |
| 48 | + }) |
| 49 | + ) |
| 50 | + .command(["app:get", "--data-dir", join(process.cwd(), "test", "unit")]) |
| 51 | + .do(({ stdout }) => |
| 52 | + expect(stdout).to.contain( |
| 53 | + $t(messages.FILE_WRITTEN_SUCCESS, { |
| 54 | + file: join( |
| 55 | + process.cwd(), |
| 56 | + "test", |
| 57 | + "unit", |
| 58 | + `${config.defaultAppFileName}.json` |
| 59 | + ), |
| 60 | + }) |
| 61 | + ) |
| 62 | + ) |
| 63 | + .it("should return manifest for selected app"); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("Get app manifest with app uid", () => { |
| 67 | + test |
| 68 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 69 | + .stub(ux.action, "stop", () => {}) |
| 70 | + .stub(ux.action, "start", () => {}) |
| 71 | + .stub(fs, "readdirSync", () => []) |
| 72 | + .stub(fs, "writeFileSync", () => new PassThrough()) |
| 73 | + .stub(cliux, "inquire", async () => "test org 1") |
| 74 | + .nock(region.cma, (api) => |
| 75 | + api |
| 76 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 77 | + .reply(200, { organizations: mock.organizations }) |
| 78 | + ) |
| 79 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 80 | + api.get("/manifests/app-uid-1").reply(200, { |
| 81 | + data: { ...manifestData, name: "test-app", version: 1 }, |
| 82 | + }) |
| 83 | + ) |
| 84 | + .command([ |
| 85 | + "app:get", |
| 86 | + "--data-dir", |
| 87 | + join(process.cwd(), "test", "unit"), |
| 88 | + "--app-uid", |
| 89 | + "app-uid-1", |
| 90 | + ]) |
| 91 | + .do(({ stdout }) => |
| 92 | + expect(stdout).to.contain( |
| 93 | + $t(messages.FILE_WRITTEN_SUCCESS, { |
| 94 | + file: join( |
| 95 | + process.cwd(), |
| 96 | + "test", |
| 97 | + "unit", |
| 98 | + `${config.defaultAppFileName}.json` |
| 99 | + ), |
| 100 | + }) |
| 101 | + ) |
| 102 | + ) |
| 103 | + .it("should return manifest for specific uid passed"); |
| 104 | + }); |
| 105 | + |
| 106 | + describe("Ask confirmation if `manifest.json` exists and go with `Yes` option", () => { |
| 107 | + test |
| 108 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 109 | + .stub(ux.action, "stop", () => {}) |
| 110 | + .stub(ux.action, "start", () => {}) |
| 111 | + .stub(fs, "readdirSync", () => ["manifest.json"]) |
| 112 | + .stub(fs, "writeFileSync", () => new PassThrough()) |
| 113 | + .stub(cliux, "inquire", async (...args: any) => { |
| 114 | + const [prompt]: any = args; |
| 115 | + const cases = { |
| 116 | + App: "App 1", |
| 117 | + Organization: "test org 1", |
| 118 | + }; |
| 119 | + |
| 120 | + return (cases as Record<string, any>)[prompt.name]; |
| 121 | + }) |
| 122 | + .stub(cliux, "confirm", async () => true) |
| 123 | + .nock(region.cma, (api) => |
| 124 | + api |
| 125 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 126 | + .reply(200, { organizations: mock.organizations }) |
| 127 | + ) |
| 128 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 129 | + api |
| 130 | + .get( |
| 131 | + "/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack" |
| 132 | + ) |
| 133 | + .reply(200, { |
| 134 | + data: mock.apps, |
| 135 | + }) |
| 136 | + ) |
| 137 | + .command([ |
| 138 | + "app:get", |
| 139 | + "--data-dir", |
| 140 | + join(process.cwd(), "test", "unit", "config"), |
| 141 | + ]) |
| 142 | + .do(({ stdout }) => |
| 143 | + expect(stdout).to.contain( |
| 144 | + $t(messages.FILE_WRITTEN_SUCCESS, { |
| 145 | + file: join( |
| 146 | + process.cwd(), |
| 147 | + "test", |
| 148 | + "unit", |
| 149 | + "config", |
| 150 | + `${config.defaultAppFileName}1.json` |
| 151 | + ), |
| 152 | + }) |
| 153 | + ) |
| 154 | + ) |
| 155 | + .it("Should create config file with the +1 mechanism"); |
| 156 | + }); |
| 157 | + |
| 158 | + describe("Ask confirmation if `manifest.json` exists and go with `No` option", () => { |
| 159 | + test |
| 160 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 161 | + .stub(ux.action, "stop", () => {}) |
| 162 | + .stub(ux.action, "start", () => {}) |
| 163 | + .stub(fs, "readdirSync", () => ["manifest.json"]) |
| 164 | + .stub(fs, "writeFileSync", () => new PassThrough()) |
| 165 | + .stub(cliux, "inquire", async (...args: any) => { |
| 166 | + const [prompt]: any = args; |
| 167 | + const cases = { |
| 168 | + App: "App 1", |
| 169 | + Organization: "test org 1", |
| 170 | + }; |
| 171 | + |
| 172 | + return (cases as Record<string, any>)[prompt.name]; |
| 173 | + }) |
| 174 | + .stub(cliux, "confirm", async () => false) |
| 175 | + .nock(region.cma, (api) => |
| 176 | + api |
| 177 | + .get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") |
| 178 | + .reply(200, { organizations: mock.organizations }) |
| 179 | + ) |
| 180 | + .nock(`https://${developerHubBaseUrl}`, (api) => |
| 181 | + api |
| 182 | + .get( |
| 183 | + "/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack" |
| 184 | + ) |
| 185 | + .reply(200, { |
| 186 | + data: mock.apps, |
| 187 | + }) |
| 188 | + ) |
| 189 | + .command([ |
| 190 | + "app:get", |
| 191 | + "--data-dir", |
| 192 | + join(process.cwd(), "test", "unit", "config"), |
| 193 | + ]) |
| 194 | + .do(({ stdout }) => |
| 195 | + expect(stdout).to.contain( |
| 196 | + $t(messages.FILE_WRITTEN_SUCCESS, { |
| 197 | + file: join( |
| 198 | + process.cwd(), |
| 199 | + "test", |
| 200 | + "unit", |
| 201 | + "config", |
| 202 | + `${config.defaultAppFileName}.json` |
| 203 | + ), |
| 204 | + }) |
| 205 | + ) |
| 206 | + ) |
| 207 | + .it("Should overwrite config file with"); |
| 208 | + }); |
| 209 | + |
| 210 | + describe("Pass wrong org uid through flag", () => { |
| 211 | + test |
| 212 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 213 | + .stub(commonUtils, "getOrganizations", async () => []) |
| 214 | + .command(["app:get", "--org", "test-uid-1"]) |
| 215 | + .exit(1) |
| 216 | + .do(({ stdout }) => expect(stdout).to.contain(messages.ORG_UID_NOT_FOUND)) |
| 217 | + .it("should fail with error message"); |
| 218 | + }); |
| 219 | +}); |
0 commit comments