Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
W.I.P.
Browse files Browse the repository at this point in the history
  • Loading branch information
pierce-h committed Sep 2, 2020
1 parent 90d5a1f commit b70bc4c
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 191 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"axios": "^0.20.0",
"chai": "^4.2.0",
"chalk": "^4.1.0",
"cli-table": "^0.3.1",
"cli-ux": "^5.5.0",
"escape-string-regexp": "^4.0.0",
"express": "^4.17.1",
Expand Down Expand Up @@ -59,6 +60,7 @@
"@oclif/dev-cli": "^1.22.2",
"@oclif/test": "^1.2.6",
"@types/chai": "^4.2.12",
"@types/cli-table": "^0.3.0",
"@types/form-data": "^2.5.0",
"@types/fs-extra": "^9.0.1",
"@types/hapi__joi": "^17.1.4",
Expand Down
23 changes: 11 additions & 12 deletions src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ApiKeyStore from "./core/utils/api-key-store";
import APIClient from "./core/api-client";
import fs from "fs";
import path from "path";
// import { AppUser } from "./core/types";
import { AppUser } from "./core/types";
import { Command as Base } from "@oclif/command";

interface PackageJSON {
Expand All @@ -20,16 +20,15 @@ export default abstract class BaseCommand extends Base {
return new APIClient(apiKey);
}

// public async currentUser(): Promise<AppUser> {
// try {
// const apiClient = await this.apiClient();
// const user = await apiClient.user.getCurrent();
/**
* Get the current user logged into the CLI
* @returns {Promise<AppUser>} A promise w/ the user object
*/
public async getCurrentUser(): Promise<AppUser> {
const apiKey = await ApiKeyStore.get()
const apiClient = new APIClient(apiKey);
const user = await apiClient.user.getCurrent()

// return user;
// }
// catch (error) {
// const err = error as Error;
// this.error(err.message);
// }
// }
return user;
}
}
42 changes: 23 additions & 19 deletions src/commands/apps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import BaseCommand from "../base-command";
import { flags } from "@oclif/command";
// import { checkAppLoginStatus } from "../core/utils/current-user";
import Login from './login';
import Table from 'cli-table';

export default class AppsIndex extends BaseCommand {
export default class Apps extends BaseCommand {
public static description = "list your apps";

public static flags = {
Expand All @@ -12,24 +13,27 @@ export default class AppsIndex extends BaseCommand {
}),
};

// hide the command from help
// public static hidden = true;

async run(): Promise<void> {
// When the -h flag is present the following line haults execution
this.parse(AppsIndex);

// await checkAppLoginStatus(this);

// try {
// if (this.appsClient) {
// const apps = this.appsClient.apps.getAll();
// (await apps).items.forEach((app) => {
// this.log(app.name);
// });
// }
// } catch (error) {
// this.error(error);
// }
this.parse(Apps);

try {
await this.getCurrentUser();
} catch {
await Login.run([])
}

const table = new Table({
head: ['ID', 'Name', "Type"]
});

const apiClient = await this.apiClient()
const apps = apiClient.apps.getAll();

(await apps).items.forEach((app) => {
table.push([app.id, app.name, app.type])
});

console.log(table.toString());
}
}
3 changes: 1 addition & 2 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import BaseCommand from "../base-command";
import { flags } from "@oclif/command";
import cli from "cli-ux";
import * as ApiKeyStore from "../core/utils/api-key-store";
import { getCurrentUser } from '../core/utils/current-user';

