Skip to content
Open
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
155 changes: 155 additions & 0 deletions tests/lib/Service/OauthServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/**
* SPDX-FileCopyrightText: 2024 Jankari Tech Pvt. Ltd.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenProject\Service;

use OCA\OAuth2\Db\Client;
use OCA\OAuth2\Db\ClientMapper;
use OCA\OAuth2\Exceptions\ClientNotFoundException;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class OauthServiceTest extends TestCase {

/**
* @param MockObject|null $clientMapperMock
*
* @return OauthService
*/
protected function getOauthServiceMock(MockObject $clientMapperMock = null): OauthService {
$secureRandomMock = $this->createMock(ISecureRandom::class);
$secureRandomMock->method('generate')->willReturn('randomString');
$cryptoMock = $this->createMock(ICrypto::class);
$cryptoMock->method('calculateHMAC')->willReturn('hmacValue');

if ($clientMapperMock === null) {
$clientMapperMock = $this->createMock(ClientMapper::class);
}

return new OauthService(
$clientMapperMock,
$secureRandomMock,
$cryptoMock
);
}

/**
* @return Client
*/
protected function getClient(): Client {
$client = new Client();
$client->setId(1);
$client->setName('Test Client');
$client->setRedirectUri('https://example.com/callback');
$client->setClientIdentifier('randomString');
$client->setSecret('randomString');
Comment on lines +50 to +51

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these values the same? Are they supposed to be different?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we are mocking random generate method to return randomString.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it can be anything here. secrets won't be used in the test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused because both values are the same.

If the specific values don't matter for this test, could we use different strings instead? It would make it clearer that clientIdentifier and secret are separate values.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because this a test client for assertion and we are mocking generate method which will always return same.


return $client;
}

/**
* @return void
*/
public function testCreateNcOauthClient(): void {
$client = $this->getClient();
$expectedClient = [
'id' => 1,
'nextcloud_oauth_client_name' => $client->getName(),
'openproject_redirect_uri' => $client->getRedirectUri(),
'nextcloud_client_id' => $client->getClientIdentifier(),
'nextcloud_client_secret' => $client->getClientIdentifier(),
Comment thread
saw-jan marked this conversation as resolved.
];

$clientMapperMock = $this->createMock(ClientMapper::class);
$clientMapperMock->expects($this->once())->method('insert')
->with($this->isInstanceOf(Client::class))
->willReturn($client);

$oauthService = $this->getOauthServiceMock($clientMapperMock);

$clientInfo = $oauthService->createNcOauthClient($client->getName(), $client->getRedirectUri());
$this->assertEquals($expectedClient, $clientInfo);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals($expectedClient, $clientInfo);
$this->assertSame($expectedClient, $clientInfo);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to do strict assertion 👍

}

/**
* @return void
*/
public function testGetClientInfo(): void {
$client = $this->getClient();
$expectedClient = [
'id' => 1,
'nextcloud_oauth_client_name' => $client->getName(),
'openproject_redirect_uri' => $client->getRedirectUri(),
'nextcloud_client_id' => $client->getClientIdentifier(),
];

$clientMapperMock = $this->createMock(ClientMapper::class);
$clientMapperMock->expects($this->once())->method('getByUid')
->with(1)
->willReturn($client);

$oauthService = $this->getOauthServiceMock($clientMapperMock);

$clientInfo = $oauthService->getClientInfo(1);
$this->assertIsArray($clientInfo);
$this->assertEquals($expectedClient, $clientInfo);
}

/**
* @return void
*/
public function testGetClientInfoError(): void {
$clientMapperMock = $this->createMock(ClientMapper::class);
$clientMapperMock->expects($this->once())->method('getByUid')
->with(1)
->willThrowException(new ClientNotFoundException());

$oauthService = $this->getOauthServiceMock($clientMapperMock);

$clientInfo = $oauthService->getClientInfo(1);
$this->assertNull($clientInfo);
}

/**
* @return void
*/
public function testSetClientRedirectUri(): void {
$client = $this->getClient();

$clientMapperMock = $this->createMock(ClientMapper::class);
$clientMapperMock->expects($this->once())->method('getByUid')
->with(1)
->willReturn($client);
$clientMapperMock->expects($this->once())->method('update')
->with($client);

$oauthService = $this->getOauthServiceMock($clientMapperMock);

$result = $oauthService->setClientRedirectUri(1, $client->getRedirectUri());
$this->assertEquals(true, $result);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals(true, $result);
$this->assertTrue($result);

}

/**
* @return void
*/
public function testSetClientRedirectUriError(): void {
$client = $this->getClient();

$clientMapperMock = $this->createMock(ClientMapper::class);
$clientMapperMock->expects($this->once())->method('getByUid')
->with(1)
->willThrowException(new ClientNotFoundException());
$clientMapperMock->expects($this->never())->method('update');

$oauthService = $this->getOauthServiceMock($clientMapperMock);

$result = $oauthService->setClientRedirectUri(1, $client->getRedirectUri());
$this->assertEquals(false, $result);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals(false, $result);
$this->assertFalse($result);

}
}
38 changes: 0 additions & 38 deletions tests/lib/Service/OauthSeviceTest.php

This file was deleted.

Loading