Skip to content

Commit 7332748

Browse files
author
naman-contentstack
committed
created a helper file for stubbing authentication
1 parent 76188a0 commit 7332748

10 files changed

Lines changed: 74 additions & 203 deletions

File tree

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,34 @@ $ csdx plugins:install @contentstack/apps-cli
1616
This plugin requires you to be authenticated using [csdx auth:login](https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/).
1717

1818
<!-- usage -->
19+
1920
```sh-session
2021
$ npm install -g @contentstack/apps-cli
2122
$ csdx COMMAND
2223
running command...
2324
$ csdx (--version|-v)
24-
@contentstack/apps-cli/1.6.0 darwin-arm64 node-v22.13.1
25+
@contentstack/apps-cli/1.6.1 darwin-arm64 node-v22.13.1
2526
$ csdx --help [COMMAND]
2627
USAGE
2728
$ csdx COMMAND
2829
...
2930
```
31+
3032
<!-- usagestop -->
3133

3234
# Commands
3335

3436
<!-- commands -->
35-
* [`csdx app`](#csdx-app)
36-
* [`csdx app:create`](#csdx-appcreate)
37-
* [`csdx app:delete`](#csdx-appdelete)
38-
* [`csdx app:deploy`](#csdx-appdeploy)
39-
* [`csdx app:get`](#csdx-appget)
40-
* [`csdx app:install`](#csdx-appinstall)
41-
* [`csdx app:reinstall`](#csdx-appreinstall)
42-
* [`csdx app:uninstall`](#csdx-appuninstall)
43-
* [`csdx app:update`](#csdx-appupdate)
37+
38+
- [`csdx app`](#csdx-app)
39+
- [`csdx app:create`](#csdx-appcreate)
40+
- [`csdx app:delete`](#csdx-appdelete)
41+
- [`csdx app:deploy`](#csdx-appdeploy)
42+
- [`csdx app:get`](#csdx-appget)
43+
- [`csdx app:install`](#csdx-appinstall)
44+
- [`csdx app:reinstall`](#csdx-appreinstall)
45+
- [`csdx app:uninstall`](#csdx-appuninstall)
46+
- [`csdx app:update`](#csdx-appupdate)
4447

4548
## `csdx app`
4649

@@ -307,4 +310,5 @@ EXAMPLES
307310
```
308311

309312
_See code: [src/commands/app/update.ts](https://github.com/contentstack/apps-cli/blob/v1.6.0/src/commands/app/update.ts)_
313+
310314
<!-- commandsstop -->

test/unit/commands/app/create.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import manifestData from "../../../../src/config/manifest.json";
1515
import orgManifestData from "../../../unit/config/org_manifest.json";
1616
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
1717
import axios from "axios";
18+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
1819

1920
const { origin, pathname } = new URL(config.appBoilerplateGithubUrl);
2021
const zipPath = join(process.cwd(), "test", "unit", "mock", "boilerplate.zip");
@@ -36,30 +37,8 @@ describe("app:create", () => {
3637
sandbox = sinon.createSandbox();
3738
axios.defaults.adapter = "http";
3839

39-
// Stub authentication
40-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
41-
if (key === "region") {
42-
return {
43-
cma: "https://api.contentstack.io",
44-
cda: "https://cdn.contentstack.io",
45-
region: "us",
46-
};
47-
}
48-
if (key === "authtoken") {
49-
return "mock-auth-token";
50-
}
51-
if (key === "authorisationType") {
52-
return "BASIC";
53-
}
54-
return undefined;
55-
});
56-
57-
sandbox
58-
.stub(
59-
require("../../../../src/base-command").BaseCommand.prototype,
60-
"validateRegionAndAuth"
61-
)
62-
.callsFake(() => {});
40+
// Stub authentication using shared helper
41+
stubAuthentication(sandbox);
6342

6443
writeStreamMock = new MockWriteStream();
6544
sandbox.stub(fs, "renameSync").callsFake(() => {});

test/unit/commands/app/delete.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as mock from "../../mock/common.mock.json";
66
import messages, { $t } from "../../../../src/messages";
77
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
88
import nock from "nock";
9+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
910

1011
const region: { cma: string; name: string; cda: string } =
1112
configHandler.get("region");
@@ -16,30 +17,8 @@ describe("app:delete", () => {
1617
beforeEach(() => {
1718
sandbox = sinon.createSandbox();
1819

19-
// Stub authentication
20-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
21-
if (key === "region") {
22-
return {
23-
cma: "https://api.contentstack.io",
24-
cda: "https://cdn.contentstack.io",
25-
region: "us",
26-
};
27-
}
28-
if (key === "authtoken") {
29-
return "mock-auth-token";
30-
}
31-
if (key === "authorisationType") {
32-
return "BASIC";
33-
}
34-
return undefined;
35-
});
36-
37-
sandbox
38-
.stub(
39-
require("../../../../src/base-command").BaseCommand.prototype,
40-
"validateRegionAndAuth"
41-
)
42-
.callsFake(() => {});
20+
// Stub authentication using shared helper
21+
stubAuthentication(sandbox);
4322

4423
nock(region.cma)
4524
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")

test/unit/commands/app/deploy.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import messages, { $t } from "../../../../src/messages";
66
import * as mock from "../../mock/common.mock.json";
77
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
88
import sinon from "sinon";
9+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
910

1011
const region = configHandler.get("region");
1112
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -16,30 +17,8 @@ describe("app:deploy", () => {
1617
beforeEach(() => {
1718
sandbox = sinon.createSandbox();
1819

19-
// Stub authentication
20-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
21-
if (key === "region") {
22-
return {
23-
cma: "https://api.contentstack.io",
24-
cda: "https://cdn.contentstack.io",
25-
region: "us",
26-
};
27-
}
28-
if (key === "authtoken") {
29-
return "mock-auth-token";
30-
}
31-
if (key === "authorisationType") {
32-
return "BASIC";
33-
}
34-
return undefined;
35-
});
36-
37-
sandbox
38-
.stub(
39-
require("../../../../src/base-command").BaseCommand.prototype,
40-
"validateRegionAndAuth"
41-
)
42-
.callsFake(() => {});
20+
// Stub authentication using shared helper
21+
stubAuthentication(sandbox);
4322

4423
sandbox.stub(cliux, "loader").callsFake(() => {});
4524
sandbox.stub(cliux, "inquire").callsFake((prompt: any) => {

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as mock from "../../mock/common.mock.json";
1111
import manifestData from "../../../../src/config/manifest.json";
1212
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
1313
import config from "../../../../src/config";
14+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
1415

1516
const region = configHandler.get("region");
1617
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -21,30 +22,8 @@ describe("app:get", () => {
2122
beforeEach(() => {
2223
sandbox = sinon.createSandbox();
2324

24-
// Stub authentication
25-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
26-
if (key === "region") {
27-
return {
28-
cma: "https://api.contentstack.io",
29-
cda: "https://cdn.contentstack.io",
30-
region: "us",
31-
};
32-
}
33-
if (key === "authtoken") {
34-
return "mock-auth-token";
35-
}
36-
if (key === "authorisationType") {
37-
return "BASIC";
38-
}
39-
return undefined;
40-
});
41-
42-
sandbox
43-
.stub(
44-
require("../../../../src/base-command").BaseCommand.prototype,
45-
"validateRegionAndAuth"
46-
)
47-
.callsFake(() => {});
25+
// Stub authentication using shared helper
26+
stubAuthentication(sandbox);
4827

4928
sandbox.stub(cliux, "loader").callsFake(() => {});
5029
sandbox.stub(fs, "writeFileSync").callsFake(() => {});

test/unit/commands/app/install.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import messages from "../../../../src/messages";
77
import * as mock from "../../mock/common.mock.json";
88
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
99
import axios from "axios";
10+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
1011

1112
const region = configHandler.get("region");
1213
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -18,30 +19,8 @@ describe("app:install", () => {
1819
sandbox = sinon.createSandbox();
1920
axios.defaults.adapter = "http";
2021

21-
// Stub authentication
22-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
23-
if (key === "region") {
24-
return {
25-
cma: "https://api.contentstack.io",
26-
cda: "https://cdn.contentstack.io",
27-
region: "us",
28-
};
29-
}
30-
if (key === "authtoken") {
31-
return "mock-auth-token";
32-
}
33-
if (key === "authorisationType") {
34-
return "BASIC";
35-
}
36-
return undefined;
37-
});
38-
39-
sandbox
40-
.stub(
41-
require("../../../../src/base-command").BaseCommand.prototype,
42-
"validateRegionAndAuth"
43-
)
44-
.callsFake(() => {});
22+
// Stub authentication using shared helper
23+
stubAuthentication(sandbox);
4524

4625
sandbox.stub(cliux, "loader").callsFake(() => {});
4726

test/unit/commands/app/reinstall.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { cliux, configHandler } from "@contentstack/cli-utilities";
66
import messages, { $t } from "../../../../src/messages";
77
import * as mock from "../../mock/common.mock.json";
88
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
9+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
910

1011
const region = configHandler.get("region");
1112
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -16,30 +17,8 @@ describe("app:reinstall", () => {
1617
beforeEach(() => {
1718
sandbox = sinon.createSandbox();
1819

19-
// Stub authentication
20-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
21-
if (key === "region") {
22-
return {
23-
cma: "https://api.contentstack.io",
24-
cda: "https://cdn.contentstack.io",
25-
region: "us",
26-
};
27-
}
28-
if (key === "authtoken") {
29-
return "mock-auth-token";
30-
}
31-
if (key === "authorisationType") {
32-
return "BASIC";
33-
}
34-
return undefined;
35-
});
36-
37-
sandbox
38-
.stub(
39-
require("../../../../src/base-command").BaseCommand.prototype,
40-
"validateRegionAndAuth"
41-
)
42-
.callsFake(() => {});
20+
// Stub authentication using shared helper
21+
stubAuthentication(sandbox);
4322

4423
sandbox.stub(cliux, "loader").callsFake(() => {});
4524

test/unit/commands/app/uninstall.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { cliux, configHandler } from "@contentstack/cli-utilities";
66
import messages, { $t } from "../../../../src/messages";
77
import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
88
import * as mock from "../../mock/common.mock.json";
9+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
910

1011
const region = configHandler.get("region");
1112
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -16,30 +17,8 @@ describe("app:uninstall", () => {
1617
beforeEach(() => {
1718
sandbox = sinon.createSandbox();
1819

19-
// Stub authentication
20-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
21-
if (key === "region") {
22-
return {
23-
cma: "https://api.contentstack.io",
24-
cda: "https://cdn.contentstack.io",
25-
region: "us",
26-
};
27-
}
28-
if (key === "authtoken") {
29-
return "mock-auth-token";
30-
}
31-
if (key === "authorisationType") {
32-
return "BASIC";
33-
}
34-
return undefined;
35-
});
36-
37-
sandbox
38-
.stub(
39-
require("../../../../src/base-command").BaseCommand.prototype,
40-
"validateRegionAndAuth"
41-
)
42-
.callsFake(() => {});
20+
// Stub authentication using shared helper
21+
stubAuthentication(sandbox);
4322

4423
sandbox.stub(cliux, "loader").callsFake(() => {});
4524

test/unit/commands/app/update.test.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getDeveloperHubUrl } from "../../../../src/util/inquirer";
99
import sinon from "sinon";
1010
import nock from "nock";
1111
import fs from "fs";
12+
import { stubAuthentication } from "../../helpers/auth-stub-helper";
1213

1314
const region = configHandler.get("region");
1415
const developerHubBaseUrl = getDeveloperHubUrl();
@@ -19,31 +20,8 @@ describe("app:update", () => {
1920
beforeEach(() => {
2021
sandbox = sinon.createSandbox();
2122

22-
// Stub authentication
23-
sandbox.stub(configHandler, "get").callsFake((key: string) => {
24-
if (key === "region") {
25-
return {
26-
cma: "https://api.contentstack.io",
27-
cda: "https://cdn.contentstack.io",
28-
region: "us",
29-
};
30-
}
31-
if (key === "authtoken") {
32-
return "mock-auth-token";
33-
}
34-
if (key === "authorisationType") {
35-
return "BASIC";
36-
}
37-
return undefined;
38-
});
39-
40-
// Stub the validateRegionAndAuth method to skip authentication check
41-
sandbox
42-
.stub(
43-
require("../../../../src/base-command").BaseCommand.prototype,
44-
"validateRegionAndAuth"
45-
)
46-
.callsFake(() => {});
23+
// Stub authentication using shared helper
24+
stubAuthentication(sandbox);
4725

4826
nock(region.cma)
4927
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")

0 commit comments

Comments
 (0)