backfill tests for makeProfileInfo
Signed-off-by: Jamie Klassen <jamie.klassen@broadcom.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user