Skip to content

Commit 0baccec

Browse files
committed
fix: improving tests
1 parent e619651 commit 0baccec

11 files changed

+2800
-504
lines changed

__tests__/__fixtures__/http-fixtures.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export default {
445445
selected_as_input: false,
446446
},
447447
{
448-
value: 2,
448+
value: 100,
449449
token_data: 1,
450450
script: "dqkU/qcZVmiK7oEMzDyVX9kwfldkR8CIrA==",
451451
decoded: {
@@ -494,6 +494,7 @@ export default {
494494
token_name: "01",
495495
token_symbol: "01",
496496
tokens: [],
497+
height: 5
497498
},
498499
],
499500
},

__tests__/__fixtures__/ws-fixtures.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,79 @@ export default {
88
type: "dashboard:metrics",
99
time: 1615218985.77608,
1010
},
11+
melt: {
12+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
13+
history: {
14+
tx_id: "00f69d6b861fc70e6b1ed9896e0184c3e8d94b01ae1921cfc42176ce91192fb6",
15+
version: 1,
16+
weight: 8.000001,
17+
timestamp: 1615093636,
18+
is_voided: false,
19+
inputs: [
20+
{
21+
value: 2,
22+
token_data: 129,
23+
script: "dqkUVvf1nM1vemtqwnMKE6SAITXvXFqIrA==",
24+
decoded: {
25+
type: "P2PKH",
26+
address: "WewDeXWyvHP7jJTs7tjLoQfoB72LLxJQqN",
27+
timelock: null,
28+
},
29+
token:
30+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
31+
tx_id:
32+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
33+
index: 3,
34+
},
35+
{
36+
value: 100,
37+
token_data: 1,
38+
script: "dqkUX1XQjI1bgpXys7keNvHcewIVMsyIrA==",
39+
decoded: {
40+
type: "P2PKH",
41+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
42+
timelock: null,
43+
},
44+
token:
45+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
46+
tx_id:
47+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
48+
index: 1,
49+
},
50+
],
51+
outputs: [
52+
{
53+
value: 2,
54+
token_data: 129,
55+
script: "dqkUxwjHrGjBoPy1Zvgc8xpWNEwBe1OIrA==",
56+
decoded: {
57+
type: "P2PKH",
58+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
59+
timelock: null,
60+
},
61+
token:
62+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
63+
spent_by: null,
64+
},
65+
{
66+
value: 1,
67+
token_data: 0,
68+
script: "dqkUDoWv/T10Ovt/nLTx/EsBVTRRvs+IrA==",
69+
decoded: {
70+
type: "P2PKH",
71+
address: "WWbt2ww4W45YLUAumnumZiyWrABYDzCTdN",
72+
timelock: null,
73+
},
74+
token: "00",
75+
spent_by: null,
76+
},
77+
],
78+
parents: [
79+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
80+
"0000035822a76a9dee0ed2c023b5699c97d08755301aa6e6e357f72ebf98053c",
81+
],
82+
height: 6
83+
},
84+
type: "wallet:address_history",
85+
},
1186
};

__tests__/address.test.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
import { wallet as walletUtils } from "@hathor/wallet-lib";
12
import TestUtils from "./test-utils";
23

