|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as t from 'io-ts'; |
| 3 | +import { |
| 4 | + AcceptShareRequestParams, |
| 5 | + AcceptShareRequestBody, |
| 6 | + PostAcceptShare, |
| 7 | +} from '../../../src/typedRoutes/api/common/acceptShare'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Helper function to test io-ts codec decoding |
| 11 | + */ |
| 12 | +export function assertDecode<T>(codec: t.Type<T, unknown>, input: unknown): T { |
| 13 | + const result = codec.decode(input); |
| 14 | + if (result._tag === 'Left') { |
| 15 | + const errors = JSON.stringify(result.left, null, 2); |
| 16 | + assert.fail(`Decode failed with errors:\n${errors}`); |
| 17 | + } |
| 18 | + return result.right; |
| 19 | +} |
| 20 | + |
| 21 | +describe('AcceptShare codec tests', function () { |
| 22 | + describe('AcceptShareRequestParams', function () { |
| 23 | + it('should validate valid params', function () { |
| 24 | + const validParams = { |
| 25 | + shareId: '123456789abcdef', |
| 26 | + }; |
| 27 | + |
| 28 | + const decoded = assertDecode(t.type(AcceptShareRequestParams), validParams); |
| 29 | + assert.strictEqual(decoded.shareId, validParams.shareId); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should reject params with missing shareId', function () { |
| 33 | + const invalidParams = {}; |
| 34 | + |
| 35 | + assert.throws(() => { |
| 36 | + assertDecode(t.type(AcceptShareRequestParams), invalidParams); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should reject params with non-string shareId', function () { |
| 41 | + const invalidParams = { |
| 42 | + shareId: 12345, // number instead of string |
| 43 | + }; |
| 44 | + |
| 45 | + assert.throws(() => { |
| 46 | + assertDecode(t.type(AcceptShareRequestParams), invalidParams); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('AcceptShareRequestBody', function () { |
| 52 | + it('should validate body with all fields', function () { |
| 53 | + const validBody = { |
| 54 | + userPassword: 'mySecurePassword', |
| 55 | + newWalletPassphrase: 'myNewPassphrase', |
| 56 | + overrideEncryptedXprv: 'encryptedXprvString', |
| 57 | + walletShareId: 'walletShare123', |
| 58 | + }; |
| 59 | + |
| 60 | + const decoded = assertDecode(t.type(AcceptShareRequestBody), validBody); |
| 61 | + assert.strictEqual(decoded.userPassword, validBody.userPassword); |
| 62 | + assert.strictEqual(decoded.newWalletPassphrase, validBody.newWalletPassphrase); |
| 63 | + assert.strictEqual(decoded.overrideEncryptedXprv, validBody.overrideEncryptedXprv); |
| 64 | + assert.strictEqual(decoded.walletShareId, validBody.walletShareId); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should validate body with only required fields', function () { |
| 68 | + const validBody = { |
| 69 | + walletShareId: 'walletShare123', |
| 70 | + }; |
| 71 | + |
| 72 | + const decoded = assertDecode(t.type(AcceptShareRequestBody), validBody); |
| 73 | + assert.strictEqual(decoded.walletShareId, validBody.walletShareId); |
| 74 | + assert.strictEqual(decoded.userPassword, undefined); |
| 75 | + assert.strictEqual(decoded.newWalletPassphrase, undefined); |
| 76 | + assert.strictEqual(decoded.overrideEncryptedXprv, undefined); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should reject body with missing walletShareId', function () { |
| 80 | + const invalidBody = { |
| 81 | + userPassword: 'mySecurePassword', |
| 82 | + newWalletPassphrase: 'myNewPassphrase', |
| 83 | + }; |
| 84 | + |
| 85 | + assert.throws(() => { |
| 86 | + assertDecode(t.type(AcceptShareRequestBody), invalidBody); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should reject body with non-string walletShareId', function () { |
| 91 | + const invalidBody = { |
| 92 | + walletShareId: 12345, // number instead of string |
| 93 | + }; |
| 94 | + |
| 95 | + assert.throws(() => { |
| 96 | + assertDecode(t.type(AcceptShareRequestBody), invalidBody); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should reject body with non-string optional fields', function () { |
| 101 | + const invalidBody = { |
| 102 | + userPassword: 12345, // number instead of string |
| 103 | + walletShareId: 'walletShare123', |
| 104 | + }; |
| 105 | + |
| 106 | + assert.throws(() => { |
| 107 | + assertDecode(t.type(AcceptShareRequestBody), invalidBody); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('Edge cases', function () { |
| 113 | + it('should handle empty strings for required fields', function () { |
| 114 | + const body = { |
| 115 | + walletShareId: '', // empty string |
| 116 | + }; |
| 117 | + |
| 118 | + // Empty strings are still valid strings |
| 119 | + const decoded = assertDecode(t.type(AcceptShareRequestBody), body); |
| 120 | + assert.strictEqual(decoded.walletShareId, ''); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('PostAcceptShare route definition', function () { |
| 124 | + it('should have the correct path', function () { |
| 125 | + assert.strictEqual(PostAcceptShare.path, '/api/v1/walletshare/:shareId/acceptShare'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should have the correct HTTP method', function () { |
| 129 | + assert.strictEqual(PostAcceptShare.method, 'POST'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should have the correct request configuration', function () { |
| 133 | + // Verify the route is configured with a request property |
| 134 | + assert.ok(PostAcceptShare.request); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should have the correct response types', function () { |
| 138 | + // Check that the response object has the expected status codes |
| 139 | + assert.ok(PostAcceptShare.response[200]); |
| 140 | + assert.ok(PostAcceptShare.response[400]); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle empty strings for optional fields', function () { |
| 145 | + const body = { |
| 146 | + userPassword: '', |
| 147 | + newWalletPassphrase: '', |
| 148 | + overrideEncryptedXprv: '', |
| 149 | + walletShareId: 'walletShare123', |
| 150 | + }; |
| 151 | + |
| 152 | + const decoded = assertDecode(t.type(AcceptShareRequestBody), body); |
| 153 | + assert.strictEqual(decoded.userPassword, ''); |
| 154 | + assert.strictEqual(decoded.newWalletPassphrase, ''); |
| 155 | + assert.strictEqual(decoded.overrideEncryptedXprv, ''); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle additional unknown properties', function () { |
| 159 | + const body = { |
| 160 | + walletShareId: 'walletShare123', |
| 161 | + unknownProperty: 'some value', |
| 162 | + }; |
| 163 | + |
| 164 | + // io-ts with t.exact() strips out additional properties |
| 165 | + const decoded = assertDecode(t.exact(t.type(AcceptShareRequestBody)), body); |
| 166 | + assert.strictEqual(decoded.walletShareId, 'walletShare123'); |
| 167 | + // @ts-expect-error - unknownProperty doesn't exist on the type |
| 168 | + assert.strictEqual(decoded.unknownProperty, undefined); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments