AppPasswordControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Tests\Core\Controller;
  25. use OC\Authentication\Token\IProvider;
  26. use OC\Authentication\Token\IToken;
  27. use OC\Core\Controller\AppPasswordController;
  28. use OCP\AppFramework\OCS\OCSForbiddenException;
  29. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  30. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  31. use OCP\Authentication\LoginCredentials\ICredentials;
  32. use OCP\Authentication\LoginCredentials\IStore;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\Security\ISecureRandom;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  38. use Test\TestCase;
  39. class AppPasswordControllerTest extends TestCase {
  40. /** @var ISession|MockObject */
  41. private $session;
  42. /** @var ISecureRandom|MockObject */
  43. private $random;
  44. /** @var IProvider|MockObject */
  45. private $tokenProvider;
  46. /** @var IStore|MockObject */
  47. private $credentialStore;
  48. /** @var IRequest|MockObject */
  49. private $request;
  50. /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
  51. private $eventDispatcher;
  52. /** @var AppPasswordController */
  53. private $controller;
  54. public function setUp() {
  55. parent::setUp();
  56. $this->session = $this->createMock(ISession::class);
  57. $this->random = $this->createMock(ISecureRandom::class);
  58. $this->tokenProvider = $this->createMock(IProvider::class);
  59. $this->credentialStore = $this->createMock(IStore::class);
  60. $this->request = $this->createMock(IRequest::class);
  61. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  62. $this->controller = new AppPasswordController(
  63. 'core',
  64. $this->request,
  65. $this->session,
  66. $this->random,
  67. $this->tokenProvider,
  68. $this->credentialStore,
  69. $this->eventDispatcher
  70. );
  71. }
  72. public function testGetAppPasswordWithAppPassword() {
  73. $this->session->method('exists')
  74. ->with('app_password')
  75. ->willReturn(true);
  76. $this->expectException(OCSForbiddenException::class);
  77. $this->controller->getAppPassword();
  78. }
  79. public function testGetAppPasswordNoLoginCreds() {
  80. $this->session->method('exists')
  81. ->with('app_password')
  82. ->willReturn(false);
  83. $this->credentialStore->method('getLoginCredentials')
  84. ->willThrowException(new CredentialsUnavailableException());
  85. $this->expectException(OCSForbiddenException::class);
  86. $this->controller->getAppPassword();
  87. }
  88. public function testGetAppPassword() {
  89. $credentials = $this->createMock(ICredentials::class);
  90. $this->session->method('exists')
  91. ->with('app_password')
  92. ->willReturn(false);
  93. $this->credentialStore->method('getLoginCredentials')
  94. ->willReturn($credentials);
  95. $credentials->method('getUid')
  96. ->willReturn('myUID');
  97. $credentials->method('getPassword')
  98. ->willReturn('myPassword');
  99. $credentials->method('getLoginName')
  100. ->willReturn('myLoginName');
  101. $this->request->method('getHeader')
  102. ->with('USER_AGENT')
  103. ->willReturn('myUA');
  104. $this->random->method('generate')
  105. ->with(
  106. 72,
  107. ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
  108. )->willReturn('myToken');
  109. $this->tokenProvider->expects($this->once())
  110. ->method('generateToken')
  111. ->with(
  112. 'myToken',
  113. 'myUID',
  114. 'myLoginName',
  115. 'myPassword',
  116. 'myUA',
  117. IToken::PERMANENT_TOKEN,
  118. IToken::DO_NOT_REMEMBER
  119. );
  120. $this->eventDispatcher->expects($this->once())
  121. ->method('dispatch');
  122. $this->controller->getAppPassword();
  123. }
  124. public function testGetAppPasswordNoPassword() {
  125. $credentials = $this->createMock(ICredentials::class);
  126. $this->session->method('exists')
  127. ->with('app_password')
  128. ->willReturn(false);
  129. $this->credentialStore->method('getLoginCredentials')
  130. ->willReturn($credentials);
  131. $credentials->method('getUid')
  132. ->willReturn('myUID');
  133. $credentials->method('getPassword')
  134. ->willThrowException(new PasswordUnavailableException());
  135. $credentials->method('getLoginName')
  136. ->willReturn('myLoginName');
  137. $this->request->method('getHeader')
  138. ->with('USER_AGENT')
  139. ->willReturn('myUA');
  140. $this->random->method('generate')
  141. ->with(
  142. 72,
  143. ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
  144. )->willReturn('myToken');
  145. $this->tokenProvider->expects($this->once())
  146. ->method('generateToken')
  147. ->with(
  148. 'myToken',
  149. 'myUID',
  150. 'myLoginName',
  151. null,
  152. 'myUA',
  153. IToken::PERMANENT_TOKEN,
  154. IToken::DO_NOT_REMEMBER
  155. );
  156. $this->eventDispatcher->expects($this->once())
  157. ->method('dispatch');
  158. $this->controller->getAppPassword();
  159. }
  160. }