You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.1 KiB

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