-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add auth:login command (#8)
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |