Passed
Push — feat-promise ( 16f386...c54a27 )
by Johan
02:08
created

test/blockfolio-api.js   A

Size

Lines of Code 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
noi 3
c 0
b 0
f 0
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
            it("should fail to initialize with a disposable token", function (done) {
58
                Blockfolio.init("40f027b891222cdf7fe7d7390a29e4bb5c79ea7adbab660c855b2d6c603de2d710c10aebcc4ee76c6da4402457cbfd50", (err) => {
59
                    should.exist(err.message);
60
                    return done();
61
                });
62
            });
63
            // Expand timeout for initialization
64
            this.timeout(5000);
65
            it("should be ok with a working token", function (done) {
66
                try {
67
                    Blockfolio.init(FAKE_TOKEN, (err) => {
68
                        if (err) { return done(err); }
69
70
                        return done();
71
                    });
72
                } catch (e) {
73
                    return done(e);
74
                }
75
            });
76
        });
77
        describe("Tools", function () {
78
            it("should return a random token", function (done) {
79
                const generatedToken = Blockfolio.utils.generateClientToken();
80
                expect(generatedToken).to.be.a("string");
81
                return done();
82
            });
83
            it("should convert properly XRP/BTC to a pair struct", function (done) {
84
                const pair = Blockfolio.utils.parseToken("XRP/BTC");
85
                expect(pair).to.be.deep.equal({base: "BTC", token: "XRP"});
86
                return done();
87
            });
88
            it("should convert properly BTC/USD to a pair struct", function (done) {
89
                const pair = Blockfolio.utils.parseToken("BTC/USD");
90
                expect(pair).to.be.deep.equal({base: "USD", token: "BTC"});
91
                return done();
92
            });
93
            it("should convert properly AEON to a pair struct", function (done) {
94
                const pair = Blockfolio.utils.parseToken("AEON");
95
                expect(pair).to.be.deep.equal({base: "BTC", token: "AEON"});
96
                return done();
97
            });
98
            it("should convert properly BTC-LTC to a pair struct", function (done) {
99
                const pair = Blockfolio.utils.parseToken("BTC-LTC");
100
                expect(pair).to.be.deep.equal({base: "BTC", token: "LTC"});
101
                return done();
102
            });
103
            it("should convert properly BTC-DASH to a pair struct", function (done) {
104
                const pair = Blockfolio.utils.parseToken("BTC-DASH");
105
                expect(pair).to.be.deep.equal({base: "BTC", token: "DASH"});
106
                return done();
107
            });
108
            it("should convert properly BTC-BCH to a pair struct", function (done) {
109
                const pair = Blockfolio.utils.parseToken("BTC-BCH");
110
                expect(pair).to.be.deep.equal({base: "BTC", token: "BCH"});
111
                return done();
112
            });
113
        });
