Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for lib\Settings\Personal #86

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

$classLoader = new ClassLoader();
$classLoader->addPsr4("OCA\\OpenProject\\Service\\", __DIR__ . '/lib/Service', true);
$classLoader->addPsr4("OCA\\OpenProject\\Settings\\", __DIR__ . '/lib/Settings', true);
$classLoader->addPsr4("OCP\\", $serverPath . '/lib/public', true);
$classLoader->addPsr4("OC\\", $serverPath . '/lib/private', true);
$classLoader->addPsr4("OCA\\Files\\Event\\", $serverPath . '/apps/files/lib/Event', true);
Expand Down
38 changes: 35 additions & 3 deletions tests/lib/Service/OpenProjectAPIServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,19 +844,51 @@ public function testGetOpenProjectOauthURL() {
);
}

/**
* @return array<mixed>
*/
public function getOpenProjectOauthURLDataProvider() {
return [
[
'clientId',
'clientSecret',
'openproject', // invalid oauth instance url
],
[
'clientId',
'clientSecret',
'', // empty oauth instance url
],
[
'clientId',
'', // empty client secret
'https://openproject',
],
[
'', // empty client id
'clientSecret',
'https://openproject',
],
];
}

/**
* @return void
*
* @dataProvider getOpenProjectOauthURLDataProvider
*/
public function testGetOpenProjectOauthURLWithInvalidAdminConfig() {
public function testGetOpenProjectOauthURLWithInvalidAdminConfig(
string $clientId, string $clientSecret, string $oauthInstanceUrl
) {
$url = $this->createMock(IURLGenerator::class);
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$configMock
->method('getAppValue')
->withConsecutive(
['integration_openproject', 'client_id'],
['integration_openproject', 'client_secret'],
['integration_openproject', 'oauth_instance_url'],
)->willReturnOnConsecutiveCalls('clientid', 'clientsecret', 'Openproject');
['integration_openproject', 'oauth_instance_url']
)->willReturnOnConsecutiveCalls($clientId, $clientSecret, $oauthInstanceUrl);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('OpenProject admin config is not valid!');
Expand Down
144 changes: 144 additions & 0 deletions tests/lib/Settings/PersonalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Nextcloud - OpenProject
*
*
* @author Kiran Parajuli <kiran@jankaritech.com>
* @copyright Kiran Parajuli 2022
*/

namespace OCA\OpenProject\Settings;

use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class PersonalTest extends TestCase {
/**
* @var Personal
*/
private $setting;

/**
* @var MockObject | IConfig
*/
private $config;

/**
* @var MockObject | IInitialState
*/
private $initialState;

/**
* @var MockObject | IURLGenerator
*/
private $url;

protected function setUp(): void {
parent::setUp();
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
$this->initialState = $this->getMockBuilder(IInitialState::class)->getMock();
$this->url = $this->getMockBuilder(IURLGenerator::class)->getMock();
$this->setting = new Personal($this->config, $this->initialState, $this->url, "testUser");
}

/**
* @return array<mixed>
*/
public function dataTestGetForm(): array {
return [
[
// valid dataset
"clientId" => 'some-client-id',
"clientSecret" => 'some-client-secret',
"oauthInstanceUrl" => 'http://some.url',
"expectedRequestUrl" => 'http://some.url/'
. 'oauth/authorize'
. '?client_id=' . 'some-client-id'
.'&redirect_uri=' . urlencode('http://redirect.url/test/')
. '&response_type=code',
],
[
// dataset with empty client secret
"clientId" => 'some-client-id',
"clientSecret" => '',
"oauthInstanceUrl" => 'http://some.url',
"expectedRequestUrl" => '',
],
[
// dataset with invalid oauth instance url
"clientId" => 'some-client-id',
"clientSecret" => 'some-secret',
"oauthInstanceUrl" => 'http:/',
"expectedRequestUrl" => '',
],
];
}

/**
* @dataProvider dataTestGetForm
*
* @param string $clientId
* @param string $clientSecret
* @param string $oauthInstanceUrl
* @param string $expectedRequestUrl
* @return void
*/
public function testGetForm(
string $clientId, string $clientSecret, string $oauthInstanceUrl, string $expectedRequestUrl
) {
$this->config
->method('getUserValue')
->withConsecutive(
['testUser', 'integration_openproject', 'token'],
['testUser', 'integration_openproject', 'user_name'],
['testUser', 'integration_openproject', 'search_enabled', '0'],
['testUser', 'integration_openproject', 'notification_enabled', '0'],
['testUser', 'integration_openproject', 'navigation_enabled', '0'],
)
->willReturnOnConsecutiveCalls(
'some-token',
'some-username',
'0', '0', '0'
);
$this->config
->method('getAppValue')
->withConsecutive(
['integration_openproject', 'client_id'],
['integration_openproject', 'client_secret'],
['integration_openproject', 'oauth_instance_url'],
['integration_openproject', 'client_id'],
['integration_openproject', 'oauth_instance_url'],
)
->willReturnOnConsecutiveCalls(
$clientId,
$clientSecret,
$oauthInstanceUrl,
$clientId,
$oauthInstanceUrl,
);

$this->url
->method('linkToRouteAbsolute')
->with('integration_openproject.config.oauthRedirect')
->willReturn('http://redirect.url/test/');

$this->initialState
->method('provideInitialState')
->with('user-config', [
'token' => 'some-token',
'user_name' => 'some-username',
'search_enabled' => false,
'notification_enabled' => false,
'navigation_enabled' => false,
'request_url' => $expectedRequestUrl === '' ? false : $expectedRequestUrl,
]);

$form = $this->setting->getForm();
$expected = new TemplateResponse('integration_openproject', 'personalSettings');
$this->assertEquals($expected, $form);
}
}