/
endpoints.ts
141 lines (114 loc) · 3.9 KB
/
endpoints.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* tslint:disable:no-relative-imports */
import { isNil } from 'ramda'
import { describe } from 'riteway'
import { app } from '../../src/app'
import { ensureBitcoinBalance, resetBitcoinServers } from '../helpers/bitcoin'
import { getHealth, getMetrics } from '../helpers/endpoints'
import { delay, runtimeId, createDatabase } from '../helpers/utils'
const Client = require('bitcoin-core')
const PREFIX = `test-functional-node-poet-${runtimeId()}`
const NODE_PORT = '28081'
const LOW_WALLET_BALANCE_IN_BITCOIN = 1
const bitcoindClient = new Client({
host: process.env.BITCOIN_URL || 'bitcoind-1',
port: 18443,
network: 'regtest',
password: 'bitcoinrpcpassword',
username: 'bitcoinrpcuser',
})
describe('Health Endpoint Returns the Correct Fields', async (assert: any) => {
const db = await createDatabase(PREFIX)
await resetBitcoinServers()
await delay(5 * 1000)
const server = await app({
BITCOIN_URL: process.env.BITCOIN_URL || 'bitcoind-1',
API_PORT: NODE_PORT,
MONGODB_DATABASE: db.settings.tempDbName,
MONGODB_USER: db.settings.tempDbUser,
MONGODB_PASSWORD: db.settings.tempDbPassword,
EXCHANGE_PREFIX: PREFIX,
LOW_WALLET_BALANCE_IN_BITCOIN,
HEALTH_INTERVAL_IN_SECONDS: 1,
})
// Allow everything to finish starting.
await delay(5 * 1000)
{
// Check health.
const response = await getHealth(NODE_PORT)
const healthData = await response.json()
const { mongoIsConnected, ipfsInfo, blockchainInfo, networkInfo, walletInfo, estimatedSmartFeeInfo } = healthData
assert({
given: 'a request to the health endpoint',
should: 'return object with property mongoIsConnected',
actual: typeof mongoIsConnected,
expected: 'boolean',
})
assert({
given: 'a request to the health endpoint',
should: 'return object with property ipfsInfo',
actual: isNil(ipfsInfo),
expected: false,
})
assert({
given: 'a request to the health endpoint',
should: 'return object with property blockchainInfo',
actual: isNil(blockchainInfo),
expected: false,
})
assert({
given: 'a request to the health endpoint',
should: 'return object with property walletInfo',
actual: isNil(walletInfo),
expected: false,
})
assert({
given: 'a request to the health endpoint',
should: 'return object with property networkInfo',
actual: isNil(networkInfo),
expected: false,
})
assert({
given: 'a request to the health endpoint',
should: 'return object with property estimatedSmartFeeInfo',
actual: isNil(estimatedSmartFeeInfo),
expected: false,
})
assert({
given: `a request to the health endpoint while the bitcoin balance is below LOW_WALLET_BALANCE_BTC`,
should: 'return walletInfo property isBalanceLow as true',
actual: walletInfo.isBalanceLow,
expected: true,
})
}
// Make sure node has regtest coins: Genrate 101 blocks at 25BTC/block if none.
await ensureBitcoinBalance(bitcoindClient)
// Make sure health service runs again.
await delay(10 * 1000)
{
// Check isBalanceLow is false.
const response = await getHealth(NODE_PORT)
const healthData = await response.json()
const {
walletInfo: { isBalanceLow },
} = healthData
assert({
given: `a request to the health endpoint while the bitcoin balance is above LOW_WALLET_BALANCE_BTC`,
should: 'return walletInfo property isBalanceLow as false',
actual: isBalanceLow,
expected: false,
})
}
{
const response = await getMetrics(NODE_PORT)
const metricsData = await response.json()
const { TotalWorkClaims } = metricsData
assert({
given: 'a request to the health endpoint',
should: 'return object with property networkInfo of type number',
actual: typeof TotalWorkClaims,
expected: 'number',
})
}
await server.stop()
await db.teardown()
})