-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-public-endpoints.js
More file actions
257 lines (216 loc) · 8.77 KB
/
test-public-endpoints.js
File metadata and controls
257 lines (216 loc) · 8.77 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const axios = require('axios');
// Configuration
const BASE_URL = 'https://quickshift-9qjun.ondigitalocean.app';
// For local testing, uncomment the line below:
// const BASE_URL = 'http://localhost:3000';
// Test configuration
const TIMEOUT = 10000; // 10 seconds
console.log('🚀 QuickShift API - Public Endpoints Test Suite');
console.log(`🌐 Testing against: ${BASE_URL}`);
console.log('=' * 50);
// Helper function to make requests
const makeRequest = async (method, url, data = null, description = '') => {
try {
const config = {
method,
url: `${BASE_URL}${url}`,
timeout: TIMEOUT,
headers: {
'Content-Type': 'application/json',
}
};
if (data) {
config.data = data;
}
const response = await axios(config);
console.log(`✅ ${method.toUpperCase()} ${url} - ${description}`);
console.log(` Status: ${response.status}`);
if (response.data && typeof response.data === 'object') {
if (response.data.data) {
const dataKeys = Object.keys(response.data.data);
console.log(` Data Keys: ${dataKeys.join(', ')}`);
// Show array length for arrays
dataKeys.forEach(key => {
if (Array.isArray(response.data.data[key])) {
console.log(` ${key}: ${response.data.data[key].length} items`);
}
});
}
if (response.data.message) {
console.log(` Message: ${response.data.message}`);
}
}
console.log('');
return response;
} catch (error) {
console.log(`❌ ${method.toUpperCase()} ${url} - ${description}`);
if (error.response) {
console.log(` Status: ${error.response.status}`);
console.log(` Error: ${error.response.data?.message || error.response.statusText}`);
} else {
console.log(` Error: ${error.message}`);
}
console.log('');
return null;
}
};
// Test functions
const testBasicEndpoints = async () => {
console.log('📝 Testing Basic Endpoints');
console.log('-'.repeat(30));
await makeRequest('GET', '/', null, 'Welcome message');
await makeRequest('GET', '/api/health', null, 'Health check');
await makeRequest('GET', '/api/public/endpoints', null, 'API documentation');
};
const testPublicAPIEndpoints = async () => {
console.log('📊 Testing Public API Endpoints');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/public/stats', null, 'Get API statistics');
await makeRequest('GET', '/api/public/recent-gigs?limit=5', null, 'Get recent gig requests');
await makeRequest('GET', '/api/public/search-gigs?location=remote&limit=3', null, 'Search gigs by location');
await makeRequest('GET', '/api/public/profiles/users?limit=3', null, 'Get user profiles');
await makeRequest('GET', '/api/public/profiles/employers?limit=3', null, 'Get employer profiles');
};
const testUserEndpoints = async () => {
console.log('👤 Testing User Endpoints (Public)');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/users', null, 'Get all users');
await makeRequest('GET', '/api/users/507f1f77bcf86cd799439011', null, 'Get user by ID (test ID)');
};
const testEmployerEndpoints = async () => {
console.log('🏢 Testing Employer Endpoints (Public)');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/employers', null, 'Get all employers');
await makeRequest('GET', '/api/employers/507f1f77bcf86cd799439011', null, 'Get employer by ID (test ID)');
};
const testGigRequestEndpoints = async () => {
console.log('💼 Testing Gig Request Endpoints (Public)');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/gig-requests', null, 'Get all gig requests');
await makeRequest('GET', '/api/gig-requests/507f1f77bcf86cd799439011', null, 'Get gig request by ID (test ID)');
await makeRequest('GET', '/api/gig-requests/user/507f1f77bcf86cd799439011', null, 'Get gig requests by user ID');
await makeRequest('GET', '/api/gig-requests/employer/507f1f77bcf86cd799439011', null, 'Get gig requests by employer ID');
await makeRequest('GET', '/api/gig-requests/instant-apply-eligible', null, 'Get instant apply eligible jobs');
};
const testGigApplicationEndpoints = async () => {
console.log('📝 Testing Gig Application Endpoints (Public)');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/gig-applications/gig/507f1f77bcf86cd799439011', null, 'Get applications by gig ID');
await makeRequest('GET', '/api/gig-applications/user/507f1f77bcf86cd799439011', null, 'Get applications by user ID');
await makeRequest('GET', '/api/gig-applications/507f1f77bcf86cd799439011', null, 'Get application by ID (test ID)');
};
const testGigCompletionEndpoints = async () => {
console.log('✅ Testing Gig Completion Endpoints (Public)');
console.log('-'.repeat(30));
await makeRequest('GET', '/api/gig-completions/worker-account-status/507f1f77bcf86cd799439011', null, 'Get worker account status');
};
const testAuthEndpoints = async () => {
console.log('🔐 Testing Authentication Endpoints (Public)');
console.log('-'.repeat(30));
// Test data for registration (these won't actually register due to validation)
const testUser = {
firstName: 'Test',
lastName: 'User',
email: 'test@example.com',
password: 'TestPassword123'
};
const testEmployer = {
companyName: 'Test Company',
email: 'test@company.com',
password: 'TestPassword123',
firstName: 'Test',
lastName: 'Employer'
};
const loginData = {
email: 'test@example.com',
password: 'TestPassword123',
userType: 'user'
};
await makeRequest('POST', '/api/auth/register/user', testUser, 'Register user (test data)');
await makeRequest('POST', '/api/auth/register/employer', testEmployer, 'Register employer (test data)');
await makeRequest('POST', '/api/auth/login', loginData, 'Login (test credentials)');
await makeRequest('GET', '/api/auth/verify-email/test-token', null, 'Verify email (test token)');
await makeRequest('POST', '/api/auth/forgot-password', { email: 'test@example.com', userType: 'user' }, 'Forgot password');
};
const testCreateEndpoints = async () => {
console.log('🆕 Testing Create Endpoints (Public - Test Data)');
console.log('-'.repeat(30));
// Test user data
const newUser = {
firstName: 'Test',
lastName: 'APIUser',
email: `test+${Date.now()}@example.com`,
password: 'TestPassword123',
phone: '+1234567890',
skills: ['JavaScript', 'Node.js'],
location: 'Remote'
};
// Test employer data
const newEmployer = {
companyName: 'Test API Company',
email: `testcompany+${Date.now()}@example.com`,
password: 'TestPassword123',
firstName: 'Test',
lastName: 'Employer',
phone: '+1234567890',
industry: 'Technology',
location: 'Remote'
};
// Test gig request data
const newGigRequest = {
title: 'Test API Job',
description: 'This is a test job created via API testing',
location: 'Remote',
jobType: 'contract',
payRange: 50,
duration: '1-3 months',
skillsRequired: ['Testing', 'API'],
employer: '507f1f77bcf86cd799439011' // Test employer ID
};
await makeRequest('POST', '/api/users', newUser, 'Create new user');
await makeRequest('POST', '/api/employers', newEmployer, 'Create new employer');
await makeRequest('POST', '/api/gig-requests', newGigRequest, 'Create new gig request');
};
// Main test runner
const runTests = async () => {
const startTime = Date.now();
try {
await testBasicEndpoints();
await testPublicAPIEndpoints();
await testUserEndpoints();
await testEmployerEndpoints();
await testGigRequestEndpoints();
await testGigApplicationEndpoints();
await testGigCompletionEndpoints();
await testAuthEndpoints();
await testCreateEndpoints();
const endTime = Date.now();
const duration = (endTime - startTime) / 1000;
console.log('🎉 Test Suite Completed!');
console.log(`⏱️ Total Duration: ${duration.toFixed(2)} seconds`);
console.log('\n📋 Summary:');
console.log('- All public endpoints have been tested');
console.log('- ✅ indicates successful requests');
console.log('- ❌ indicates failed requests (expected for some test data)');
console.log('\n🔗 Next Steps:');
console.log('1. Use successful endpoints for your application');
console.log('2. Implement authentication for protected endpoints');
console.log('3. Review any failed endpoints for debugging');
} catch (error) {
console.error('💥 Test suite failed:', error.message);
}
};
// Check if axios is available
const checkDependencies = () => {
try {
require('axios');
return true;
} catch (error) {
console.error('❌ axios is required for this test. Install it with: npm install axios');
return false;
}
};
// Run the tests
if (checkDependencies()) {
runTests();
}