export default class Login extends BaseCommand {
static description = "login with your connect API key";
Expand Down Expand Up @@ -31,7 +30,7 @@ export default class Login extends BaseCommand {

try {
cli.action.start("verifying account");
await getCurrentUser();
await this.getCurrentUser();
} catch {
await ApiKeyStore.clear();
return this.error("the given API key is not valid", {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BaseCommand from "../base-command";
import { flags } from "@oclif/command";
import { getCurrentUser } from '../core/utils/current-user';

export default class Whoami extends BaseCommand {
static description = "display the current logged in user";
Expand All @@ -19,7 +18,7 @@ export default class Whoami extends BaseCommand {
this.parse(Whoami);

try {
const appUser = await getCurrentUser();
const appUser = await this.getCurrentUser();

this.log(`you are currently logged in as: ${appUser.name}`);
} catch (error) {
Expand Down
27 changes: 0 additions & 27 deletions src/core/utils/current-user.ts

This file was deleted.

37 changes: 37 additions & 0 deletions test/specs/commands/apps.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

const ApiKeyStore = require("../../../lib/core/utils/api-key-store");
const cli = require("cli-ux").default;
const { test } = require("@oclif/test");
const { testApiKey } = require('../../utils/test-api-key')

describe("connect apps @integration", () => {
describe("when authenticated", () => {
beforeEach(async () => {
await ApiKeyStore.clear();
await ApiKeyStore.set(testApiKey);
});

test
.stdout()
.command(["apps"])
.it("runs apps");

afterEach(async () => {
await ApiKeyStore.clear();
});
});

describe("when unauthenticated", () => {
beforeEach(async () => {
await ApiKeyStore.clear();
});

test
.stub(cli, "prompt", () => async () => "invalid")
.stdout()
.command(["apps"])
.exit(1)
.it("exits with status 1 when the user is not logged in");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const double = {
run: (namespace, options, callback) => {
callback();
},
register: () => {},
register: () => { },
};

describe("the new command", () => {
describe("connect init", () => {
beforeEach(() => {
sinon.stub(yeomanEnv, "createEnv").returns(double);
});
Expand Down
23 changes: 4 additions & 19 deletions test/specs/commands/login.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
"use strict";

const { expect, test } = require("@oclif/test");
const ApiKeyStore = require("../../../lib/core/utils/api-key-store");
const cli = require("cli-ux").default;
const { expect, test } = require("@oclif/test");
const { testApiKey } = require('../../utils/test-api-key')
const ApiKeyStore = require("../../../lib/core/utils/api-key-store");

describe("The auth:login command @integration", () => {
describe("connect login @integration", () => {
describe("when a valid Apps API Key is entered", () => {
beforeEach(async () => {
await ApiKeyStore.clear();
});

test
// .nock("https://dip-webapi-dev.kubedev.sslocal.com", (api) =>
// api
// .get("/api/diagnostics/whoami")
// .reply(200, { name: "test", email: "test@test.user.com" }),
// )
.stub(cli, "prompt", () => async () => testApiKey)
.stdout()
.command(["login"])
Expand All @@ -29,12 +24,7 @@ describe("The auth:login command @integration", () => {

describe("when an invalid Apps API Key is entered", () => {
test
// .nock("https://dip-webapi-dev.kubedev.sslocal.com", (api) =>
// api
// .get("/api/diagnostics/whoami")
// .reply(401, { name: "test", email: "test@test.user.com" }),
// )
.stub(cli, "prompt", () => async () => "app_123456")
.stub(cli, "prompt", () => async () => "invalid")
.command(["login"])
.exit(1)
.it("exits with a status code of 1");
Expand All @@ -47,11 +37,6 @@ describe("The auth:login command @integration", () => {
});

test
// .nock("https://dip-webapi-dev.kubedev.sslocal.com", (api) =>
// api
// .get("/api/diagnostics/whoami")
// .reply(200, { name: "test", email: "test@test.user.com" }),
// )
.stub(cli, "prompt", () => async () => testApiKey)
.stdout()
.command(["login"])
Expand Down
4 changes: 2 additions & 2 deletions test/specs/commands/logout.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

const { expect, test } = require("@oclif/test");
const ApiKeyStore = require("../../../lib/core/utils/api-key-store");
const { expect, test } = require("@oclif/test");

describe("The auth:logout command", () => {
describe("connect logout", () => {
describe("when authenticated", () => {
before(async () => {
await ApiKeyStore.set("test");
Expand Down
9 changes: 2 additions & 7 deletions test/specs/commands/whoami.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
"use strict";

const ApiKeyStore = require("../../../lib/core/utils/api-key-store");
const { expect, test } = require("@oclif/test");
const { testApiKey } = require('../../utils/test-api-key')
const ApiKeyStore = require("../../../lib/core/utils/api-key-store");

describe("The auth:whoami command @integration", () => {
describe("connect whoami @integration", () => {
describe("when authenticated", () => {
beforeEach(async () => {
await ApiKeyStore.clear();
await ApiKeyStore.set(testApiKey);
});

test
// .nock("https://dip-webapi-dev.kubedev.sslocal.com", (api) =>
// api
// .get("/api/diagnostics/whoami")
// .reply(200, { name: "test", email: "test@test.user.com" }),
// )
.stdout()
.command(["whoami"])
.it("runs whoami", (ctx) => {
Expand Down
Loading

0 comments on commit b70bc4c

Please sign in to comment.