|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { BitGo } from 'bitgo'; |
| 4 | +import { app } from '../../src/expressApp'; |
| 5 | +import { Config } from '../../src/config'; |
| 6 | +import * as supertest from 'supertest'; |
| 7 | + |
| 8 | +describe('AuthVersion Integration Tests', function () { |
| 9 | + let testApp: any; |
| 10 | + |
| 11 | + afterEach(function () { |
| 12 | + sinon.restore(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should create BitGo instance with authVersion 2 by default', function () { |
| 16 | + const config: Config = { |
| 17 | + port: 3080, |
| 18 | + bind: 'localhost', |
| 19 | + env: 'test', |
| 20 | + debugNamespace: [], |
| 21 | + logFile: '', |
| 22 | + disableSSL: false, |
| 23 | + disableProxy: false, |
| 24 | + disableEnvCheck: true, |
| 25 | + timeout: 305000, |
| 26 | + authVersion: 2, |
| 27 | + }; |
| 28 | + |
| 29 | + testApp = app(config); |
| 30 | + |
| 31 | + // The BitGo instance will be created on each request |
| 32 | + // We verify the config is set correctly |
| 33 | + assert.strictEqual(config.authVersion, 2); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should create BitGo instance with authVersion 4 when configured', function () { |
| 37 | + const config: Config = { |
| 38 | + port: 3080, |
| 39 | + bind: 'localhost', |
| 40 | + env: 'test', |
| 41 | + debugNamespace: [], |
| 42 | + logFile: '', |
| 43 | + disableSSL: false, |
| 44 | + disableProxy: false, |
| 45 | + disableEnvCheck: true, |
| 46 | + timeout: 305000, |
| 47 | + authVersion: 4, |
| 48 | + }; |
| 49 | + |
| 50 | + testApp = app(config); |
| 51 | + |
| 52 | + // The BitGo instance will be created on each request with authVersion 4 |
| 53 | + assert.strictEqual(config.authVersion, 4); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should pass authVersion to BitGo constructor on request', async function () { |
| 57 | + const config: Config = { |
| 58 | + port: 3080, |
| 59 | + bind: 'localhost', |
| 60 | + env: 'test', |
| 61 | + debugNamespace: [], |
| 62 | + logFile: '', |
| 63 | + disableSSL: false, |
| 64 | + disableProxy: false, |
| 65 | + disableEnvCheck: true, |
| 66 | + timeout: 305000, |
| 67 | + authVersion: 4, |
| 68 | + }; |
| 69 | + |
| 70 | + testApp = app(config); |
| 71 | + |
| 72 | + // Stub BitGo methods to verify authVersion is used |
| 73 | + const pingStub = sinon.stub(BitGo.prototype, 'ping').resolves({ status: 'ok' }); |
| 74 | + |
| 75 | + const agent = supertest.agent(testApp); |
| 76 | + await agent.get('/api/v1/ping').expect(200); |
| 77 | + |
| 78 | + // Verify that a BitGo instance was created (ping was called) |
| 79 | + assert.ok(pingStub.called, 'BitGo ping should have been called'); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('V4 Authentication Flow', function () { |
| 83 | + it('should handle V4 login request structure', async function () { |
| 84 | + const config: Config = { |
| 85 | + port: 3080, |
| 86 | + bind: 'localhost', |
| 87 | + env: 'test', |
| 88 | + debugNamespace: [], |
| 89 | + logFile: '', |
| 90 | + disableSSL: false, |
| 91 | + disableProxy: false, |
| 92 | + disableEnvCheck: true, |
| 93 | + timeout: 305000, |
| 94 | + authVersion: 4, |
| 95 | + }; |
| 96 | + |
| 97 | + testApp = app(config); |
| 98 | + |
| 99 | + const mockV4Response = { |
| 100 | + email: 'test@example.com', |
| 101 | + password: 'testpass', |
| 102 | + forceSMS: false, |
| 103 | + }; |
| 104 | + |
| 105 | + // Stub authenticate to return a V4-style response |
| 106 | + const authenticateStub = sinon.stub(BitGo.prototype, 'authenticate').resolves(mockV4Response); |
| 107 | + |
| 108 | + const agent = supertest.agent(testApp); |
| 109 | + const response = await agent |
| 110 | + .post('/api/v1/user/login') |
| 111 | + .send({ |
| 112 | + email: 'test@example.com', |
| 113 | + password: 'testpass', |
| 114 | + }) |
| 115 | + .expect(200); |
| 116 | + |
| 117 | + assert.ok(authenticateStub.called, 'authenticate should have been called'); |
| 118 | + assert.strictEqual(response.body.email, mockV4Response.email); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should use authVersion 4 for HMAC calculation in authenticated requests', async function () { |
| 122 | + const config: Config = { |
| 123 | + port: 3080, |
| 124 | + bind: 'localhost', |
| 125 | + env: 'test', |
| 126 | + debugNamespace: [], |
| 127 | + logFile: '', |
| 128 | + disableSSL: false, |
| 129 | + disableProxy: false, |
| 130 | + disableEnvCheck: true, |
| 131 | + timeout: 305000, |
| 132 | + authVersion: 4, |
| 133 | + }; |
| 134 | + |
| 135 | + testApp = app(config); |
| 136 | + |
| 137 | + const agent = supertest.agent(testApp); |
| 138 | + |
| 139 | + // Make any authenticated request to trigger BitGo instantiation |
| 140 | + const pingStub = sinon.stub(BitGo.prototype, 'ping').resolves({ status: 'ok' }); |
| 141 | + await agent.get('/api/v1/ping').expect(200); |
| 142 | + |
| 143 | + // Since prepareBitGo creates a new BitGo instance per request with authVersion from config, |
| 144 | + // the instance should use authVersion 4 for all operations |
| 145 | + assert.ok(pingStub.called); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments