SettingsControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author rakekniven <mark.ziegler@rakekniven.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\OAuth2\Tests\Controller;
  28. use OCA\OAuth2\Controller\SettingsController;
  29. use OCA\OAuth2\Db\AccessTokenMapper;
  30. use OCA\OAuth2\Db\Client;
  31. use OCA\OAuth2\Db\ClientMapper;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\Security\ISecureRandom;
  37. use Test\TestCase;
  38. class SettingsControllerTest extends TestCase {
  39. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  40. private $request;
  41. /** @var ClientMapper|\PHPUnit\Framework\MockObject\MockObject */
  42. private $clientMapper;
  43. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  44. private $secureRandom;
  45. /** @var AccessTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
  46. private $accessTokenMapper;
  47. /** @var SettingsController */
  48. private $settingsController;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->request = $this->createMock(IRequest::class);
  52. $this->clientMapper = $this->createMock(ClientMapper::class);
  53. $this->secureRandom = $this->createMock(ISecureRandom::class);
  54. $this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
  55. $l = $this->createMock(IL10N::class);
  56. $l->method('t')
  57. ->willReturnArgument(0);
  58. $this->settingsController = new SettingsController(
  59. 'oauth2',
  60. $this->request,
  61. $this->clientMapper,
  62. $this->secureRandom,
  63. $this->accessTokenMapper,
  64. $l
  65. );
  66. }
  67. public function testAddClient() {
  68. $this->secureRandom
  69. ->expects($this->exactly(2))
  70. ->method('generate')
  71. ->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  72. ->willReturnOnConsecutiveCalls(
  73. 'MySecret',
  74. 'MyClientIdentifier');
  75. $client = new Client();
  76. $client->setName('My Client Name');
  77. $client->setRedirectUri('https://example.com/');
  78. $client->setSecret('MySecret');
  79. $client->setClientIdentifier('MyClientIdentifier');
  80. $this->clientMapper
  81. ->expects($this->once())
  82. ->method('insert')
  83. ->with($this->callback(function (Client $c) {
  84. return $c->getName() === 'My Client Name' &&
  85. $c->getRedirectUri() === 'https://example.com/' &&
  86. $c->getSecret() === 'MySecret' &&
  87. $c->getClientIdentifier() === 'MyClientIdentifier';
  88. }))->willReturnCallback(function (Client $c) {
  89. $c->setId(42);
  90. return $c;
  91. });
  92. $result = $this->settingsController->addClient('My Client Name', 'https://example.com/');
  93. $this->assertInstanceOf(JSONResponse::class, $result);
  94. $data = $result->getData();
  95. $this->assertEquals([
  96. 'id' => 42,
  97. 'name' => 'My Client Name',
  98. 'redirectUri' => 'https://example.com/',
  99. 'clientId' => 'MyClientIdentifier',
  100. 'clientSecret' => 'MySecret',
  101. ], $data);
  102. }
  103. public function testDeleteClient() {
  104. $client = new Client();
  105. $client->setId(123);
  106. $client->setName('My Client Name');
  107. $client->setRedirectUri('https://example.com/');
  108. $client->setSecret('MySecret');
  109. $client->setClientIdentifier('MyClientIdentifier');
  110. $this->clientMapper
  111. ->method('getByUid')
  112. ->with(123)
  113. ->willReturn($client);
  114. $this->accessTokenMapper
  115. ->expects($this->once())
  116. ->method('deleteByClientId')
  117. ->with(123);
  118. $this->clientMapper
  119. ->method('delete')
  120. ->with($client);
  121. $result = $this->settingsController->deleteClient(123);
  122. $this->assertInstanceOf(JSONResponse::class, $result);
  123. $this->assertEquals([], $result->getData());
  124. }
  125. public function testInvalidRedirectUri() {
  126. $result = $this->settingsController->addClient('test', 'invalidurl');
  127. $this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
  128. $this->assertSame(['message' => 'Your redirect URL needs to be a full URL for example: https://yourdomain.com/path'], $result->getData());
  129. }
  130. }