-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: recover tests for auth action #2859
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
208 changes: 0 additions & 208 deletions
208
packages/authentication/src/__tests__/acceptance/basic-auth.acceptance.ts
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
packages/authentication/src/__tests__/unit/fixtures/mock-strategy-passport.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,67 @@ | ||
| // Copyright IBM Corp. 2019. All Rights Reserved. | ||
| // Node module: @loopback/authentication | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Request} from 'express'; | ||
| import {AuthenticateOptions, Strategy} from 'passport'; | ||
| import {UserProfile} from '../../../types'; | ||
|
|
||
| /** | ||
| * Test fixture for a mock asynchronous passport-strategy | ||
| */ | ||
| export class MockPassportStrategy extends Strategy { | ||
| // user to return for successful authentication | ||
| private mockUser: UserProfile; | ||
|
|
||
| setMockUser(userObj: UserProfile) { | ||
| this.mockUser = userObj; | ||
| } | ||
|
|
||
| /** | ||
| * authenticate() function similar to passport-strategy packages | ||
| * @param req | ||
| */ | ||
| async authenticate(req: Request, options?: AuthenticateOptions) { | ||
| await this.verify(req); | ||
| } | ||
| /** | ||
| * @param req | ||
| * mock verification function; usually passed in as constructor argument for | ||
| * passport-strategy | ||
| * | ||
| * For the purpose of mock tests we have this here | ||
| * pass req.query.testState = 'fail' to mock failed authorization | ||
| * pass req.query.testState = 'error' to mock unexpected error | ||
| */ | ||
| async verify(request: Request) { | ||
| if ( | ||
| request.headers && | ||
| request.headers.testState && | ||
| request.headers.testState === 'fail' | ||
| ) { | ||
| this.returnUnauthorized('authorization failed'); | ||
| return; | ||
| } else if ( | ||
| request.headers && | ||
| request.headers.testState && | ||
| request.headers.testState === 'error' | ||
| ) { | ||
| this.returnError('unexpected error'); | ||
| return; | ||
| } | ||
| process.nextTick(this.returnMockUser.bind(this)); | ||
| } | ||
|
|
||
| returnMockUser() { | ||
| this.success(this.mockUser); | ||
| } | ||
|
|
||
| returnUnauthorized(challenge?: string | number, status?: number) { | ||
| this.fail(challenge, status); | ||
| } | ||
|
|
||
| returnError(err: string) { | ||
| this.error(err); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.