|
| 1 | +import { AppMode, EnclavedConfig, TlsMode } from "../../../initConfig"; |
| 2 | +import { app as enclavedApp } from "../../../enclavedApp"; |
| 3 | + |
| 4 | +import express from 'express'; |
| 5 | +import nock from "nock"; |
| 6 | +import * as request from 'supertest'; |
| 7 | +import * as sinon from 'sinon'; |
| 8 | +import * as configModule from '../../../initConfig'; |
| 9 | + |
| 10 | +describe('KMS Client', () => { |
| 11 | + let cfg: EnclavedConfig; |
| 12 | + let app: express.Application; |
| 13 | + let agent: request.SuperAgentTest; |
| 14 | + |
| 15 | + const kmsUrl = 'https://kms.invalid'; |
| 16 | + const coin = 'hteth'; |
| 17 | + const accessToken = 'test-token'; |
| 18 | + |
| 19 | + let configStub: sinon.SinonStub; |
| 20 | + |
| 21 | + before (() => { |
| 22 | + nock.disableNetConnect(); |
| 23 | + nock.enableNetConnect('127.0.0.1'); |
| 24 | + |
| 25 | + cfg = { |
| 26 | + appMode: AppMode.ENCLAVED, |
| 27 | + port: 0, // Let OS assign a free port |
| 28 | + bind: 'localhost', |
| 29 | + timeout: 60000, |
| 30 | + httpLoggerFile: '', |
| 31 | + kmsUrl: kmsUrl, |
| 32 | + tlsMode: TlsMode.DISABLED, |
| 33 | + allowSelfSigned: true, |
| 34 | + recoveryMode: true, |
| 35 | + }; |
| 36 | + |
| 37 | + // configStub = sinon.stub(configModule, 'initConfig').returns(cfg); |
| 38 | + |
| 39 | + // app setup |
| 40 | + app = enclavedApp(cfg); |
| 41 | + agent = request.agent(app); |
| 42 | + }); |
| 43 | + |
| 44 | + after(() => { |
| 45 | + // configStub.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + nock.cleanAll(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should bubble up KMS errors', async () => { |
| 53 | + nock(kmsUrl) |
| 54 | + .get(/.*/) |
| 55 | + .reply(400, { message: 'This is an error message' }); |
| 56 | + |
| 57 | + const response = await agent |
| 58 | + .get(`/api/${coin}/mpcv2/initialize`) |
| 59 | + .set('Authorization', `Bearer ${accessToken}`) |
| 60 | + .send({ source: 'user' }); |
| 61 | + |
| 62 | + console.log(response.body); |
| 63 | + |
| 64 | + // response.status.should.equal(400); |
| 65 | + response.body.should.have.property('error', 'BadRequestError'); |
| 66 | + response.body.should.have.property('details', 'This is an error message'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should cast unknown KMS error codes into 500', async () => { |
| 70 | + nock(kmsUrl) |
| 71 | + .post(/.*/) |
| 72 | + .reply(418, { error: 'I am a teapot' }); |
| 73 | + |
| 74 | + const response = await agent |
| 75 | + .post(`/api/${coin}/mpc/sign/mpcv2round1`) |
| 76 | + .set('Authorization', `Bearer ${accessToken}`) |
| 77 | + .send({ |
| 78 | + source: 'user', |
| 79 | + pub: 'test-pub', |
| 80 | + txRequest: {}, |
| 81 | + bitgoPublicGpgKey: 'test-bitgo-pub' |
| 82 | + }); |
| 83 | + |
| 84 | + console.log(response.body); |
| 85 | + |
| 86 | + // response.status.should.equal(500); |
| 87 | + response.body.should.have.property('error', 'InternalServerError'); |
| 88 | + response.body.should.have.property('details', 'KMS returned unexpected response. 418: I am a teapot'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments