-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_spec.js
282 lines (244 loc) · 8.23 KB
/
server_spec.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
process.env.NODE_ENV = 'test'
var chai = require('chai')
var chaiHttp = require('chai-http')
var expect = chai.expect
var nock = require('nock')
var extend = require('util')._extend
var _ = require('lodash')
const { URL } = require('url')
chai.use(chaiHttp)
const chains = require('../lib/chains')
const bitcore = chains.get()
var Unit = bitcore.Unit
const config = require('config')
const db = require('../lib/db')
const server = require('../server/app')
const request = chai.request(server)
const Insights = require('./fixtures/insight')
const insights = new Insights()
const records = require('./fixtures/records')
const insightApiUrl = new URL(config.get('insightApiUrl'))
const magicNumber = config.get('app.magicNumber')
describe('register a document', () => {
var address = records.address
var digest = records.digest
var document = records.document
expected_price_satoshi = chains.documentPrice()
expected_network = bitcore.Networks.defaultNetwork.name
it('it should return payment details', (done) => {
request
.post('/api/v1/register')
.type('form')
.send({d: digest})
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
var register = res.body
expect(register.success).to.equal('true')
expect(register.digest).to.equal(digest)
expect(register.price).to.equal(expected_price_satoshi)
expect(register.pay_address).to.be.a('string')
expect(register.pay_address).to.have.lengthOf(34)
done()
})
})
it('it should return an error message if the document is already registered', (done) => {
db.batch()
.put(`map-${digest}`, address)
.put(address, JSON.stringify(document))
.write(() => {
request
.post('/api/v1/register')
.type('form')
.send({d: digest})
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
let register = res.body
expect(register.success).to.be.false
expect(register.reason).to.equal('existing')
expect(register.digest).to.equal(digest)
done()
})
})
})
it('it should return an error on invalid hash', (done) => {
request
.post('/api/v1/register')
.type('form')
.send({d: 'invalid'})
.end((err, res) => {
expect(err).to.not.be.null
expect(res).to.have.status(400)
expect(res).to.be.json
expect(res.body).to.deep.equal({reason: 'Invalid `hash` field' })
done()
})
})
it('it should update a status', (done) => {
db.batch()
.put(`map-${digest}`, address)
.put(address, JSON.stringify(document))
.write(() => {
request
.post('/api/v1/status')
.type('form')
.send({d: digest})
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
status = res.body
expect(status.success).to.equal(true)
expect(status.pending).to.equal(false)
expect(status.digest).to.equal(digest)
expect(status.payment_address).to.equal(address)
expect(status.price).to.equal(expected_price_satoshi)
expect(status.network).to.equal(expected_network)
expect(status.timestamp).to.be.a('string')
expect(status.txstamp).to.be.a('string')
expect(status.blockstamp).to.be.a('string')
done()
})
})
})
it('it should get a status', (done) => {
db.batch()
.put(`map-${digest}`, address)
.put(address, JSON.stringify(document))
.write(() => {
request
.get(`/api/v1/status/${digest}`)
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
status = res.body
expect(status.success).to.equal(true)
expect(status.pending).to.equal(true)
expect(status.digest).to.equal(digest)
expect(status.payment_address).to.equal(address)
expect(status.price).to.equal(expected_price_satoshi)
expect(status.network).to.equal(expected_network)
expect(status.timestamp).to.be.a('string')
expect(status.txstamp).to.be.a('string')
expect(status.blockstamp).to.be.a('string')
done()
})
})
})
it('it should query a status', (done) => {
db.batch()
.put(`map-${digest}`, address)
.put(address, JSON.stringify(document))
.write(() => {
request
.get(`/api/v1/status?d=${digest}`)
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
status = res.body
expect(status.success).to.equal(true)
expect(status.pending).to.equal(true)
expect(status.digest).to.equal(digest)
expect(status.payment_address).to.equal(address)
expect(status.price).to.equal(expected_price_satoshi)
expect(status.network).to.equal(expected_network)
expect(status.timestamp).to.be.a('string')
expect(status.txstamp).to.be.a('string')
expect(status.blockstamp).to.be.a('string')
done()
})
})
})
it('it should handle a missing status', (done) => {
request
.get(`/api/v1/status/${digest}`)
.end((err, res) => {
expect(err).to.not.be.null
expect(res).to.have.status(404)
expect(res).to.be.json
status = res.body
expect(status.success).to.be.false
expect(status.reason).to.equal('nonexistent')
done()
})
})
})
describe('/GET latest unconfirmed', () => {
it('it should GET the latest unconfirmed', (done) => {
db.batch()
.put('latest-unconfirmed', JSON.stringify([records.unconfirmed]))
.write(() => {
request
.get('/api/internal/latest/unconfirmed')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
var unconfirmed = res.body[0]
expect(unconfirmed.digest).to.equal(records.unconfirmed.digest)
done()
})
})
})
})
describe('/GET latest confirmed', () => {
it('it should GET the latest confirmed', (done) => {
db.batch()
.put('latest-confirmed', JSON.stringify([records.confirmed]))
.write(() => {
request
.get('/api/internal/latest/confirmed')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(res).to.be.json
var confirmed = res.body[0]
expect(confirmed.digest).to.equal(records.confirmed.digest)
done()
})
})
})
})
beforeEach(() => {
db.del(`map-${records.digest}`)
db.del(records.address)
})
before(() => {
const insight = nock(insightApiUrl.origin, {allowUnmocked: false}).persist()
const inpath = insightApiUrl.pathname === '/' ? '' : insightApiUrl.pathname
insight.get(`${inpath}/txs`)
.query({address: records.address})
.reply(200, insights.addrFull(), {'Content-Type': 'application/json'})
insight.get(`${inpath}/utils/estimatefee`)
.query({nbBlocks: 2})
.reply(200, insights.estimateFee, {'Content-Type': 'application/json'})
insight.post(`${inpath}/tx/send`, ((body) => {
const tx = new bitcore.Transaction(body.rawtx).toObject()
const checkPayment = _.some(tx.inputs, {
prevTxId: 'a1802a77ae533a862f7df5d7be2e7fb24fe65d6b6d1b05921a91bdfcdb0c2d1b'
})
const checkDocproof = _.some(tx.outputs, {
script: `6a28444f4350524f4f46${records.digest}`
})
const checkFee = _.some(tx.outputs, {
satoshis: 79872
})
return checkPayment && checkDocproof && checkFee
}))
.reply(200, ((uri, body) => {
const tx = new bitcore.Transaction(body.tx)
return insights.txSend(tx.hash)
}), {
'Content-Type': 'application/json'
})
})
after(() => {
server.stop()
db.destroy()
insights.reset()
})