-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hook support for authenticating using Password Realm grant
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,8 @@ const mockAuth0 = { | |
loginWithOTP: jest.fn().mockResolvedValue(mockCredentials), | ||
loginWithRecoveryCode: jest.fn().mockResolvedValue(mockCredentials), | ||
hasValidCredentials: jest.fn().mockResolvedValue(), | ||
loginWithRecoveryCode: jest.fn().mockResolvedValue(mockCredentials), | ||
passwordRealm: jest.fn().mockResolvedValue(mockCredentials), | ||
}, | ||
credentialsManager: { | ||
getCredentials: jest.fn().mockResolvedValue(mockCredentials), | ||
|
@@ -781,6 +783,64 @@ describe('The useAuth0 hook', () => { | |
expect(result.current.error).toBe(mockAuthError); | ||
}); | ||
|
||
it('can authorize with password realm, passing through all parameters', async () => { | ||
const { result } = renderHook(() => useAuth0(), { | ||
wrapper, | ||
}); | ||
|
||
let promise = result.current.authorizeWithPasswordRealm({ | ||
username: '[email protected]', | ||
password: 'random-password', | ||
realm: 'react-native-sample-app', | ||
}); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toBe(false)); | ||
|
||
expect(mockAuth0.auth.passwordRealm).toHaveBeenCalledWith({ | ||
username: '[email protected]', | ||
password: 'random-password', | ||
realm: 'react-native-sample-app', | ||
}); | ||
|
||
let credentials; | ||
await act(async () => { | ||
credentials = await promise; | ||
}); | ||
expect(credentials).toEqual({ | ||
idToken: | ||
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiaHR0cHM6Ly9hdXRoMC5jb20iLCJhdWQiOiJjbGllbnQxMjMiLCJuYW1lIjoiVGVzdCBVc2VyIiwiZmFtaWx5X25hbWUiOiJVc2VyIiwicGljdHVyZSI6Imh0dHBzOi8vaW1hZ2VzL3BpYy5wbmcifQ==.c2lnbmF0dXJl', | ||
accessToken: 'ACCESS TOKEN', | ||
}); | ||
}); | ||
|
||
it('sets the user prop after successful authentication', async () => { | ||
const { result } = renderHook(() => useAuth0(), { | ||
wrapper, | ||
}); | ||
|
||
result.current.authorizeWithPasswordRealm(); | ||
await waitFor(() => expect(result.current.isLoading).toBe(false)); | ||
|
||
expect(result.current.user).toMatchObject({ | ||
name: 'Test User', | ||
familyName: 'User', | ||
picture: 'https://images/pic.png', | ||
}); | ||
}); | ||
|
||
it('does not set user prop when authentication fails', async () => { | ||
mockAuth0.auth.passwordRealm.mockRejectedValue(mockAuthError); | ||
const { result } = renderHook(() => useAuth0(), { | ||
wrapper, | ||
}); | ||
|
||
result.current.authorizeWithPasswordRealm(); | ||
await waitFor(() => expect(result.current.isLoading).toBe(false)); | ||
|
||
expect(result.current.user).toBeNull(); | ||
expect(result.current.error).toBe(mockAuthError); | ||
}); | ||
|
||
it('can clear the session', async () => { | ||
const { result } = renderHook(() => useAuth0(), { | ||
wrapper, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters