-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.js
More file actions
157 lines (138 loc) · 5.39 KB
/
Copy pathtest_api.js
File metadata and controls
157 lines (138 loc) · 5.39 KB
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
const API_URL = "http://104.214.170.158/api/v1";
async function runTests() {
console.log("🚀 Starting Automated API Tests...\n");
try {
// 1. Create User A (Sender)
console.log("1️⃣ Registering User A...");
const userA_res = await fetch(`${API_URL}/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: `sender_${Date.now()}@test.com`,
password: "password123",
fullName: "Sender Tester",
phone: "01700000001"
})
});
console.log(` Status: ${userA_res.status}`);
if (userA_res.status === 500) {
console.log(` ❌ ERROR: ${await userA_res.clone().text()}`);
}
// 2. Login User A
console.log("2️⃣ Logging in User A...");
const loginA_res = await fetch(`${API_URL}/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: (await userA_res.clone().json()).data.user.email,
password: "password123"
})
});
const tokenA = (await loginA_res.json()).data.token;
console.log(` Token A received: ✅`);
// 3. Get User A Wallet
console.log("3️⃣ Fetching User A Wallet...");
let walletA_res = await fetch(`${API_URL}/wallets`, {
method: "GET",
headers: { "Authorization": `Bearer ${tokenA}` }
});
let walletA_data = (await walletA_res.json()).data;
// If no wallet exists yet, create one
if (!walletA_data || walletA_data.length === 0) {
console.log(" (Creating wallet for User A...)");
await fetch(`${API_URL}/wallets`, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${tokenA}` },
body: JSON.stringify({ currency: "USD" })
});
walletA_res = await fetch(`${API_URL}/wallets`, { method: "GET", headers: { "Authorization": `Bearer ${tokenA}` } });
walletA_data = (await walletA_res.json()).data;
}
const walletA = walletA_data[0];
console.log(` Wallet A ID: ${walletA.id}`);
console.log("\n-----------------------------------\n");
// 4. Create User B (Receiver)
console.log("4️⃣ Registering User B...");
const userB_res = await fetch(`${API_URL}/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: `receiver_${Date.now()}@test.com`,
password: "password123",
fullName: "Receiver Tester",
phone: `01700${Math.floor(Math.random() * 100000)}`
})
});
console.log(` Status: ${userB_res.status}`);
// 5. Login User B
console.log("5️⃣ Logging in User B...");
const loginB_res = await fetch(`${API_URL}/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: (await userB_res.clone().json()).data.user.email,
password: "password123"
})
});
const tokenB = (await loginB_res.json()).data.token;
console.log(` Token B received: ✅`);
// 6. Get User B Wallet
console.log("6️⃣ Fetching User B Wallet...");
let walletB_res = await fetch(`${API_URL}/wallets`, {
method: "GET",
headers: { "Authorization": `Bearer ${tokenB}` }
});
let walletB_data = (await walletB_res.json()).data;
// If no wallet exists yet, create one
if (!walletB_data || walletB_data.length === 0) {
console.log(" (Creating wallet for User B...)");
await fetch(`${API_URL}/wallets`, {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${tokenB}` },
body: JSON.stringify({ currency: "USD" })
});
walletB_res = await fetch(`${API_URL}/wallets`, { method: "GET", headers: { "Authorization": `Bearer ${tokenB}` } });
walletB_data = (await walletB_res.json()).data;
}
const walletB = walletB_data[0];
console.log(` Wallet B Number: ${walletB.wallet_number}`);
console.log("\n-----------------------------------\n");
// 6.5 Top Up User A Wallet
console.log("💰 Topping up User A with $100...");
await fetch(`${API_URL}/wallets/${walletA.id}/top-up`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${tokenA}`
},
body: JSON.stringify({ amount: 100.00, provider: "mock" })
});
console.log(` Top-up successful! ✅`);
console.log("\n-----------------------------------\n");
// 7. Send Money! (From A to B)
console.log("💸 Sending $50 from User A to User B...");
const transfer_res = await fetch(`${API_URL}/transfers`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${tokenA}`
},
body: JSON.stringify({
fromWalletId: walletA.id,
toWalletNumber: walletB.wallet_number,
amount: 50.00,
note: "Automated test transfer!"
})
});
const transferData = await transfer_res.json();
console.log(` Transfer Status: ${transfer_res.status}`);
if(transfer_res.status === 201) {
console.log(` 🎉 SUCCESS! User A Balance is now: $${transferData.data.fromBalance}`);
} else {
console.log(" ❌ FAILED:", transferData);
}
} catch (err) {
console.error("Test script crashed:", err);
}
}
runTests();