ClientMapperTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Client;
  8. use OCA\OAuth2\Db\ClientMapper;
  9. use OCA\OAuth2\Exceptions\ClientNotFoundException;
  10. use Test\TestCase;
  11. /**
  12. * @group DB
  13. */
  14. class ClientMapperTest extends TestCase {
  15. /** @var ClientMapper */
  16. private $clientMapper;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->clientMapper = new ClientMapper(\OC::$server->getDatabaseConnection());
  20. }
  21. protected function tearDown(): void {
  22. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  23. $query->delete('oauth2_clients')->execute();
  24. parent::tearDown();
  25. }
  26. public function testGetByIdentifier(): void {
  27. $client = new Client();
  28. $client->setClientIdentifier('MyAwesomeClientIdentifier');
  29. $client->setName('Client Name');
  30. $client->setRedirectUri('https://example.com/');
  31. $client->setSecret('TotallyNotSecret');
  32. $this->clientMapper->insert($client);
  33. $client->resetUpdatedFields();
  34. $this->assertEquals($client, $this->clientMapper->getByIdentifier('MyAwesomeClientIdentifier'));
  35. }
  36. public function testGetByIdentifierNotExisting(): void {
  37. $this->expectException(ClientNotFoundException::class);
  38. $this->clientMapper->getByIdentifier('MyTotallyNotExistingClient');
  39. }
  40. public function testGetByUid(): void {
  41. $client = new Client();
  42. $client->setClientIdentifier('MyNewClient');
  43. $client->setName('Client Name');
  44. $client->setRedirectUri('https://example.com/');
  45. $client->setSecret('TotallyNotSecret');
  46. $this->clientMapper->insert($client);
  47. $client->resetUpdatedFields();
  48. $this->assertEquals($client, $this->clientMapper->getByUid($client->getId()));
  49. }
  50. public function testGetByUidNotExisting(): void {
  51. $this->expectException(ClientNotFoundException::class);
  52. $this->clientMapper->getByUid(1234);
  53. }
  54. public function testGetClients(): void {
  55. $this->assertSame('array', gettype($this->clientMapper->getClients()));
  56. }
  57. public function testInsertLongEncryptedSecret(): void {
  58. $client = new Client();
  59. $client->setClientIdentifier('MyNewClient');
  60. $client->setName('Client Name');
  61. $client->setRedirectUri('https://example.com/');
  62. $client->setSecret('b81dc8e2dc178817bf28ca7b37265aa96559ca02e6dcdeb74b42221d096ed5ef63681e836ae0ba1077b5fb5e6c2fa7748c78463f66fe0110c8dcb8dd7eb0305b16d0cd993e2ae275879994a2abf88c68|e466d9befa6b0102341458e45ecd551a|013af9e277374483123437f180a3b0371a411ad4f34c451547909769181a7d7cc191f0f5c2de78376d124dd7751b8c9660aabdd913f5e071fc6b819ba2e3d919|3');
  63. $this->clientMapper->insert($client);
  64. $this->assertTrue(true);
  65. }
  66. }