Repository navigation
Expand file tree
/
Copy pathverify.js
More file actions
176 lines (163 loc) Β· 5.4 KB
/
Copy pathverify.js
File metadata and controls
176 lines (163 loc) Β· 5.4 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
const BASE_URL = 'http://localhost:5000/api/v1';
async function runTests() {
console.log('π Starting API Verification Tests...\n');
try {
const healthRes = await fetch('http://localhost:5000/health');
const healthData = await healthRes.json();
console.log('β
Health Check:', healthData.message);
} catch (err) {
console.error('β Could not connect to server. Make sure it is running on port 5000.');
console.error(err.message);
process.exit(1);
}
const testEmail = `user_${Date.now()}@example.com`;
const testPassword = 'Password123!';
let token;
let taskId;
try {
console.log(`\n1. Registering user with email: ${testEmail}...`);
const registerRes = await fetch(`${BASE_URL}/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Test User',
email: testEmail,
password: testPassword
})
});
const registerData = await registerRes.json();
if (registerRes.ok && registerData.token) {
console.log('β
Register success!');
token = registerData.token;
} else {
console.error('β Register failed:', registerData);
process.exit(1);
}
} catch (err) {
console.error('β Register error:', err);
process.exit(1);
}
try {
console.log('\n2. Logging in...');
const loginRes = await fetch(`${BASE_URL}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: testEmail,
password: testPassword
})
});
const loginData = await loginRes.json();
if (loginRes.ok && loginData.token) {
console.log('β
Login success! JWT Token received.');
token = loginData.token;
} else {
console.error('β Login failed:', loginData);
process.exit(1);
}
} catch (err) {
console.error('β Login error:', err);
process.exit(1);
}
try {
console.log('\n3. Fetching User Profile...');
const profileRes = await fetch(`${BASE_URL}/auth/me`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const profileData = await profileRes.json();
if (profileRes.ok) {
console.log(`β
Profile retrieved. Hello, ${profileData.data.user.name}!`);
} else {
console.error('β Profile check failed:', profileData);
}
} catch (err) {
console.error('β Profile check error:', err);
}
try {
console.log('\n4. Creating a task...');
const taskRes = await fetch(`${BASE_URL}/tasks`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
title: 'Learn MVC Architecture',
description: 'Understand controllers, models, routes, and error handlers',
priority: 'high'
})
});
const taskData = await taskRes.json();
if (taskRes.ok) {
taskId = taskData.data.task._id;
console.log(`β
Task created successfully. ID: ${taskId}`);
} else {
console.error('β Task creation failed:', taskData);
process.exit(1);
}
} catch (err) {
console.error('β Task creation error:', err);
process.exit(1);
}
try {
console.log('\n5. Getting all user tasks...');
const tasksRes = await fetch(`${BASE_URL}/tasks`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const tasksData = await tasksRes.json();
if (tasksRes.ok) {
console.log(`β
Retrieved ${tasksData.results} tasks. (Total: ${tasksData.total})`);
console.log(' Task title:', tasksData.data.tasks[0]?.title);
} else {
console.error('β Fetching tasks failed:', tasksData);
}
} catch (err) {
console.error('β Fetching tasks error:', err);
}
try {
console.log(`\n6. Updating task status to completed (ID: ${taskId})...`);
const updateRes = await fetch(`${BASE_URL}/tasks/${taskId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
status: 'completed'
})
});
const updateData = await updateRes.json();
if (updateRes.ok) {
console.log('β
Task updated. New Status:', updateData.data.task.status);
} else {
console.error('β Task update failed:', updateData);
}
} catch (err) {
console.error('β Task update error:', err);
}
try {
console.log(`\n7. Deleting task (ID: ${taskId})...`);
const deleteRes = await fetch(`${BASE_URL}/tasks/${taskId}`, {
method: 'DELETE',
headers: { 'Authorization': `Bearer ${token}` }
});
if (deleteRes.status === 204) {
console.log('β
Task deleted successfully.');
} else {
const deleteData = await deleteRes.json().catch(() => ({}));
console.error('β Task deletion failed. Status:', deleteRes.status, deleteData);
}
} catch (err) {
console.error('β Task deletion error:', err);
}
try {
console.log('\n8. Verification of Error Middleware (non-existing route)...');
const invalidRes = await fetch(`http://localhost:5000/invalid-url-path`);
const invalidData = await invalidRes.json();
console.log(`β
Global Error handler intercepted 404: "${invalidData.message}"`);
} catch (err) {
console.error('β Error test failed:', err.message);
}
console.log('\nπ API verification completed successfully!');
}
runTests();