backfill tests for makeProfileInfo

Signed-off-by: Jamie Klassen <jamie.klassen@broadcom.com>
This commit is contained in:
Jamie Klassen
2024-01-26 14:02:55 -05:00
parent 67187d68ed
commit c4aaad016d
@@ -15,17 +15,118 @@
*/
import express from 'express';
import { UnsecuredJWT } from 'jose';
import passport from 'passport';
import { InternalOAuthError } from 'passport-oauth2';
import {
executeRedirectStrategy,
executeFrameHandlerStrategy,
executeRefreshTokenStrategy,
makeProfileInfo,
} from './PassportStrategyHelper';
import { PassportProfile } from './types';
const mockRequest = {} as unknown as express.Request;
describe('PassportStrategyHelper', () => {
describe('makeProfileInfo', () => {
it('retrieves email from passport profile', () => {
const profile: PassportProfile = {
emails: [{ value: 'email' }],
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(profile);
expect(profileInfo.email).toEqual('email');
});
it('retrieves picture from passport profile avatarUrl', () => {
const profile: PassportProfile = {
avatarUrl: 'avatarUrl',
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(profile);
expect(profileInfo.picture).toEqual('avatarUrl');
});
it('falls back to picture from passport profile photos field', () => {
const profile: PassportProfile = {
photos: [{ value: 'picture' }],
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(profile);
expect(profileInfo.picture).toEqual('picture');
});
it('falls back to email from ID token', async () => {
const profile: PassportProfile = {
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(
profile,
await new UnsecuredJWT({ email: 'email' }).encode(),
);
expect(profileInfo.email).toEqual('email');
});
it('falls back to picture from ID token', async () => {
const profile: PassportProfile = {
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(
profile,
await new UnsecuredJWT({ picture: 'picture' }).encode(),
);
expect(profileInfo.picture).toEqual('picture');
});
it('falls back to name from ID token', async () => {
const profile: PassportProfile = {
provider: '',
id: '',
displayName: '',
};
const profileInfo = makeProfileInfo(
profile,
await new UnsecuredJWT({ name: 'name' }).encode(),
);
expect(profileInfo.displayName).toEqual('name');
});
it('fails when attempting to fall back to invalid JWT', () => {
const profile: PassportProfile = {
provider: '',
id: '',
displayName: '',
};
expect(() => makeProfileInfo(profile, 'invalid JWT')).toThrow(
'Failed to parse id token and get profile info',
);
});
});
class MyCustomRedirectStrategy extends passport.Strategy {
authenticate() {
this.redirect('a', 302);