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) ); } }