-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostIndependentKey.test.ts
More file actions
103 lines (83 loc) · 2.84 KB
/
Copy pathpostIndependentKey.test.ts
File metadata and controls
103 lines (83 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'should';
import * as request from 'supertest';
import nock from 'nock';
import { app as enclavedApp } from '../../../enclavedApp';
import { AppMode, EnclavedConfig, TlsMode } from '../../../shared/types';
import express from 'express';
import * as sinon from 'sinon';
import coinFactory from '../../../shared/coinFactory';
import { BaseCoin } from '@bitgo-beta/sdk-core';
describe('postIndependentKey', () => {
let cfg: EnclavedConfig;
let app: express.Application;
let agent: request.SuperAgentTest;
// test cofig
const kmsUrl = 'http://kms.invalid';
const coin = 'hteth';
const accessToken = 'test-token';
// sinon stubs
before(() => {
// nock config
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
// app config
cfg = {
appMode: AppMode.ENCLAVED,
port: 0, // Let OS assign a free port
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
kmsUrl: kmsUrl,
tlsMode: TlsMode.DISABLED,
allowSelfSigned: true,
};
// app setup
app = enclavedApp(cfg);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
// test cases
it('should post an independent key successfully', async () => {
const mockKmsResponse = {
coin: coin,
pub: 'xpub661MyMwAqRbcGAEfZmG74QD11P4dCKRkuwpsJG87QKVPcMdA1PLe76de1Ted54rZ2gyqLYhmdhBCFMrt7AoVwPZwXa3Na9aUnvndvXbvmwu',
source: 'user',
type: 'independent',
};
const kmsNock = nock(kmsUrl).post(`/key`).reply(200, mockKmsResponse);
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(200);
response.body.should.have.property('pub', mockKmsResponse.pub);
response.body.should.have.property('coin', mockKmsResponse.coin);
response.body.should.have.property('source', mockKmsResponse.source);
kmsNock.done();
});
it('should fail to post an independent key if source is not provided', async () => {
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({});
response.status.should.equal(400);
});
it('should fail if there is an error in creating the public and private key pairs', async () => {
const coinStub = sinon.stub(coinFactory, 'getCoin').returns(
Promise.resolve({
keychains: () => ({
create: () => ({}),
}),
} as unknown as BaseCoin),
);
const response = await agent
.post(`/api/${coin}/key/independent`)
.set('Authorization', `Bearer ${accessToken}`)
.send({ source: 'user' });
response.status.should.equal(500);
response.body.should.have.property('details', 'BitGo SDK failed to create public key');
coinStub.restore();
});
});