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
22 changes: 18 additions & 4 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name: Run Unit Tests
name: Unit Test & Reports

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
run-tests:
build-test:
name: Build & Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -24,5 +25,18 @@ jobs:
- name: Configure Region
run: csdx config:set:region AWS-NA

- name: Run tests
run: npm run test
- name: Run tests with coverage
run: npm run test:unit:report:json

- name: Test Report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: Mocha Unit Test
path: report.json
reporter: mocha-json

- name: Coverage Report
uses: lucassabreu/comment-coverage-clover@main
with:
file: coverage/clover.xml
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md",
"clean": "rm -rf ./lib tsconfig.tsbuildinfo oclif.manifest.json",
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\"",
"test:unit:report:json": "mocha --reporter json --reporter-options output=report.json --forbid-only \"test/unit/**/*.test.ts\" && nyc --reporter=clover --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
},
"engines": {
"node": ">=16"
Expand Down
73 changes: 73 additions & 0 deletions test/unit/commands/app/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,77 @@ describe("app:create", () => {
expect(result.stdout).to.contain("App could not be registered");
});
});

describe("App creation with duplicate app name", () => {
beforeEach(() => {
nock(`https://${developerHubBaseUrl}`)
.post("/manifests", (body) => {
return body.name === "test-app" && body.target_type === "stack";
})
.reply(409, {
errorMessage: "App with this name already exists.",
});

sandbox.stub(cliux, "loader").callsFake(() => {});
sandbox.stub(fs, "writeFileSync").callsFake(() => {});
sandbox.stub(cliux, "inquire").callsFake((prompt: any) => {
const cases: Record<string, any> = {
appName: "test-app",
cloneBoilerplate: true,
Organization: "test org 1",
};
return Promise.resolve(cases[prompt.name]);
});
});

it("should fail when app name already exists", async () => {
const result = await runCommand([
"app:create",
"--name",
"test-app",
"--data-dir",
process.cwd(),
]);
expect(result.stdout).to.contain("already exists");
});
});

describe("App creation with organization UID instead of app UID", () => {
beforeEach(() => {
nock(region.cma)
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
.reply(200, { organizations: mock.organizations });

nock(`https://${developerHubBaseUrl}`)
.post("/manifests", (body) => {
return body.name === "test-app" && body.target_type === "stack";
})
.reply(400, {
errorMessage:
"Invalid app configuration. Organization UID provided instead of app data.",
});

sandbox.stub(cliux, "loader").callsFake(() => {});
sandbox.stub(fs, "writeFileSync").callsFake(() => {});
sandbox.stub(cliux, "inquire").callsFake((prompt: any) => {
const cases: Record<string, any> = {
appName: "test-app",
cloneBoilerplate: true,
Organization: "test org 1",
};
return Promise.resolve(cases[prompt.name]);
});
});

it("should fail when organization UID is used instead of app data", async () => {
const result = await runCommand([
"app:create",
"--name",
"test-app",
"--data-dir",
process.cwd(),
]);
expect(result.stdout).to.contain("App could not be registered");
});
});
});
106 changes: 106 additions & 0 deletions test/unit/commands/app/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,110 @@ describe("app:deploy", () => {
);
});
});

describe("Deploy app error handling", () => {
it("should fail with invalid hosting type", async () => {
sandbox.restore();
sandbox = sinon.createSandbox();
stubAuthentication(sandbox);

sandbox.stub(cliux, "loader").callsFake(() => {});
sandbox.stub(cliux, "inquire").callsFake((prompt: any) => {
const cases: Record<string, any> = {
App: mock.apps[1].name,
Organization: mock.organizations[0].name,
"hosting types": "invalid-hosting",
};
return Promise.resolve(cases[prompt.name]);
});

sandbox
.stub(require("../../../../src/util/common-utils"), "getProjects")
.resolves([]);
sandbox
.stub(require("../../../../src/util/common-utils"), "updateApp")
.resolves();

nock(region.cma)
.get(
"/v3/organizations?limit=100&asc=name&asc=name&include_count=true&skip=0"
)
.reply(200, { organizations: mock.organizations });

nock(`https://${developerHubBaseUrl}`)
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
.reply(200, { data: mock.apps2 });

const { stdout } = await runCommand(["app:deploy"], {
root: process.cwd(),
});
expect(stdout).to.contain("Please provide a valid Hosting Type.");
});

it("should handle new project creation with hosting-with-launch", async () => {
sandbox.restore();
sandbox = sinon.createSandbox();
stubAuthentication(sandbox);

sandbox.stub(cliux, "loader").callsFake(() => {});
sandbox.stub(cliux, "inquire").callsFake((prompt: any) => {
const cases: Record<string, any> = {
App: mock.apps[1].name,
Organization: mock.organizations[0].name,
"hosting types": "hosting-with-launch",
"launch project": "new",
};
return Promise.resolve(cases[prompt.name]);
});

sandbox
.stub(require("../../../../src/util/common-utils"), "getProjects")
.resolves([
{
name: "new-project",
uid: "project-2",
url: "https://new-project.com",
environmentUid: "env-2",
},
]);
sandbox
.stub(require("../../../../src/util/common-utils"), "setupConfig")
.returns({
name: "new-project",
type: "react",
environment: "production",
framework: "nextjs",
});
sandbox
.stub(
require("../../../../src/util/common-utils"),
"handleProjectNameConflict"
)
.resolves("new-project");
sandbox
.stub(require("@contentstack/cli-launch").Launch, "run")
.resolves();

nock(region.cma)
.get(
"/v3/organizations?limit=100&asc=name&asc=name&include_count=true&skip=0"
)
.reply(200, { organizations: mock.organizations });

nock(`https://${developerHubBaseUrl}`)
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
.reply(200, { data: mock.apps2 });

nock(`https://${developerHubBaseUrl}`)
.put(`/manifests/${mock.apps2[1].uid}`)
.reply(200, mock.deploy_launch_host);

const { stdout } = await runCommand(["app:deploy"], {
root: process.cwd(),
});
expect(stdout).to.contain(
$t(messages.APP_DEPLOYED, { app: mock.apps[1].name })
);
});
});
});
Loading