-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-app.mjs
More file actions
231 lines (194 loc) Β· 7.75 KB
/
test-app.mjs
File metadata and controls
231 lines (194 loc) Β· 7.75 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Comprehensive Application Test Suite
* Tests all endpoints and features
*/
import { createClient } from '@supabase/supabase-js';
const API_URL = 'http://localhost:3001/api/v1';
const supabaseUrl = 'https://arxsyeioxxjrukonnzwm.supabase.co';
const supabaseKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFyeHN5ZWlveHhqcnVrb25uendtIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc2Nzc1NjA3NCwiZXhwIjoyMDgzMzMyMDc0fQ.y5z5u4L-wwy6wpvisf3KEFqAMDDmR8Bls5dpIHOUYcM';
const supabase = createClient(supabaseUrl, supabaseKey);
const results = {
passed: [],
failed: [],
warnings: []
};
function log(type, category, message) {
const emoji = type === 'pass' ? 'β
' : type === 'fail' ? 'β' : 'β οΈ';
console.log(`${emoji} [${category}] ${message}`);
if (type === 'pass') results.passed.push({ category, message });
else if (type === 'fail') results.failed.push({ category, message });
else results.warnings.push({ category, message });
}
async function testDatabaseConnection() {
console.log('\nπ Testing Database Connection...\n');
try {
const { data, error } = await supabase.from('users').select('count').limit(1);
if (error) throw error;
log('pass', 'Database', 'Supabase connection successful');
} catch (error) {
log('fail', 'Database', `Connection failed: ${error.message}`);
}
}
async function testAuthEndpoints() {
console.log('\nπ Testing Authentication Endpoints...\n');
// Test 1: Login endpoint exists
try {
const response = await fetch(`${API_URL}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'cb.sc.u4cse23209@cb.students.amrita.edu',
password: 'Admin@123'
})
});
if (response.ok) {
const data = await response.json();
if (data.data && data.data.requiresOtp !== undefined) {
log('pass', 'Auth', 'Login endpoint working - MFA initiated');
} else {
log('fail', 'Auth', 'Login response format incorrect');
}
} else if (response.status === 401) {
log('pass', 'Auth', 'Login endpoint accessible (invalid credentials)');
} else {
log('fail', 'Auth', `Login endpoint error: ${response.status}`);
}
} catch (error) {
log('fail', 'Auth', `Login endpoint unreachable: ${error.message}`);
}
// Test 2: Register endpoint
try {
const response = await fetch(`${API_URL}/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'test@example.com',
password: 'Test@123',
firstName: 'Test',
lastName: 'User',
role: 'STUDENT'
})
});
if (response.status === 201 || response.status === 400 || response.status === 409) {
log('pass', 'Auth', 'Register endpoint accessible');
} else {
log('fail', 'Auth', `Register endpoint error: ${response.status}`);
}
} catch (error) {
log('fail', 'Auth', `Register endpoint unreachable: ${error.message}`);
}
}
async function testRoomEndpoints() {
console.log('\nπ Testing Room Endpoints...\n');
try {
const response = await fetch(`${API_URL}/rooms`);
if (response.ok) {
const data = await response.json();
if (Array.isArray(data.data)) {
log('pass', 'Rooms', `GET /rooms working - ${data.data.length} rooms found`);
} else {
log('fail', 'Rooms', 'Invalid response format');
}
} else {
log('fail', 'Rooms', `GET /rooms failed: ${response.status}`);
}
} catch (error) {
log('fail', 'Rooms', `Rooms endpoint unreachable: ${error.message}`);
}
}
async function testBookingEndpoints() {
console.log('\nπ Testing Booking Endpoints...\n');
try {
const response = await fetch(`${API_URL}/bookings`, {
headers: {
'Authorization': 'Bearer dummy-token-for-structure-test'
}
});
if (response.status === 401) {
log('pass', 'Bookings', 'GET /bookings requires auth (as expected)');
} else if (response.ok) {
log('pass', 'Bookings', 'GET /bookings accessible');
} else {
log('warn', 'Bookings', `Unexpected status: ${response.status}`);
}
} catch (error) {
log('fail', 'Bookings', `Bookings endpoint unreachable: ${error.message}`);
}
}
async function checkServerHealth() {
console.log('\nπ Checking Server Health...\n');
try {
const response = await fetch(`${API_URL.replace('/api/v1', '')}/health`);
if (response.ok) {
log('pass', 'Server', 'Health check endpoint working');
} else {
log('warn', 'Server', 'No health check endpoint found');
}
} catch (error) {
log('fail', 'Server', `Server unreachable: ${error.message}`);
}
}
async function verifyDatabaseData() {
console.log('\nπ Verifying Database Data...\n');
try {
// Check users
const { data: users, error: usersError } = await supabase
.from('users')
.select('id, email, role')
.limit(5);
if (usersError) throw usersError;
log('pass', 'Data', `Found ${users.length} users in database`);
// Check rooms
const { data: rooms, error: roomsError } = await supabase
.from('rooms')
.select('id, name, capacity')
.limit(5);
if (roomsError) throw roomsError;
log('pass', 'Data', `Found ${rooms.length} rooms in database`);
// Check if admin email is updated
const { data: admin } = await supabase
.from('users')
.select('email')
.eq('role', 'ADMIN')
.single();
if (admin && admin.email === 'cb.sc.u4cse23209@cb.students.amrita.edu') {
log('pass', 'Data', 'Admin email correctly updated');
} else {
log('warn', 'Data', 'Admin email may not be updated');
}
} catch (error) {
log('fail', 'Data', `Database query failed: ${error.message}`);
}
}
async function runAllTests() {
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(' CAMPUS RESOURCE ENGINE - COMPREHENSIVE TEST');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
await testDatabaseConnection();
await testAuthEndpoints();
await testRoomEndpoints();
await testBookingEndpoints();
await checkServerHealth();
await verifyDatabaseData();
console.log('\nβββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(' TEST SUMMARY');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
console.log(`β
Passed: ${results.passed.length}`);
console.log(`β Failed: ${results.failed.length}`);
console.log(`β οΈ Warnings: ${results.warnings.length}`);
if (results.failed.length > 0) {
console.log('\nβ FAILURES:\n');
results.failed.forEach(r => console.log(` - [${r.category}] ${r.message}`));
}
if (results.warnings.length > 0) {
console.log('\nβ οΈ WARNINGS:\n');
results.warnings.forEach(r => console.log(` - [${r.category}] ${r.message}`));
}
console.log('\nβββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
if (results.failed.length === 0) {
console.log('π ALL CRITICAL TESTS PASSED!\n');
} else {
console.log('β οΈ SOME TESTS FAILED - REVIEW REQUIRED\n');
}
}
runAllTests();