SettingsControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\OAuth2\Tests\Controller;
  22. use OC\Authentication\Token\DefaultTokenMapper;
  23. use OCA\OAuth2\Controller\SettingsController;
  24. use OCA\OAuth2\Db\AccessTokenMapper;
  25. use OCA\OAuth2\Db\Client;
  26. use OCA\OAuth2\Db\ClientMapper;
  27. use OCP\AppFramework\Http\RedirectResponse;
  28. use OCP\IRequest;
  29. use OCP\IURLGenerator;
  30. use OCP\Security\ISecureRandom;
  31. use Test\TestCase;
  32. class SettingsControllerTest extends TestCase {
  33. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  34. private $request;
  35. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  36. private $urlGenerator;
  37. /** @var ClientMapper|\PHPUnit_Framework_MockObject_MockObject */
  38. private $clientMapper;
  39. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  40. private $secureRandom;
  41. /** @var AccessTokenMapper|\PHPUnit_Framework_MockObject_MockObject */
  42. private $accessTokenMapper;
  43. /** @var DefaultTokenMapper|\PHPUnit_Framework_MockObject_MockObject */
  44. private $defaultTokenMapper;
  45. /** @var SettingsController */
  46. private $settingsController;
  47. public function setUp() {
  48. parent::setUp();
  49. $this->request = $this->createMock(IRequest::class);
  50. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  51. $this->clientMapper = $this->createMock(ClientMapper::class);
  52. $this->secureRandom = $this->createMock(ISecureRandom::class);
  53. $this->accessTokenMapper = $this->createMock(AccessTokenMapper::class);
  54. $this->defaultTokenMapper = $this->createMock(DefaultTokenMapper::class);
  55. $this->settingsController = new SettingsController(
  56. 'oauth2',
  57. $this->request,
  58. $this->urlGenerator,
  59. $this->clientMapper,
  60. $this->secureRandom,
  61. $this->accessTokenMapper,
  62. $this->defaultTokenMapper
  63. );
  64. }
  65. public function testAddClient() {
  66. $this->secureRandom
  67. ->expects($this->at(0))
  68. ->method('generate')
  69. ->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  70. ->willReturn('MySecret');
  71. $this->secureRandom
  72. ->expects($this->at(1))
  73. ->method('generate')
  74. ->with(64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  75. ->willReturn('MyClientIdentifier');
  76. $client = new Client();
  77. $client->setName('My Client Name');
  78. $client->setRedirectUri('https://example.com/');
  79. $client->setSecret('MySecret');
  80. $client->setClientIdentifier('MyClientIdentifier');
  81. $this->clientMapper
  82. ->expects($this->once())
  83. ->method('insert')
  84. ->with($client);
  85. $this->urlGenerator
  86. ->expects($this->once())
  87. ->method('getAbsoluteURL')
  88. ->with('/index.php/settings/admin/security')
  89. ->willReturn('https://example.com/index.php/settings/admin/security');
  90. $expected = new RedirectResponse('https://example.com/index.php/settings/admin/security');
  91. $this->assertEquals($expected, $this->settingsController->addClient('My Client Name', 'https://example.com/'));
  92. }
  93. public function testDeleteClient() {
  94. $client = new Client();
  95. $client->setName('My Client Name');
  96. $client->setRedirectUri('https://example.com/');
  97. $client->setSecret('MySecret');
  98. $client->setClientIdentifier('MyClientIdentifier');
  99. $this->clientMapper
  100. ->expects($this->at(0))
  101. ->method('getByUid')
  102. ->with(123)
  103. ->willReturn($client);
  104. $this->accessTokenMapper
  105. ->expects($this->once())
  106. ->method('deleteByClientId')
  107. ->with(123);
  108. $this->defaultTokenMapper
  109. ->expects($this->once())
  110. ->method('deleteByName')
  111. ->with('My Client Name');
  112. $this->clientMapper
  113. ->expects($this->at(1))
  114. ->method('delete')
  115. ->with($client);
  116. $this->urlGenerator
  117. ->expects($this->once())
  118. ->method('getAbsoluteURL')
  119. ->with('/index.php/settings/admin/security')
  120. ->willReturn('https://example.com/index.php/settings/admin/security');
  121. $expected = new RedirectResponse('https://example.com/index.php/settings/admin/security');
  122. $this->assertEquals($expected, $this->settingsController->deleteClient(123));
  123. }
  124. }