AccessTokenMapperTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\OAuth2\Tests\Db;
  7. use OCA\OAuth2\Db\AccessToken;
  8. use OCA\OAuth2\Db\AccessTokenMapper;
  9. use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use Test\TestCase;
  12. /**
  13. * @group DB
  14. */
  15. class AccessTokenMapperTest extends TestCase {
  16. /** @var AccessTokenMapper */
  17. private $accessTokenMapper;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->accessTokenMapper = new AccessTokenMapper(\OC::$server->getDatabaseConnection(), \OC::$server->get(ITimeFactory::class));
  21. }
  22. public function testGetByCode(): void {
  23. $this->accessTokenMapper->deleteByClientId(1234);
  24. $token = new AccessToken();
  25. $token->setClientId(1234);
  26. $token->setTokenId((string)time());
  27. $token->setEncryptedToken('MyEncryptedToken');
  28. $token->setHashedCode(hash('sha512', 'MyAwesomeToken'));
  29. $this->accessTokenMapper->insert($token);
  30. $token->resetUpdatedFields();
  31. $result = $this->accessTokenMapper->getByCode('MyAwesomeToken');
  32. $this->assertEquals($token, $result);
  33. $this->accessTokenMapper->delete($token);
  34. }
  35. public function testDeleteByClientId(): void {
  36. $this->expectException(AccessTokenNotFoundException::class);
  37. $this->accessTokenMapper->deleteByClientId(1234);
  38. $token = new AccessToken();
  39. $token->setClientId(1234);
  40. $token->setTokenId((string)time());
  41. $token->setEncryptedToken('MyEncryptedToken');
  42. $token->setHashedCode(hash('sha512', 'MyAwesomeToken'));
  43. $this->accessTokenMapper->insert($token);
  44. $token->resetUpdatedFields();
  45. $this->accessTokenMapper->deleteByClientId(1234);
  46. $this->accessTokenMapper->getByCode('MyAwesomeToken');
  47. }
  48. }