Skip to content

Commit b9786e4

Browse files
Merge pull request #76 from contentstack/feat/CS-39665
test: unit test cases for get command
2 parents e88f735 + a8d47bc commit b9786e4

File tree

2 files changed

+278
-2
lines changed

2 files changed

+278
-2
lines changed

test/unit/commands/app/get.test.ts

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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+
});

test/unit/util/common-utils.test.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import { fancy } from "fancy-test";
22
import { expect } from "@oclif/test";
33
import {
4+
cliux,
45
configHandler,
56
ContentstackClient,
67
managementSDKClient,
78
} from "@contentstack/cli-utilities";
89

9-
import { getOrganizations } from "../../../src/util/common-utils";
10-
import * as mock from "../mock/common.mock.json";
10+
import config from "../../../src/config";
1111
import { LogFn } from "../../../src/types";
12+
import * as mock from "../mock/common.mock.json";
13+
import { fetchApps, getOrganizations } from "../../../src/util/common-utils";
1214

1315
const region: { cma: string; name: string; cda: string } =
1416
configHandler.get("region");
17+
const developerHubBaseUrl = (config.developerHubUrls as Record<string, any>)[
18+
region.cma
19+
];
1520

1621
describe("common utils", () => {
1722
const log: LogFn = () => {};
1823
let managementSdk: ContentstackClient;
24+
let managementAppSdk: ContentstackClient;
1925

2026
before(async () => {
2127
managementSdk = await managementSDKClient({
2228
host: region.cma.replace("https://", ""),
2329
});
30+
managementAppSdk = await managementSDKClient({
31+
host: developerHubBaseUrl,
32+
});
2433
});
2534

2635
describe("getOrganizations", () => {
@@ -81,4 +90,52 @@ describe("common utils", () => {
8190
.it("API fails with status code 400");
8291
});
8392
});
93+
94+
describe("fetchApps", () => {
95+
describe("Get list of Apps", () => {
96+
fancy
97+
.stub(cliux, "loader", () => {})
98+
.nock(`https://${developerHubBaseUrl}`, (api) =>
99+
api
100+
.get(
101+
"/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack"
102+
)
103+
.reply(200, {
104+
data: mock.apps,
105+
})
106+
)
107+
.it("Returns list of apps", async () => {
108+
const [app] = await fetchApps(
109+
{ "app-type": "stack" as any },
110+
"test-uid-1",
111+
{
112+
log,
113+
managementSdk: managementAppSdk,
114+
}
115+
);
116+
expect(app.uid).to.equal(mock.apps[0].uid);
117+
});
118+
});
119+
120+
describe("Get list of Apps API fail case", () => {
121+
fancy
122+
.stub(cliux, "loader", () => {})
123+
.nock(`https://${developerHubBaseUrl}`, (api) =>
124+
api
125+
.get(
126+
"/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack"
127+
)
128+
.reply(400)
129+
)
130+
.do(
131+
async () =>
132+
await fetchApps({ "app-type": "stack" as any }, "test-uid-1", {
133+
log,
134+
managementSdk: managementAppSdk,
135+
})
136+
)
137+
.catch(({ message }) => expect(message).to.contains('"status":400'))
138+
.it("Returns error code with 400");
139+
});
140+
});
84141
});

0 commit comments

Comments
 (0)