Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ jobs:
- name: Install dependencies for all plugins
run: |
npm run setup-repo-old

- name: Run tests for Audit plugin
working-directory: ./packages/contentstack-audit
run: npm run test:unit

- name: Run tests for Contentstack Config
working-directory: ./packages/contentstack-config
run: npm run test
# - name: Fetch latest references
# run: |
# git fetch --prune
Expand All @@ -41,11 +44,11 @@ jobs:
# CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
# echo "Changed files:"
# echo "$CHANGED_FILES"

# # Identify affected plugins
# AFFECTED_PLUGINS=$(echo "$CHANGED_FILES" | grep -oP '(?<=^packages/)([^/]+)' | sort -u | tr '\n' ' ')
# echo "Affected plugins: $AFFECTED_PLUGINS"

# # Set output for the next step
# echo "::set-output name=affected_plugins::$AFFECTED_PLUGINS"

Expand All @@ -59,4 +62,4 @@ jobs:
# else
# echo "contentstack-audit has not changed. Skipping tests."
# fi
# done
# done
2 changes: 1 addition & 1 deletion packages/contentstack-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version": "oclif readme && git add README.md",
"test:report": "tsc -p test && nyc --reporter=lcov --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
"pretest": "tsc -p test",
"test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
"test": "mocha --require ts-node/register 'test/**/*.test.ts'",
"posttest": "npm run lint",
"lint": "eslint src/**/*.ts",
"format": "eslint src/**/*.ts --fix",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default class RegionSetCommand extends BaseCommand<typeof RegionSetComman
'$ csdx config:set:region',
'$ csdx config:set:region NA',
'$ csdx config:set:region EU',
'$ csdx config:set:region AWS-NA',
'$ csdx config:set:region AWS-EU',
'$ csdx config:set:region AZURE-NA',
'$ csdx config:set:region AZURE-EU',
'$ csdx config:set:region GCP-NA',
Expand Down
5 changes: 3 additions & 2 deletions packages/contentstack-config/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
type: string;
name: string;
message: string;
choices?: Array<any>;

Check warning on line 9 in packages/contentstack-config/src/interfaces/index.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type
// eslint-disable-next-line @typescript-eslint/ban-types
transformer?: Function;
}

Expand All @@ -27,8 +28,8 @@

export interface RateLimitConfig {
getLimit?: Limit;
limit?: Limit
bulkLimit?: Limit
limit?: Limit;
bulkLimit?: Limit;
}

export interface SetRateLimitConfig {
Expand Down
5 changes: 3 additions & 2 deletions packages/contentstack-config/src/utils/interactive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isEmpty } from 'lodash';
import { cliux, messageHandler } from '@contentstack/cli-utilities';

export const askRegions = async (): Promise<string> => {
Expand All @@ -9,17 +8,19 @@
choices: [
{ name: 'NA', value: 'NA' },
{ name: 'EU', value: 'EU' },
{ name: 'AWS-NA', value: 'NA' },
{ name: 'AWS-EU', value: 'EU' },
{ name: 'AZURE-NA', value: 'AZURE-NA' },
{ name: 'AZURE-EU', value: 'AZURE-EU' },
{ name: 'GCP-NA', value: 'GCP-NA' },
{ name: 'GCP-EU', value: 'GCP-EU' },
{ name: 'Custom', value: 'custom' },
{ name: 'exit', value: 'exit' },
{ name: 'Exit', value: 'exit' },
],
});
};

