11import { renderHook } from '@testing-library/react-native' ;
2- import axios from 'axios' ;
32import * as AuthSession from 'expo-auth-session' ;
4- import * as WebBrowser from 'expo-web-browser' ;
53
64import { useOidcLogin } from '../use-oidc-login' ;
75
86jest . mock ( 'expo-auth-session' ) ;
97jest . mock ( 'expo-web-browser' ) ;
10- jest . mock ( 'axios' ) ;
11- jest . mock ( '@/lib/storage/app' , ( ) => ( {
12- getBaseApiUrl : jest . fn ( ( ) => 'https://api.resgrid.com/api/v4' ) ,
13- } ) ) ;
148jest . mock ( '@/lib/logging' , ( ) => ( {
159 logger : { info : jest . fn ( ) , warn : jest . fn ( ) , error : jest . fn ( ) } ,
1610} ) ) ;
1711
1812const mockedAuthSession = AuthSession as jest . Mocked < typeof AuthSession > ;
19- const mockedAxios = axios as jest . Mocked < typeof axios > ;
2013
2114describe ( 'useOidcLogin' , ( ) => {
2215 const mockPromptAsync = jest . fn ( ) ;
@@ -61,7 +54,7 @@ describe('useOidcLogin', () => {
6154 useOidcLogin ( { authority : 'https://idp.example.com' , clientId : 'client123' } ) ,
6255 ) ;
6356
64- const tokenResult = await result . current . exchangeForResgridToken ( 'john.doe' ) ;
57+ const tokenResult = await result . current . exchangeForResgridToken ( ) ;
6558 expect ( tokenResult ) . toBeNull ( ) ;
6659 } ) ;
6760
@@ -81,11 +74,11 @@ describe('useOidcLogin', () => {
8174 useOidcLogin ( { authority : 'https://idp.example.com' , clientId : 'client123' } ) ,
8275 ) ;
8376
84- const tokenResult = await result . current . exchangeForResgridToken ( 'john.doe' ) ;
77+ const tokenResult = await result . current . exchangeForResgridToken ( ) ;
8578 expect ( tokenResult ) . toBeNull ( ) ;
8679 } ) ;
8780
88- it ( 'exchanges id_token for Resgrid token on success' , async ( ) => {
81+ it ( 'returns the IdP id_token string on success' , async ( ) => {
8982 ( mockedAuthSession . useAuthRequest as jest . Mock ) . mockReturnValue ( [
9083 { codeVerifier : 'verifier123' } ,
9184 { type : 'success' , params : { code : 'auth-code-123' } } ,
@@ -97,53 +90,29 @@ describe('useOidcLogin', () => {
9790 accessToken : 'oidc-access' ,
9891 } ) ;
9992
100- mockedAxios . post = jest . fn ( ) . mockResolvedValueOnce ( {
101- data : {
102- access_token : 'rg-access' ,
103- refresh_token : 'rg-refresh' ,
104- expires_in : 3600 ,
105- token_type : 'Bearer' ,
106- } ,
107- } ) ;
108-
10993 const { result } = renderHook ( ( ) =>
11094 useOidcLogin ( { authority : 'https://idp.example.com' , clientId : 'client123' } ) ,
11195 ) ;
11296
113- const tokenResult = await result . current . exchangeForResgridToken ( 'john.doe' ) ;
114-
115- expect ( tokenResult ) . toEqual ( {
116- access_token : 'rg-access' ,
117- refresh_token : 'rg-refresh' ,
118- expires_in : 3600 ,
119- token_type : 'Bearer' ,
120- } ) ;
97+ const tokenResult = await result . current . exchangeForResgridToken ( ) ;
12198
122- expect ( mockedAxios . post ) . toHaveBeenCalledWith (
123- 'https://api.resgrid.com/api/v4/connect/external-token' ,
124- expect . stringContaining ( 'external_token=oidc-id-token' ) ,
125- expect . objectContaining ( { headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } } ) ,
126- ) ;
99+ expect ( tokenResult ) . toBe ( 'oidc-id-token' ) ;
127100 } ) ;
128101
129- it ( 'returns null when Resgrid API call fails' , async ( ) => {
102+ it ( 'returns null when IdP code exchange fails' , async ( ) => {
130103 ( mockedAuthSession . useAuthRequest as jest . Mock ) . mockReturnValue ( [
131104 { codeVerifier : 'verifier123' } ,
132105 { type : 'success' , params : { code : 'auth-code-123' } } ,
133106 mockPromptAsync ,
134107 ] ) ;
135108
136- ( mockedAuthSession . exchangeCodeAsync as jest . Mock ) . mockResolvedValueOnce ( {
137- idToken : 'oidc-id-token' ,
138- } ) ;
139-
140- mockedAxios . post = jest . fn ( ) . mockRejectedValueOnce ( new Error ( 'API Error' ) ) ;
109+ ( mockedAuthSession . exchangeCodeAsync as jest . Mock ) . mockRejectedValueOnce ( new Error ( 'IdP Error' ) ) ;
141110
142111 const { result } = renderHook ( ( ) =>
143112 useOidcLogin ( { authority : 'https://idp.example.com' , clientId : 'client123' } ) ,
144113 ) ;
145114
146- const tokenResult = await result . current . exchangeForResgridToken ( 'john.doe' ) ;
115+ const tokenResult = await result . current . exchangeForResgridToken ( ) ;
147116 expect ( tokenResult ) . toBeNull ( ) ;
148117 } ) ;
149118} ) ;
0 commit comments