| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/tests/ |
Upload File : |
<?php
/**
* Comprehensive API Test Script for RepoRev API
* Tests all endpoints systematically and reports errors
*/
class ApiTester {
private $baseUrl = 'http://localhost:8080';
private $token = null;
private $refreshToken = null;
private $userId = null;
private $companyId = null;
private $reportId = null;
private $sessionId = null;
private $errors = [];
private $passedTests = 0;
private $totalTests = 0;
public function __construct() {
echo "š Starting RepoRev API Test Suite\n";
echo "Base URL: {$this->baseUrl}\n";
echo str_repeat("=", 50) . "\n";
}
/**
* Run all tests
*/
public function runAllTests() {
// Test system endpoints first
$this->testSystemEndpoints();
// Test authentication flow
$this->testAuthenticationFlow();
// Test user management
$this->testUserManagement();
// Test company management
$this->testCompanyManagement();
// Test report management
$this->testReportManagement();
// Test admin endpoints
$this->testAdminEndpoints();
// Summary
$this->printSummary();
}
/**
* Make HTTP request
*/
private function makeRequest($method, $endpoint, $data = null, $headers = []) {
$this->totalTests++;
$url = $this->baseUrl . $endpoint;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => array_merge([
'Content-Type: application/json',
'Accept: application/json'
], $headers),
CURLOPT_TIMEOUT => 30
]);
if ($data && in_array($method, ['POST', 'PUT', 'PATCH'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
$this->logError($endpoint, "CURL Error: $error");
return false;
}
$decoded = json_decode($response, true);
return [
'status_code' => $httpCode,
'body' => $decoded,
'raw' => $response
];
}
/**
* Test with authentication
*/
private function makeAuthRequest($method, $endpoint, $data = null) {
$headers = [];
if ($this->token) {
$headers[] = "Authorization: Bearer {$this->token}";
}
return $this->makeRequest($method, $endpoint, $data, $headers);
}
/**
* Log error
*/
private function logError($endpoint, $message) {
$this->errors[] = "ā $endpoint: $message";
echo "ā FAIL: $endpoint - $message\n";
}
/**
* Log success
*/
private function logSuccess($endpoint, $message = null) {
$this->passedTests++;
$msg = $message ? " - $message" : "";
echo "ā
PASS: $endpoint$msg\n";
}
/**
* Test system endpoints
*/
private function testSystemEndpoints() {
echo "\nš Testing System Endpoints\n" . str_repeat("-", 30) . "\n";
// Health check
$response = $this->makeRequest('GET', '/health');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('/health');
} else {
$this->logError('/health', 'Health check failed');
}
// API info
$response = $this->makeRequest('GET', '/api');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('/api');
} else {
$this->logError('/api', 'API info failed');
}
}
/**
* Test authentication flow
*/
private function testAuthenticationFlow() {
echo "\nš Testing Authentication Flow\n" . str_repeat("-", 30) . "\n";
// Test login with invalid credentials
$response = $this->makeRequest('POST', '/api/v1/auth/login', [
'email' => 'invalid@test.com',
'password' => 'wrongpassword'
]);
if ($response && $response['status_code'] === 401) {
$this->logSuccess('POST /api/v1/auth/login', 'Correctly rejected invalid credentials');
} else {
$this->logError('POST /api/v1/auth/login', 'Should reject invalid credentials with 401');
}
// Test registration
$testUser = [
'email' => 'test_' . time() . '@example.com',
'password' => 'password123',
'first_name' => 'Test',
'last_name' => 'User',
'company_id' => 1,
'phone' => '+1234567890'
];
$response = $this->makeRequest('POST', '/api/v1/auth/register', $testUser);
if ($response && in_array($response['status_code'], [200, 201])) {
$this->logSuccess('POST /api/v1/auth/register');
// Try to login with the new user
$loginResponse = $this->makeRequest('POST', '/api/v1/auth/login', [
'email' => $testUser['email'],
'password' => $testUser['password']
]);
if ($loginResponse && $loginResponse['status_code'] === 200 && isset($loginResponse['body']['data']['access_token'])) {
$this->token = $loginResponse['body']['data']['access_token'];
$this->refreshToken = $loginResponse['body']['data']['refresh_token'] ?? null;
$this->userId = $loginResponse['body']['data']['user']['user_id'] ?? null;
$this->logSuccess('POST /api/v1/auth/login', 'Successfully logged in');
} else {
$this->logError('POST /api/v1/auth/login', 'Login failed after registration');
}
} else {
$this->logError('POST /api/v1/auth/register', 'Registration failed');
}
// Test auth check
if ($this->token) {
$response = $this->makeAuthRequest('GET', '/api/v1/auth/check');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/auth/check');
} else {
$this->logError('GET /api/v1/auth/check', 'Auth check failed');
}
}
// Test refresh token
if ($this->refreshToken) {
$response = $this->makeRequest('POST', '/api/v1/auth/refresh', [
'refresh_token' => $this->refreshToken
]);
if ($response && $response['status_code'] === 200) {
if (isset($response['body']['data']['access_token'])) {
$this->token = $response['body']['data']['access_token'];
}
$this->logSuccess('POST /api/v1/auth/refresh');
} else {
$this->logError('POST /api/v1/auth/refresh', 'Token refresh failed');
}
}
// Test sessions
if ($this->token) {
$response = $this->makeAuthRequest('GET', '/api/v1/auth/sessions');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/auth/sessions');
// Store session ID for later tests
if (isset($response['body']['data'][0]['session_id'])) {
$this->sessionId = $response['body']['data'][0]['session_id'];
}
} else {
$this->logError('GET /api/v1/auth/sessions', 'Get sessions failed');
}
}
}
/**
* Test user management
*/
private function testUserManagement() {
echo "\nš¤ Testing User Management\n" . str_repeat("-", 30) . "\n";
if (!$this->token) {
$this->logError('User Management', 'No authentication token available');
return;
}
// Get profile
$response = $this->makeAuthRequest('GET', '/api/v1/users/profile');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/users/profile');
} else {
$this->logError('GET /api/v1/users/profile', 'Get profile failed');
}
// Update profile
$response = $this->makeAuthRequest('PUT', '/api/v1/users/profile', [
'first_name' => 'Updated Test',
'last_name' => 'User Updated'
]);
if ($response && in_array($response['status_code'], [200, 204])) {
$this->logSuccess('PUT /api/v1/users/profile');
} else {
$this->logError('PUT /api/v1/users/profile', 'Update profile failed');
}
// List users
$response = $this->makeAuthRequest('GET', '/api/v1/users?page=1&limit=5');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/users');
} else {
$this->logError('GET /api/v1/users', 'List users failed');
}
// Get user by ID
if ($this->userId) {
$response = $this->makeAuthRequest('GET', "/api/v1/users/{$this->userId}");
if ($response && $response['status_code'] === 200) {
$this->logSuccess("GET /api/v1/users/{$this->userId}");
} else {
$this->logError("GET /api/v1/users/{$this->userId}", 'Get user by ID failed');
}
}
// Search users
$response = $this->makeAuthRequest('GET', '/api/v1/users/search?q=test');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/users/search');
} else {
$this->logError('GET /api/v1/users/search', 'Search users failed');
}
// Filter users
$response = $this->makeAuthRequest('GET', '/api/v1/users/filter?is_active=true');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/users/filter');
} else {
$this->logError('GET /api/v1/users/filter', 'Filter users failed');
}
}
/**
* Test company management
*/
private function testCompanyManagement() {
echo "\nš¢ Testing Company Management\n" . str_repeat("-", 30) . "\n";
if (!$this->token) {
$this->logError('Company Management', 'No authentication token available');
return;
}
// List companies
$response = $this->makeAuthRequest('GET', '/api/v1/companies?page=1&limit=5');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/companies');
// Store company ID for later tests
if (isset($response['body']['data'][0]['company_id'])) {
$this->companyId = $response['body']['data'][0]['company_id'];
}
} else {
$this->logError('GET /api/v1/companies', 'List companies failed');
}
// Create company
$newCompany = [
'name' => 'Test Company ' . time(),
'domain' => 'test' . time() . '.com',
'description' => 'A test company',
'industry' => 'Technology',
'size' => 'small'
];
$response = $this->makeAuthRequest('POST', '/api/v1/companies', $newCompany);
if ($response && in_array($response['status_code'], [200, 201])) {
$this->logSuccess('POST /api/v1/companies');
if (isset($response['body']['data']['company_id'])) {
$testCompanyId = $response['body']['data']['company_id'];
// Get company by ID
$response = $this->makeAuthRequest('GET', "/api/v1/companies/$testCompanyId");
if ($response && $response['status_code'] === 200) {
$this->logSuccess("GET /api/v1/companies/$testCompanyId");
} else {
$this->logError("GET /api/v1/companies/$testCompanyId", 'Get company by ID failed');
}
// Update company
$response = $this->makeAuthRequest('PUT', "/api/v1/companies/$testCompanyId", [
'name' => 'Updated Test Company',
'description' => 'Updated description'
]);
if ($response && in_array($response['status_code'], [200, 204])) {
$this->logSuccess("PUT /api/v1/companies/$testCompanyId");
} else {
$this->logError("PUT /api/v1/companies/$testCompanyId", 'Update company failed');
}
// Get company users
$response = $this->makeAuthRequest('GET', "/api/v1/companies/$testCompanyId/users");
if ($response && $response['status_code'] === 200) {
$this->logSuccess("GET /api/v1/companies/$testCompanyId/users");
} else {
$this->logError("GET /api/v1/companies/$testCompanyId/users", 'Get company users failed');
}
}
} else {
$this->logError('POST /api/v1/companies', 'Create company failed');
}
}
/**
* Test report management
*/
private function testReportManagement() {
echo "\nš Testing Report Management\n" . str_repeat("-", 30) . "\n";
if (!$this->token) {
$this->logError('Report Management', 'No authentication token available');
return;
}
// List reports
$response = $this->makeAuthRequest('GET', '/api/v1/reports?page=1&limit=5');
if ($response && $response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/reports');
} else {
$this->logError('GET /api/v1/reports', 'List reports failed');
}
// Create report
$newReport = [
'title' => 'Test Report ' . time(),
'description' => 'A test report for API testing',
'report_type' => 'user_activity',
'priority' => 'medium',
'data' => [
'date_range' => [
'start' => '2024-01-01',
'end' => '2024-01-31'
]
]
];
$response = $this->makeAuthRequest('POST', '/api/v1/reports', $newReport);
if ($response && in_array($response['status_code'], [200, 201])) {
$this->logSuccess('POST /api/v1/reports');
if (isset($response['body']['data']['report_id'])) {
$this->reportId = $response['body']['data']['report_id'];
// Get report by ID
$response = $this->makeAuthRequest('GET', "/api/v1/reports/{$this->reportId}");
if ($response && $response['status_code'] === 200) {
$this->logSuccess("GET /api/v1/reports/{$this->reportId}");
} else {
$this->logError("GET /api/v1/reports/{$this->reportId}", 'Get report by ID failed');
}
// Update report
$response = $this->makeAuthRequest('PUT', "/api/v1/reports/{$this->reportId}", [
'title' => 'Updated Test Report',
'status' => 'completed'
]);
if ($response && in_array($response['status_code'], [200, 204])) {
$this->logSuccess("PUT /api/v1/reports/{$this->reportId}");
} else {
$this->logError("PUT /api/v1/reports/{$this->reportId}", 'Update report failed');
}
}
} else {
$this->logError('POST /api/v1/reports', 'Create report failed');
}
// Test generate reports
if ($this->userId) {
$response = $this->makeAuthRequest('POST', "/api/v1/reports/generate/user/{$this->userId}", [
'report_type' => 'user_activity',
'format' => 'pdf'
]);
if ($response && in_array($response['status_code'], [200, 201, 202])) {
$this->logSuccess("POST /api/v1/reports/generate/user/{$this->userId}");
} else {
$this->logError("POST /api/v1/reports/generate/user/{$this->userId}", 'Generate user report failed');
}
}
if ($this->companyId) {
$response = $this->makeAuthRequest('POST', "/api/v1/reports/generate/company/{$this->companyId}", [
'report_type' => 'company_overview',
'format' => 'excel'
]);
if ($response && in_array($response['status_code'], [200, 201, 202])) {
$this->logSuccess("POST /api/v1/reports/generate/company/{$this->companyId}");
} else {
$this->logError("POST /api/v1/reports/generate/company/{$this->companyId}", 'Generate company report failed');
}
}
}
/**
* Test admin endpoints
*/
private function testAdminEndpoints() {
echo "\nāļø Testing Admin Endpoints\n" . str_repeat("-", 30) . "\n";
if (!$this->token) {
$this->logError('Admin Endpoints', 'No authentication token available');
return;
}
// Admin dashboard
$response = $this->makeAuthRequest('GET', '/api/v1/admin/dashboard');
if ($response && in_array($response['status_code'], [200, 403])) {
if ($response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/admin/dashboard');
} else {
$this->logSuccess('GET /api/v1/admin/dashboard', 'Correctly denied access (403)');
}
} else {
$this->logError('GET /api/v1/admin/dashboard', 'Admin dashboard test failed');
}
// System overview
$response = $this->makeAuthRequest('GET', '/api/v1/admin/overview');
if ($response && in_array($response['status_code'], [200, 403])) {
if ($response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/admin/overview');
} else {
$this->logSuccess('GET /api/v1/admin/overview', 'Correctly denied access (403)');
}
} else {
$this->logError('GET /api/v1/admin/overview', 'System overview test failed');
}
// System info
$response = $this->makeAuthRequest('GET', '/api/v1/admin/system/info');
if ($response && in_array($response['status_code'], [200, 403])) {
if ($response['status_code'] === 200) {
$this->logSuccess('GET /api/v1/admin/system/info');
} else {
$this->logSuccess('GET /api/v1/admin/system/info', 'Correctly denied access (403)');
}
} else {
$this->logError('GET /api/v1/admin/system/info', 'System info test failed');
}
}
/**
* Print test summary
*/
private function printSummary() {
echo "\n" . str_repeat("=", 50) . "\n";
echo "š TEST SUMMARY\n";
echo str_repeat("=", 50) . "\n";
echo "ā
Passed: {$this->passedTests}/{$this->totalTests}\n";
echo "ā Failed: " . count($this->errors) . "/{$this->totalTests}\n";
echo "š Success Rate: " . round(($this->passedTests / $this->totalTests) * 100, 2) . "%\n";
if (!empty($this->errors)) {
echo "\nā FAILED TESTS:\n";
echo str_repeat("-", 30) . "\n";
foreach ($this->errors as $error) {
echo "$error\n";
}
}
echo "\nš Test completed!\n";
}
}
// Run the tests
$tester = new ApiTester();
$tester->runAllTests();