-
Notifications
You must be signed in to change notification settings - Fork 21
test(php): add phpunit tests for OauthService #1131
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'); | ||||||
|
|
||||||
| 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(), | ||||||
|
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); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @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); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
This file was deleted.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
generatemethod to returnrandomString.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
generatemethod which will always return same.