34
describe("address api", () => {
45
it("should return 200 with a valid body", async () => {
5-
const response = await TestUtils.request
6+
let response = await TestUtils.request
7+
.get("/wallet/address")
8+
.set({ "x-wallet-id": TestUtils.walletId });
9+
expect(response.status).toBe(200);
10+
expect(response.body.address).toBe(TestUtils.addresses[1]);
11+
12+
// Should return the same address for a second call
13+
response = await TestUtils.request
614
.get("/wallet/address")
715
.set({ "x-wallet-id": TestUtils.walletId });
816
expect(response.status).toBe(200);
9-
expect(response.body.address).toBe("WmtWgtk5GxdcDKwjNwmXXn74nQWTPWhKfx");
17+
expect(response.body.address).toBe(TestUtils.addresses[1]);
1018
});
1119

1220
it("should return 200 with a valid body for index = 0", async () => {
@@ -15,7 +23,7 @@ describe("address api", () => {
1523
.query({ index: 0 })
1624
.set({ "x-wallet-id": TestUtils.walletId });
1725
expect(response.status).toBe(200);
18-
expect(response.body.address).toBe("WewDeXWyvHP7jJTs7tjLoQfoB72LLxJQqN");
26+
expect(response.body.address).toBe(TestUtils.addresses[0]);
1927
});
2028

2129
it("should return 200 for a custom index (number or string)", async () => {
@@ -24,25 +32,31 @@ describe("address api", () => {
2432
.query({ index: 2 })
2533
.set({ "x-wallet-id": TestUtils.walletId });
2634
expect(response.status).toBe(200);
27-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
35+
expect(response.body.address).toBe(TestUtils.addresses[2]);
2836

2937
response = await TestUtils.request
3038
.get("/wallet/address")
3139
.query({ index: "2" })
3240
.set({ "x-wallet-id": TestUtils.walletId });
33-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
41+
expect(response.body.address).toBe(TestUtils.addresses[2]);
3442
});
3543

36-
it("should return a new address with mark_as_used", async () => {
37-
let response = await TestUtils.request
38-
.get("/wallet/address")
39-
.query({ mark_as_used: true })
40-
.set({ "x-wallet-id": TestUtils.walletId });
41-
expect(response.status).toBe(200);
44+
it("should return a new address with mark_as_used until the gapLimit is reached", async () => {
45+
const gapLimit = walletUtils.getGapLimit();
46+
for (let index = 1; index <= gapLimit; index++) {
47+
const response = await TestUtils.request
48+
.get("/wallet/address")
49+
.query({ mark_as_used: true })
50+
.set({ "x-wallet-id": TestUtils.walletId });
51+
expect(response.status).toBe(200);
52+
expect(response.body.address).toBe(TestUtils.addresses[index]);
53+
}
4254

43-
response = await TestUtils.request
55+
// Subsequent calls should return the last address as the gap limit was reached
56+
const response = await TestUtils.request
4457
.get("/wallet/address")
4558
.set({ "x-wallet-id": TestUtils.walletId });
46-
expect(response.body.address).toBe("WPynsVhyU6nP7RSZAkqfijEutC88KgAyFc");
59+
expect(response.status).toBe(200);
60+
expect(response.body.address).toBe(TestUtils.addresses[gapLimit]);
4761
});
4862
});

__tests__/addresses.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
import { wallet as walletUtils } from "@hathor/wallet-lib";
12
import TestUtils from "./test-utils";
23

34
describe("addresses api", () => {
45
it("should return 200 with a valid body", async () => {
6+
const gapLimit = walletUtils.getGapLimit();
57
const response = await TestUtils.request
68
.get("/wallet/addresses")
79
.set({ "x-wallet-id": TestUtils.walletId });
810
expect(response.status).toBe(200);
9-
expect(response.body.addresses.length).toBeGreaterThan(20);
11+
expect(response.body.addresses.length).toBe(gapLimit + 1);
12+
expect(response.body.addresses).toEqual(TestUtils.addresses);
13+
});
14+
15+
it("should return all addresses for a custom gap limit", async () => {
16+
walletUtils.setGapLimit(100);
17+
18+
// restart the wallet to use the new gap limit
19+
await TestUtils.stopWallet();
20+
await TestUtils.startWallet();
21+
22+
const gapLimit = 100;
23+
const response = await TestUtils.request
24+
.get("/wallet/addresses")
25+
.set({ "x-wallet-id": TestUtils.walletId });
26+
expect(response.status).toBe(200);
27+
expect(response.body.addresses.length).toBe(gapLimit + 1);
28+
expect(response.body.addresses.slice(0, TestUtils.addresses.length)).toEqual(TestUtils.addresses);
1029
});
1130
});

__tests__/create-token.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestUtils from "./test-utils";
2+
import httpFixtures from "./__fixtures__/http-fixtures";
23

34
describe("create-token api", () => {
45
it("should return 200 with a valid body", async () => {
@@ -11,6 +12,7 @@ describe("create-token api", () => {
1112
})
1213
.set({ "x-wallet-id": TestUtils.walletId });
1314
expect(response.status).toBe(200);
15+
expect(response.body.success).toBeTruthy();
1416
expect(response.body.hash).toBeDefined();
1517
});
1618

@@ -24,6 +26,7 @@ describe("create-token api", () => {
2426
})
2527
.set({ "x-wallet-id": TestUtils.walletId });
2628
expect(response.status).toBe(200);
29+
expect(response.body.success).toBeTruthy();
2730
expect(response.body.hash).toBeDefined();
2831
});
2932

@@ -44,6 +47,19 @@ describe("create-token api", () => {
4447
});
4548
});
4649

50+
it("should not create a token without the required funds to cover it", async () => {
51+
const response = await TestUtils.request
52+
.post("/wallet/create-token")
53+
.send({
54+
name: "stub_token",
55+
symbol: "03",
56+
amount: 10**9,
57+
})
58+
.set({ "x-wallet-id": TestUtils.walletId });
59+
expect(response.status).toBe(200);
60+
expect(response.body.success).toBeFalsy();
61+
});
62+
4763
it("should receive an error when trying to do concurrent create-token (lock/unlock behavior)", async () => {
4864
const promise1 = TestUtils.request
4965
.post("/wallet/create-token")
@@ -68,4 +84,20 @@ describe("create-token api", () => {
6884
expect(response2.status).toBe(200);
6985
expect(response2.body.success).toBeFalsy();
7086
});
87+
88+
// TODO: fix this test case crashing when mocking push_tx with status 400
89+
it.skip("should not create a token with symbol HTR ", async () => {
90+
TestUtils.httpMock.onPost('push_tx').replyOnce(400);
91+
TestUtils.reorderHandlers();
92+
const response = await TestUtils.request
93+
.post("/wallet/create-token")
94+
.send({
95+
name: "Hathor",
96+
symbol: "HTR",
97+
amount: 1,
98+
})
99+
.set({ "x-wallet-id": TestUtils.walletId });
100+
expect(response.status).toBe(200);
101+
expect(response.body.success).toBeFalsy();
102+
});
71103
});

__tests__/melt-tokens.test.js

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import TestUtils from "./test-utils";
2+
import wsFixtures from "./__fixtures__/ws-fixtures";
23

34
// TODO: Add a fixture to melt token 01
45
describe("melt-tokens api", () => {
56
it("should return 200 with a valid body", async () => {
67
const response = await TestUtils.request
78
.post("/wallet/melt-tokens")
89
.send({
9-
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
10+
token:
11+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
1012
amount: 1,
1113
})
1214
.set({ "x-wallet-id": TestUtils.walletId });
@@ -18,7 +20,8 @@ describe("melt-tokens api", () => {
1820
const response = await TestUtils.request
1921
.post("/wallet/melt-tokens")
2022
.send({
21-
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
23+
token:
24+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
2225
amount: "1",
2326
})
2427
.set({ "x-wallet-id": TestUtils.walletId });
@@ -29,7 +32,8 @@ describe("melt-tokens api", () => {
2932
it("should not melt a token without the required parameters", async () => {
3033
["token", "amount"].forEach(async (field) => {
3134
const token = {
32-
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
35+
token:
36+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
3337
amount: 1,
3438
};
3539
delete token[field];
@@ -42,18 +46,88 @@ describe("melt-tokens api", () => {
4246
});
4347
});
4448

49+
it("should not melt less than 1 token", async () => {
50+
const token = {
51+
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
52+
amount: 0,
53+
};
54+
const response = await TestUtils.request
55+
.post("/wallet/melt-tokens")
56+
.send(token)
57+
.set({ "x-wallet-id": TestUtils.walletId });
58+
expect(response.status).toBe(400);
59+
expect(response.body.success).toBeFalsy();
60+
});
61+
62+
it("should not melt less than 1 token", async () => {
63+
const token = {
64+
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
65+
amount: 0,
66+
};
67+
const response = await TestUtils.request
68+
.post("/wallet/melt-tokens")
69+
.send(token)
70+
.set({ "x-wallet-id": TestUtils.walletId });
71+
expect(response.status).toBe(400);
72+
expect(response.body.success).toBeFalsy();
73+
});
74+
75+
it("should not melt more tokens than total amount", async () => {
76+
const token = {
77+
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
78+
amount: 101,
79+
};
80+
const response = await TestUtils.request
81+
.post("/wallet/melt-tokens")
82+
.send(token)
83+
.set({ "x-wallet-id": TestUtils.walletId });
84+
expect(response.status).toBe(200);
85+
expect(response.body.success).toBeFalsy();
86+
});
87+
88+
it.skip("should melt all tokens and return HTR as change", async () => {
89+
const token = {
90+
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
91+
amount: 100,
92+
};
93+
let response = await TestUtils.request
94+
.post("/wallet/melt-tokens")
95+
.send(token)
96+
.set({ "x-wallet-id": TestUtils.walletId });
97+
expect(response.status).toBe(200);
98+
expect(response.body.success).toBeFalsy();
99+
100+
// Mimic the full node wallet:address_history message
101+
TestUtils.socket.send(JSON.stringify(wsFixtures.melt));
102+
await new Promise((resolve) => setTimeout(resolve, 2000));
103+
104+
// Check the balance
105+
response = await TestUtils.request
106+
.get("/wallet/balance")
107+
.query({
108+
token:
109+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
110+
})
111+
.set({ "x-wallet-id": TestUtils.walletId });
112+
expect(response.status).toBe(200);
113+
expect(response.body.available).toBe(0);
114+
expect(response.body.locked).toBe(0);
115+
});
116+
45117
it("should receive an error when trying to do concurrent melt-tokens (lock/unlock behavior)", async () => {
46118
const promise1 = TestUtils.request
47119
.post("/wallet/melt-tokens")
48120
.send({
49-
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
121+
token:
122+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
50123
amount: 1,
51124
})
52125
.set({ "x-wallet-id": TestUtils.walletId });
53126
const promise2 = TestUtils.request
54127
.post("/wallet/melt-tokens")
55128
.send({
56-
token: "00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
129+
token:
130+
"00da712d64e04866c8c9aa8fceca70e80d1693864176b6b443220cf29adab5ed",
57131
amount: 1,
58132
})
59133
.set({ "x-wallet-id": TestUtils.walletId });

0 commit comments

Comments
 (0)