ClientMapperTest.php 2.6 KB

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