-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): adding updating unit tests
- Loading branch information
Showing
5 changed files
with
330 additions
and
0 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
154 changes: 154 additions & 0 deletions
154
packages/auth/__tests__/providers/cognito/associateWebAuthnCredential.test.ts
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 |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { Amplify, fetchAuthSession } from '@aws-amplify/core'; | ||
import { decodeJWT } from '@aws-amplify/core/internals/utils'; | ||
|
||
import { | ||
createGetWebAuthnRegistrationOptionsClient, | ||
createVerifyWebAuthnRegistrationResultClient, | ||
} from '../../../src/foundation/factories/serviceClients/cognitoIdentityProvider'; | ||
import { | ||
PasskeyError, | ||
PasskeyErrorCode, | ||
} from '../../../src/utils/passkey/errors'; | ||
import { associateWebAuthnCredential } from '../../../src/providers/cognito/apis/associateWebAuthnCredential'; | ||
import { | ||
passkeyCredentialCreateOptions, | ||
passkeyRegistrationResult, | ||
} from '../../mockData'; | ||
import { serializePkcToJson } from '../../../src/utils/passkey/serde'; | ||
import * as utils from '../../../src/utils'; | ||
|
||
import { setUpGetConfig } from './testUtils/setUpGetConfig'; | ||
import { mockAccessToken } from './testUtils/data'; | ||
|
||
jest.mock('@aws-amplify/core', () => ({ | ||
...(jest.createMockFromModule('@aws-amplify/core') as object), | ||
Amplify: { getConfig: jest.fn(() => ({})) }, | ||
})); | ||
jest.mock('@aws-amplify/core/internals/utils', () => ({ | ||
...jest.requireActual('@aws-amplify/core/internals/utils'), | ||
isBrowser: jest.fn(() => false), | ||
})); | ||
jest.mock( | ||
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider', | ||
); | ||
jest.mock('../../../src/providers/cognito/factories'); | ||
|
||
Object.assign(navigator, { | ||
credentials: { | ||
create: jest.fn(), | ||
}, | ||
}); | ||
|
||
describe('associateWebAuthnCredential', () => { | ||
const navigatorCredentialsCreateSpy = jest.spyOn( | ||
navigator.credentials, | ||
'create', | ||
); | ||
const registerPasskeySpy = jest.spyOn(utils, 'registerPasskey'); | ||
|
||
const mockFetchAuthSession = jest.mocked(fetchAuthSession); | ||
|
||
const mockGetWebAuthnRegistrationOptions = jest.fn(); | ||
const mockCreateGetWebAuthnRegistrationOptionsClient = jest.mocked( | ||
createGetWebAuthnRegistrationOptionsClient, | ||
); | ||
|
||
const mockVerifyWebAuthnRegistrationResult = jest.fn(); | ||
const mockCreateVerifyWebAuthnRegistrationResultClient = jest.mocked( | ||
createVerifyWebAuthnRegistrationResultClient, | ||
); | ||
|
||
beforeAll(() => { | ||
setUpGetConfig(Amplify); | ||
mockFetchAuthSession.mockResolvedValue({ | ||
tokens: { accessToken: decodeJWT(mockAccessToken) }, | ||
}); | ||
mockCreateGetWebAuthnRegistrationOptionsClient.mockReturnValue( | ||
mockGetWebAuthnRegistrationOptions, | ||
); | ||
mockCreateVerifyWebAuthnRegistrationResultClient.mockReturnValue( | ||
mockVerifyWebAuthnRegistrationResult, | ||
); | ||
mockVerifyWebAuthnRegistrationResult.mockImplementation(() => ({ | ||
CredentialId: '12345', | ||
})); | ||
|
||
navigatorCredentialsCreateSpy.mockResolvedValue(passkeyRegistrationResult); | ||
}); | ||
|
||
afterEach(() => { | ||
mockFetchAuthSession.mockClear(); | ||
mockGetWebAuthnRegistrationOptions.mockReset(); | ||
navigatorCredentialsCreateSpy.mockClear(); | ||
}); | ||
|
||
it('should pass the correct service options when retrieving credential creation options', async () => { | ||
mockGetWebAuthnRegistrationOptions.mockImplementation(() => ({ | ||
CredentialCreationOptions: passkeyCredentialCreateOptions, | ||
})); | ||
|
||
await associateWebAuthnCredential(); | ||
|
||
expect(mockGetWebAuthnRegistrationOptions).toHaveBeenCalledWith( | ||
{ | ||
region: 'us-west-2', | ||
userAgentValue: expect.any(String), | ||
}, | ||
{ | ||
AccessToken: mockAccessToken, | ||
}, | ||
); | ||
}); | ||
|
||
it('should pass the correct service options when verifying a credential', async () => { | ||
mockGetWebAuthnRegistrationOptions.mockImplementation(() => ({ | ||
CredentialCreationOptions: passkeyCredentialCreateOptions, | ||
})); | ||
|
||
await associateWebAuthnCredential(); | ||
|
||
expect(mockVerifyWebAuthnRegistrationResult).toHaveBeenCalledWith( | ||
{ | ||
region: 'us-west-2', | ||
userAgentValue: expect.any(String), | ||
}, | ||
{ | ||
AccessToken: mockAccessToken, | ||
Credential: JSON.stringify( | ||
serializePkcToJson(passkeyRegistrationResult), | ||
), | ||
}, | ||
); | ||
}); | ||
|
||
it('should call the registerPasskey function with correct input', async () => { | ||
mockGetWebAuthnRegistrationOptions.mockImplementation(() => ({ | ||
CredentialCreationOptions: passkeyCredentialCreateOptions, | ||
})); | ||
|
||
await associateWebAuthnCredential(); | ||
|
||
expect(registerPasskeySpy).toHaveBeenCalledWith( | ||
JSON.parse(passkeyCredentialCreateOptions), | ||
); | ||
|
||
expect(navigatorCredentialsCreateSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error when service returns empty credential creation options', async () => { | ||
expect.assertions(2); | ||
|
||
mockGetWebAuthnRegistrationOptions.mockImplementation(() => ({ | ||
CredentialCreationOptions: undefined, | ||
})); | ||
|
||
try { | ||
await associateWebAuthnCredential(); | ||
} catch (error: any) { | ||
expect(error).toBeInstanceOf(PasskeyError); | ||
expect(error.name).toBe( | ||
PasskeyErrorCode.InvalidCredentialCreationOptions, | ||
); | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
convertArrayBufferToBase64Url, | ||
convertBase64UrlToArrayBuffer, | ||
} from '../../src/utils/passkey/base64Url'; | ||
import { | ||
deserializeJsonToPkcCreationOptions, | ||
serializePkcToJson, | ||
} from '../../src/utils/passkey/serde'; | ||
import { | ||
passkeyRegistrationRequest, | ||
passkeyRegistrationRequestJson, | ||
passkeyRegistrationResult, | ||
passkeyRegistrationResultJson, | ||
} from '../mockData'; | ||
|
||
describe('passkey', () => { | ||
it('converts ArrayBuffer values to base64url', () => { | ||
expect(convertArrayBufferToBase64Url(new Uint8Array([]))).toBe(''); | ||
expect(convertArrayBufferToBase64Url(new Uint8Array([0]))).toBe('AA'); | ||
expect(convertArrayBufferToBase64Url(new Uint8Array([1, 2, 3]))).toBe( | ||
'AQID', | ||
); | ||
}); | ||
it('converts base64url values to ArrayBuffer', () => { | ||
expect( | ||
convertArrayBufferToBase64Url(convertBase64UrlToArrayBuffer('')), | ||
).toBe(convertArrayBufferToBase64Url(new Uint8Array([]))); | ||
expect( | ||
convertArrayBufferToBase64Url(convertBase64UrlToArrayBuffer('AA')), | ||
).toBe(convertArrayBufferToBase64Url(new Uint8Array([0]))); | ||
expect( | ||
convertArrayBufferToBase64Url(convertBase64UrlToArrayBuffer('AQID')), | ||
).toBe(convertArrayBufferToBase64Url(new Uint8Array([1, 2, 3]))); | ||
}); | ||
|
||
it('converts base64url to ArrayBuffer and back without data loss', () => { | ||
const input = '_h7NMedx8qUAz_yHKhgHt74P2UrTU_qcB4_ToULz12M'; | ||
expect( | ||
convertArrayBufferToBase64Url(convertBase64UrlToArrayBuffer(input)), | ||
).toBe(input); | ||
}); | ||
|
||
it('serializes pkc into correct json format', () => { | ||
expect(JSON.stringify(serializePkcToJson(passkeyRegistrationResult))).toBe( | ||
JSON.stringify(passkeyRegistrationResultJson), | ||
); | ||
}); | ||
|
||
it('deserializes json into correct pkc format', () => { | ||
const deserialized = deserializeJsonToPkcCreationOptions( | ||
passkeyRegistrationRequestJson, | ||
); | ||
|
||
expect(deserialized.challenge.byteLength).toEqual( | ||
passkeyRegistrationRequest.challenge.byteLength, | ||
); | ||
expect(deserialized.user.id.byteLength).toEqual( | ||
passkeyRegistrationRequest.user.id.byteLength, | ||
); | ||
|
||
expect(deserialized).toEqual( | ||
expect.objectContaining({ | ||
rp: expect.any(Object), | ||
user: { | ||
id: expect.any(ArrayBuffer), | ||
name: expect.any(String), | ||
displayName: expect.any(String), | ||
}, | ||
challenge: expect.any(ArrayBuffer), | ||
pubKeyCredParams: expect.any(Array), | ||
timeout: expect.any(Number), | ||
excludeCredentials: expect.any(Array), | ||
authenticatorSelection: expect.any(Object), | ||
}), | ||
); | ||
}); | ||
}); |
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