ClientMapperTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\OAuth2\Tests\Db;
  26. use OCA\OAuth2\Db\Client;
  27. use OCA\OAuth2\Db\ClientMapper;
  28. use Test\TestCase;
  29. /**
  30. * @group DB
  31. */
  32. class ClientMapperTest extends TestCase {
  33. /** @var ClientMapper */
  34. private $clientMapper;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->clientMapper = new ClientMapper(\OC::$server->getDatabaseConnection());
  38. }
  39. protected function tearDown(): void {
  40. $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  41. $query->delete('oauth2_clients')->execute();
  42. parent::tearDown();
  43. }
  44. public function testGetByIdentifier() {
  45. $client = new Client();
  46. $client->setClientIdentifier('MyAwesomeClientIdentifier');
  47. $client->setName('Client Name');
  48. $client->setRedirectUri('https://example.com/');
  49. $client->setSecret('TotallyNotSecret');
  50. $this->clientMapper->insert($client);
  51. $client->resetUpdatedFields();
  52. $this->assertEquals($client, $this->clientMapper->getByIdentifier('MyAwesomeClientIdentifier'));
  53. }
  54. public function testGetByIdentifierNotExisting() {
  55. $this->expectException(\OCA\OAuth2\Exceptions\ClientNotFoundException::class);
  56. $this->clientMapper->getByIdentifier('MyTotallyNotExistingClient');
  57. }
  58. public function testGetByUid() {
  59. $client = new Client();
  60. $client->setClientIdentifier('MyNewClient');
  61. $client->setName('Client Name');
  62. $client->setRedirectUri('https://example.com/');
  63. $client->setSecret('TotallyNotSecret');
  64. $this->clientMapper->insert($client);
  65. $client->resetUpdatedFields();
  66. $this->assertEquals($client, $this->clientMapper->getByUid($client->getId()));
  67. }
  68. public function testGetByUidNotExisting() {
  69. $this->expectException(\OCA\OAuth2\Exceptions\ClientNotFoundException::class);
  70. $this->clientMapper->getByUid(1234);
  71. }
  72. public function testGetClients() {
  73. $this->assertSame('array', gettype($this->clientMapper->getClients()));
  74. }
  75. public function testInsertLongEncryptedSecret(): void {
  76. $client = new Client();
  77. $client->setClientIdentifier('MyNewClient');
  78. $client->setName('Client Name');
  79. $client->setRedirectUri('https://example.com/');
  80. $client->setSecret('b81dc8e2dc178817bf28ca7b37265aa96559ca02e6dcdeb74b42221d096ed5ef63681e836ae0ba1077b5fb5e6c2fa7748c78463f66fe0110c8dcb8dd7eb0305b16d0cd993e2ae275879994a2abf88c68|e466d9befa6b0102341458e45ecd551a|013af9e277374483123437f180a3b0371a411ad4f34c451547909769181a7d7cc191f0f5c2de78376d124dd7751b8c9660aabdd913f5e071fc6b819ba2e3d919|3');
  81. $this->clientMapper->insert($client);
  82. $this->assertTrue(true);
  83. }
  84. }