Skip to content

Commit

Permalink
OHRM5X-2166: Complete oauth unit tests (orangehrm#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
RajithaKumara authored Jun 29, 2023
1 parent 713c5aa commit 3c39f52
Show file tree
Hide file tree
Showing 11 changed files with 696 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/plugins/orangehrmCoreOAuthPlugin/Dao/OAuthClientDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ private function getOAuthClientQueryBuilderWrapper(OAuthClientSearchFilterParams
*/
public function getOAuthClientById(int $id): ?OAuthClient
{
$oauthClient = $this->getRepository(OAuthClient::class)->find($id);
if ($oauthClient instanceof OAuthClient) {
return $oauthClient;
}
return null;
return $this->getRepository(OAuthClient::class)->find($id);
}

/**
Expand All @@ -86,11 +82,7 @@ public function getOAuthClientById(int $id): ?OAuthClient
*/
public function getOAuthClientByClientId(string $clientId): ?OAuthClient
{
$oauthClient = $this->getRepository(OAuthClient::class)->findOneBy(['clientId' => $clientId]);
if ($oauthClient instanceof OAuthClient) {
return $oauthClient;
}
return null;
return $this->getRepository(OAuthClient::class)->findOneBy(['clientId' => $clientId]);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/plugins/orangehrmCoreOAuthPlugin/Dto/Entity/ScopeEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
* all the essential functionalities required for any enterprise.
* Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
*
* OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/

namespace OrangeHRM\OAuth\Dto\Entity;

use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\Traits\ScopeTrait;

class ScopeEntity implements ScopeEntityInterface
{
use ScopeTrait;

private string $identifier;

/**
* @param string $identifier
*/
public function __construct(string $identifier)
{
$this->identifier = $identifier;
}

/**
* @inheritDoc
*/
public function getIdentifier(): string
{
return $this->identifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function createPsr7Request(Request $request): ServerRequestInterface
* @param \Symfony\Component\HttpFoundation\Response|Response $response
* @return ResponseInterface
*/
public function createPsr7Response(Response $response): ResponseInterface
public function createPsr7Response($response): ResponseInterface
{
return $this->getPsrHttpFactory()->createResponse($response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public function testGetOAuthClientById(): void
$this->assertNull($client);
}

public function testGetOAuthClientByClientId(): void
{
$client = $this->oAuthClientDao->getOAuthClientByClientId('85c5ce5fe84ee8dc2035378d9b35f04dfabf9e8e0aa7eb636cb0d90ed5c7f906');

$this->assertEquals("ohrm-mobile-updated", $client->getName());
$this->assertEquals(1, $client->getId());

$client = $this->oAuthClientDao->getOAuthClientByClientId('invalid-id');
$this->assertNull($client);
}

public function testDeleteOAuthClients(): void
{
$this->oAuthClientDao->deleteOAuthClients([1]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
* all the essential functionalities required for any enterprise.
* Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
*
* OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/

namespace OrangeHRM\Tests\OAuth\Dto\Entity;

use DateInterval;
use DateTime;
use DateTimeImmutable;
use OrangeHRM\Core\Service\DateTimeHelperService;
use OrangeHRM\Framework\Services;
use OrangeHRM\OAuth\Dto\Entity\AuthCodeEntity;
use OrangeHRM\OAuth\Dto\Entity\ClientEntity;
use OrangeHRM\OAuth\Dto\Entity\ScopeEntity;
use OrangeHRM\OAuth\Server\OAuthServer;
use OrangeHRM\Tests\Util\KernelTestCase;

/**
* @group OAuth
* @group Entity
*/
class AuthCodeEntityTest extends KernelTestCase
{
public function testEntity(): void
{
$oauthServer = $this->getMockBuilder(OAuthServer::class)
->onlyMethods(['getAuthCodeTTL'])
->getMock();
$oauthServer->expects($this->atLeastOnce())
->method('getAuthCodeTTL')
->willReturnCallback(fn () => new DateInterval('PT5M'));
$dateTimeHelper = $this->getMockBuilder(DateTimeHelperService::class)
->onlyMethods(['getNow'])
->getMock();
$dateTimeHelper->expects($this->atLeastOnce())
->method('getNow')
->willReturnCallback(fn () => new DateTime('2024-02-29 23:59:59'));
$this->createKernelWithMockServices([
Services::DATETIME_HELPER_SERVICE => $dateTimeHelper,
Services::OAUTH_SERVER => $oauthServer
]);
$clientEntity = new ClientEntity(5, 'client-1', 'https://example.org/callback', false, 'Client');
$scopeEntity = new ScopeEntity('root');

$authCodeEntity = new AuthCodeEntity();
$authCodeEntity->setUserIdentifier(10);
$authCodeEntity->setExpiryDateTime(new DateTimeImmutable('2023-04-28 23:59:59'));
$authCodeEntity->setIdentifier('qwertyui');
$authCodeEntity->setRedirectUri('https://example.org/callback');
$authCodeEntity->setClient($clientEntity);
$authCodeEntity->addScope($scopeEntity);

$this->assertEquals(10, $authCodeEntity->getUserIdentifier());
$this->assertEquals('2024-03-01 00:04:59', $authCodeEntity->getExpiryDateTime()->format('Y-m-d H:i:s'));
$this->assertEquals('qwertyui', $authCodeEntity->getIdentifier());
$this->assertEquals('https://example.org/callback', $authCodeEntity->getRedirectUri());
$this->assertEquals(5, $authCodeEntity->getClient()->getIdentifier());
$this->assertEquals('client-1', $authCodeEntity->getClient()->getName());
$this->assertEquals('root', $authCodeEntity->getScopes()[0]->getIdentifier());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
* all the essential functionalities required for any enterprise.
* Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
*
* OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/

namespace OrangeHRM\Tests\OAuth\Dto\Entity;

use OrangeHRM\Entity\User;
use OrangeHRM\OAuth\Dto\Entity\UserEntity;
use OrangeHRM\Tests\Util\TestCase;

/**
* @group OAuth
* @group Entity
*/
class UserEntityTest extends TestCase
{
public function testCreateFromEntity(): void
{
$user = new User();
$user->setId(100);

$userEntity = UserEntity::createFromEntity($user);
$this->assertEquals(100, $userEntity->getIdentifier());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,22 @@ public function testIsAccessTokenRevokedForNonExistingAccessToken(): void
$this->assertFalse($revoked);
// Invalid state, should check whether access token exist before execute this method
}

public function testGetAccessToken(): void
{
$accessToken = $this->accessTokenRepository->getAccessToken(
'05a81084f60f6440c8bd2555200836584e365210aee54ffee8e9dc04c7ec0068a6cde45ef5999e47'
);
$this->assertEquals(1, $accessToken->getId());

$accessToken = $this->accessTokenRepository->getAccessToken(
'388b9ae63e03fafa079ef891142828dc869e5b5f67c6094614492259ce95794e3f18a9057b667eae'
);
$this->assertEquals(2, $accessToken->getId());

$accessToken = $this->accessTokenRepository->getAccessToken(
'invalid-token'
);
$this->assertNull($accessToken);
}
}
Loading

0 comments on commit 3c39f52

Please sign in to comment.