-
Notifications
You must be signed in to change notification settings - Fork 31
test(provider): add test cases for Cognito OAuth #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,32 @@ | ||
| import {Provider} from '@loopback/context'; | ||
| import {Request} from '@loopback/rest'; | ||
| import {Cognito, IAuthUser, VerifyFunction} from '../../../types'; | ||
|
|
||
| /** | ||
| * A provider for default implementation of VerifyFunction.CognitoAuthFn | ||
| * | ||
| * It will just throw an error saying Not Implemented | ||
| */ | ||
| export class BearerTokenVerifyProvider | ||
| implements Provider<VerifyFunction.CognitoAuthFn> | ||
| { | ||
| constructor() {} | ||
|
|
||
| value(): VerifyFunction.CognitoAuthFn { | ||
| return async ( | ||
| accessToken: string, | ||
| refreshToken: string, | ||
| profile: Cognito.Profile, | ||
| cb: Cognito.VerifyCallback, | ||
| req?: Request, | ||
| ) => { | ||
| const userToPass: IAuthUser = { | ||
| id: 1, | ||
| username: 'xyz', | ||
| password: 'pass', | ||
| }; | ||
|
|
||
| return userToPass; | ||
| }; | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
...tests__/integration/action-sequence/passport-cognito-oauth2/cognito-oauth2.integration.ts
This file contains hidden or 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,76 @@ | ||
| import {Client, createClientForHandler} from '@loopback/testlab'; | ||
| import {RestServer, Request} from '@loopback/rest'; | ||
| import {Application, Provider} from '@loopback/core'; | ||
| import {get} from '@loopback/openapi-v3'; | ||
| import {authenticate} from '../../../../decorators'; | ||
| import {STRATEGY} from '../../../../strategy-name.enum'; | ||
| import {getApp} from '../helpers/helpers'; | ||
| import {MyAuthenticationSequence} from '../../../fixtures/sequences/authentication.sequence'; | ||
| import {Strategies} from '../../../../strategies/keys'; | ||
| import {VerifyFunction} from '../../../../strategies'; | ||
| import {userWithoutReqObj} from '../../../fixtures/data/bearer-data'; | ||
| import {Cognito} from '../../../../types'; | ||
|
|
||
| describe('getting cognito oauth2 strategy with options', () => { | ||
| let app: Application; | ||
| let server: RestServer; | ||
| beforeEach(givenAServer); | ||
| beforeEach(givenAuthenticatedSequence); | ||
| beforeEach(getAuthVerifier); | ||
|
|
||
| it('should return 302 when client id is passed and passReqToCallback is set true', async () => { | ||
| class TestController { | ||
| @get('/test') | ||
| @authenticate(STRATEGY.COGNITO_OAUTH2, { | ||
| clientID: 'string', | ||
| clientSecret: 'string', | ||
| passReqToCallback: true, | ||
| }) | ||
| test() { | ||
| return 'test successful'; | ||
| } | ||
| } | ||
|
|
||
| app.controller(TestController); | ||
|
|
||
| await whenIMakeRequestTo(server).get('/test').expect(302); | ||
| }); | ||
|
|
||
| function whenIMakeRequestTo(restServer: RestServer): Client { | ||
| return createClientForHandler(restServer.requestHandler); | ||
| } | ||
|
|
||
| async function givenAServer() { | ||
| app = getApp(); | ||
| server = await app.getServer(RestServer); | ||
| } | ||
|
|
||
| function getAuthVerifier() { | ||
| app | ||
| .bind(Strategies.Passport.COGNITO_OAUTH2_VERIFIER) | ||
| .toProvider(CognitoAuthVerifyProvider); | ||
| } | ||
|
|
||
| function givenAuthenticatedSequence() { | ||
| // bind user defined sequence | ||
| server.sequence(MyAuthenticationSequence); | ||
| } | ||
| }); | ||
|
|
||
| class CognitoAuthVerifyProvider | ||
| implements Provider<VerifyFunction.CognitoAuthFn> | ||
| { | ||
| constructor() {} | ||
|
|
||
| value(): VerifyFunction.CognitoAuthFn { | ||
| return async ( | ||
| accessToken: string, | ||
| refreshToken: string, | ||
| profile: Cognito.Profile, | ||
| cd: Cognito.VerifyCallback, | ||
| req?: Request, | ||
| ) => { | ||
| return userWithoutReqObj; | ||
| }; | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...s__/integration/middleware-sequence/passport-cognito-oauth2/cognito-oauth2.integration.ts
This file contains hidden or 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,76 @@ | ||
| import {Client, createClientForHandler} from '@loopback/testlab'; | ||
| import {RestServer, Request} from '@loopback/rest'; | ||
| import {Application, Provider} from '@loopback/core'; | ||
| import {get} from '@loopback/openapi-v3'; | ||
| import {authenticate} from '../../../../decorators'; | ||
| import {STRATEGY} from '../../../../strategy-name.enum'; | ||
| import {getApp} from '../helpers/helpers'; | ||
| import {Strategies} from '../../../../strategies/keys'; | ||
| import {VerifyFunction} from '../../../../strategies'; | ||
| import {userWithoutReqObj} from '../../../fixtures/data/bearer-data'; | ||
| import {Cognito} from '../../../../types'; | ||
| import {MyAuthenticationMiddlewareSequence} from '../../../fixtures/sequences/authentication-middleware.sequence'; | ||
|
|
||
| describe('getting cognito oauth2 strategy with options using Middleware Sequence', () => { | ||
| let app: Application; | ||
| let server: RestServer; | ||
| beforeEach(givenAServer); | ||
| beforeEach(givenAuthenticatedSequence); | ||
| beforeEach(getAuthVerifier); | ||
|
|
||
| it('should return 302 when client id is passed and passReqToCallback is set true', async () => { | ||
| class TestController { | ||
| @get('/test') | ||
| @authenticate(STRATEGY.COGNITO_OAUTH2, { | ||
| clientID: 'string', | ||
| clientSecret: 'string', | ||
| passReqToCallback: true, | ||
| }) | ||
| test() { | ||
| return 'test successful'; | ||
| } | ||
| } | ||
|
|
||
| app.controller(TestController); | ||
|
|
||
| await whenIMakeRequestTo(server).get('/test').expect(302); | ||
| }); | ||
|
|
||
| function whenIMakeRequestTo(restServer: RestServer): Client { | ||
| return createClientForHandler(restServer.requestHandler); | ||
| } | ||
|
|
||
| async function givenAServer() { | ||
| app = getApp(); | ||
| server = await app.getServer(RestServer); | ||
| } | ||
|
|
||
| function getAuthVerifier() { | ||
| app | ||
| .bind(Strategies.Passport.COGNITO_OAUTH2_VERIFIER) | ||
| .toProvider(CognitoAuthVerifyProvider); | ||
| } | ||
|
|
||
| function givenAuthenticatedSequence() { | ||
| // bind user defined sequence | ||
| server.sequence(MyAuthenticationMiddlewareSequence); | ||
| } | ||
| }); | ||
|
|
||
| class CognitoAuthVerifyProvider | ||
| implements Provider<VerifyFunction.CognitoAuthFn> | ||
| { | ||
| constructor() {} | ||
|
|
||
| value(): VerifyFunction.CognitoAuthFn { | ||
| return async ( | ||
| accessToken: string, | ||
| refreshToken: string, | ||
| profile: Cognito.Profile, | ||
| cd: Cognito.VerifyCallback, | ||
| req?: Request, | ||
| ) => { | ||
| return userWithoutReqObj; | ||
| }; | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
src/__tests__/unit/passport-cognito-oauth2/cognito-auth-strategy.unit.ts
This file contains hidden or 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,74 @@ | ||
| import {Cognito, IAuthUser} from '../../../types'; | ||
| import {expect} from '@loopback/testlab'; | ||
| import { | ||
| CognitoStrategyFactoryProvider, | ||
| CognitoAuthStrategyFactory, | ||
| } from '../../../strategies/passport/passport-cognito-oauth2'; | ||
|
|
||
| describe('getting cognito-auth strategy with options', () => { | ||
| it('should return strategy by passing options and passReqToCallback as true', async () => { | ||
| const strategyVerifier: CognitoAuthStrategyFactory = await getStrategy(); | ||
|
|
||
| const options: Cognito.StrategyOptions = { | ||
| callbackURL: 'string', | ||
| clientDomain: 'string', | ||
| clientID: 'string', | ||
| clientSecret: 'string', | ||
| region: 'string', | ||
| passReqToCallback: true, | ||
| }; | ||
|
|
||
| const cognitoAuthStrategyVerifier = strategyVerifier(options); | ||
|
|
||
| expect(cognitoAuthStrategyVerifier).to.have.property('name'); | ||
| expect(cognitoAuthStrategyVerifier) | ||
| .to.have.property('authenticate') | ||
| .which.is.a.Function(); | ||
| }); | ||
|
|
||
| it('should return strategy by passing options and passReqToCallback as false', async () => { | ||
| const strategyVerifier: CognitoAuthStrategyFactory = await getStrategy(); | ||
|
|
||
| const options: Cognito.StrategyOptions = { | ||
| callbackURL: 'string', | ||
| clientDomain: 'string', | ||
| clientID: 'string', | ||
| clientSecret: 'string', | ||
| region: 'string', | ||
| passReqToCallback: false, | ||
| }; | ||
|
|
||
| const cognitoAuthStrategyVerifier = strategyVerifier(options); | ||
|
|
||
| expect(cognitoAuthStrategyVerifier).to.have.property('name'); | ||
| expect(cognitoAuthStrategyVerifier) | ||
| .to.have.property('authenticate') | ||
| .which.is.a.Function(); | ||
| }); | ||
| }); | ||
|
|
||
| async function getStrategy() { | ||
| const provider = new CognitoStrategyFactoryProvider(verifierBearer); | ||
|
|
||
| //this fuction will return a function which will then accept options. | ||
| return provider.value(); | ||
| } | ||
|
|
||
| //returning a user | ||
| function verifierBearer( | ||
| accessToken: string, | ||
| refreshToken: string, | ||
| profile: Cognito.Profile, | ||
| ): Promise<IAuthUser | null> { | ||
| const userToPass: IAuthUser = { | ||
| id: 1, | ||
| username: 'xyz', | ||
| password: 'pass', | ||
| }; | ||
|
|
||
| return new Promise(function (resolve, reject) { | ||
| if (userToPass) { | ||
| resolve(userToPass); | ||
| } | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't look like it will do this 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mock-verify-provider naa. It will just return the user (like other verify-providers).
This comment is from the actual verify provider, forgot to remove it. 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok 🙁