export const askCustomRegion = async (): Promise<any> => {

Check warning on line 23 in packages/contentstack-config/src/utils/interactive.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type
const name = await cliux.inquire<string>({
type: 'input',
name: 'name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { limitNamesConfig, defaultRalteLimitConfig } from '../utils/common-utilities';
import { Limit } from '../interfaces';

let client: any;

Check warning on line 5 in packages/contentstack-config/src/utils/rate-limit-handler.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected any. Specify a different type

export class RateLimitHandler {
setClient(managementSDKClient) {
Expand Down Expand Up @@ -32,7 +32,7 @@
const features = organizations.plan?.features || [];

const limitsToUpdate: { [key: string]: Limit } = { ...rateLimit[config.org] };
let utilizationMap = {};
const utilizationMap = {};
limitNames.forEach((name, index) => {
if (utilizeValues[index] !== undefined) {
utilizationMap[name] = utilizeValues[index];
Expand Down
5 changes: 2 additions & 3 deletions packages/contentstack-config/src/utils/region-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class UserConfig {
* @returns {object} region object with cma, cda, region property
*/
setRegion(region) {
let selectedRegion = regions[region];
const selectedRegion = regions[region];
if (selectedRegion) {
configHandler.set('region', selectedRegion);
return selectedRegion;
Expand Down Expand Up @@ -151,8 +151,7 @@ class UserConfig {
* @returns { object } JSON object with only valid keys for region
*/
sanitizeRegionObject(regionObject) {
let sanitizedRegion;
sanitizedRegion = {
const sanitizedRegion = {
cma: regionObject.cma,
cda: regionObject.cda,
uiHost: regionObject.uiHost,
Expand Down
183 changes: 23 additions & 160 deletions packages/contentstack-config/test/integration/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,170 +1,33 @@
import * as path from "path";
import { expect, test } from "@oclif/test";
// @ts-ignore
import { Helper } from "./utils";
import { cliux, messageHandler } from "@contentstack/cli-utilities";
// @ts-ignore
import { PRINT_LOGS, CDA, CMA, REGION_NAME } from "./config.json";
import { expect } from 'chai';
import * as sinon from 'sinon';
import { spawnSync } from 'child_process';
import { cliux } from '@contentstack/cli-utilities';

const messageFilePath = path.join(
__dirname,
"..",
"..",
"..",
"contentstack-config",
"messages/index.json"
);
describe('ContentStack-Config Plugin Tests', () => {
it("Should execute 'config:set:region --AZURE-NA'", () => {
const result = spawnSync('csdx', ['config:set:region', 'AZURE-NA'], { encoding: 'utf-8' });
const output = result.stdout + result.stderr;

describe("ContentStack-Config plugin test", () => {
beforeEach(() => {
messageHandler.init({ messageFilePath });
});
afterEach(() => {
messageHandler.init({ messageFilePath: "" });
});

describe('Running config:set:region command without any flags and set AZURE-NA as Region', () => {
test
.stub(cliux, 'inquire', async () => 'AZURE-NA')
.stdout({ print: PRINT_LOGS || false })
.command(['config:set:region'])
.it('Check config:set:region command output [It should set AZURE-NA as region]', (ctx) => {
expect(ctx.stdout)
.to.be.a('string')
.that.have.includes(
'Region has been set to AZURE-NA\nCDA HOST: https://azure-na-cdn.contentstack.com\nCMA HOST: https://azure-na-api.contentstack.com\n',
'AZURE-NA region is not setup.!',
);
});
});

describe('Running config:set:region command without any flags and set AZURE-EU as Region', () => {
test
.stub(cliux, 'inquire', async () => 'AZURE-EU')
.stdout({ print: PRINT_LOGS || false })
.command(['config:set:region'])
.it('Check config:set:region command output [It should set AZURE-EU as region]', (ctx) => {
expect(ctx.stdout)
.to.be.a('string')
.that.have.includes(
'Region has been set to AZURE-EU\nCDA HOST: https://azure-eu-cdn.contentstack.com\nCMA HOST: https://azure-eu-api.contentstack.com\n',
'AZURE-EU region is not setup.!',
);
});
expect(output).to.include('Region has been set to AZURE-NA');
expect(output).to.include('CDA HOST: https://azure-na-cdn.contentstack.com');
expect(output).to.include('CMA HOST: https://azure-na-api.contentstack.com');
});

describe("Running config:set:region command with arg as NA region", () => {
test
.stdout({ print: PRINT_LOGS || false })
.command(["config:set:region", "NA"])
.it("Check NA region has setup", (ctx) => {
expect(ctx.stdout).to.equal(
"Region has been set to NA\nCDA HOST: https://cdn.contentstack.io\nCMA HOST: https://api.contentstack.io\n"
);
});
});

describe("Running config:set:region command with custom Name, CMA, CDA flags", () => {
test
.stdout({ print: PRINT_LOGS || false })
.command([
"config:set:region",
`-n=${REGION_NAME || "Test"}`,
`-d=${CDA || "https://cdn.contentstack.io"}`,
`-m=${CMA || "https://api.contentstack.io"}`,
])
.it(
`Check Name=${REGION_NAME || "Test"}, CDA=${
CDA || "https://cdn.contentstack.io"
}, CMA=${CMA || "https://api.contentstack.io"} values has setup`,
(ctx) => {
expect(ctx.stdout).to.equal(
`Custom region has been set to ${
REGION_NAME || "Test"
}\nCMA HOST: ${CMA || "https://api.contentstack.io"}\nCDA HOST: ${
CDA || "https://cdn.contentstack.io"
}\n`
);
}
);
});
it("Should execute 'config:get:region' and return the current region", () => {
const result = spawnSync('csdx', ['config:get:region'], { encoding: 'utf-8' });
const output = result.stdout + result.stderr;

describe("Running config:set:region command without any flags and setting custom values", () => {
test
// @ts-ignore
.stub(cliux, "inquire", async (inquire) => {
switch (inquire.name) {
case "selectedRegion":
return "custom";
case "name":
return REGION_NAME || "Test";
case "cma":
return CMA || "https://api.contentstack.io";
case "cda":
return CDA || "https://cdn.contentstack.io";
}
})
.stdout({ print: PRINT_LOGS || false })
.stderr()
.command(["config:set:region"])
.it("Verifying output with custom values", async (ctx) => {
expect(ctx.stdout)
.to.be.a("string")
.that.have.includes(
`Custom region has been set to ${
REGION_NAME || "Test"
}\nCMA HOST: ${CMA || "https://api.contentstack.io"}\nCDA HOST: ${
CDA || "https://cdn.contentstack.io"
}\n`,
"Custom CDA, CMA setup failed.!"
);
});
expect(output).to.include('Currently using');
expect(output).to.include('CDA HOST:');
expect(output).to.include('CMA HOST:');
});

describe("Running config:get:region command", () => {
let currentRegion;
before(async () => {
currentRegion = await Helper.run();
});
after(() => {
currentRegion = null;
});
it("Should execute 'config:set:region NA' and set NA region", () => {
const result = spawnSync('csdx', ['config:set:region', 'NA'], { encoding: 'utf-8' });
const output = result.stdout + result.stderr;

test
.stdout({ print: PRINT_LOGS || false })
.command(["config:get:region"])
.it("Should print current region and it's CDA, CMA as", (ctx) => {
expect(ctx.stdout, "Expected output not found.!").to.equal(
`Currently using ${currentRegion.name} region\nCDA HOST: ${currentRegion.cda}\nCMA HOST: ${currentRegion.cma}\n`
);
});
expect(output).to.include('Region has been set to NA');
expect(output).to.include('CDA HOST: https://cdn.contentstack.io');
expect(output).to.include('CMA HOST: https://api.contentstack.io');
});

// describe('Running config:get:region command to check if a region is setup', () => {
// let currentRegion;
// before(async () => {
// currentRegion = await Helper.run();
// });
// after(() => {
// currentRegion = null;
// });

// test
// .stdout({ print: PRINT_LOGS || false })
// .command(['config:get:region'])
// .catch((error) => {
// if (error.message) {
// expect(error.message).to.be.a('string').equals('EEXIT: 0');
// }
// })
// .it("Should print current region and it's CDA, CMA or region should be undefined", (ctx) => {
// if (currentRegion) {
// expect(ctx.stdout).to.includes(
// `Currently using ${currentRegion.name} region\nCDA HOST: ${currentRegion.cda}\nCMA HOST: ${currentRegion.cma}\n`,
// );
// } else {
// expect(ctx.error).to.be.undefined;
// }
// });
// });
});
Loading
Loading