Files
nextcloud-server-mirror/apps/admin_audit/tests/Actions/ActionTest.php
Ghassen kefi 3d9abd7a68 feat(admin_audit): log structured parameters with audit entries
Audit entries now carry the parameters they are built from as a nested
"params" object in their data (e.g. data.params.shareWith), so log
consumers no longer need to parse the human-readable message. Numbers
and booleans keep their type. Parameters are left out when the call
asks for obfuscation.

The audit logger requests a custom logger with structured context, so
only audit.log is affected.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ghassen kefi <ghassen.kefi.dev@gmail.com>
2026-10-13 14:41:55 +02:00

120 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AdminAudit\Tests\Actions;
use OCA\AdminAudit\Actions\Action;
use OCA\AdminAudit\IAuditLogger;
use OCA\AdminAudit\Operation;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ActionTest extends TestCase {
private IAuditLogger&MockObject $logger;
private Action $action;
protected function setUp(): void {
parent::setUp();
$this->logger = $this->createMock(IAuditLogger::class);
$this->action = new Action($this->logger);
}
public function testLogAddsOperationToContext(): void {
$this->logger->expects($this->once())
->method('info')
->with('File "/a.txt" deleted', ['app' => 'admin_audit', 'operation' => 'files.file.deleted', 'params' => ['path' => '/a.txt']]);
$this->action->log(Operation::FileDeleted, 'File "%s" deleted', ['path' => '/a.txt'], ['path']);
}
public function testLogAcceptsOperationString(): void {
$this->logger->expects($this->once())
->method('info')
->with('Share "42" accepted', ['app' => 'admin_audit', 'operation' => 'federatedfilesharing.share.accepted', 'params' => ['id' => '42']]);
$this->action->log('federatedfilesharing.share.accepted', 'Share "%s" accepted', ['id' => '42'], ['id']);
}
public function testLogWithoutOperation(): void {
$this->logger->expects($this->once())
->method('info')
->with('File "/a.txt" deleted', ['app' => 'admin_audit', 'params' => ['path' => '/a.txt']]);
$this->action->log(null, 'File "%s" deleted', ['path' => '/a.txt'], ['path']);
}
public function testLogWithOperationAndNamedPlaceholders(): void {
$this->logger->expects($this->once())
->method('info')
->with(
'File "{path}" deleted',
['app' => 'admin_audit', 'operation' => 'files.file.deleted', 'path' => '/a.txt', 'params' => ['path' => '/a.txt']],
);
$this->action->log(Operation::FileDeleted, 'File "{path}" deleted', ['path' => '/a.txt'], ['path']);
}
public function testMissingParameterKeepsOperation(): void {
$this->logger->expects($this->never())
->method('info');
$this->logger->expects($this->once())
->method('critical')
->with(
'$params["path"] was missing. Transferred value: {params}',
['app' => 'admin_audit', 'operation' => 'files.file.deleted', 'params' => ['id' => 42]],
);
$this->action->log(Operation::FileDeleted, 'File "%s" deleted', ['id' => 42], ['path']);
}
public function testMissingParameterObfuscatedKeepsOperation(): void {
$this->logger->expects($this->once())
->method('critical')
->with(
'$params["uid"] was missing.',
['app' => 'admin_audit', 'operation' => 'auth.login.failed'],
);
$this->action->log(Operation::LoginFailed, 'Login failed: "%s"', [], ['uid'], true);
}
public function testObfuscatedCallHasNoParams(): void {
$this->logger->expects($this->once())
->method('info')
->with('Login failed: "bob"', ['app' => 'admin_audit', 'operation' => 'auth.login.failed']);
$this->action->log(Operation::LoginFailed, 'Login failed: "%s"', ['loginName' => 'bob'], ['loginName'], true);
}
public function testCallWithoutParametersHasNoParams(): void {
$this->logger->expects($this->once())
->method('info')
->with('Logout occurred', ['app' => 'admin_audit', 'operation' => 'auth.logout.performed']);
$this->action->log(Operation::LogoutPerformed, 'Logout occurred', [], []);
}
public function testParamsOnlyContainListedElements(): void {
$this->logger->expects($this->once())
->method('info')
->with('File "/a.txt" deleted', ['app' => 'admin_audit', 'operation' => 'files.file.deleted', 'params' => ['path' => '/a.txt']]);
$this->action->log(Operation::FileDeleted, 'File "%s" deleted', ['path' => '/a.txt', 'secret' => 'not listed'], ['path']);
}
public function testParamsPlaceholderTakesPrecedence(): void {
$this->logger->expects($this->once())
->method('info')
->with('Imported {params}', ['app' => 'admin_audit', 'params' => '3 cards']);
$this->action->log(null, 'Imported {params}', ['params' => '3 cards'], ['params']);
}
}