1
0

SettingsControllerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Authentication\Token\IProvider as IAuthTokenProvider;
  35. use OCP\IL10N;
  36. use OCP\IRequest;
  37. use OCP\IUser;
  38. use OCP\IUserManager;
  39. use OCP\Security\ICrypto;
  40. use OCP\Security\ISecureRandom;
  41. use Test\TestCase;
  42. /**
  43. * @group DB
  44. */
  45. class SettingsControllerTest extends TestCase {
  46. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  47. private $request;
  48. /** @var ClientMapper|\PHPUnit\Framework\MockObject\MockObject */
  49. private $clientMapper;
  50. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  51. private $secureRandom;
  52. /** @var AccessTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
  53. private $accessTokenMapper;
  54. /** @var IAuthTokenProvider|\PHPUnit\Framework\MockObject\MockObject */
  55. private $authTokenProvider;
  56. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  57. private $userManager;
  58. /** @var SettingsController */
  59. private $settingsController;
  60. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  61. private $l;
  62. /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
  63. private $crypto;
  64. protected function setUp(): void {
  65. parent::setUp();
  66. $this->request = $this->createMock(IRequest::class);
  67. $this->clientMapper = $this->createMock(ClientMapper::class);
  68. $this->secureRandom = $this->createMock(ISecureRandom::class);
  69. $this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
  70. $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class);
  71. $this->userManager = $this->createMock(IUserManager::class);
  72. $this->crypto = $this->createMock(ICrypto::class);
  73. $this->l = $this->createMock(IL10N::class);
  74. $this->l->method('t')
  75. ->willReturnArgument(0);
  76. $this->settingsController = new SettingsController(
  77. 'oauth2',
  78. $this->request,
  79. $this->clientMapper,
  80. $this->secureRandom,
  81. $this->accessTokenMapper,
  82. $this->l,
  83. $this->authTokenProvider,
  84. $this->userManager,
  85. $this->crypto
  86. );
  87. }
  88. public function testAddClient() {
  89. $this->secureRandom
  90. ->expects($this->exactly(2))
  91. ->method('generate')
  92. ->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  93. ->willReturnOnConsecutiveCalls(
  94. 'MySecret',
  95. 'MyClientIdentifier');
  96. $this->crypto
  97. ->expects($this->once())
  98. ->method('encrypt')
  99. ->willReturn('MyEncryptedSecret');
  100. $client = new Client();
  101. $client->setName('My Client Name');
  102. $client->setRedirectUri('https://example.com/');
  103. $client->setSecret('MySecret');
  104. $client->setClientIdentifier('MyClientIdentifier');
  105. $this->clientMapper
  106. ->expects($this->once())
  107. ->method('insert')
  108. ->with($this->callback(function (Client $c) {
  109. return $c->getName() === 'My Client Name' &&
  110. $c->getRedirectUri() === 'https://example.com/' &&
  111. $c->getSecret() === 'MyEncryptedSecret' &&
  112. $c->getClientIdentifier() === 'MyClientIdentifier';
  113. }))->willReturnCallback(function (Client $c) {
  114. $c->setId(42);
  115. return $c;
  116. });
  117. $result = $this->settingsController->addClient('My Client Name', 'https://example.com/');
  118. $this->assertInstanceOf(JSONResponse::class, $result);
  119. $data = $result->getData();
  120. $this->assertEquals([
  121. 'id' => 42,
  122. 'name' => 'My Client Name',
  123. 'redirectUri' => 'https://example.com/',
  124. 'clientId' => 'MyClientIdentifier',
  125. 'clientSecret' => 'MySecret',
  126. ], $data);
  127. }
  128. public function testDeleteClient() {
  129. $userManager = \OC::$server->getUserManager();
  130. // count other users in the db before adding our own
  131. $count = 0;
  132. $function = function (IUser $user) use (&$count) {
  133. $count++;
  134. };
  135. $userManager->callForAllUsers($function);
  136. $user1 = $userManager->createUser('test101', 'test101');
  137. $tokenProviderMock = $this->getMockBuilder(IAuthTokenProvider::class)->getMock();
  138. // expect one call per user and ensure the correct client name
  139. $tokenProviderMock
  140. ->expects($this->exactly($count + 1))
  141. ->method('invalidateTokensOfUser')
  142. ->with($this->isType('string'), 'My Client Name');
  143. $client = new Client();
  144. $client->setId(123);
  145. $client->setName('My Client Name');
  146. $client->setRedirectUri('https://example.com/');
  147. $client->setSecret('MySecret');
  148. $client->setClientIdentifier('MyClientIdentifier');
  149. $this->clientMapper
  150. ->method('getByUid')
  151. ->with(123)
  152. ->willReturn($client);
  153. $this->accessTokenMapper
  154. ->expects($this->once())
  155. ->method('deleteByClientId')
  156. ->with(123);
  157. $this->clientMapper
  158. ->expects($this->once())
  159. ->method('delete')
  160. ->with($client);
  161. $settingsController = new SettingsController(
  162. 'oauth2',
  163. $this->request,
  164. $this->clientMapper,
  165. $this->secureRandom,
  166. $this->accessTokenMapper,
  167. $this->l,
  168. $tokenProviderMock,
  169. $userManager,
  170. $this->crypto
  171. );
  172. $result = $settingsController->deleteClient(123);
  173. $this->assertInstanceOf(JSONResponse::class, $result);
  174. $this->assertEquals([], $result->getData());
  175. $user1->delete();
  176. }
  177. public function testInvalidRedirectUri() {
  178. $result = $this->settingsController->addClient('test', 'invalidurl');
  179. $this->assertEquals(Http::STATUS_BAD_REQUEST, $result->getStatus());
  180. $this->assertSame(['message' => 'Your redirect URL needs to be a full URL for example: https://yourdomain.com/path'], $result->getData());
  181. }
  182. }