Skip to content

Commit f383af9

Browse files
Merge pull request #21 from navcoindev/#9-process-incoming
#9 process incoming
2 parents 3d0b472 + 03ca8b2 commit f383af9

File tree

4 files changed

+346
-3
lines changed

4 files changed

+346
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ keys/public/*
55
log.txt
66
npm-debug.log
77
config/default.json
8+
.idea/

src/lib/ProcessIncoming.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const lodash = require('lodash')
22
const ursa = require('ursa')
33

4-
const Logger = require('./Logger.js')
5-
const EncryptedData = require('./EncryptedData.js')
4+
let Logger = require('./Logger.js') // eslint-disable-line
5+
let EncryptedData = require('./EncryptedData.js')
66
const privateSettings = require('../settings/private.settings.json')
7-
const SendToAddress = require('./SendToAddress.js')
7+
let SendToAddress = require('./SendToAddress.js')
88

99
const ProcessIncoming = {}
1010

test/ProcessIncoming.spec.js

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
'use strict'
2+
3+
const expect = require('expect')
4+
const rewire = require('rewire')
5+
const sinon = require('sinon')
6+
const config = require('config')
7+
8+
const globalSettings = config.get('GLOBAL')
9+
const privateSettings = require('../src/settings/private.settings.json')
10+
11+
let ProcessIncoming = rewire('../src/lib/ProcessIncoming')
12+
13+
let mockLogger = {
14+
writeLog: sinon.spy(),
15+
}
16+
17+
let mockRuntime = {
18+
currentBatch: [],
19+
settings: {setting: true},
20+
subClient: {test: true},
21+
navClient: {test: true},
22+
outgoingPubKey: '123443',
23+
subAddresses: [],
24+
transactionsToReturn: [],
25+
successfulSubTransactions: [],
26+
remainingTransactions: [],
27+
}
28+
29+
beforeEach(() => {
30+
ProcessIncoming = rewire('../src/lib/ProcessIncoming')
31+
mockLogger = {
32+
writeLog: sinon.spy()
33+
}
34+
mockRuntime = {}
35+
})
36+
37+
describe('[ProcessIncoming]', () => {
38+
describe('(run)', () => {
39+
it('should fail on params', (done) => {
40+
const callback = (success, data) => {
41+
expect(success).toBe(false)
42+
expect(data.message).toBeA('string')
43+
sinon.assert.calledOnce(mockLogger.writeLog)
44+
done()
45+
}
46+
47+
ProcessIncoming.__set__('Logger', mockLogger)
48+
ProcessIncoming.run({junkParam: 1234}, callback)
49+
})
50+
it('should callback with success when remainingTransactions < 1', (done) => {
51+
const callback = (success, data) => {
52+
expect(success).toBe(true)
53+
expect(data.successfulSubTransactions.length).toBe(0)
54+
expect(data.transactionsToReturn.length).toBe(0)
55+
done()
56+
}
57+
const mockOptions = {
58+
currentBatch: [],
59+
settings: {setting: true},
60+
subClient: {test: true},
61+
navClient: {test: true},
62+
outgoingPubKey: '123443',
63+
subAddresses: [],
64+
}
65+
ProcessIncoming.__set__('Logger', mockLogger)
66+
ProcessIncoming.run(mockOptions, callback)
67+
})
68+
})
69+
describe('(transActionFailed)', () => {
70+
it('should transfer policy from remainingTransactions to TransactionsToReturn when failed', (done) => {
71+
const callback = (success, data) => {
72+
}
73+
mockRuntime = {
74+
callback,
75+
currentBatch: [],
76+
settings: {setting: true},
77+
subClient: {test: true},
78+
navClient: {test: true},
79+
outgoingPubKey: '123443',
80+
subAddresses: [],
81+
transactionsToReturn: [],
82+
successfulSubTransactions: [],
83+
remainingTransactions: [],
84+
}
85+
ProcessIncoming.__set__('Logger', mockLogger)
86+
ProcessIncoming.runtime = mockRuntime
87+
ProcessIncoming.transactionFailed()
88+
expect(ProcessIncoming.runtime.remainingTransactions.length).toBe(0)
89+
expect(ProcessIncoming.runtime.transactionsToReturn.length).toBe(1)
90+
done()
91+
})
92+
})
93+
describe('(checkDecrypted)', () => {
94+
it('should log a message out when success is false', (done) => {
95+
const mockTransactionFailed = () => {
96+
sinon.assert.calledOnce(mockLogger.writeLog)
97+
done()
98+
}
99+
ProcessIncoming.transactionFailed = mockTransactionFailed
100+
ProcessIncoming.__set__('Logger', mockLogger)
101+
ProcessIncoming.checkDecrypted(false, {transaction:true,data:true})
102+
})
103+
it('should log a message out when isValid is false', (done) => {
104+
const callback = (success, data) => {
105+
}
106+
const addressInfo = {isvalid:false}
107+
mockRuntime = {
108+
callback,
109+
currentBatch: [],
110+
settings: {setting: true},
111+
subClient: {test: true},
112+
navClient: {
113+
validateAddress: () => {
114+
return Promise.resolve(addressInfo)
115+
},
116+
test: true
117+
},
118+
outgoingPubKey: '123443',
119+
subAddresses: [],
120+
transactionsToReturn: [],
121+
successfulSubTransactions: [],
122+
remainingTransactions: [],
123+
}
124+
ProcessIncoming.runtime = mockRuntime
125+
ProcessIncoming.transactionFailed = () => {
126+
sinon.assert.calledOnce(mockLogger.writeLog)
127+
done()
128+
}
129+
ProcessIncoming.__set__('Logger', mockLogger)
130+
ProcessIncoming.checkDecrypted(true, {transaction:true,decrypted:true})
131+
})
132+
it('should log a message out when validateAddress call comes back false', (done) => {
133+
const callback = (success, data) => {
134+
}
135+
mockRuntime = {
136+
callback,
137+
currentBatch: [],
138+
settings: {setting: true},
139+
subClient: {test: true},
140+
navClient: {
141+
validateAddress: () => {
142+
return Promise.reject({message:'mock failure'})
143+
},
144+
test: true
145+
},
146+
outgoingPubKey: '123443',
147+
subAddresses: [],
148+
transactionsToReturn: [],
149+
successfulSubTransactions: [],
150+
remainingTransactions: [],
151+
}
152+
ProcessIncoming.runtime = mockRuntime
153+
ProcessIncoming.transactionFailed = () => {
154+
sinon.assert.calledOnce(mockLogger.writeLog)
155+
done()
156+
}
157+
ProcessIncoming.__set__('Logger', mockLogger)
158+
ProcessIncoming.checkDecrypted(true, {transaction:true,decrypted:true})
159+
})
160+
161+
})
162+
describe('(reEncryptAddress)', ()=> {
163+
it('should log a message when the encryption fails', (done) => {
164+
const callback = (success, data) => {
165+
}
166+
mockRuntime = {
167+
callback,
168+
currentBatch: [],
169+
settings: {setting: true},
170+
subClient: {test: true},
171+
navClient: {
172+
validateAddress: () => {
173+
return Promise.reject({message:'mock failure'})
174+
},
175+
test: true
176+
},
177+
outgoingPubKey: {
178+
encrypt: () => {return '12345'}
179+
},
180+
subAddresses: [],
181+
transactionsToReturn: [],
182+
successfulSubTransactions: [],
183+
remainingTransactions: [],
184+
}
185+
ProcessIncoming.runtime = mockRuntime
186+
ProcessIncoming.__set__('Logger', mockLogger)
187+
ProcessIncoming.reEncryptAddress(true, {transaction:true,decrypted:true}, 0)
188+
sinon.assert.calledOnce(mockLogger.writeLog)
189+
done()
190+
})
191+
it('should log a message when the counter exceeds the limit', (done) => {
192+
const callback = (success, data) => {
193+
}
194+
mockRuntime = {
195+
callback,
196+
currentBatch: [],
197+
settings: {setting: true},
198+
subClient: {test: true},
199+
navClient: {
200+
validateAddress: () => {
201+
return Promise.reject({message:'mock failure'})
202+
},
203+
test: true
204+
},
205+
outgoingPubKey: {
206+
encrypt: () => {return '00000000'}
207+
},
208+
subAddresses: [],
209+
transactionsToReturn: [],
210+
successfulSubTransactions: [],
211+
remainingTransactions: [],
212+
}
213+
ProcessIncoming.runtime = mockRuntime
214+
ProcessIncoming.__set__('Logger', mockLogger)
215+
ProcessIncoming.reEncryptAddress(true, {transaction:true,decrypted:true}, 11)
216+
sinon.assert.calledOnce(mockLogger.writeLog)
217+
done()
218+
})
219+
it('should log a message when the counter exceeds the limit', (done) => {
220+
const callback = (success, data) => {
221+
}
222+
mockRuntime = {
223+
callback,
224+
currentBatch: [],
225+
settings: {setting: true},
226+
subClient: {test: true},
227+
navClient: {
228+
validateAddress: () => {
229+
return Promise.reject({message:'mock failure'})
230+
},
231+
test: true
232+
},
233+
outgoingPubKey: {
234+
encrypt: () => {return '00000000'}
235+
},
236+
subAddresses: [],
237+
transactionsToReturn: [],
238+
successfulSubTransactions: [],
239+
remainingTransactions: [],
240+
}
241+
ProcessIncoming.runtime = mockRuntime
242+
ProcessIncoming.__set__('Logger', mockLogger)
243+
ProcessIncoming.reEncryptAddress(true, {transaction:true,decrypted:true}, 11)
244+
sinon.assert.calledOnce(mockLogger.writeLog)
245+
done()
246+
})
247+
})
248+
describe('(sentSubToOutgoing)', ()=> {
249+
it('should log a message out when success is false', (done) => {
250+
const mockTransactionFailed = () => {
251+
sinon.assert.calledOnce(mockLogger.writeLog)
252+
done()
253+
}
254+
ProcessIncoming.transactionFailed = mockTransactionFailed
255+
ProcessIncoming.__set__('Logger', mockLogger)
256+
ProcessIncoming.sentSubToOutgoing(false, {transaction:true,data:true})
257+
})
258+
it('should log a message out when no data is false', (done) => {
259+
const mockTransactionFailed = () => {
260+
sinon.assert.calledOnce(mockLogger.writeLog)
261+
done()
262+
}
263+
ProcessIncoming.transactionFailed = mockTransactionFailed
264+
ProcessIncoming.__set__('Logger', mockLogger)
265+
ProcessIncoming.sentSubToOutgoing(true, {sendOutcome:false,data:true})
266+
})
267+
it('should remove data from subAddresses and remainingTransactions and add to successfulSubTransactions', (done) => {
268+
const mockTransactionFailed = () => {
269+
sinon.assert.calledOnce(mockLogger.writeLog)
270+
done()
271+
}
272+
ProcessIncoming.transactionFailed = mockTransactionFailed
273+
ProcessIncoming.__set__('Logger', mockLogger)
274+
const callback = (success, data) => {
275+
}
276+
mockRuntime = {
277+
callback,
278+
currentBatch: [],
279+
settings: {setting: true},
280+
subClient: {test: true},
281+
navClient: {
282+
validateAddress: () => {
283+
return Promise.reject({message:'mock failure'})
284+
},
285+
test: true
286+
},
287+
outgoingPubKey: {
288+
encrypt: () => {return '00000000'}
289+
},
290+
subAddresses: ['1234'],
291+
transactionsToReturn: [],
292+
successfulSubTransactions: [],
293+
remainingTransactions: ['1234'],
294+
}
295+
ProcessIncoming.runtime = mockRuntime
296+
ProcessIncoming.sentSubToOutgoing(true, {sendOutcome:true,transaction:'1234'})
297+
expect(mockRuntime.subAddresses.length).toBe(0)
298+
expect(mockRuntime.remainingTransactions.length).toBe(0)
299+
expect(mockRuntime.successfulSubTransactions.length).toBe(1)
300+
done()
301+
302+
})
303+
})
304+
describe('makeSubchainTx', (done)=> {
305+
it('should call sendToAddress', (done) => {
306+
const callback = (success, data) => {
307+
}
308+
mockRuntime = {
309+
callback,
310+
currentBatch: [],
311+
settings: {setting: true},
312+
subClient: {test: true},
313+
navClient: {
314+
validateAddress: () => {
315+
return Promise.reject({message:'mock failure'})
316+
},
317+
test: true
318+
},
319+
outgoingPubKey: {
320+
encrypt: () => {return '00000000'}
321+
},
322+
subAddresses: ['1234'],
323+
transactionsToReturn: [],
324+
successfulSubTransactions: [],
325+
remainingTransactions: ['1234'],
326+
}
327+
ProcessIncoming.runtime = mockRuntime
328+
const mockSendToAddress = {
329+
send: sinon.spy()
330+
}
331+
ProcessIncoming.__set__('SendToAddress',mockSendToAddress)
332+
ProcessIncoming.__set__('Logger', mockLogger)
333+
ProcessIncoming.makeSubchainTx('test','test')
334+
sinon.assert.calledOnce(mockSendToAddress.send)
335+
done()
336+
})
337+
})
338+
339+
340+
341+
})
342+

test/ProcessIncoming.spec.test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)