From 4451bba3db93677b3ab2cdaa614302a532a7e432 Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Wed, 29 Jul 2026 15:46:44 +0545 Subject: [PATCH] test(php): add phpunit tests for OauthService Signed-off-by: Saw-jan --- tests/lib/Service/OauthServiceTest.php | 155 +++++++++++++++++++++++++ tests/lib/Service/OauthSeviceTest.php | 38 ------ 2 files changed, 155 insertions(+), 38 deletions(-) create mode 100644 tests/lib/Service/OauthServiceTest.php delete mode 100644 tests/lib/Service/OauthSeviceTest.php diff --git a/tests/lib/Service/OauthServiceTest.php b/tests/lib/Service/OauthServiceTest.php new file mode 100644 index 000000000..dc7f18454 --- /dev/null +++ b/tests/lib/Service/OauthServiceTest.php @@ -0,0 +1,155 @@ +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(), + ]; + + $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); + } + + /** + * @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); + } + + /** + * @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); + } +} diff --git a/tests/lib/Service/OauthSeviceTest.php b/tests/lib/Service/OauthSeviceTest.php deleted file mode 100644 index e5642f158..000000000 --- a/tests/lib/Service/OauthSeviceTest.php +++ /dev/null @@ -1,38 +0,0 @@ -getMockBuilder(ClientMapper::class)->disableOriginalConstructor()->getMock(); - } - if ($iSecureRandomMock === null) { - $iSecureRandomMock = $this->getMockBuilder(ISecureRandom::class)->getMock(); - } - if ($iCryptoMock === null) { - $iCryptoMock = $this->getMockBuilder(ICrypto::class)->getMock(); - } - - return new OauthService( - $clientMapperMock, - $iSecureRandomMock, - $iCryptoMock - ); - } -}