|
1 | 1 | import { PluginEnvironment } from './types'; |
| 2 | +import { resolverResult } from './plugins/plugins_helper/googleAuthResolver'; |
| 3 | +import { TokenParams } from '@backstage/plugin-auth-backend'; |
| 4 | +import 'jest-canvas-mock'; |
| 5 | + |
| 6 | +let mockProfile: any; |
| 7 | +let mockSignInInfo: any; |
| 8 | +let mockContext: any; |
2 | 9 |
|
3 | 10 | describe('test', () => { |
4 | 11 | it('unbreaks the test runner', () => { |
5 | 12 | const unbreaker = {} as PluginEnvironment; |
6 | 13 | expect(unbreaker).toBeTruthy(); |
7 | 14 | }); |
8 | 15 | }); |
| 16 | + |
| 17 | +describe('providers.google.create.signIn.resolver logic', () => { |
| 18 | + beforeEach(() => { |
| 19 | + mockProfile = { |
| 20 | + displayName: 'John Doe', |
| 21 | + picture: 'https://example.com/avatar.jpg', |
| 22 | + }; |
| 23 | + mockSignInInfo = { profile: mockProfile, result: {} }; |
| 24 | + mockContext = { |
| 25 | + issueToken: (params: TokenParams) => { |
| 26 | + // Mock implementation for issueToken method |
| 27 | + return { token: params.claims.sub + params.claims.ent }; |
| 28 | + }, |
| 29 | + findCatalogUser: jest.fn(), |
| 30 | + signInWithCatalogUser: jest.fn(), |
| 31 | + }; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw an exception for empty email address', async () => { |
| 35 | + mockProfile.email = ''; |
| 36 | + mockSignInInfo.profile = mockProfile; |
| 37 | + |
| 38 | + await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( |
| 39 | + 'Login failed, user profile does not contain a valid email', |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should throw an exception for invalid non empty email address', async () => { |
| 44 | + mockProfile.email = 'test.example.com'; |
| 45 | + mockSignInInfo.profile = mockProfile; |
| 46 | + |
| 47 | + await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( |
| 48 | + 'Login failed, user profile does not contain a valid email', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw an exception for valid email address with incorrect domain', async () => { |
| 53 | + mockProfile.email = '[email protected]'; |
| 54 | + mockSignInInfo.profile = mockProfile; |
| 55 | + |
| 56 | + await expect(resolverResult(mockSignInInfo, mockContext)).rejects.toThrow( |
| 57 | + `Login failed due to incorrect email domain.`, |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should return a token for valid email address with correct domain', async () => { |
| 62 | + mockProfile.email = '[email protected]'; |
| 63 | + mockSignInInfo.profile = mockProfile; |
| 64 | + |
| 65 | + return expect(resolverResult(mockSignInInfo, mockContext)).resolves.toEqual( |
| 66 | + { token: 'user:default/john_doeuser:default/john_doe' }, |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
0 commit comments