AppPasswordControllerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Tests\Core\Controller;
  8. use OC\Authentication\Exceptions\InvalidTokenException;
  9. use OC\Authentication\Token\IProvider;
  10. use OC\Authentication\Token\IToken;
  11. use OC\Core\Controller\AppPasswordController;
  12. use OC\User\Session;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\AppFramework\OCS\OCSForbiddenException;
  15. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  16. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  17. use OCP\Authentication\LoginCredentials\ICredentials;
  18. use OCP\Authentication\LoginCredentials\IStore;
  19. use OCP\EventDispatcher\IEventDispatcher;
  20. use OCP\IRequest;
  21. use OCP\ISession;
  22. use OCP\IUserManager;
  23. use OCP\Security\Bruteforce\IThrottler;
  24. use OCP\Security\ISecureRandom;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. use Test\TestCase;
  27. class AppPasswordControllerTest extends TestCase {
  28. /** @var ISession|MockObject */
  29. private $session;
  30. /** @var ISecureRandom|MockObject */
  31. private $random;
  32. /** @var IProvider|MockObject */
  33. private $tokenProvider;
  34. /** @var IStore|MockObject */
  35. private $credentialStore;
  36. /** @var IRequest|MockObject */
  37. private $request;
  38. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  39. private $eventDispatcher;
  40. /** @var Session|MockObject */
  41. private $userSession;
  42. /** @var IUserManager|MockObject */
  43. private $userManager;
  44. /** @var IThrottler|MockObject */
  45. private $throttler;
  46. /** @var AppPasswordController */
  47. private $controller;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->session = $this->createMock(ISession::class);
  51. $this->random = $this->createMock(ISecureRandom::class);
  52. $this->tokenProvider = $this->createMock(IProvider::class);
  53. $this->credentialStore = $this->createMock(IStore::class);
  54. $this->request = $this->createMock(IRequest::class);
  55. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  56. $this->userSession = $this->createMock(Session::class);
  57. $this->userManager = $this->createMock(IUserManager::class);
  58. $this->throttler = $this->createMock(IThrottler::class);
  59. $this->controller = new AppPasswordController(
  60. 'core',
  61. $this->request,
  62. $this->session,
  63. $this->random,
  64. $this->tokenProvider,
  65. $this->credentialStore,
  66. $this->eventDispatcher,
  67. $this->userSession,
  68. $this->userManager,
  69. $this->throttler
  70. );
  71. }
  72. public function testGetAppPasswordWithAppPassword(): void {
  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(): void {
  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(): void {
  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('dispatchTyped');
  122. $this->controller->getAppPassword();
  123. }
  124. public function testGetAppPasswordNoPassword(): void {
  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('dispatchTyped');
  158. $this->controller->getAppPassword();
  159. }
  160. public function testDeleteAppPasswordNoAppPassword(): void {
  161. $this->session->method('exists')
  162. ->with('app_password')
  163. ->willReturn(false);
  164. $this->expectException(OCSForbiddenException::class);
  165. $this->controller->deleteAppPassword();
  166. }
  167. public function testDeleteAppPasswordFails(): void {
  168. $this->session->method('exists')
  169. ->with('app_password')
  170. ->willReturn(true);
  171. $this->session->method('get')
  172. ->with('app_password')
  173. ->willReturn('myAppPassword');
  174. $this->tokenProvider->method('getToken')
  175. ->with('myAppPassword')
  176. ->willThrowException(new InvalidTokenException());
  177. $this->expectException(OCSForbiddenException::class);
  178. $this->controller->deleteAppPassword();
  179. }
  180. public function testDeleteAppPasswordSuccess(): void {
  181. $this->session->method('exists')
  182. ->with('app_password')
  183. ->willReturn(true);
  184. $this->session->method('get')
  185. ->with('app_password')
  186. ->willReturn('myAppPassword');
  187. $token = $this->createMock(IToken::class);
  188. $this->tokenProvider->method('getToken')
  189. ->with('myAppPassword')
  190. ->willReturn($token);
  191. $token->method('getUID')
  192. ->willReturn('myUID');
  193. $token->method('getId')
  194. ->willReturn(42);
  195. $this->tokenProvider->expects($this->once())
  196. ->method('invalidateTokenById')
  197. ->with(
  198. 'myUID',
  199. 42
  200. );
  201. $result = $this->controller->deleteAppPassword();
  202. $this->assertEquals(new DataResponse(), $result);
  203. }
  204. }