-
Notifications
You must be signed in to change notification settings - Fork 0
ADD: tests to google oauth #22
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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5e12230
Add GoogleAuth
Tsangington c1ed984
ADD: Google Login Without Catalog For Testing Purposes
Tsangington 49672d0
feat: Add Custom Domain Check for code.berlin emails
Tsangington b2dd9af
Merge branch 'main' into feature/oauth
BMG93 5e77477
Chore: Add details to readme about google client env variables
Tsangington b6055dd
Removed Github SignIn Provider In Frontend
Tsangington 43ef4d6
DEL: Removed Github SignIn Provider In Frontend
Tsangington b20a498
Merge branch 'feature/oauth' of github.com:codeuniversity/code-idp in…
Tsangington b652da1
UPD: Refactored google oauth to make the function testable
alkaline-0 1436240
ADD: added unit tests for index.test.ts to verify the logic provided …
alkaline-0 7fb552a
Merge branch 'main' into feature/test-google-auth
alkaline-0 d59e2b2
ADD: install jest canvas mock
alkaline-0 3e49800
fixed canvas issue
alkaline-0 ec076f5
UPD: removed env variable email domain as it was breaking the workflow
alkaline-0 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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -1,8 +1,69 @@ | ||
| import { PluginEnvironment } from './types'; | ||
| import { resolverResult } from './plugins/plugins_helper/googleAuthResolver'; | ||
| import { TokenParams } from '@backstage/plugin-auth-backend'; | ||
| import 'jest-canvas-mock'; | ||
|
|
||
| let mockProfile: any; | ||
| let mockSignInInfo: any; | ||
| let mockContext: any; | ||
|
|
||
| describe('test', () => { | ||
| it('unbreaks the test runner', () => { | ||
| const unbreaker = {} as PluginEnvironment; | ||
| expect(unbreaker).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('providers.google.create.signIn.resolver logic', () => { | ||
| beforeEach(() => { | ||
| mockProfile = { | ||
| displayName: 'John Doe', | ||
| picture: 'https://example.com/avatar.jpg', | ||
| }; | ||
| mockSignInInfo = { profile: mockProfile, result: {} }; | ||
| mockContext = { | ||
| issueToken: (params: TokenParams) => { | ||
| // Mock implementation for issueToken method | ||
| return { token: params.claims.sub + params.claims.ent }; | ||
| }, | ||
| findCatalogUser: jest.fn(), | ||
| signInWithCatalogUser: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| it('should throw an exception for empty email address', async () => { | ||
| mockProfile.email = ''; | ||
| mockSignInInfo.profile = mockProfile; | ||
|
|
||
| await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( | ||
| 'Login failed, user profile does not contain a valid email', | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw an exception for invalid non empty email address', async () => { | ||
| mockProfile.email = 'test.example.com'; | ||
| mockSignInInfo.profile = mockProfile; | ||
|
|
||
| await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( | ||
| 'Login failed, user profile does not contain a valid email', | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw an exception for valid email address with incorrect domain', async () => { | ||
| mockProfile.email = 'user@example.com'; | ||
| mockSignInInfo.profile = mockProfile; | ||
|
|
||
| await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( | ||
| `Login failed due to incorrect email domain.`, | ||
| ); | ||
| }); | ||
|
|
||
| it('should return a token for valid email address with correct domain', async () => { | ||
| mockProfile.email = 'john_doe@code.berlin'; | ||
| mockSignInInfo.profile = mockProfile; | ||
|
|
||
| return expect(resolverResult(mockSignInInfo, mockContext)).resolves.toEqual( | ||
| { token: 'user:default/john_doeuser:default/john_doe' }, | ||
| ); | ||
| }); | ||
| }); | ||
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
41 changes: 41 additions & 0 deletions
41
packages/backend/src/plugins/plugins_helper/googleAuthResolver.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,41 @@ | ||
| import { | ||
| stringifyEntityRef, | ||
| DEFAULT_NAMESPACE, | ||
| } from '@backstage/catalog-model'; | ||
| import { OAuthResult } from '@backstage/plugin-auth-backend'; | ||
| import { SignInInfo, AuthResolverContext } from '@backstage/plugin-auth-node'; | ||
|
|
||
| export const resolverResult = async ( | ||
| profile_input: SignInInfo<OAuthResult>, | ||
| ctx: AuthResolverContext, | ||
| ) => { | ||
| const profile = profile_input.profile; | ||
| const regexp = new RegExp( | ||
| /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | ||
| ); | ||
|
|
||
| if (!profile.email || !regexp.test(profile.email)) { | ||
| throw new Error( | ||
| 'Login failed, user profile does not contain a valid email', | ||
| ); | ||
| } | ||
|
|
||
| const [localPart, domain] = profile.email.split('@'); | ||
|
|
||
| if (domain !== 'code.berlin') { | ||
alkaline-0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new Error(`Login failed due to incorrect email domain.`); | ||
| } | ||
|
|
||
| const userEntityRef = stringifyEntityRef({ | ||
| kind: 'User', | ||
| name: localPart, | ||
| namespace: DEFAULT_NAMESPACE, | ||
| }); | ||
|
|
||
| return ctx.issueToken({ | ||
| claims: { | ||
| sub: userEntityRef, | ||
| ent: [userEntityRef], | ||
| }, | ||
| }); | ||
| }; | ||
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.