Skip to content

Commit

Permalink
test: added test to AuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Nov 4, 2024
1 parent 81274e2 commit 975adb5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 6 deletions.
130 changes: 130 additions & 0 deletions frontend/src/__test__/services/AuthService.test.ts
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',
});
});
});
});
7 changes: 1 addition & 6 deletions frontend/src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const parseToken = (idToken: JWT | undefined): FamLoginUser | undefined =
const decodedIdToken = idToken?.payload;

const displayName = decodedIdToken?.['custom:idp_display_name'] as string || '';
const idpProvider = (decodedIdToken?.['custom:idp_name'] as string || '').toUpperCase();
const hasComma = displayName.includes(',');

let [lastName, firstName] = hasComma
Expand All @@ -62,12 +63,6 @@ export const parseToken = (idToken: JWT | undefined): FamLoginUser | undefined =
const userName = decodedIdToken?.['custom:idp_username'] as string || '';
const email = decodedIdToken?.['email'] as string || '';

let idpProvider = '';
if (decodedIdToken?.['identities']) {
const identities = decodedIdToken['identities'] as object;
idpProvider = (identities && 'providerName' in identities) ? identities?.['providerName'] as string || '' : '';
}

const rolesArray: UserClientRolesType[] = formatRolesArray(decodedIdToken);

return {
Expand Down

0 comments on commit 975adb5

Please sign in to comment.