Skip to content

Commit

Permalink
feat(cli): add auth:login command (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Jan 28, 2021
1 parent 59bc8c1 commit fc29937
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
"bin": "coveo",
"plugins": [
"@oclif/plugin-help"
]
],
"topics": {
"auth": {
"description": "Manage authentification against the Coveo platform"
}
}
},
"repository": "coveo/cli",
"scripts": {
Expand Down
69 changes: 69 additions & 0 deletions packages/cli/src/commands/auth/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {test} from '@oclif/test';
import {mocked} from 'ts-jest/utils';
import {OAuth} from '../../lib/oauth/oauth';
jest.mock('../../lib/oauth/oauth');
const mockedOAuth = mocked(OAuth, true);

describe('auth:login', () => {
test
.command(['auth:login', '-e', 'foo'])
.catch(/Expected --environment=foo/)
.it('reject invalid environment');

test
.command(['auth:login', '-r', 'foo'])
.catch(/Expected --region=foo/)
.it('reject invalid region');

['dev', 'qa', 'prod', 'hipaa'].forEach((environment) => {
test
.stdout()
.command(['auth:login', '-e', environment])
.it(
`passes the -e=${environment} flag to oauth as an environment`,
() => {
expect(mockedOAuth.mock.calls[0][0]?.environment).toBe(environment);
}
);
});

[
'us-east-1',
'eu-west-1',
'eu-west-3',
'ap-southeast-2',
'us-west-2',
].forEach((region) => {
test
.stdout()
.command(['auth:login', '-r', region])
.it(`passes the -e=${region} flag to oauth as an region`, () => {
expect(mockedOAuth.mock.calls[0][0]?.region).toBe(region);
});
});

test
.stdout()
.command(['auth:login', '-e', 'qa'])
.it('passed the -e=dev flag to oauth as an environment', () => {
expect(mockedOAuth.mock.calls[0][0]?.environment).toBe('qa');
});

describe('retrieves token from oauth service', () => {
beforeEach(() => {
mockedOAuth.mockImplementationOnce(
() =>
({
getToken: () => Promise.resolve('this-is-the-token'),
} as OAuth)
);
});

test
.stdout()
.command(['auth:login'])
.it('retrieves token from oauth service', (ctx) => {
expect(ctx.stdout).toContain('this-is-the-token');
});
});
});
43 changes: 43 additions & 0 deletions packages/cli/src/commands/auth/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Command, flags} from '@oclif/command';
import {OAuth} from '../../lib/oauth/oauth';
import {
PlatformEnvironment,
PlatformRegion,
} from '../../lib/platform/environment';

export default class Login extends Command {
static description = 'Log into Coveo platform using OAuth2 flow';

static examples = ['$ coveo auth:login'];

static flags = {
region: flags.string({
char: 'r',
options: [
'us-east-1',
'eu-west-1',
'eu-west-3',
'ap-southeast-2',
'us-west-2',
],
default: 'us-east-1',
description:
'The platform region inside which to login. See https://docs.coveo.com/en/2976',
}),
environment: flags.string({
char: 'e',
options: ['dev', 'qa', 'prod', 'hipaa'],
default: 'prod',
description: 'The platform environment inside which to login.',
}),
};

async run() {
const {flags} = this.parse(Login);
const tok = await new OAuth({
environment: flags.environment as PlatformEnvironment,
region: flags.region as PlatformRegion,
}).getToken();
console.log('GOT TOKEN', tok);
}
}

0 comments on commit fc29937

Please sign in to comment.