generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81274e2
commit 975adb5
Showing
2 changed files
with
131 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { describe, it, expect, beforeEach } from 'vitest'; | ||
import { parseToken, getAuthIdToken } from '../../services/AuthService'; | ||
import { formatRolesArray } from '../../utils/famUtils'; | ||
import { JWT } from '../../types/amplify'; | ||
|
||
vi.mock('../../utils/famUtils', () => ({ | ||
formatRolesArray: vi.fn(), | ||
})); | ||
|
||
describe('AuthService', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('parseToken', () => { | ||
it('should return undefined if idToken is undefined', () => { | ||
const result = parseToken(undefined); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should set authIdToken and return parsed user data', () => { | ||
const mockToken: JWT = { | ||
payload: { | ||
"cognito:groups": [ | ||
"group1", | ||
"group2" | ||
], | ||
"preferred_username": "b5ecdb094dfb4149a6a8445a01a96bf0@idir", | ||
"custom:idp_user_id": "B5ECDB094DFB4149A6A8445A01A96BF0", | ||
"custom:idp_username": "JRYAN", | ||
"custom:idp_display_name": "Ryan, Jack CIA:IN", | ||
"email": "[email protected]", | ||
"email_verified": false, | ||
"custom:idp_name": "idir", | ||
"given_name": "Jack", | ||
"name": "Jack Ryan", | ||
"family_name": "Ryan", | ||
exp: 1234567890 | ||
}, | ||
toString: () => 'mockTokenString', | ||
}; | ||
|
||
(formatRolesArray as vi.Mock).mockReturnValue([{ role: 'role1' }, { role: 'role2' }]); | ||
|
||
const result = parseToken(mockToken); | ||
|
||
expect(getAuthIdToken()).toBe('mockTokenString'); | ||
expect(result).toEqual({ | ||
userName: 'JRYAN', | ||
displayName: "Ryan, Jack CIA:IN", | ||
email: '[email protected]', | ||
idpProvider: 'IDIR', | ||
clientRoles: [{ role: 'role1' }, { role: 'role2' }], | ||
exp: 1234567890, | ||
firstName: 'Jack', | ||
lastName: 'Ryan', | ||
providerUsername: 'IDIR\\JRYAN', | ||
}); | ||
}); | ||
|
||
it('should handle displayName without comma correctly', () => { | ||
const mockToken: JWT = { | ||
payload: { | ||
"cognito:groups": [ | ||
"group1", | ||
"group2" | ||
], | ||
"preferred_username": "b5ecdb094dfb4149a6a8445a01a96bf0@idir", | ||
"custom:idp_user_id": "B5ECDB094DFB4149A6A8445A01A96BF0", | ||
"custom:idp_username": "JRYAN", | ||
"custom:idp_display_name": "Jack Ryan", | ||
"email": "[email protected]", | ||
"email_verified": false, | ||
"custom:idp_name": "idir", | ||
"given_name": "Jack", | ||
"name": "Jack Ryan", | ||
"family_name": "Ryan", | ||
exp: 1234567890 | ||
}, | ||
toString: () => 'mockTokenString', | ||
}; | ||
|
||
(formatRolesArray as vi.Mock).mockReturnValue([{ role: 'role1' }, { role: 'role2' }]); | ||
|
||
const result = parseToken(mockToken); | ||
|
||
expect(getAuthIdToken()).toBe('mockTokenString'); | ||
expect(result).toEqual({ | ||
userName: 'JRYAN', | ||
displayName: 'Jack Ryan', | ||
email: '[email protected]', | ||
idpProvider: 'IDIR', | ||
clientRoles: [{ role: 'role1' }, { role: 'role2' }], | ||
exp: 1234567890, | ||
firstName: 'Jack', | ||
lastName: 'Ryan', | ||
providerUsername: 'IDIR\\JRYAN', | ||
}); | ||
}); | ||
|
||
it('should handle missing identities correctly', () => { | ||
const mockToken: JWT = { | ||
payload: { | ||
'custom:idp_display_name': 'Doe, John', | ||
'custom:idp_username': 'johndoe', | ||
'email': '[email protected]', | ||
exp: 1234567890, | ||
}, | ||
toString: () => 'mockTokenString', | ||
}; | ||
|
||
(formatRolesArray as vi.Mock).mockReturnValue([{ role: 'role1' }, { role: 'role2' }]); | ||
|
||
const result = parseToken(mockToken); | ||
|
||
expect(getAuthIdToken()).toBe('mockTokenString'); | ||
expect(result).toEqual({ | ||
userName: 'johndoe', | ||
displayName: 'Doe, John', | ||
email: '[email protected]', | ||
idpProvider: '', | ||
clientRoles: [{ role: 'role1' }, { role: 'role2' }], | ||
exp: 1234567890, | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
providerUsername: '\\johndoe', | ||
}); | ||
}); | ||
}); | ||
}); |
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