1
|
|
|
/*jslint node: true, nomen: true, regexp: true, plusplus: true */ |
2
|
|
|
(function () { |
3
|
|
|
|
4
|
|
|
'use strict'; |
|
|
|
|
5
|
|
|
|
6
|
|
|
const chai = require("chai"), |
7
|
|
|
should = chai.should(), |
8
|
|
|
expect = chai.expect; |
9
|
|
|
|
10
|
|
|
const Blockfolio = require("../index"); |
11
|
|
|
|
12
|
|
|
const FAKE_TOKEN = "1915f3d2ef313e86"; |
13
|
|
|
|
14
|
|
|
describe("Blockfolio API", function() { |
15
|
|
|
describe("General", function () { |
16
|
|
|
it("Get the API version", function (done) { |
17
|
|
|
Blockfolio.getVersion((err, version) => { |
18
|
|
|
if (err) { return done(err); } |
19
|
|
|
should.exist(version); |
20
|
|
|
expect(version).to.be.a("number"); |
21
|
|
|
return done(); |
22
|
|
|
}); |
23
|
|
|
}); |
24
|
|
|
it("Get the system status of the API", function (done) { |
25
|
|
|
Blockfolio.getStatus((err, statusMsg) => { |
26
|
|
|
if (err) { return done(err); } |
27
|
|
|
should.exist(statusMsg); |
28
|
|
|
expect(statusMsg).to.be.a("string"); |
29
|
|
|
return done(); |
30
|
|
|
}); |
31
|
|
|
}); |
32
|
|
|
it("Get the announcements from SIGNAL", function (done) { |
33
|
|
|
Blockfolio.getAnnouncements((err, announcements) => { |
34
|
|
|
if (err) { return done(err); } |
35
|
|
|
should.exist(announcements); |
36
|
|
|
expect(announcements).to.be.an("array"); |
37
|
|
|
return done(); |
38
|
|
|
}); |
39
|
|
|
}); |
40
|
|
|
it("should fail at registering an already activated DEVICE_TOKEN", function (done) { |
41
|
|
|
Blockfolio._register(FAKE_TOKEN, (err, response) => { |
42
|
|
|
should.exist(err.message); |
43
|
|
|
should.not.exist(response); |
44
|
|
|
return done(); |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
}); |
48
|
|
|
describe("Module Instanciation", function () { |
49
|
|
|
it("a protected method called without it should return an error", function (done) { |
50
|
|
|
Blockfolio.getPositions("BTC/USD", (err, positions) => { |
51
|
|
|
should.exist(err.message); |
52
|
|
|
expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)"); |
53
|
|
|
should.not.exist(positions); |
54
|
|
|
return done(); |
55
|
|
|
}); |
56
|
|
|
}); |
57
|
|
|
// Expand timeout for initialization |
58
|
|
|
this.timeout(5000); |
59
|
|
|
it("should be ok with a working token", function (done) { |
60
|
|
|
try { |
61
|
|
|
Blockfolio.init(FAKE_TOKEN, (err) => { |
|
|
|
|
62
|
|
|
if (err) { return done(err); } |
63
|
|
|
|
64
|
|
|
return done(); |
65
|
|
|
}); |
66
|
|
|
} catch (e) { |
67
|
|
|
return done(e); |
68
|
|
|
} |
69
|
|
|
}); |
70
|
|
|
}); |
71
|
|
|
describe("Tools", function () { |
72
|
|
|
it("should return a random token", function (done) { |
73
|
|
|
const generatedToken = Blockfolio.utils.generateClientToken(); |
74
|
|
|
expect(generatedToken).to.be.a("string"); |
75
|
|
|
return done(); |
76
|
|
|
}); |
77
|
|
|
it("should convert properly XRP/BTC to a pair struct", function (done) { |
78
|
|
|
const pair = Blockfolio.utils.parseToken("XRP/BTC"); |
79
|
|
|
expect(pair).to.be.deep.equal({base: "BTC", token: "XRP"}); |
80
|
|
|
return done(); |
81
|
|
|
}); |
82
|
|
|
it("should convert properly BTC/USD to a pair struct", function (done) { |
83
|
|
|
const pair = Blockfolio.utils.parseToken("BTC/USD"); |
84
|
|
|
expect(pair).to.be.deep.equal({base: "USD", token: "BTC"}); |
85
|
|
|
return done(); |
86
|
|
|
}); |
87
|
|
|
it("should convert properly AEON to a pair struct", function (done) { |
88
|
|
|
const pair = Blockfolio.utils.parseToken("AEON"); |
89
|
|
|
expect(pair).to.be.deep.equal({base: "BTC", token: "AEON"}); |
90
|
|
|
return done(); |
91
|
|
|
}); |
92
|
|
|
it("should convert properly BTC-LTC to a pair struct", function (done) { |
93
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-LTC"); |
94
|
|
|
expect(pair).to.be.deep.equal({base: "BTC", token: "LTC"}); |
95
|
|
|
return done(); |
96
|
|
|
}); |
97
|
|
|
it("should convert properly BTC-DASH to a pair struct", function (done) { |
98
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-DASH"); |
99
|
|
|
expect(pair).to.be.deep.equal({base: "BTC", token: "DASH"}); |
100
|
|
|
return done(); |
101
|
|
|
}); |
102
|
|
|
it("should convert properly BTC-BCH to a pair struct", function (done) { |
103
|
|
|
const pair = Blockfolio.utils.parseToken("BTC-BCH"); |
104
|
|
|
expect(pair).to.be.deep.equal({base: "BTC", token: "BCH"}); |
105
|
|
|
return done(); |
106
|
|
|
}); |
107
|
|
|
}); |
108
|
|
|
describe("Endpoints", function () { |
109
|
|
|
// Expand timeout for network & API lentency |
110
|
|
|
this.timeout(10000); |
111
|
|
|
it("Get the currencies list", function (done) { |
112
|
|
|
Blockfolio.getCurrencies((err, currencies) => { |
113
|
|
|
if (err) { return done(err); } |
114
|
|
|
should.exist(currencies); |
115
|
|
|
expect(currencies).to.be.an("array"); |
116
|
|
|
return done(); |
117
|
|
|
}); |
118
|
|
|
}); |
119
|
|
|
it("Get the coins list", function (done) { |
120
|
|
|
Blockfolio.getCoinsList((err, coins) => { |
121
|
|
|
if (err) { return done(err); } |
122
|
|
|
should.exist(coins); |
123
|
|
|
expect(coins).to.be.an("array"); |
124
|
|
|
return done(); |
125
|
|
|
}); |
126
|
|
|
}); |
127
|
|
|
it("Get a Disposable Device Token", function (done) { |
128
|
|
|
Blockfolio.getDisposableDeviceToken(token => { |
129
|
|
|
should.exist(token); |
130
|
|
|
return done(); |
131
|
|
|
}); |
132
|
|
|
}); |
133
|
|
|
it("Get market details for an AEON/BTC", function (done) { |
134
|
|
|
Blockfolio.getMarketDetails("AEON/BTC", "bittrex", (err, details) => { |
135
|
|
|
if (err) { return done(err); } |
136
|
|
|
|
137
|
|
|
should.exist(details.ask); |
138
|
|
|
expect(details.ask).to.be.a("string"); |
139
|
|
|
return done(); |
140
|
|
|
}); |
141
|
|
|
}); |
142
|
|
|
it("Get available exchanges for this token", function (done) { |
143
|
|
|
Blockfolio.getExchanges("AEON/BTC", (err, exchanges) => { |
144
|
|
|
if (err) { return done(err); } |
145
|
|
|
|
146
|
|
|
expect(exchanges).to.be.an("array"); |
147
|
|
|
return done(); |
148
|
|
|
}); |
149
|
|
|
}); |
150
|
|
|
it("Get available exchanges for an incorrect token", function (done) { |
151
|
|
|
Blockfolio.getExchanges("ZSKJD/BTC", (err, exchanges) => { |
152
|
|
|
should.exist(err.message); |
153
|
|
|
expect(err.message).to.equal("ZSKJD is not an available token on Blockfolio!"); |
154
|
|
|
should.not.exist(exchanges); |
155
|
|
|
return done(); |
156
|
|
|
}); |
157
|
|
|
}); |
158
|
|
|
it("Add a token pair to watch from Bittrex", function (done) { |
159
|
|
|
Blockfolio.watchCoin("AEON/BTC", "bittrex", (err, res) => { |
160
|
|
|
if (err) { return done(err); } |
161
|
|
|
|
162
|
|
|
expect(res).to.equal("success"); |
163
|
|
|
return done(); |
164
|
|
|
}); |
165
|
|
|
}); |
166
|
|
|
it("Get the last price of an incorrect token on this exchange", function (done) { |
167
|
|
|
Blockfolio.getPrice("EAZRREZREZ/BTC", "bittrex", (err, rPrice) => { |
168
|
|
|
should.exist(err.message); |
169
|
|
|
expect(err.message).to.equal("EAZRREZREZ is not an available token on Blockfolio!"); |
170
|
|
|
should.not.exist(rPrice); |
171
|
|
|
return done(); |
172
|
|
|
}); |
173
|
|
|
}); |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
it("... and with a valid token, but an incorrect base", function (done) { |
177
|
|
|
Blockfolio.getPrice("BTC/DSQFSDFDSF", "bittrex", (err, nPrice) => { |
|
|
|
|
178
|
|
|
should.exist(err.message); |
179
|
|
|
expect(err.message).to.equal("BTC is not an available in DSQFSDFDSF on Blockfolio!"); |
180
|
|
|
return done(); |
181
|
|
|
}); |
182
|
|
|
}); |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
it("Get the last price of AEON on this exchange", function (done) { |
186
|
|
|
Blockfolio.getPrice("AEON/BTC", "bittrex", (err, cPrice) => { |
187
|
|
|
if (err) { return done(err); } |
188
|
|
|
|
189
|
|
|
expect(cPrice).to.be.a("number"); |
190
|
|
|
return done(); |
191
|
|
|
}); |
192
|
|
|
}); |
193
|
|
|
it("Add a BTC position on this pair", function (done) { |
194
|
|
|
Blockfolio.addPosition(true, "AEON/BTC", "bittrex", 0.00018, 200, "AEON FTW", (err, res) => { |
195
|
|
|
if (err) { return done(err); } |
196
|
|
|
|
197
|
|
|
expect(res).to.equal("success"); |
198
|
|
|
return done(); |
199
|
|
|
}); |
200
|
|
|
}); |
201
|
|
|
it("Get the summary for current position", function (done) { |
202
|
|
|
Blockfolio.getHoldings("AEON/BTC", (err, summary) => { |
203
|
|
|
if (err) { return done(err); } |
204
|
|
|
|
205
|
|
|
should.exist(summary.holdingValueString); |
206
|
|
|
expect(summary.holdingValueString).to.be.a("string"); |
207
|
|
|
return done(); |
208
|
|
|
}); |
209
|
|
|
}); |
210
|
|
|
it("Get orders details for this position", function (done) { |
211
|
|
|
Blockfolio.getPositions("AEON/BTC", (err, positions) => { |
212
|
|
|
if (err) { return done(err); } |
213
|
|
|
|
214
|
|
|
should.exist(positions); |
215
|
|
|
expect(positions).to.be.an("array"); |
216
|
|
|
return done(); |
217
|
|
|
}); |
218
|
|
|
}); |
219
|
|
|
it("And then remove the coin from portfolio", function (done) { |
220
|
|
|
Blockfolio.removeCoin("AEON/BTC", (err, res) => { |
221
|
|
|
if (err) { return done(err); } |
222
|
|
|
|
223
|
|
|
expect(res).to.equal("success"); |
224
|
|
|
return done(); |
225
|
|
|
}); |
226
|
|
|
}); |
227
|
|
|
it("Get actual positions", function (done) { |
228
|
|
|
Blockfolio.getPositions((err, positions) => { |
229
|
|
|
if (err) { return done(err); } |
230
|
|
|
|
231
|
|
|
should.exist(positions); |
232
|
|
|
expect(positions).to.be.an("array"); |
233
|
|
|
return done(); |
234
|
|
|
}); |
235
|
|
|
}); |
236
|
|
|
}); |
237
|
|
|
}); |
238
|
|
|
|
239
|
|
|
})(); |