Files
nuqloud-ai/tests/unit/Service/TalkBotRegistrationServiceTest.php

108 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\EducAI\Tests\Unit\Service;
use OCA\EducAI\Db\Settings;
use OCA\EducAI\Db\SettingsMapper;
use OCA\EducAI\Service\CredentialService;
use OCA\EducAI\Service\TalkBotRegistrationService;
use OCP\App\IAppManager;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
if (!class_exists(\OC::class)) {
eval('namespace { class OC { public static bool $CLI = false; } }');
}
class TalkBotRegistrationServiceTest extends TestCase {
public function testInstallationSyncRotatesAnInvalidStoredWebhookSecret(): void {
\OC::$CLI = false;
$_COOKIE = [];
$settings = new Settings();
$settings->setWebhookSecret('encrypted-legacy-secret');
$mapper = $this->createMock(SettingsMapper::class);
$credentials = $this->createMock(CredentialService::class);
$mapper->expects($this->exactly(2))
->method('getSettings')
->willReturn($settings);
$mapper->expects($this->once())
->method('update')
->with($settings)
->willReturn($settings);
$credentials->expects($this->once())
->method('decrypt')
->with('encrypted-legacy-secret')
->willReturn('too-short');
$credentials->expects($this->once())
->method('encrypt')
->with($this->callback(function (string $secret): bool {
return preg_match('/^[a-f0-9]{64}$/', $secret) === 1;
}))
->willReturn('encrypted-rotated-secret');
$service = $this->createService($mapper, $credentials);
$result = $service->syncRegistration(null, false, true);
$this->assertSame('skipped', $result['status']);
$this->assertSame('encrypted-rotated-secret', $settings->getWebhookSecret());
}
public function testInstallationSyncPreservesAValidStoredWebhookSecret(): void {
\OC::$CLI = false;
$_COOKIE = [];
$settings = new Settings();
$settings->setWebhookSecret('encrypted-valid-secret');
$mapper = $this->createMock(SettingsMapper::class);
$credentials = $this->createMock(CredentialService::class);
$validSecret = str_repeat('a', 64);
$mapper->expects($this->once())
->method('getSettings')
->willReturn($settings);
$mapper->expects($this->never())
->method('update');
$credentials->expects($this->once())
->method('decrypt')
->with('encrypted-valid-secret')
->willReturn($validSecret);
$credentials->expects($this->never())
->method('encrypt');
$service = $this->createService($mapper, $credentials);
$result = $service->syncRegistration(null, false, true);
$this->assertSame('skipped', $result['status']);
$this->assertSame('encrypted-valid-secret', $settings->getWebhookSecret());
}
private function createService(SettingsMapper $mapper, CredentialService $credentials): TalkBotRegistrationService {
$appManagerBuilder = $this->getMockBuilder(IAppManager::class);
if (!method_exists(IAppManager::class, 'isInstalled')) {
$appManagerBuilder->addMethods(['isInstalled']);
}
$appManager = $appManagerBuilder->getMock();
$appManager->method('isInstalled')
->willReturnCallback(static fn (string $appId): bool => $appId === 'spreed');
$request = $this->createMock(IRequest::class);
$request->method('getHeader')->willReturn('');
return new TalkBotRegistrationService(
$mapper,
$credentials,
$appManager,
$this->createMock(IConfig::class),
$request,
$this->createMock(IURLGenerator::class),
$this->createMock(IClientService::class),
$this->createMock(LoggerInterface::class)
);
}
}