|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import 'should'; |
| 4 | +import { removePasskeyFromWallet } from '../../../../src/bitgo/passkey/removePasskeyFromWallet'; |
| 5 | +import { WebAuthnOtpDevice } from '../../../../src/bitgo/passkey/types'; |
| 6 | + |
| 7 | +describe('removePasskeyFromWallet', function () { |
| 8 | + const walletId = 'wallet-abc123'; |
| 9 | + const keychainId = 'key-user-id'; |
| 10 | + const encryptedPrv = 'encrypted-prv-string'; |
| 11 | + const walletPassphrase = 'correct-passphrase'; |
| 12 | + const decryptedPrv = 'xprv-decrypted'; |
| 13 | + |
| 14 | + const device: WebAuthnOtpDevice = { |
| 15 | + id: 'mongo-object-id-123', |
| 16 | + credentialId: 'cred-id-456', |
| 17 | + prfSalt: 'some-salt', |
| 18 | + isPasskey: true, |
| 19 | + }; |
| 20 | + |
| 21 | + let mockBitGo: sinon.SinonStubbedInstance<{ |
| 22 | + url: (path: string, version?: number) => string; |
| 23 | + get: (url: string) => { result: () => Promise<unknown> }; |
| 24 | + del: (url: string) => { result: () => Promise<unknown> }; |
| 25 | + decrypt: (params: { password: string; input: string }) => string; |
| 26 | + }>; |
| 27 | + |
| 28 | + beforeEach(function () { |
| 29 | + mockBitGo = { |
| 30 | + url: sinon.stub().callsFake((path: string, version?: number) => `/api/v${version ?? 1}${path}`), |
| 31 | + get: sinon.stub(), |
| 32 | + del: sinon.stub(), |
| 33 | + decrypt: sinon.stub(), |
| 34 | + }; |
| 35 | + |
| 36 | + // Default: wallet fetch returns coin + keys |
| 37 | + (mockBitGo.get as sinon.SinonStub).withArgs(`/api/v2/wallet/${walletId}`).returns({ |
| 38 | + result: sinon.stub().resolves({ coin: 'tbtc', keys: [keychainId, 'backup-key-id', 'bitgo-key-id'] }), |
| 39 | + }); |
| 40 | + |
| 41 | + // Default: keychain fetch returns encryptedPrv |
| 42 | + (mockBitGo.get as sinon.SinonStub).withArgs(`/api/v2/tbtc/key/${keychainId}`).returns({ |
| 43 | + result: sinon.stub().resolves({ id: keychainId, encryptedPrv }), |
| 44 | + }); |
| 45 | + |
| 46 | + // Default: decrypt succeeds |
| 47 | + (mockBitGo.decrypt as sinon.SinonStub).returns(decryptedPrv); |
| 48 | + |
| 49 | + // Default: DELETE succeeds |
| 50 | + (mockBitGo.del as sinon.SinonStub).returns({ |
| 51 | + result: sinon.stub().resolves({}), |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(function () { |
| 56 | + sinon.restore(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should successfully remove a passkey device', async function () { |
| 60 | + await removePasskeyFromWallet({ |
| 61 | + bitgo: mockBitGo as any, |
| 62 | + walletId, |
| 63 | + device, |
| 64 | + walletPassphrase, |
| 65 | + }); |
| 66 | + |
| 67 | + // Verify decrypt was called with the right args |
| 68 | + sinon.assert.calledOnce(mockBitGo.decrypt); |
| 69 | + sinon.assert.calledWithExactly(mockBitGo.decrypt, { password: walletPassphrase, input: encryptedPrv }); |
| 70 | + |
| 71 | + // Verify DELETE was called with device.id (not credentialId) |
| 72 | + sinon.assert.calledOnce(mockBitGo.del); |
| 73 | + sinon.assert.calledWithExactly(mockBitGo.del, `/api/v2/key/${keychainId}/webauthndevice/${device.id}`); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should throw and not call DELETE if passphrase is wrong', async function () { |
| 77 | + (mockBitGo.decrypt as sinon.SinonStub).throws(new Error('decryption failed')); |
| 78 | + |
| 79 | + await assert.rejects( |
| 80 | + () => |
| 81 | + removePasskeyFromWallet({ |
| 82 | + bitgo: mockBitGo as any, |
| 83 | + walletId, |
| 84 | + device, |
| 85 | + walletPassphrase: 'wrong-passphrase', |
| 86 | + }), |
| 87 | + (err: Error) => { |
| 88 | + assert.ok(err.message.includes('Incorrect wallet passphrase')); |
| 89 | + assert.ok(err.message.includes('Passkey removal aborted to prevent lockout')); |
| 90 | + return true; |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + // DELETE must NOT have been called |
| 95 | + sinon.assert.notCalled(mockBitGo.del); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should throw descriptively if keychain has no encryptedPrv', async function () { |
| 99 | + (mockBitGo.get as sinon.SinonStub).withArgs(`/api/v2/tbtc/key/${keychainId}`).returns({ |
| 100 | + result: sinon.stub().resolves({ id: keychainId }), |
| 101 | + }); |
| 102 | + |
| 103 | + await assert.rejects( |
| 104 | + () => |
| 105 | + removePasskeyFromWallet({ |
| 106 | + bitgo: mockBitGo as any, |
| 107 | + walletId, |
| 108 | + device, |
| 109 | + walletPassphrase, |
| 110 | + }), |
| 111 | + (err: Error) => { |
| 112 | + assert.ok(err.message.includes('no encryptedPrv')); |
| 113 | + return true; |
| 114 | + } |
| 115 | + ); |
| 116 | + |
| 117 | + // No decrypt or DELETE should be called |
| 118 | + sinon.assert.notCalled(mockBitGo.decrypt); |
| 119 | + sinon.assert.notCalled(mockBitGo.del); |
| 120 | + }); |
| 121 | +}); |
0 commit comments