Skip to content

Commit

Permalink
test: updated unit tests according to latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
desusai7 committed Aug 2, 2024
1 parent 81e6e89 commit fb8b5f7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 96 deletions.
20 changes: 0 additions & 20 deletions src/credentials-manager/__tests__/credentials-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,4 @@ describe('credentials manager tests', () => {
newNativeModule.mockRestore();
});
});

describe('test enabling local authentication', () => {
it('enable local authentication for iOS', async () => {
Platform.OS = 'ios';
const newNativeModule = jest
.spyOn(credentialsManager.Auth0Module, 'enableLocalAuthentication')
.mockImplementation(() => {});
await expect(credentialsManager.requireLocalAuthentication()).resolves;
newNativeModule.mockRestore();
});

it('enable local authentication for Android', async () => {
Platform.OS = 'android';
const newNativeModule = jest
.spyOn(credentialsManager.Auth0Module, 'enableLocalAuthentication')
.mockImplementation(() => {});
await expect(credentialsManager.requireLocalAuthentication()).resolves;
newNativeModule.mockRestore();
});
});
});
56 changes: 0 additions & 56 deletions src/hooks/__tests__/use-auth0.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const mockAuth0 = {
},
credentialsManager: {
getCredentials: jest.fn().mockResolvedValue(mockCredentials),
requireLocalAuthentication: jest.fn().mockResolvedValue(),
clearCredentials: jest.fn().mockResolvedValue(),
saveCredentials: jest.fn().mockResolvedValue(),
hasValidCredentials: jest.fn(),
Expand Down Expand Up @@ -1051,61 +1050,6 @@ describe('The useAuth0 hook', () => {
expect(result.current.error).toEqual(thrownError);
});

it('can require local authentication', async () => {
const { result } = renderHook(() => useAuth0(), {
wrapper,
});

await waitFor(() => expect(result.current.isLoading).toBe(false));

result.current.requireLocalAuthentication();

expect(
mockAuth0.credentialsManager.requireLocalAuthentication
).toHaveBeenCalled();
});

it('can require local authentication with options', async () => {
const { result } = renderHook(() => useAuth0(), {
wrapper,
});

await waitFor(() => expect(result.current.isLoading).toBe(false));

result.current.requireLocalAuthentication(
'title',
'description',
'cancel',
'fallback',
LocalAuthenticationStrategy.deviceOwner
);

expect(
mockAuth0.credentialsManager.requireLocalAuthentication
).toHaveBeenCalledWith(
'title',
'description',
'cancel',
'fallback',
LocalAuthenticationStrategy.deviceOwner
);
});

it('dispatches an error when requireLocalAuthentication fails', async () => {
const { result } = renderHook(() => useAuth0(), {
wrapper,
});
const thrownError = new Error('requireLocalAuthentication failed');

mockAuth0.credentialsManager.requireLocalAuthentication.mockRejectedValue(
thrownError
);

result.current.requireLocalAuthentication();
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.error).toEqual(thrownError);
});

it('calls hasValidCredentials with correct parameters', async () => {
const { result } = renderHook(() => useAuth0(), {
wrapper,
Expand Down
66 changes: 54 additions & 12 deletions src/webauth/__tests__/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import * as nativeUtils from '../../utils/nativeHelper';
import Agent from '../agent';
import { NativeModules, Platform, Linking } from 'react-native';

const localAuthenticationOptions = {
title: 'Authenticate With Your Biometrics',
evaluationPolicy: 1,
authenticationLevel: 0,
};

jest.mock('react-native', () => {
// Require the original module to not be mocked...
return {
Expand Down Expand Up @@ -58,9 +64,15 @@ describe('Agent', () => {
clientId: clientId,
domain: domain,
},
{ customScheme: 'test' }
{ customScheme: 'test' },
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
});

it('should ensure login is called with proper parameters', async () => {
Expand Down Expand Up @@ -91,9 +103,15 @@ describe('Agent', () => {
ephemeralSession: true,
safariViewControllerPresentationStyle: 0,
additionalParameters: { test: 'test' },
}
},
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'test',
'test://test.com/ios/com.my.app/callback',
Expand Down Expand Up @@ -128,9 +146,15 @@ describe('Agent', () => {
},
{
redirectUrl: 'redirect://redirect.com',
}
},
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'com.my.app.auth0',
'redirect://redirect.com',
Expand Down Expand Up @@ -173,9 +197,15 @@ describe('Agent', () => {
clientId: clientId,
domain: domain,
},
{ customScheme: 'test' }
{ customScheme: 'test' },
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
});

it('should ensure logout is called with proper parameters', async () => {
Expand All @@ -195,9 +225,15 @@ describe('Agent', () => {
{
customScheme: 'test',
federated: true,
}
},
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'test',
true,
Expand All @@ -221,9 +257,15 @@ describe('Agent', () => {
},
{
returnToUrl: 'redirect://redirect.com',
}
},
localAuthenticationOptions
);
expect(mock).toBeCalledWith(
NativeModules.A0Auth0,
clientId,
domain,
localAuthenticationOptions
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'com.my.app.auth0',
false,
Expand Down
35 changes: 27 additions & 8 deletions src/webauth/__tests__/webauth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ describe('WebAuth', () => {
const domain = 'auth0.com';
const baseUrl = 'https://' + domain;
const auth = new Auth({ baseUrl: baseUrl, clientId: clientId });
const webauth = new WebAuth(auth);
const localAuthenticationOptions = {
title: 'Authenticate With Your Biometrics',
evaluationPolicy: 1,
authenticationLevel: 0,
};
const webauth = new WebAuth(auth, localAuthenticationOptions);

describe('authorize', () => {
it('should authorize with provided parameters', async () => {
Expand Down Expand Up @@ -49,7 +54,12 @@ describe('WebAuth', () => {
).resolves.toMatchSnapshot();
expect(showMock).toHaveBeenCalledWith(
{ clientId, domain },
{ ...parameters, ...options, safariViewControllerPresentationStyle: -2 }
{
...parameters,
...options,
safariViewControllerPresentationStyle: -2,
},
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand Down Expand Up @@ -88,7 +98,8 @@ describe('WebAuth', () => {
).resolves.toMatchSnapshot();
expect(showMock).toHaveBeenCalledWith(
{ clientId, domain },
{ ...parameters, ...options, safariViewControllerPresentationStyle: 0 }
{ ...parameters, ...options, safariViewControllerPresentationStyle: 0 },
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand Down Expand Up @@ -130,7 +141,8 @@ describe('WebAuth', () => {
...parameters,
...options,
safariViewControllerPresentationStyle: undefined,
}
},
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand Down Expand Up @@ -173,7 +185,8 @@ describe('WebAuth', () => {
...parameters,
...options,
safariViewControllerPresentationStyle: undefined,
}
},
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand Down Expand Up @@ -216,7 +229,8 @@ describe('WebAuth', () => {
...parameters,
...options,
safariViewControllerPresentationStyle: 0,
}
},
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand All @@ -234,10 +248,15 @@ describe('WebAuth', () => {
const showMock = jest
.spyOn(webauth.agent, 'logout')
.mockImplementation(() => Promise.resolve());
await webauth.clearSession(parameters, options);
await webauth.clearSession(
parameters,
options,
localAuthenticationOptions
);
expect(showMock).toHaveBeenCalledWith(
{ clientId, domain },
{ ...parameters, ...options }
{ ...parameters, ...options },
localAuthenticationOptions
);
showMock.mockRestore();
});
Expand Down

0 comments on commit fb8b5f7

Please sign in to comment.