diff options
author | Matthew Miller <matthew@millerti.me> | 2020-05-21 11:53:46 -0700 |
---|---|---|
committer | Matthew Miller <matthew@millerti.me> | 2020-05-21 11:53:46 -0700 |
commit | 0ef186ddb89a4c03a95db864260f51c5ccfadb39 (patch) | |
tree | 81d85a4e70f66a19f8fe1caf115e362c620ee104 /packages/browser/src/methods/startAttestation.test.ts | |
parent | 690e6112b233f640ff25ff10f254ba083dec6236 (diff) |
Add tests for startAttestation in browser
Diffstat (limited to 'packages/browser/src/methods/startAttestation.test.ts')
-rw-r--r-- | packages/browser/src/methods/startAttestation.test.ts | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/packages/browser/src/methods/startAttestation.test.ts b/packages/browser/src/methods/startAttestation.test.ts new file mode 100644 index 0000000..0efec48 --- /dev/null +++ b/packages/browser/src/methods/startAttestation.test.ts @@ -0,0 +1,112 @@ +import base64js from 'base64-js'; + +import { AttestationCredential, PublicKeyCredentialCreationOptionsJSON } from '@webauthntine/typescript-types'; + +import toUint8Array from '../helpers/toUint8Array'; +import supportsWebauthn from '../helpers/supportsWebauthn'; + +import startAttestation from './startAttestation'; + +jest.mock('../helpers/supportsWebauthn'); + +const mockNavigatorCreate = (window.navigator.credentials.create as jest.Mock); +const mockSupportsWebauthn = (supportsWebauthn as jest.Mock); + +const mockAttestationObject = 'mockAtte'; +const mockClientDataJSON = 'mockClie'; + +const goodOpts1: PublicKeyCredentialCreationOptionsJSON = { + publicKey: { + challenge: 'fizz', + attestation: 'direct', + pubKeyCredParams: [{ + alg: -7, + type: "public-key", + }], + rp: { + id: '1234', + name: 'webauthntine', + }, + user: { + id: '5678', + displayName: 'username', + name: 'username', + }, + timeout: 1, + }, +}; + +beforeEach(() => { + mockNavigatorCreate.mockReset(); + mockSupportsWebauthn.mockReset(); +}); + +test('should convert options before passing to navigator.credentials.create(...)', async (done) => { + mockSupportsWebauthn.mockReturnValue(true); + + // Stub out a response so the method won't throw + mockNavigatorCreate.mockImplementation((): Promise<any> => { + return new Promise((resolve) => { + resolve({ response: {} }); + }); + }); + + await startAttestation(goodOpts1); + + const argsPublicKey = mockNavigatorCreate.mock.calls[0][0].publicKey; + + expect(argsPublicKey.challenge).toEqual(toUint8Array(goodOpts1.publicKey.challenge)); + expect(argsPublicKey.user.id).toEqual(toUint8Array(goodOpts1.publicKey.user.id)); + + done(); +}); + +test('should return base64-encoded response values', async (done) => { + mockSupportsWebauthn.mockReturnValue(true); + + mockNavigatorCreate.mockImplementation((): Promise<AttestationCredential> => { + return new Promise((resolve) => { + resolve({ + id: 'foobar', + rawId: toUint8Array('foobar'), + response: { + attestationObject: base64js.toByteArray(mockAttestationObject), + clientDataJSON: base64js.toByteArray(mockClientDataJSON), + }, + getClientExtensionResults: () => ({}), + type: 'webauthn.create', + }); + }); + }); + + const response = await startAttestation(goodOpts1); + + expect(response).toEqual({ + base64AttestationObject: mockAttestationObject, + base64ClientDataJSON: mockClientDataJSON, + }); + + done(); +}) + +test('should throw error if WebAuthn isn\'t supported', async (done) => { + mockSupportsWebauthn.mockReturnValue(false); + + await expect(startAttestation(goodOpts1)).rejects.toThrow('WebAuthn is not supported in this browser'); + + done(); +}); + +test('should throw error if attestation is cancelled for some reason', async (done) => { + mockSupportsWebauthn.mockReturnValue(true); + + mockNavigatorCreate.mockImplementation((): Promise<null> => { + return new Promise((resolve) => { + resolve(null); + }); + }); + + await expect(startAttestation(goodOpts1)).rejects.toThrow('Attestation was not completed'); + + done(); +}); |