forked from orangehrm/orangehrm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OHRM5X-2166: Complete oauth unit tests (orangehrm#1683)
- Loading branch information
1 parent
713c5aa
commit 3c39f52
Showing
11 changed files
with
696 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/plugins/orangehrmCoreOAuthPlugin/Dto/Entity/ScopeEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/plugins/orangehrmCoreOAuthPlugin/test/Dto/Entity/AuthCodeEntityTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/plugins/orangehrmCoreOAuthPlugin/test/Dto/Entity/UserEntityTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.