114
        describe("Endpoints", function () {
115
            // Expand timeout for network & API lentency
116
            this.timeout(10000);
117
            it("Get the currencies list", function (done) {
118
                Blockfolio.getCurrencies((err, currencies) => {
119
                    if (err) { return done(err); }
120
                    should.exist(currencies);
121
                    expect(currencies).to.be.an("array");
122
                    return done();
123
                });
124
            });
125
            it("Get the coins list", function (done) {
126
                Blockfolio.getCoinsList((err, coins) => {
127
                    if (err) { return done(err); }
128
                    should.exist(coins);
129
                    expect(coins).to.be.an("array");
130
                    return done();
131
                });
132
            });
133
            it("Get a Disposable Device Token", function (done) {
134
                Blockfolio.getDisposableDeviceToken(token => {
135
                   should.exist(token);
136
                   return done();
137
                });
138
            });
139
            it("Get market details for an AEON/BTC", function (done) {
140
                Blockfolio.getMarketDetails("AEON/BTC", "bittrex", (err, details) => {
141
                    if (err) { return done(err); }
142
143
                    should.exist(details.ask);
144
                    expect(details.ask).to.be.a("string");
145
                    return done();
146
                });
147
            });
148
            it("Get available exchanges for this token", function (done) {
149
                Blockfolio.getExchanges("AEON/BTC", (err, exchanges) => {
150
                    if (err) { return done(err); }
151
152
                    expect(exchanges).to.be.an("array");
153
                    return done();
154
                });
155
            });
156
            it("Get available exchanges for an incorrect token", function (done) {
157
                Blockfolio.getExchanges("ZSKJD/BTC", (err, exchanges) => {
158
                    should.exist(err.message);
159
                    expect(err.message).to.equal("ZSKJD is not an available token on Blockfolio!");
160
                    should.not.exist(exchanges);
161
                    return done();
162
                });
163
            });
164
            it("Add a token pair to watch from Bittrex", function (done) {
165
                Blockfolio.watchCoin("AEON/BTC", "bittrex", (err, res) => {
166
                    if (err) { return done(err); }
167
168
                    return done();
169
                });
170
            });
171
            it("... should works with a promise too", function (done) {
172
                Blockfolio.watchCoin("AEON/BTC", "bittrex").then(() => {
173
                    return done();
174
                }).catch((err) => {
175
                    return done(err);
176
                });
177
            });
178
            it("Get the last price of an incorrect token on this exchange", function (done) {
179
                Blockfolio.getPrice("EAZRREZREZ/BTC", "bittrex", (err, rPrice) => {
180
                    should.exist(err.message);
181
                    expect(err.message).to.equal("EAZRREZREZ is not an available token on Blockfolio!");
182
                    should.not.exist(rPrice);
183
                    return done();
184
                });
185
            });
186
187
188
            it("... and with a valid token, but an incorrect base", function (done) {
189
                Blockfolio.getPrice("BTC/DSQFSDFDSF", "bittrex", (err, nPrice) => {
190
                    should.exist(err.message);
191
                    expect(err.message).to.equal("BTC is not an available in DSQFSDFDSF on Blockfolio!");
192
                    return done();
193
                });
194
            });
195
196
197
            it("Get the last price of AEON on this exchange", function (done) {
198
                Blockfolio.getPrice("AEON/BTC", "bittrex", (err, cPrice) => {
199
                    if (err) { return done(err); }
200
201
                    expect(cPrice).to.be.a("number");
202
                    return done();
203
                });
204
            });
205
            it("Add a BTC position on this pair", function (done) {
206
                Blockfolio.addPosition("AEON/BTC", {
207
                    exchange: "bittrex",
208
                    price: 0.00018,
209
                    amount: 200,
210
                    note: "AEON FTW"
211
                }, (err) => {
212
                    if (err) { return done(err); }
213
214
                    return done();
215
                });
216
            });
217
            it("Get the summary for current position", function (done) {
218
                Blockfolio.getHoldings("AEON/BTC", (err, summary) => {
219
                    if (err) { return done(err); }
220
221
                    should.exist(summary.holdingValueString);
222
                    expect(summary.holdingValueString).to.be.a("string");
223
                    return done();
224
                });
225
            });
226
            it("Get orders details for this position", function (done) {
227
                Blockfolio.getPositions("AEON/BTC", (err, positions) => {
228
                    if (err) { return done(err); }
229
230
                    should.exist(positions);
231
                    expect(positions).to.be.an("array");
232
                    return done();
233
                });
234
            });
235
            it("And then remove the coin from portfolio", function (done) {
236
                Blockfolio.removeCoin("AEON/BTC", (err, res) => {
237
                    if (err) { return done(err); }
238
239
                    expect(res).to.equal("success");
240
                    return done();
241
                });
242
            });
243
            it("Get actual positions", function (done) {
244
                Blockfolio.getPositions((err, positions) => {
245
                    if (err) { return done(err); }
246
247
                    should.exist(positions);
248
                    expect(positions).to.be.an("array");
249
                    return done();
250
                });
251
            });
252
        });
253
    });
254
255
})();