request = $request; return $this; } public function setResponse(ResponseInterface $response): self { $this->response = $response; return $this; } public function setUserModel(UserModel $model): self { $this->userModel = $model; return $this; } public function setIpAttemptModel(IpAttemptModel $model): self { $this->ipAttemptModel = $model; return $this; } public function setLoginActivityModel(LoginActivityModel $model): self { $this->loginActivityModel = $model; return $this; } } class AuthControllerTest extends CIUnitTestCase { private TestableAuthController $controller; private UserModel $userModel; private IpAttemptModel $ipAttemptModel; private LoginActivityModel $loginActivityModel; protected function setUp(): void { parent::setUp(); $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->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() { $payload = [ 'email' => 'admin@example.com', 'password' => '12345678', ]; $user = [ 'id' => 16, 'email' => $payload['email'], 'firstname' => 'Admin', 'lastname' => 'Panel', 'password' => pbkdf2_hash($payload['password']), 'is_suspended' => false, 'user_type' => 'admin', ]; $this->ipAttemptModel->method('where')->willReturnSelf(); $this->ipAttemptModel->method('first')->willReturn(null); $this->userModel->method('where')->willReturnSelf(); $this->userModel->method('first')->willReturn($user); $this->userModel->method('update')->willReturn(true); $this->loginActivityModel->method('insert')->willReturn(true); $this->controller->setRequest($this->makeRequest('POST', '/api/v1/login', $payload)); $response = $this->controller->apiLogin(); $body = json_decode((string) $response->getBody(), true); $this->assertIsArray($body); $this->assertTrue($body['status']); $this->assertArrayHasKey('token', $body); $this->assertArrayHasKey('user', $body); $this->assertSame($user['id'], $body['user']['id']); $this->assertArrayHasKey('roles', $body['user']); $this->assertTrue($body['user']['roles']['admin'] ?? false); $this->assertIsString($body['token']); } }