You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/AuthRequestCacheTest.php

185 lines
5.5 KiB
PHTML

<?php
namespace App\Services {
final class TestAuthDriver
{
public static $getUserCalls = 0;
public static $loginCalls = 0;
public static $logoutCalls = 0;
public static $user;
public function getUser()
{
self::$getUserCalls++;
return self::$user;
}
public function login($uid, $time)
{
self::$loginCalls++;
self::$user = (object) ['id' => (int) $uid, 'enable' => 1, 'isLogin' => true];
}
public function logout()
{
self::$logoutCalls++;
self::$user = (object) ['enable' => 1, 'isLogin' => false];
}
}
final class Factory
{
public static $createCalls = 0;
public static function createAuth()
{
self::$createCalls++;
return new TestAuthDriver();
}
}
final class Config
{
public static $calls = 0;
public static function get($name, $default = null)
{
self::$calls++;
return $default;
}
}
}
namespace App\Services\Cloud {
final class CloudSessionService
{
public static $revokeCalls = 0;
public static function revokeCurrent()
{
self::$revokeCalls++;
}
}
}
namespace App\Models {
final class User
{
public static $version = 0;
public $id = 1;
private $attributes = ['login_session_version' => 0];
public static function where($column, $operator, $value)
{
return new TestUserVersionQuery();
}
public static function query()
{
return new TestUserVersionQuery();
}
public function refresh()
{
$this->attributes['login_session_version'] = self::$version;
return $this;
}
public function getAttribute($name)
{
return $this->attributes[$name] ?? null;
}
}
final class TestUserVersionQuery
{
public function increment($column)
{
User::$version++;
return 1;
}
}
}
namespace Predis {
final class Client
{
public static $constructCalls = 0;
public function __construct($config)
{
self::$constructCalls++;
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Middleware\Auth as AuthMiddleware;
use App\Services\Auth;
use App\Services\Factory;
use App\Services\LoginSessionRevocationService;
use App\Services\RedisClient;
use App\Services\TestAuthDriver;
use Predis\Client;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
$assert = function ($condition, $message) {
if (!$condition) {
throw new \RuntimeException($message);
}
};
Auth::resetRequestCache();
TestAuthDriver::$user = (object) ['id' => 7, 'enable' => 1, 'isLogin' => true];
$first = Auth::getUser();
$second = Auth::getUser();
$assert($first === $second, 'Resolved authentication identity was not reused.');
$assert(TestAuthDriver::$getUserCalls === 1, 'Authentication driver was queried more than once.');
$assert(Factory::$createCalls === 1, 'Authentication driver was constructed more than once.');
$request = Request::createFromEnvironment(Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/user',
]));
$nextCalled = false;
$next = function ($request, $response) use (&$nextCalled, $assert, $first) {
$nextCalled = true;
$assert($request->getAttribute('auth_user') === $first, 'Middleware did not expose the cached user.');
return $response;
};
$reply = (new AuthMiddleware())($request, new Response(), $next);
$assert($nextCalled && $reply->getStatusCode() === 200, 'Authenticated middleware request failed.');
$assert(TestAuthDriver::$getUserCalls === 1, 'Middleware repeated the authenticated user lookup.');
Auth::login(9, 300);
$loggedIn = Auth::getUser();
$assert(TestAuthDriver::$loginCalls === 1 && (int) $loggedIn->id === 9, 'Login retained a stale user cache.');
$assert(TestAuthDriver::$getUserCalls === 2, 'Login did not force one fresh identity lookup.');
LoginSessionRevocationService::revokeUser(new \App\Models\User());
Auth::getUser();
$assert(TestAuthDriver::$getUserCalls === 3, 'Session revocation retained a cached identity.');
Auth::logout();
$loggedOut = Auth::getUser();
$assert(TestAuthDriver::$logoutCalls === 1 && !$loggedOut->isLogin, 'Logout retained an authenticated cache entry.');
$assert(\App\Services\Cloud\CloudSessionService::$revokeCalls === 1, 'Logout did not revoke cloud session state.');
$assert(TestAuthDriver::$getUserCalls === 4, 'Logout did not force one fresh identity lookup.');
RedisClient::resetSharedClient();
$redisA = new RedisClient();
$redisB = new RedisClient();
$assert($redisA->getClient() === $redisB->getClient(), 'Redis client was not reused in the request.');
$assert(Client::$constructCalls === 1, 'Predis client was constructed more than once.');
RedisClient::resetSharedClient();
$redisC = new RedisClient();
$assert($redisC->getClient() !== $redisA->getClient() && Client::$constructCalls === 2,
'Redis client reset did not release request-local state.');
echo "Authentication request cache and Redis client reuse checks passed.\n";
}