Skip to content

Commit

Permalink
refactor: consolidate auth checks (#562)
Browse files Browse the repository at this point in the history
* refactor: consolidate auth checks

* test: use stricter checks for this API key error
  • Loading branch information
kanadgupta authored Aug 9, 2022
1 parent 7db2e59 commit fba45ad
Show file tree
Hide file tree
Showing 22 changed files with 39 additions and 80 deletions.
4 changes: 3 additions & 1 deletion __tests__/cmds/changelogs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('rdme changelogs', () => {
afterAll(() => nock.cleanAll());

it('should error if no api key provided', () => {
return expect(changelogs.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(changelogs.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
});

it('should error if no folder provided', () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/cmds/changelogs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('rdme changelogs:single', () => {
afterAll(() => nock.cleanAll());

it('should error if no api key provided', () => {
return expect(changelogsSingle.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(changelogsSingle.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
});

it('should error if no file path provided', () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const category = 'CATEGORY_ID';

describe('rdme docs:edit', () => {
it('should error if no api key provided', () => {
return expect(docsEdit.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(docsEdit.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
});

it('should error if no slug provided', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('rdme docs', () => {
afterAll(() => nock.cleanAll());

it('should error if no api key provided', () => {
return expect(docs.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
});

it('should error if no folder provided', () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/cmds/docs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe('rdme docs:single', () => {
afterAll(() => nock.cleanAll());

it('should error if no api key provided', () => {
return expect(docsSingle.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(docsSingle.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
});

it('should error if no file path provided', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/cmds/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ describe('rdme openapi', () => {
it('should error if no api key provided', () => {
return expect(
openapi.run({ spec: require.resolve('@readme/oas-examples/3.0/json/petstore.json') })
).rejects.toThrow('No project API key provided. Please use `--key`.');
).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
});

it('should error if invalid API key is sent and version list does not load', async () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/cmds/versions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe('rdme versions:create', () => {
afterEach(() => nock.cleanAll());

it('should error if no api key provided', () => {
return expect(createVersion.run({})).rejects.toThrow('No project API key provided. Please use `--key`.');
return expect(createVersion.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
});

it('should create a specific version', async () => {
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/categories/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ export default class CategoriesCreateCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { categoryType, title, key, version, preventDuplicates } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!title) {
return Promise.reject(new Error(`No title provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ export default class CategoriesCommand extends Command {
}

async run(opts: CommandOptions<{}>) {
super.run(opts);
super.run(opts, true);

const { key, version } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

const selectedVersion = await getProjectVersion(version, key, true);

debug(`selectedVersion: ${selectedVersion}`);
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/changelogs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ export default class ChangelogsCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { dryRun, folder, key } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!folder) {
return Promise.reject(new Error(`No folder provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/changelogs/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ export default class SingleChangelogCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { dryRun, filePath, key } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!filePath) {
return Promise.reject(new Error(`No file path provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
9 changes: 2 additions & 7 deletions src/cmds/custompages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ export default class CustomPagesCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
const { dryRun, folder, key } = opts;

debug(`command: ${this.command}`);
debug(`opts: ${JSON.stringify(opts)}`);
super.run(opts, true);

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}
const { dryRun, folder, key } = opts;

if (!folder) {
return Promise.reject(new Error(`No folder provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/custompages/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@ export default class SingleCustomPageCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { dryRun, filePath, key } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!filePath) {
return Promise.reject(new Error(`No file path provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/docs/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ export default class EditDocsCommand extends Command {
}

async run(opts: CommandOptions<Options>): Promise<undefined> {
super.run(opts);
super.run(opts, true);

const { slug, key, version } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!slug) {
return Promise.reject(new Error(`No slug provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ export default class DocsCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { dryRun, folder, key, version } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!folder) {
return Promise.reject(new Error(`No folder provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/docs/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ export default class SingleDocCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { dryRun, filePath, key, version } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!filePath) {
return Promise.reject(new Error(`No file path provided. Usage \`${config.get('cli')} ${this.usage}\`.`));
}
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,10 @@ export default class OpenAPICommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { key, id, spec, version, workingDirectory } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

let selectedVersion: string;
let isUpdate: boolean;
const spinner = ora({ ...oraOptions() });
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/versions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@ export default class CreateVersionCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

let versionList;
const { key, version, codename, fork, main, beta, isPublic } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

if (!version || !semver.valid(semver.coerce(version))) {
return Promise.reject(
new Error(`Please specify a semantic version. See \`${config.get('cli')} help ${this.command}\` for help.`)
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/versions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ export default class DeleteVersionCommand extends Command {
}

async run(opts: CommandOptions<{}>) {
super.run(opts);
super.run(opts, true);

const { key, version } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

const selectedVersion = await getProjectVersion(version, key, false).catch(e => {
return Promise.reject(e);
});
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,10 @@ export default class VersionsCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { key, version, raw } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

const uri = version ? `${config.get('host')}/api/v1/version/${version}` : `${config.get('host')}/api/v1/version`;

return fetch(uri, {
Expand Down
6 changes: 1 addition & 5 deletions src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,10 @@ export default class UpdateVersionCommand extends Command {
}

async run(opts: CommandOptions<Options>) {
super.run(opts);
super.run(opts, true);

const { key, version, codename, newVersion, main, beta, isPublic, deprecated } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
}

const selectedVersion = await getProjectVersion(version, key, false).catch(e => {
return Promise.reject(e);
});
Expand Down
8 changes: 7 additions & 1 deletion src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ export default class Command {

args: OptionDefinition[];

run(opts: CommandOptions<{}>): void | Promise<string> {
run(opts: CommandOptions<{}>, requiresAuth?: boolean): void | Promise<string> {
debug(`command: ${this.command}`);
debug(`opts: ${JSON.stringify(opts)}`);

if (requiresAuth) {
if (!opts.key) {
throw new Error('No project API key provided. Please use `--key`.');
}
}
}
}

0 comments on commit fba45ad

Please sign in to comment.