@@ -6,11 +6,35 @@ use App\Controllers\AuthController;
|
||||
use App\Models\IpAttemptModel;
|
||||
use App\Models\LoginActivityModel;
|
||||
use App\Models\UserModel;
|
||||
use CodeIgniter\Database\BaseBuilder;
|
||||
use CodeIgniter\Database\BaseConnection;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\HTTP\URI;
|
||||
use CodeIgniter\HTTP\UserAgent;
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
use CodeIgniter\Test\Mock\MockIncomingRequest;
|
||||
use Config\App;
|
||||
|
||||
class FakeAuthJsonRequest extends MockIncomingRequest
|
||||
{
|
||||
public function __construct(private readonly array $payload)
|
||||
{
|
||||
parent::__construct(config(App::class), new URI('https://test.alrahmaisgl.org'), 'php://input', new UserAgent());
|
||||
}
|
||||
|
||||
public function getJSON(bool $assoc = false, int $depth = 512, int $options = 0): array
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
|
||||
public function getPost($index = null, $filter = null, $flags = null)
|
||||
{
|
||||
if ($index === null) {
|
||||
return $this->payload;
|
||||
}
|
||||
|
||||
return $this->payload[$index] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
class TestableAuthController extends AuthController
|
||||
{
|
||||
@@ -48,6 +72,11 @@ class TestableAuthController extends AuthController
|
||||
$this->loginActivityModel = $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getUserRoleNames(int $userId): array
|
||||
{
|
||||
return ['admin'];
|
||||
}
|
||||
}
|
||||
|
||||
class AuthControllerTest extends CIUnitTestCase
|
||||
@@ -60,35 +89,32 @@ class AuthControllerTest extends CIUnitTestCase
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
helper('security');
|
||||
putenv('JWT_SECRET=test-secret-for-auth-controller');
|
||||
$_ENV['JWT_SECRET'] = 'test-secret-for-auth-controller';
|
||||
$_SERVER['JWT_SECRET'] = 'test-secret-for-auth-controller';
|
||||
|
||||
$this->controller = new TestableAuthController();
|
||||
$this->controller->setResponse(service('response'));
|
||||
|
||||
$this->userModel = $this->createMock(UserModel::class);
|
||||
$this->ipAttemptModel = $this->createMock(IpAttemptModel::class);
|
||||
$this->loginActivityModel = $this->createMock(LoginActivityModel::class);
|
||||
$this->userModel = $this->getMockBuilder(UserModel::class)
|
||||
->disableOriginalConstructor()
|
||||
->addMethods(['where'])
|
||||
->onlyMethods(['first', 'update'])
|
||||
->getMock();
|
||||
$this->ipAttemptModel = $this->getMockBuilder(IpAttemptModel::class)
|
||||
->disableOriginalConstructor()
|
||||
->addMethods(['where'])
|
||||
->onlyMethods(['first'])
|
||||
->getMock();
|
||||
$this->loginActivityModel = $this->getMockBuilder(LoginActivityModel::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['insert'])
|
||||
->getMock();
|
||||
|
||||
$this->controller->setUserModel($this->userModel);
|
||||
$this->controller->setIpAttemptModel($this->ipAttemptModel);
|
||||
$this->controller->setLoginActivityModel($this->loginActivityModel);
|
||||
|
||||
$builder = $this->createMock(BaseBuilder::class);
|
||||
$builder->method('select')->willReturnSelf();
|
||||
$builder->method('join')->willReturnSelf();
|
||||
$builder->method('where')->willReturnSelf();
|
||||
$builder->method('get')->willReturnSelf();
|
||||
$builder->method('getResultArray')->willReturn([
|
||||
['name' => 'admin'],
|
||||
]);
|
||||
|
||||
$fakeDb = $this->createMock(BaseConnection::class);
|
||||
$fakeDb->method('table')->willReturn($builder);
|
||||
|
||||
if (! class_exists('Config\\FakeDatabase')) {
|
||||
eval('namespace Config; class FakeDatabase extends \\Config\\Database { public static function connect($group = null, bool $getShared = true) { return $GLOBALS["__fakeDb__"]; } }');
|
||||
}
|
||||
$GLOBALS['__fakeDb__'] = $fakeDb;
|
||||
class_alias('Config\\FakeDatabase', 'Config\\Database');
|
||||
}
|
||||
|
||||
public function testApiLoginReturnsTokenAndRoles()
|
||||
@@ -117,13 +143,13 @@ class AuthControllerTest extends CIUnitTestCase
|
||||
|
||||
$this->loginActivityModel->method('insert')->willReturn(true);
|
||||
|
||||
$this->controller->setRequest($this->makeRequest('POST', '/api/v1/login', $payload));
|
||||
$this->controller->setRequest(new FakeAuthJsonRequest($payload));
|
||||
|
||||
$response = $this->controller->apiLogin();
|
||||
$body = json_decode((string) $response->getBody(), true);
|
||||
|
||||
$this->assertIsArray($body);
|
||||
$this->assertTrue($body['status']);
|
||||
$this->assertTrue($body['status'], json_encode($body));
|
||||
$this->assertArrayHasKey('token', $body);
|
||||
$this->assertArrayHasKey('user', $body);
|
||||
$this->assertSame($user['id'], $body['user']['id']);
|
||||
|
||||
Reference in New Issue
Block a user