|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as t from 'io-ts'; |
| 3 | +import { VerifyAddressBody, PostVerifyAddress } from '../../../src/typedRoutes/api/common/verifyAddress'; |
| 4 | +import { assertDecode } from './common'; |
| 5 | +import 'should'; |
| 6 | +import 'should-http'; |
| 7 | +import 'should-sinon'; |
| 8 | +import * as sinon from 'sinon'; |
| 9 | +import { BitGo } from 'bitgo'; |
| 10 | +import { setupAgent } from '../../lib/testutil'; |
| 11 | + |
| 12 | +describe('VerifyAddress codec tests', function () { |
| 13 | + describe('VerifyAddressBody', function () { |
| 14 | + it('should validate body with required field (address)', function () { |
| 15 | + const validBody = { |
| 16 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 17 | + }; |
| 18 | + |
| 19 | + const decoded = assertDecode(t.type(VerifyAddressBody), validBody); |
| 20 | + assert.strictEqual(decoded.address, validBody.address); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should reject body with missing address', function () { |
| 24 | + const invalidBody = {}; |
| 25 | + |
| 26 | + assert.throws(() => { |
| 27 | + assertDecode(t.type(VerifyAddressBody), invalidBody); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should reject body with non-string address', function () { |
| 32 | + const invalidBody = { |
| 33 | + address: 12345, |
| 34 | + }; |
| 35 | + |
| 36 | + assert.throws(() => { |
| 37 | + assertDecode(t.type(VerifyAddressBody), invalidBody); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('VerifyAddressResponse', function () { |
| 43 | + const VerifyAddressResponse = PostVerifyAddress.response[200]; |
| 44 | + |
| 45 | + it('should validate response with verified=true', function () { |
| 46 | + const validResponse = { |
| 47 | + verified: true, |
| 48 | + }; |
| 49 | + |
| 50 | + const decoded = assertDecode(VerifyAddressResponse, validResponse); |
| 51 | + assert.strictEqual(decoded.verified, true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should validate response with verified=false', function () { |
| 55 | + const validResponse = { |
| 56 | + verified: false, |
| 57 | + }; |
| 58 | + |
| 59 | + const decoded = assertDecode(VerifyAddressResponse, validResponse); |
| 60 | + assert.strictEqual(decoded.verified, false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should reject response with missing verified field', function () { |
| 64 | + const invalidResponse = {}; |
| 65 | + |
| 66 | + assert.throws(() => { |
| 67 | + assertDecode(VerifyAddressResponse, invalidResponse); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should reject response with non-boolean verified field', function () { |
| 72 | + const invalidResponse = { |
| 73 | + verified: 'true', |
| 74 | + }; |
| 75 | + |
| 76 | + assert.throws(() => { |
| 77 | + assertDecode(VerifyAddressResponse, invalidResponse); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('PostVerifyAddress route definition', function () { |
| 83 | + it('should have the correct path', function () { |
| 84 | + assert.strictEqual(PostVerifyAddress.path, '/api/v[12]/verifyaddress'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should have the correct HTTP method', function () { |
| 88 | + assert.strictEqual(PostVerifyAddress.method, 'POST'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should have the correct response types', function () { |
| 92 | + assert.ok(PostVerifyAddress.response[200]); |
| 93 | + assert.ok(PostVerifyAddress.response[404]); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // ========================================== |
| 98 | + // SUPERTEST INTEGRATION TESTS |
| 99 | + // ========================================== |
| 100 | + |
| 101 | + describe('Supertest Integration Tests', function () { |
| 102 | + const agent = setupAgent(); |
| 103 | + |
| 104 | + afterEach(function () { |
| 105 | + sinon.restore(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should successfully verify valid address (v1)', async function () { |
| 109 | + const requestBody = { |
| 110 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 111 | + }; |
| 112 | + |
| 113 | + sinon.stub(BitGo.prototype, 'verifyAddress').returns(true); |
| 114 | + |
| 115 | + const result = await agent |
| 116 | + .post('/api/v1/verifyaddress') |
| 117 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 118 | + .set('Content-Type', 'application/json') |
| 119 | + .send(requestBody); |
| 120 | + |
| 121 | + assert.strictEqual(result.status, 200); |
| 122 | + result.body.should.have.property('verified'); |
| 123 | + assert.strictEqual(result.body.verified, true); |
| 124 | + |
| 125 | + const decodedResponse = assertDecode(PostVerifyAddress.response[200], result.body); |
| 126 | + assert.strictEqual(decodedResponse.verified, true); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should successfully verify valid address (v2)', async function () { |
| 130 | + const requestBody = { |
| 131 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 132 | + }; |
| 133 | + |
| 134 | + sinon.stub(BitGo.prototype, 'verifyAddress').returns(true); |
| 135 | + |
| 136 | + const result = await agent |
| 137 | + .post('/api/v2/verifyaddress') |
| 138 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 139 | + .set('Content-Type', 'application/json') |
| 140 | + .send(requestBody); |
| 141 | + |
| 142 | + assert.strictEqual(result.status, 200); |
| 143 | + assert.strictEqual(result.body.verified, true); |
| 144 | + |
| 145 | + const decodedResponse = assertDecode(PostVerifyAddress.response[200], result.body); |
| 146 | + assert.strictEqual(decodedResponse.verified, true); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return verified=false for invalid address', async function () { |
| 150 | + const requestBody = { |
| 151 | + address: 'invalid_address_123', |
| 152 | + }; |
| 153 | + |
| 154 | + sinon.stub(BitGo.prototype, 'verifyAddress').returns(false); |
| 155 | + |
| 156 | + const result = await agent |
| 157 | + .post('/api/v1/verifyaddress') |
| 158 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 159 | + .set('Content-Type', 'application/json') |
| 160 | + .send(requestBody); |
| 161 | + |
| 162 | + assert.strictEqual(result.status, 200); |
| 163 | + result.body.should.have.property('verified'); |
| 164 | + assert.strictEqual(result.body.verified, false); |
| 165 | + |
| 166 | + const decodedResponse = assertDecode(PostVerifyAddress.response[200], result.body); |
| 167 | + assert.strictEqual(decodedResponse.verified, false); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should pass entire body to verifyAddress method', async function () { |
| 171 | + const requestBody = { |
| 172 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 173 | + }; |
| 174 | + |
| 175 | + const verifyAddressStub = sinon.stub(BitGo.prototype, 'verifyAddress').returns(true); |
| 176 | + |
| 177 | + await agent |
| 178 | + .post('/api/v1/verifyaddress') |
| 179 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 180 | + .set('Content-Type', 'application/json') |
| 181 | + .send(requestBody); |
| 182 | + |
| 183 | + sinon.assert.calledOnce(verifyAddressStub); |
| 184 | + sinon.assert.calledWith(verifyAddressStub, requestBody); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + // ========================================== |
| 189 | + // ERROR HANDLING TESTS |
| 190 | + // ========================================== |
| 191 | + |
| 192 | + describe('Error Handling Tests', function () { |
| 193 | + const agent = setupAgent(); |
| 194 | + |
| 195 | + afterEach(function () { |
| 196 | + sinon.restore(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should return 400 for missing address field', async function () { |
| 200 | + const requestBody = {}; |
| 201 | + |
| 202 | + const result = await agent |
| 203 | + .post('/api/v1/verifyaddress') |
| 204 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 205 | + .set('Content-Type', 'application/json') |
| 206 | + .send(requestBody); |
| 207 | + |
| 208 | + assert.strictEqual(result.status, 400); |
| 209 | + assert.ok(Array.isArray(result.body)); |
| 210 | + assert.ok(result.body.length > 0); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should return 400 for non-string address field', async function () { |
| 214 | + const requestBody = { |
| 215 | + address: 12345, |
| 216 | + }; |
| 217 | + |
| 218 | + const result = await agent |
| 219 | + .post('/api/v1/verifyaddress') |
| 220 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 221 | + .set('Content-Type', 'application/json') |
| 222 | + .send(requestBody); |
| 223 | + |
| 224 | + assert.strictEqual(result.status, 400); |
| 225 | + assert.ok(Array.isArray(result.body)); |
| 226 | + assert.ok(result.body.length > 0); |
| 227 | + }); |
| 228 | + |
| 229 | + it('should handle verifyAddress method throwing error', async function () { |
| 230 | + const requestBody = { |
| 231 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 232 | + }; |
| 233 | + |
| 234 | + sinon.stub(BitGo.prototype, 'verifyAddress').throws(new Error('Address verification failed')); |
| 235 | + |
| 236 | + const result = await agent |
| 237 | + .post('/api/v1/verifyaddress') |
| 238 | + .set('Authorization', 'Bearer test_access_token_12345') |
| 239 | + .set('Content-Type', 'application/json') |
| 240 | + .send(requestBody); |
| 241 | + |
| 242 | + assert.strictEqual(result.status, 500); |
| 243 | + result.body.should.have.property('error'); |
| 244 | + }); |
| 245 | + }); |
| 246 | +}); |
0 commit comments