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 23, 2024
1 parent 45d490f commit df4c298
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
### Changed
- Add application's support for Nextcloud 30
- Audit logging in `/audit.log` for admin configuration.

## 2.6.3 - 2024-04-17
### Changed
Expand Down
10 changes: 4 additions & 6 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Log\ILogFactory;
Expand Down Expand Up @@ -1084,8 +1083,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 +1092,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
98 changes: 96 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 @@ -3558,6 +3558,7 @@ public function testGetWorkPackageInfoForExistentWorkPackage(): void {
$configMock,
null,
null,
null,
$iULGeneratorMock
);
$imageURL = 'http://nextcloud/server/index.php/apps/integration_openproject/avatar?userId=3&userName=OpenProject Admin';
Expand Down Expand Up @@ -3618,4 +3619,97 @@ public function testGetWorkPackageInfoForNoUserAccessToken(): void {
$resultGetWorkPackageInfo = $service->getWorkPackageInfo('testUser', 123);
$this->assertNull($resultGetWorkPackageInfo);
}

public function auditLogDataProvider(): array {
return [
[
// log level less than 1
0,
\OC::$SERVERROOT . '/data/audit.log',
[],
true,
false
],
[
// wrong path to audit file
1,
'wrong-path-to-audit-log/audit.log',
[],
true,
false
],
[
// no audit apps in apps section
1,
'/audit.log',
['apps' => ['no_audit_app']],
true,
false
],
[
// multiple values including apps with no audit apps in apps section
1,
'/audit.log',
['apps' => ['no_audit_app'], 'others' => []],
true,
false
],
[
// all values configured correctly
1,
'/audit.log',
['apps' => ['admin_audit']],
true,
true
],
[
// admin_audit app not installed
1,
'/audit.log',
['apps' => ['admin_audit']],
false,
false
],
];
}

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

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

0 comments on commit df4c298

Please sign in to comment.