Repository navigation
Expand file tree
/
Copy pathtest_api.php
More file actions
184 lines (137 loc) · 5.33 KB
/
Copy pathtest_api.php
File metadata and controls
184 lines (137 loc) · 5.33 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
<?php
/**
* Simple PHP script to test SahelLicenseManager API
*
* Usage: php test_api.php
*
* Make sure to update the $baseUrl variable with your actual API URL
*/
$baseUrl = 'http://localhost:8000/api';
$token = '';
// Helper function to make HTTP requests
function makeRequest($url, $method = 'GET', $data = null, $headers = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
'Content-Type: application/json',
'Accept: application/json'
], $headers));
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status_code' => $httpCode,
'body' => json_decode($response, true)
];
}
// Test functions
function testUserRegistration($baseUrl) {
echo "=== Testing User Registration ===\n";
$data = [
'name' => 'Test Admin',
'email' => 'admin@test.com',
'password' => 'password123',
'password_confirmation' => 'password123'
];
$response = makeRequest($baseUrl . '/register-user', 'POST', $data);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['data']['token'] ?? null;
}
function testUserLogin($baseUrl) {
echo "=== Testing User Login ===\n";
$data = [
'email' => 'admin@test.com',
'password' => 'password123'
];
$response = makeRequest($baseUrl . '/login', 'POST', $data);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['data']['token'] ?? null;
}
function testLicenseRegistration($baseUrl) {
echo "=== Testing License Registration ===\n";
$data = [
'license_key' => 'SAHEL-TEST-1234-5678-9012',
'first_name' => 'John',
'last_name' => 'Doe',
'brand_name' => 'Test Restaurant',
'phone' => '+1234567890',
'os' => 'Windows 11',
'additional_data' => [
'restaurant_id' => 'TEST001',
'location' => 'Test City'
]
];
$response = makeRequest($baseUrl . '/register', 'POST', $data);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['success'] ?? false;
}
function testLicenseActivation($baseUrl) {
echo "=== Testing License Activation ===\n";
$data = [
'license_key' => 'SAHEL-TEST-1234-5678-9012'
];
$response = makeRequest($baseUrl . '/activate', 'POST', $data);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['success'] ?? false;
}
function testLicenseValidation($baseUrl) {
echo "=== Testing License Validation ===\n";
$response = makeRequest($baseUrl . '/validate/SAHEL-TEST-1234-5678-9012');
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['success'] ?? false;
}
function testListLicenses($baseUrl, $token) {
echo "=== Testing List Licenses ===\n";
$headers = ['Authorization: Bearer ' . $token];
$response = makeRequest($baseUrl . '/licenses', 'GET', null, $headers);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['success'] ?? false;
}
function testGetUserInfo($baseUrl, $token) {
echo "=== Testing Get User Info ===\n";
$headers = ['Authorization: Bearer ' . $token];
$response = makeRequest($baseUrl . '/me', 'GET', null, $headers);
echo "Status Code: " . $response['status_code'] . "\n";
echo "Response: " . json_encode($response['body'], JSON_PRETTY_PRINT) . "\n\n";
return $response['body']['success'] ?? false;
}
// Main test execution
echo "SahelLicenseManager API Test Script\n";
echo "====================================\n\n";
// Test 1: User Registration
$token = testUserRegistration($baseUrl);
// Test 2: User Login (if registration failed, try login)
if (!$token) {
$token = testUserLogin($baseUrl);
}
// Test 3: License Registration
testLicenseRegistration($baseUrl);
// Test 4: License Activation
testLicenseActivation($baseUrl);
// Test 5: License Validation
testLicenseValidation($baseUrl);
// Test 6: Get User Info (requires authentication)
if ($token) {
testGetUserInfo($baseUrl, $token);
// Test 7: List Licenses (requires authentication)
testListLicenses($baseUrl, $token);
} else {
echo "Skipping authenticated tests - no token available\n\n";
}
echo "Test completed!\n";
echo "Note: Make sure your Laravel application is running on the specified URL.\n";
echo "You can start the development server with: php artisan serve\n";
?>