From baadfebf867e6c638707a6046d419b05b8138012 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 29 May 2024 12:13:37 +0530 Subject: [PATCH 1/2] fix: removed static url and updated the value of the url in test cases --- src/config/index.ts | 10 ---------- test/unit/commands/app/create.test.ts | 4 +--- test/unit/commands/app/delete.test.ts | 5 +---- test/unit/commands/app/get.test.ts | 4 +--- test/unit/commands/app/install.test.ts | 3 +-- test/unit/commands/app/uninstall.test.ts | 3 +-- test/unit/commands/app/update.test.ts | 5 +---- test/unit/util/common-utils.test.ts | 5 +---- 8 files changed, 7 insertions(+), 32 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index a0f84d4..b0ba642 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -5,16 +5,6 @@ const config = { manifestPath: resolve(__dirname, "manifest.json"), boilerplateName: "marketplace-app-boilerplate-main", developerHubBaseUrl: "", - developerHubUrls: { - // NOTE CDA url used as developer-hub url mapper to avoid conflict if user used any custom name - "https://api.contentstack.io": "developerhub-api.contentstack.com", - "https://eu-api.contentstack.com": "eu-developerhub-api.contentstack.com", - "https://azure-na-api.contentstack.com": - "azure-na-developerhub-api.contentstack.com", - "https://azure-eu-api.contentstack.com": - "azure-eu-developerhub-api.contentstack.com", - "https://gcp-na-api.contentstack.com": "gcp-na-developerhub-api.contentstack.com", - }, appBoilerplateGithubUrl: "https://codeload.github.com/contentstack/marketplace-app-boilerplate/zip/refs/heads/main", defaultAppFileName: "manifest", diff --git a/test/unit/commands/app/create.test.ts b/test/unit/commands/app/create.test.ts index 587a6f5..7805f2b 100644 --- a/test/unit/commands/app/create.test.ts +++ b/test/unit/commands/app/create.test.ts @@ -17,9 +17,7 @@ const { origin, pathname } = new URL(config.appBoilerplateGithubUrl); const zipPath = join(process.cwd(), "test", "unit", "mock", "boilerplate.zip"); const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[ - region.cma -]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:create", () => { beforeEach(() => { diff --git a/test/unit/commands/app/delete.test.ts b/test/unit/commands/app/delete.test.ts index eadbaae..4128dbc 100644 --- a/test/unit/commands/app/delete.test.ts +++ b/test/unit/commands/app/delete.test.ts @@ -2,14 +2,11 @@ import { test, expect } from "@oclif/test"; import { cliux, configHandler, ux } from "@contentstack/cli-utilities"; import * as mock from "../../mock/common.mock.json" -import config from "../../../../src/config"; import messages, {$t} from "../../../../src/messages"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[ - region.cma -]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:delete", () => { describe("app:delete with --org and --app-uid flags", () => { diff --git a/test/unit/commands/app/get.test.ts b/test/unit/commands/app/get.test.ts index 0202dbd..72763e9 100644 --- a/test/unit/commands/app/get.test.ts +++ b/test/unit/commands/app/get.test.ts @@ -12,9 +12,7 @@ import * as commonUtils from "../../../../src/util/common-utils"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[ - region.cma -]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:get", () => { describe("Get app manifest", () => { diff --git a/test/unit/commands/app/install.test.ts b/test/unit/commands/app/install.test.ts index 694d367..c6a4506 100644 --- a/test/unit/commands/app/install.test.ts +++ b/test/unit/commands/app/install.test.ts @@ -3,11 +3,10 @@ import {expect, test} from "@oclif/test"; import * as mock from "../../mock/common.mock.json" -import config from "../../../../src/config"; import messages, { $t } from "../../../../src/messages"; const region: { cma: string, cda: string, name: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[region.cma]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:install", () => { describe("Install an app on organization", () => { diff --git a/test/unit/commands/app/uninstall.test.ts b/test/unit/commands/app/uninstall.test.ts index 55157a0..f41c1e9 100644 --- a/test/unit/commands/app/uninstall.test.ts +++ b/test/unit/commands/app/uninstall.test.ts @@ -3,11 +3,10 @@ import { expect, test } from "@oclif/test"; import * as mock from "../../mock/common.mock.json" -import config from "../../../../src/config" import messages, {$t} from "../../../../src/messages"; const region: { cma: string, cda: string, name: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[region.cma]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:uninstall", () => { describe("Uninstall an app from organization", () => { diff --git a/test/unit/commands/app/update.test.ts b/test/unit/commands/app/update.test.ts index 39f2ec9..d5b57b7 100644 --- a/test/unit/commands/app/update.test.ts +++ b/test/unit/commands/app/update.test.ts @@ -4,16 +4,13 @@ import { PassThrough } from "stream"; import { expect, test } from "@oclif/test"; import { cliux, ux, configHandler } from "@contentstack/cli-utilities"; -import config from "../../../../src/config"; import messages from "../../../../src/messages"; import * as mock from "../../mock/common.mock.json"; import manifestData from "../../config/manifest.json"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[ - region.cma -]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("app:update", () => { describe("Update app with `--app-manifest` flag", () => { diff --git a/test/unit/util/common-utils.test.ts b/test/unit/util/common-utils.test.ts index ea166bd..5ddc7f7 100644 --- a/test/unit/util/common-utils.test.ts +++ b/test/unit/util/common-utils.test.ts @@ -7,16 +7,13 @@ import { managementSDKClient, } from "@contentstack/cli-utilities"; -import config from "../../../src/config"; import { LogFn } from "../../../src/types"; import * as mock from "../mock/common.mock.json"; import { fetchApps, getOrganizations } from "../../../src/util/common-utils"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = (config.developerHubUrls as Record)[ - region.cma -]; +const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); describe("common utils", () => { const log: LogFn = () => {}; From 065e56b641b6e718b30e2ff56a7415c04f4cb7f2 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 29 May 2024 13:19:17 +0530 Subject: [PATCH 2/2] fix: updated value of developerHubBaseUrl to fetch dynamically --- src/base-command.ts | 10 +++++----- src/util/inquirer.ts | 2 +- test/unit/commands/app/create.test.ts | 3 ++- test/unit/commands/app/delete.test.ts | 3 ++- test/unit/commands/app/get.test.ts | 4 +++- test/unit/commands/app/install.test.ts | 3 ++- test/unit/commands/app/reinstall.test.ts | 6 ++++-- test/unit/commands/app/uninstall.test.ts | 3 ++- test/unit/commands/app/update.test.ts | 3 ++- test/unit/util/common-utils.test.ts | 4 +++- 10 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/base-command.ts b/src/base-command.ts index 570b3b0..6ab435e 100644 --- a/src/base-command.ts +++ b/src/base-command.ts @@ -70,9 +70,9 @@ export abstract class BaseCommand extends Command { this.registerConfig(); this.developerHubBaseUrl = - this.sharedConfig.developerHubBaseUrl || (await getDeveloperHubUrl()); + this.sharedConfig.developerHubBaseUrl || getDeveloperHubUrl(); await this.initCmaSDK(); - + // Init logger const logger = new Logger(this.sharedConfig); this.log = logger.log.bind(logger); @@ -164,9 +164,9 @@ export abstract class BaseCommand extends Command { validateRegionAndAuth() { if (this.region) { if (!isAuthenticated()) { - this.log(this.messages.CLI_APP_CLI_LOGIN_FAILED, "error"); - this.exit(1); - } + this.log(this.messages.CLI_APP_CLI_LOGIN_FAILED, "error"); + this.exit(1); + } } } } diff --git a/src/util/inquirer.ts b/src/util/inquirer.ts index 4d6bfb8..51f44e2 100644 --- a/src/util/inquirer.ts +++ b/src/util/inquirer.ts @@ -158,7 +158,7 @@ async function getInstalledApps( * * @return {*} {Promise} */ -async function getDeveloperHubUrl(): Promise { +function getDeveloperHubUrl(): string { const { cma } = configHandler.get("region") || {}; let developerHubBaseUrl = cma.replace("api", "developerhub-api"); diff --git a/test/unit/commands/app/create.test.ts b/test/unit/commands/app/create.test.ts index 7805f2b..cedd9ea 100644 --- a/test/unit/commands/app/create.test.ts +++ b/test/unit/commands/app/create.test.ts @@ -12,12 +12,13 @@ import config from "../../../../src/config"; import messages from "../../../../src/messages"; import * as mock from "../../mock/common.mock.json"; import manifestData from "../../../../src/config/manifest.json"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const { origin, pathname } = new URL(config.appBoilerplateGithubUrl); const zipPath = join(process.cwd(), "test", "unit", "mock", "boilerplate.zip"); const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); describe("app:create", () => { beforeEach(() => { diff --git a/test/unit/commands/app/delete.test.ts b/test/unit/commands/app/delete.test.ts index 4128dbc..c3d8dea 100644 --- a/test/unit/commands/app/delete.test.ts +++ b/test/unit/commands/app/delete.test.ts @@ -3,10 +3,11 @@ import { cliux, configHandler, ux } from "@contentstack/cli-utilities"; import * as mock from "../../mock/common.mock.json" import messages, {$t} from "../../../../src/messages"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); describe("app:delete", () => { describe("app:delete with --org and --app-uid flags", () => { diff --git a/test/unit/commands/app/get.test.ts b/test/unit/commands/app/get.test.ts index 72763e9..c71ff7c 100644 --- a/test/unit/commands/app/get.test.ts +++ b/test/unit/commands/app/get.test.ts @@ -9,10 +9,12 @@ import * as mock from "../../mock/common.mock.json"; import manifestData from "../../config/manifest.json"; import messages, { $t } from "../../../../src/messages"; import * as commonUtils from "../../../../src/util/common-utils"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); + describe("app:get", () => { describe("Get app manifest", () => { diff --git a/test/unit/commands/app/install.test.ts b/test/unit/commands/app/install.test.ts index c6a4506..7516db4 100644 --- a/test/unit/commands/app/install.test.ts +++ b/test/unit/commands/app/install.test.ts @@ -4,9 +4,10 @@ import {expect, test} from "@oclif/test"; import * as mock from "../../mock/common.mock.json" import messages, { $t } from "../../../../src/messages"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const region: { cma: string, cda: string, name: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); describe("app:install", () => { describe("Install an app on organization", () => { diff --git a/test/unit/commands/app/reinstall.test.ts b/test/unit/commands/app/reinstall.test.ts index a73baf1..d34b546 100644 --- a/test/unit/commands/app/reinstall.test.ts +++ b/test/unit/commands/app/reinstall.test.ts @@ -2,14 +2,16 @@ import { ux, cliux, configHandler } from "@contentstack/cli-utilities"; import { expect, test } from "@oclif/test"; import * as mock from "../../mock/common.mock.json"; import messages, { $t } from "../../../../src/messages"; +import { getDeveloperHubUrl } from "../../../../lib/util/inquirer"; const region: { cma: string; cda: string; name: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); + describe("app:reinstall", () => { describe("Reinstall an app on a stack", () => { test - .stdout({ print: true }) + .stdout({ print: process.env.PRINT === "true" || false }) .stub(ux.action, "stop", () => {}) .stub(ux.action, "start", () => {}) .stub(cliux, "inquire", async (...args: any) => { diff --git a/test/unit/commands/app/uninstall.test.ts b/test/unit/commands/app/uninstall.test.ts index f41c1e9..55cf098 100644 --- a/test/unit/commands/app/uninstall.test.ts +++ b/test/unit/commands/app/uninstall.test.ts @@ -4,9 +4,10 @@ import { expect, test } from "@oclif/test"; import * as mock from "../../mock/common.mock.json" import messages, {$t} from "../../../../src/messages"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const region: { cma: string, cda: string, name: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); describe("app:uninstall", () => { describe("Uninstall an app from organization", () => { diff --git a/test/unit/commands/app/update.test.ts b/test/unit/commands/app/update.test.ts index d5b57b7..36f375e 100644 --- a/test/unit/commands/app/update.test.ts +++ b/test/unit/commands/app/update.test.ts @@ -7,10 +7,11 @@ import { cliux, ux, configHandler } from "@contentstack/cli-utilities"; import messages from "../../../../src/messages"; import * as mock from "../../mock/common.mock.json"; import manifestData from "../../config/manifest.json"; +import { getDeveloperHubUrl } from "../../../../src/util/inquirer"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); describe("app:update", () => { describe("Update app with `--app-manifest` flag", () => { diff --git a/test/unit/util/common-utils.test.ts b/test/unit/util/common-utils.test.ts index 5ddc7f7..8c288df 100644 --- a/test/unit/util/common-utils.test.ts +++ b/test/unit/util/common-utils.test.ts @@ -10,10 +10,12 @@ import { import { LogFn } from "../../../src/types"; import * as mock from "../mock/common.mock.json"; import { fetchApps, getOrganizations } from "../../../src/util/common-utils"; +import { getDeveloperHubUrl } from "../../../src/util/inquirer"; const region: { cma: string; name: string; cda: string } = configHandler.get("region"); -const developerHubBaseUrl = configHandler.get("developerHubBaseUrl"); +const developerHubBaseUrl = getDeveloperHubUrl(); + describe("common utils", () => { const log: LogFn = () => {};