-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.js
More file actions
202 lines (174 loc) Β· 7.42 KB
/
Copy pathtest_api.js
File metadata and controls
202 lines (174 loc) Β· 7.42 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const { execSync } = require('child_process');
const API_URL = "http://localhost:3000/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);
}
console.log("\n-----------------------------------\n");
// 8. Auth Tests
console.log("π‘οΈ Testing Authentication & Authorization...");
// Missing Token
const missingTokenRes = await fetch(`${API_URL}/auth/users`);
console.log(` Missing Token: ${missingTokenRes.status === 401 ? 'β
' : 'β'}`);
// Invalid Token
const invalidTokenRes = await fetch(`${API_URL}/auth/users`, { headers: { "Authorization": "Bearer fake_token" } });
console.log(` Invalid Token: ${invalidTokenRes.status === 401 ? 'β
' : 'β'}`);
// RBAC: Standard User attempting to access Admin endpoint
const rbacRes = await fetch(`${API_URL}/auth/users`, { headers: { "Authorization": `Bearer ${tokenA}` } });
console.log(` Standard User Access (Expect 403): ${rbacRes.status === 403 ? 'β
' : 'β'}`);
// Create Admin User
const adminEmail = `admin_${Date.now()}@test.com`;
await fetch(`${API_URL}/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: adminEmail, password: "password123", fullName: "Admin Tester" })
});
// Promote to Admin via DB
try {
execSync(`docker exec paynext-db psql -U postgres -d paynext_db -c "UPDATE users SET role = 'admin' WHERE email = '${adminEmail}';"`);
} catch(e) {
console.log(" (Skipping DB promote, docker not available locally)");
}
// Login as Admin
const loginAdmin_res = await fetch(`${API_URL}/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: adminEmail, password: "password123" })
});
const tokenAdmin = (await loginAdmin_res.json()).data.token;
// RBAC: Admin User attempting to access Admin endpoint
const adminAccessRes = await fetch(`${API_URL}/auth/users`, { headers: { "Authorization": `Bearer ${tokenAdmin}` } });
console.log(` Admin Access (Expect 200): ${adminAccessRes.status === 200 ? 'β
' : 'β'}`);
} catch (err) {
console.error("Test script crashed:", err);
}
}
runTests();