From c4aaad016da9386eddd4fadd20091edfe5015d01 Mon Sep 17 00:00:00 2001 From: Jamie Klassen Date: Fri, 26 Jan 2024 14:02:55 -0500 Subject: [PATCH] backfill tests for makeProfileInfo Signed-off-by: Jamie Klassen --- .../passport/PassportStrategyHelper.test.ts | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts index dd0c1579a9..4d94634473 100644 --- a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts +++ b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts @@ -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);