Files
MediaCollectorLibary/test_auth.php
Lars Behrends 929ee43001 first commit
2025-10-17 13:29:28 +02:00

71 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/vendor/autoload.php';
// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Load database configuration
$dbConfig = require __DIR__ . '/config/database.php';
\App\Database\Database::setConfig($dbConfig);
// Initialize database
try {
$pdo = \App\Database\Database::getInstance();
} catch (Exception $e) {
die('Database connection failed: ' . $e->getMessage());
}
// Start session
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Test authentication service
$authService = new \App\Services\AuthService($pdo);
echo "Testing login functionality...\n";
// Test 1: Check if admin user exists
$user = \App\Models\User::findByUsername($pdo, 'admin');
if (!$user) {
echo "ERROR: Admin user not found in database!\n";
exit(1);
}
echo "✓ Admin user exists in database\n";
// Test 2: Test password verification
$userModel = new \App\Models\User($pdo);
$userModel->password = $user['password'];
$passwordValid = $userModel->verifyPassword('admin123');
if (!$passwordValid) {
echo "ERROR: Password verification failed!\n";
exit(1);
}
echo "✓ Password verification works\n";
// Test 3: Test login method
$loginSuccess = $authService->login('admin', 'admin123', '127.0.0.1');
if (!$loginSuccess) {
echo "ERROR: Login method failed!\n";
exit(1);
}
echo "✓ Login method works\n";
// Test 4: Check if user is logged in
if (!$authService->isLoggedIn()) {
echo "ERROR: User is not logged in after successful login!\n";
exit(1);
}
echo "✓ User is logged in\n";
// Test 5: Check if user is admin
if (!$authService->isAdmin()) {
echo "ERROR: User is not recognized as admin!\n";
exit(1);
}
echo "✓ User is recognized as admin\n";
echo "\n🎉 All authentication tests passed!\n";