Files
FOSSBilling/tests-legacy/modules/Staff/Api/GuestTest.php
T
Yağızhan Burak YakarandAdam Daley 977bf4a255 Reworked Box_Mod into FOSSBilling\Module (#2754)
* Reworked Box_Mod into FOSSBilling\Module

* Tests

* Tests

* Tests

* Remove legacy integration tests

* Replace `Box_Mod` with `FOSSBilling\Module` in `ServiceTest`

Updated the `ServiceTest` to instantiate `FOSSBilling\Module` instead of `Box_Mod` when mocking modules. This aligns the test with the current module class implementation.

* Fix test class name and resolve merge conflicts in tests

Renamed ModuleTest to FOSSBilling_ModuleTest for consistency. Cleaned up ServiceTest by resolving merge conflicts, updating mock class references, and consolidating deactivate tests for module and hook extensions.

---------

Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
2025-12-24 03:53:09 +00:00

195 lines
6.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Staff\Api;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class GuestTest extends \BBTestCase
{
protected ?\Box\Mod\Staff\Api\Guest $api;
public function setUp(): void
{
$this->api = new \Box\Mod\Staff\Api\Guest();
}
public function testGetDi(): void
{
$di = $this->getDi();
$this->api->setDi($di);
$getDi = $this->api->getDi();
$this->assertEquals($di, $getDi);
}
public function testCreate(): void
{
$adminId = 1;
$apiMock = $this->getMockBuilder(\Box\Mod\Staff\Api\Guest::class)
->onlyMethods(['login'])
->getMock();
$apiMock->expects($this->atLeastOnce())
->method('login');
$serviceMock = $this->createMock(\Box\Mod\Staff\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('createAdmin')
->willReturn($adminId);
$validatorMock = $this->createMock(\FOSSBilling\Validate::class);
$validatorMock->expects($this->any())->method('checkRequiredParamsForArray');
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['validator'] = $validatorMock;
$toolsMock = $this->createMock(\FOSSBilling\Tools::class);
$toolsMock->expects($this->atLeastOnce())->method('validateAndSanitizeEmail');
$di['tools'] = $toolsMock;
$apiMock->setDi($di);
$apiMock->setService($serviceMock);
$data = [
'email' => 'example@fossbilling.org',
'password' => 'EasyToGuess',
];
$result = $apiMock->create($data);
$this->assertTrue($result);
}
public function testCreateException(): void
{
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn([[]]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->api->setDi($di);
$data = [
'email' => 'example@fossbilling.org',
'password' => 'EasyToGuess',
];
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(55);
$this->expectExceptionMessage('Administrator account already exists');
$this->api->create($data);
}
/**
* @expectedException \FOSSBilling\Exception
*/
public function testLoginWithoutEmail(): void
{
$guestApi = new \Box\Mod\Staff\Api\Guest();
$di = $this->getDi();
$di['validator'] = new \FOSSBilling\Validate();
$guestApi->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->validateRequiredParams($guestApi, 'login', []);
$guestApi->login([]);
}
/**
* @expectedException \FOSSBilling\Exception
*/
public function testLoginWithoutPassword(): void
{
$guestApi = new \Box\Mod\Staff\Api\Guest();
$di = $this->getDi();
$di['validator'] = new \FOSSBilling\Validate();
$guestApi->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->validateRequiredParams($guestApi, 'login', ['email' => 'email@domain.com']);
$guestApi->login(['email' => 'email@domain.com']);
}
public function testSuccessfulLogin(): void
{
$modMock = $this->getMockBuilder('\\' . \FOSSBilling\Module::class)
->disableOriginalConstructor()
->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn([]);
$serviceMock = $this->getMockBuilder(\Box\Mod\Staff\Service::class)
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('login')
->willReturn([]);
$sessionMock = $this->getMockBuilder(\FOSSBilling\Session::class)
->disableOriginalConstructor()
->getMock();
$di = $this->getDi();
$toolsMock = $this->createMock(\FOSSBilling\Tools::class);
$toolsMock->expects($this->atLeastOnce())->method('validateAndSanitizeEmail');
$di['tools'] = $toolsMock;
$di['session'] = $sessionMock;
$di['validator'] = new \FOSSBilling\Validate();
$guestApi = new \Box\Mod\Staff\Api\Guest();
$guestApi->setMod($modMock);
$guestApi->setService($serviceMock);
$guestApi->setDi($di);
$result = $guestApi->login(['email' => 'email@domain.com', 'password' => 'pass']);
$this->assertIsArray($result);
}
public function testLoginCheckIpException(): void
{
$modMock = $this->getMockBuilder('\\' . \FOSSBilling\Module::class)
->disableOriginalConstructor()
->getMock();
$configArr = [
'allowed_ips' => '1.1.1.1' . PHP_EOL . '2.2.2.2',
'check_ip' => true,
];
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn($configArr);
$di = $this->getDi();
$toolsMock = $this->createMock(\FOSSBilling\Tools::class);
$toolsMock->expects($this->atLeastOnce())->method('validateAndSanitizeEmail');
$di['tools'] = $toolsMock;
$di['validator'] = new \FOSSBilling\Validate();
$guestApi = new \Box\Mod\Staff\Api\Guest();
$guestApi->setMod($modMock);
$guestApi->setDi($di);
$ip = '192.168.0.1';
$guestApi->setIp($ip);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(403);
$this->expectExceptionMessage("You are not allowed to login to admin area from {$ip} address.");
$data = [
'email' => 'email@domain.com',
'password' => 'pass',
];
$guestApi->login($data);
}
}