AccessTokenMapperTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 OCP\AppFramework\Utility\ITimeFactory;
  10. use Test\TestCase;
  11. /**
  12. * @group DB
  13. */
  14. class AccessTokenMapperTest extends TestCase {
  15. /** @var AccessTokenMapper */
  16. private $accessTokenMapper;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->accessTokenMapper = new AccessTokenMapper(\OC::$server->getDatabaseConnection(), \OC::$server->get(ITimeFactory::class));
  20. }
  21. public function testGetByCode() {
  22. $this->accessTokenMapper->deleteByClientId(1234);
  23. $token = new AccessToken();
  24. $token->setClientId(1234);
  25. $token->setTokenId((string)time());
  26. $token->setEncryptedToken('MyEncryptedToken');
  27. $token->setHashedCode(hash('sha512', 'MyAwesomeToken'));
  28. $this->accessTokenMapper->insert($token);
  29. $token->resetUpdatedFields();
  30. $result = $this->accessTokenMapper->getByCode('MyAwesomeToken');
  31. $this->assertEquals($token, $result);
  32. $this->accessTokenMapper->delete($token);
  33. }
  34. public function testDeleteByClientId() {
  35. $this->expectException(\OCA\OAuth2\Exceptions\AccessTokenNotFoundException::class);
  36. $this->accessTokenMapper->deleteByClientId(1234);
  37. $token = new AccessToken();
  38. $token->setClientId(1234);
  39. $token->setTokenId((string)time());
  40. $token->setEncryptedToken('MyEncryptedToken');
  41. $token->setHashedCode(hash('sha512', 'MyAwesomeToken'));
  42. $this->accessTokenMapper->insert($token);
  43. $token->resetUpdatedFields();
  44. $this->accessTokenMapper->deleteByClientId(1234);
  45. $this->accessTokenMapper->getByCode('MyAwesomeToken');
  46. }
  47. }