Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: sagargurung1001@gmail.com <sagargurung1001@gmail.com>
  • Loading branch information
SagarGi committed Jun 21, 2024
1 parent 45d490f commit 26cf9f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
9 changes: 4 additions & 5 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,8 +1084,7 @@ class_exists('\OCA\GroupFolders\Folder\FolderManager') &&
* @return void
*/
public function logToAuditFile($auditLogMessage): void {
$result = $this->isAdminAuditConfigSetCorrectly();
if($result) {
if($this->isAdminAuditConfigSetCorrectly()) {
$this->auditLogger = new AuditLogger($this->logFactory, $this->config);
$this->auditLogger->info($auditLogMessage,
['app' => 'admin_audit']
Expand All @@ -1094,9 +1093,9 @@ public function logToAuditFile($auditLogMessage): void {
}

public function isAdminAuditConfigSetCorrectly(): bool {
$logLevel = $this->config->getSystemValue('loglevel', ILogger::WARN);
$configAuditFile = $this->config->getSystemValue('logfile_audit', ILogger::WARN);
$logCondition = $this->config->getSystemValue('log.condition', []);
$logLevel = $this->config->getSystemValue('loglevel');
$configAuditFile = $this->config->getSystemValue('logfile_audit');
$logCondition = $this->config->getSystemValue('log.condition');
// All the above config should be satisfied in order for admin audit log for the integration application
// if any of the config is failed to be set then we are not able to send the admin audit logging in the audit.log file
return (
Expand Down
53 changes: 51 additions & 2 deletions tests/lib/Service/OpenProjectAPIServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,8 @@ private function getServiceMock(
$configMock = null,
$tokenProviderMock = null,
$db = null,
$iURLGenerator = null,
$iLogFactory = null
$iLogFactory = null,
$iURLGenerator = null
): OpenProjectAPIService {
$onlyMethods[] = 'getBaseUrl';
if ($rootMock === null) {
Expand Down Expand Up @@ -3618,4 +3618,53 @@ public function testGetWorkPackageInfoForNoUserAccessToken(): void {
$resultGetWorkPackageInfo = $service->getWorkPackageInfo('testUser', 123);
$this->assertNull($resultGetWorkPackageInfo);
}


public function auditLogDataProvider(): array {
return [
[
[ '0'],
['path-to-nextcloud/data/audit.log'],
['apps' => ['admin_audit']]

Check failure on line 3628 in tests/lib/Service/OpenProjectAPIServiceTest.php

View workflow job for this annotation

GitHub Actions / builds / unit tests and linting (master, 8.1)

InvalidArrayOffset

tests/lib/Service/OpenProjectAPIServiceTest.php:3628:5: InvalidArrayOffset: Cannot access value on variable using a false offset, expecting array-key (see https://psalm.dev/115)

Check failure on line 3628 in tests/lib/Service/OpenProjectAPIServiceTest.php

View workflow job for this annotation

GitHub Actions / builds / unit tests and linting (master, 8.2)

InvalidArrayOffset

tests/lib/Service/OpenProjectAPIServiceTest.php:3628:5: InvalidArrayOffset: Cannot access value on variable using a false offset, expecting array-key (see https://psalm.dev/115)
[false]
],
];
}

/**
* @dataProvider auditLogDataProvider
* @param string $logLevel
* @param string $pathToAuditLog
* @param array<mixed> $logCondition
* @param bool $expectedResult
*
* @return void
*/

public function testIsAdminAuditConfigSetCorrectly(string $logLevel, string $pathToAuditLog, array $logCondition, bool $expectedResult): void {
$actualPath = \OC::$SERVERROOT;
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$configMock
->method('getSystemValue')
->withConsecutive(
['loglevel'],
['logfile_audit'],
['log.condition']
)->willReturnOnConsecutiveCalls($logLevel, $pathToAuditLog,$logCondition);
$userManagerMock = $this->getMockBuilder(IUserManager::class)
->getMock();
$service = $this->getServiceMock(
[],
null,
null,
$userManagerMock,
null,
null,
null,
null,
$configMock
);
$actualResult = $service->isAdminAuditConfigSetCorrectly();
$this->assertEquals($expectedResult, $actualResult);
}
}

0 comments on commit 26cf9f5

Please sign in to comment.