Skip to content

Commit

Permalink
fixes #18; fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
gdereese committed Feb 22, 2018
1 parent 996dbc1 commit 6c4aec6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/authorize-api-key-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as localStorageKeys from './local-storage-keys';
export class AuthorizeApiKeyAction {
constructor(private command) {}

public run(args, name): Promise<any> {
public run(args, schemeKey: string): Promise<any> {
return new Promise((resolve, reject) => {
this.command.log();

Expand All @@ -18,7 +18,7 @@ export class AuthorizeApiKeyAction {
auth = {};
}

auth[name] = args.value;
auth[schemeKey] = args.value;

this.command.parent.localStorage.setItem(
localStorageKeys.AUTH,
Expand Down
35 changes: 20 additions & 15 deletions src/authorize-api-key-command-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ import { Options } from './options';
import { IVorpalBuilder } from './vorpal-builder';

export class AuthorizeApiKeyCommandBuilder implements IVorpalBuilder {
public build(vorpal: any, options: Options) {
const scheme = _.find(options.spec.securityDefinitions, { type: 'apiKey' });
if (!scheme) {
return;
}
public build(vorpal: any, options: Options): any[] {
const commands = [];

for (const schemeKey of _.keys(options.spec.securityDefinitions)) {
const scheme = options.spec.securityDefinitions[schemeKey];
if (scheme.type !== 'apiKey') {
continue;
}

const command = vorpal
.command(
'authorize ' + _.kebabCase(scheme.name) + ' <value>',
'Authorize requests using an API key'
)
.action(args => {
const action = new AuthorizeApiKeyAction(vorpal.activeCommand);
return action.run(args, scheme.name);
});
const command = vorpal
.command(
'authorize ' + _.kebabCase(schemeKey) + ' <value>',
'Authorize requests using an API key'
)
.action(args => {
const action = new AuthorizeApiKeyAction(vorpal.activeCommand);
return action.run(args, schemeKey);
});
commands.push(command);
}

return command;
return commands;
}
}
4 changes: 2 additions & 2 deletions src/authorize-basic-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as localStorageKeys from './local-storage-keys';
export class AuthorizeBasicAction {
constructor(private command) {}

public run(args, name): Promise<any> {
public run(args, schemeKey: string): Promise<any> {
return new Promise((resolve, reject) => {
this.command.log();

Expand All @@ -18,7 +18,7 @@ export class AuthorizeBasicAction {
auth = {};
}

auth[name] = args.value;
auth[schemeKey] = args.value;

this.command.parent.localStorage.setItem(
localStorageKeys.AUTH,
Expand Down
35 changes: 20 additions & 15 deletions src/authorize-basic-command-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ import { Options } from './options';
import { IVorpalBuilder } from './vorpal-builder';

export class AuthorizeBasicCommandBuilder implements IVorpalBuilder {
public build(vorpal: any, options: Options) {
const scheme = _.find(options.spec.securityDefinitions, { type: 'basic' });
if (!scheme) {
return;
}
public build(vorpal: any, options: Options): any[] {
const commands = [];

for (const schemeKey of _.keys(options.spec.securityDefinitions)) {
const scheme = options.spec.securityDefinitions[schemeKey];
if (scheme.type !== 'basic') {
continue;
}

const command = vorpal
.command(
'authorize ' + _.kebabCase(scheme.name) + ' <value>',
'Authorize requests using basic authorization'
)
.action(args => {
const action = new AuthorizeBasicAction(vorpal.activeCommand);
return action.run(args, scheme.name);
});
const command = vorpal
.command(
'authorize ' + _.kebabCase(schemeKey) + ' <value>',
'Authorize requests using basic authorization'
)
.action(args => {
const action = new AuthorizeBasicAction(vorpal.activeCommand);
return action.run(args, schemeKey);
});
commands.push(command);
}

return command;
return commands;
}
}
4 changes: 2 additions & 2 deletions test/api-execute-options-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('api-execute-options-factory', () => {
const command = {
parent: {
localStorage: {
getItem: key => '{"foo":"bar"}'
getItem: key => '{"foo_auth":"bar"}'
}
}
};
Expand All @@ -170,6 +170,6 @@ describe('api-execute-options-factory', () => {

const options = factory.create(command, commandInfo, commandArgs);

expect(options.securities.foo).toBe('bar');
expect(options.securities.foo_auth).toBe('bar');
});
});
15 changes: 9 additions & 6 deletions test/authorize-api-key-command-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import * as vorpal from 'vorpal';
import { AuthorizeApiKeyCommandBuilder } from '../src/authorize-api-key-command-builder';

describe('authorize-api-key-command-builder', () => {
it('adds command if security scheme is specified', () => {
it('adds command for each apiKey scheme', () => {
const vorpalInstance = vorpal();
const options = {
operations: {
groupBy: null
},
spec: {
securityDefinitions: {
foo: {
bar_auth: {
name: 'bar',
type: 'apiKey'
},
foo_auth: {
name: 'foo',
type: 'apiKey'
}
Expand All @@ -21,10 +25,9 @@ describe('authorize-api-key-command-builder', () => {

const builder = new AuthorizeApiKeyCommandBuilder();

const command = builder.build(vorpalInstance, options);
const commands = builder.build(vorpalInstance, options);

expect(command._name).toBe(
'authorize ' + options.spec.securityDefinitions.foo.name
);
expect(commands[0]._name).toBe('authorize bar-auth');
expect(commands[1]._name).toBe('authorize foo-auth');
});
});
13 changes: 8 additions & 5 deletions test/authorize-basic-command-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ describe('authorize-basic-command-builder', () => {
},
spec: {
securityDefinitions: {
foo: {
bar_auth: {
name: 'bar',
type: 'basic'
},
foo_auth: {
name: 'foo',
type: 'basic'
}
Expand All @@ -21,10 +25,9 @@ describe('authorize-basic-command-builder', () => {

const builder = new AuthorizeBasicCommandBuilder();

const command = builder.build(vorpalInstance, options);
const commands = builder.build(vorpalInstance, options);

expect(command._name).toBe(
'authorize ' + options.spec.securityDefinitions.foo.name
);
expect(commands[0]._name).toBe('authorize bar-auth');
expect(commands[1]._name).toBe('authorize foo-auth');
});
});

0 comments on commit 6c4aec6

Please sign in to comment.