|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { removePasskeyFromAccount } from '../../src/removePasskeyFromAccount'; |
| 4 | +import type { WebAuthnOtpDevice } from '@bitgo/public-types'; |
| 5 | + |
| 6 | +describe('removePasskeyFromAccount', function () { |
| 7 | + let mockBitGo: { |
| 8 | + url: sinon.SinonStub; |
| 9 | + del: sinon.SinonStub; |
| 10 | + }; |
| 11 | + |
| 12 | + const device: WebAuthnOtpDevice = { |
| 13 | + id: 'mongo-object-id-123', |
| 14 | + credentialId: 'cred-id-should-not-be-used', |
| 15 | + prfSalt: 'some-salt', |
| 16 | + isPasskey: true, |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(function () { |
| 20 | + mockBitGo = { |
| 21 | + url: sinon.stub().callsFake((path: string) => `https://app.bitgo.com/api/v1${path}`), |
| 22 | + del: sinon.stub().returns({ |
| 23 | + result: sinon.stub().resolves(undefined), |
| 24 | + }), |
| 25 | + }; |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(function () { |
| 29 | + sinon.restore(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should DELETE /user/otp/{device.id} using device.id', async function () { |
| 33 | + await removePasskeyFromAccount({ bitgo: mockBitGo as any, device }); |
| 34 | + |
| 35 | + assert.strictEqual(mockBitGo.url.calledOnce, true); |
| 36 | + assert.strictEqual(mockBitGo.url.firstCall.args[0], `/user/otp/${device.id}`); |
| 37 | + assert.strictEqual(mockBitGo.del.calledOnce, true); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not use credentialId', async function () { |
| 41 | + await removePasskeyFromAccount({ bitgo: mockBitGo as any, device }); |
| 42 | + |
| 43 | + const urlArg: string = mockBitGo.url.firstCall.args[0]; |
| 44 | + assert.ok(!urlArg.includes(device.credentialId), 'URL should not contain credentialId'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should resolve without returning a value', async function () { |
| 48 | + const result = await removePasskeyFromAccount({ bitgo: mockBitGo as any, device }); |
| 49 | + assert.strictEqual(result, undefined); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw if device.id is empty', async function () { |
| 53 | + const badDevice: WebAuthnOtpDevice = { ...device, id: '' }; |
| 54 | + await assert.rejects(() => removePasskeyFromAccount({ bitgo: mockBitGo as any, device: badDevice }), { |
| 55 | + message: 'device.id is required to remove a passkey from the account', |
| 56 | + }); |
| 57 | + assert.strictEqual(mockBitGo.del.called, false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw if device.id is undefined', async function () { |
| 61 | + const badDevice = { ...device, id: undefined } as unknown as WebAuthnOtpDevice; |
| 62 | + await assert.rejects(() => removePasskeyFromAccount({ bitgo: mockBitGo as any, device: badDevice }), { |
| 63 | + message: 'device.id is required to remove a passkey from the account', |
| 64 | + }); |
| 65 | + assert.strictEqual(mockBitGo.del.called, false); |
| 66 | + }); |
| 67 | +}); |
0 commit comments