Files
FOSSBilling/tests-legacy/modules/Profile/Api/ClientTest.php
T
Adam DaleyandCopilot a0b3158b30 PHPUnit test-legacy refactor and cleanup (#3144)
* PHPUnit test-legacy cleanup

Refactor and cleanup the legacy PHPUnit tests.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-13 17:32:09 +00:00

146 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Profile\Api;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ClientTest extends \BBTestCase
{
protected ?\Box\Mod\Profile\Api\Client $clientApi;
public function setUp(): void
{
$this->clientApi = new \Box\Mod\Profile\Api\Client();
}
public function testGet(): void
{
$clientService = $this->createMock(\Box\Mod\Client\Service::class);
$clientService->expects($this->atLeastOnce())
->method('toApiArray')
->willReturn([]);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn () => $clientService);
$this->clientApi->setDi($di);
$this->clientApi->setIdentity(new \Model_Client());
$result = $this->clientApi->get();
$this->assertIsArray($result);
}
public function testUpdate(): void
{
$service = $this->createMock(\Box\Mod\Profile\Service::class);
$service->expects($this->atLeastOnce())
->method('updateClient')
->willReturn(true);
$this->clientApi->setService($service);
$this->clientApi->setIdentity(new \Model_Client());
$data = [];
$result = $this->clientApi->update($data);
$this->assertTrue($result);
}
public function testApiKeyGet(): void
{
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->api_token = '16047a3e69f5245756d73b419348f0c7';
$this->clientApi->setIdentity($client);
$result = $this->clientApi->api_key_get([]);
$this->assertEquals($result, $client->api_token);
}
public function testApiKeyReset(): void
{
$apiKey = '16047a3e69f5245756d73b419348f0c7';
$service = $this->createMock(\Box\Mod\Profile\Service::class);
$service->expects($this->atLeastOnce())
->method('resetApiKey')
->willReturn($apiKey);
$this->clientApi->setService($service);
$this->clientApi->setIdentity(new \Model_Client());
$result = $this->clientApi->api_key_reset([]);
$this->assertEquals($result, $apiKey);
}
public function testChangePassword(): void
{
$service = $this->createMock(\Box\Mod\Profile\Service::class);
$service->expects($this->atLeastOnce())
->method('changeClientPassword')
->willReturn(true);
$validatorMock = $this->getMockBuilder(\FOSSBilling\Validate::class)->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('isPasswordStrong')
->willReturn(true);
$di = $this->getDi();
$di['validator'] = $validatorMock;
$di['password'] = new \FOSSBilling\PasswordManager();
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->pass = $di['password']->hashIt('oldpw');
$this->clientApi->setDi($di);
$this->clientApi->setService($service);
$this->clientApi->setIdentity($model);
$data = [
'current_password' => 'oldpw',
'new_password' => '16047a3e69f5245756d73b419348f0c7',
'confirm_password' => '16047a3e69f5245756d73b419348f0c7',
];
$result = $this->clientApi->change_password($data);
$this->assertTrue($result);
}
public function testChangePasswordPasswordsDoNotMatchException(): void
{
$service = $this->createMock(\Box\Mod\Profile\Service::class);
$service->expects($this->never())
->method('changeClientPassword')
->willReturn(true);
$di = $this->getDi();
$this->clientApi->setDi($di);
$this->clientApi->setService($service);
$this->clientApi->setIdentity(new \Model_Client());
$data = [
'current_password' => '1234',
'new_password' => '16047a3e69f5245756d73b419348f0c7',
'confirm_password' => '7c0f843914b37d6575425f96e3a74061', // passwords do not match
];
$this->expectException(\Exception::class);
$result = $this->clientApi->change_password($data);
$this->assertTrue($result);
}
public function testLogout(): void
{
$service = $this->createMock(\Box\Mod\Profile\Service::class);
$service->expects($this->atLeastOnce())
->method('logoutClient')
->willReturn(true);
$this->clientApi->setService($service);
$result = $this->clientApi->logout();
$this->assertTrue($result);
}
}