AppPasswordControllerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Token\IProvider;
  27. use OC\Authentication\Token\IToken;
  28. use OC\Core\Controller\AppPasswordController;
  29. use OC\User\Session;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSForbiddenException;
  32. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  33. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  34. use OCP\Authentication\LoginCredentials\ICredentials;
  35. use OCP\Authentication\LoginCredentials\IStore;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\IRequest;
  38. use OCP\ISession;
  39. use OCP\IUserManager;
  40. use OCP\Security\Bruteforce\IThrottler;
  41. use OCP\Security\ISecureRandom;
  42. use PHPUnit\Framework\MockObject\MockObject;
  43. use Test\TestCase;
  44. class AppPasswordControllerTest extends TestCase {
  45. /** @var ISession|MockObject */
  46. private $session;
  47. /** @var ISecureRandom|MockObject */
  48. private $random;
  49. /** @var IProvider|MockObject */
  50. private $tokenProvider;
  51. /** @var IStore|MockObject */
  52. private $credentialStore;
  53. /** @var IRequest|MockObject */
  54. private $request;
  55. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  56. private $eventDispatcher;
  57. /** @var Session|MockObject */
  58. private $userSession;
  59. /** @var IUserManager|MockObject */
  60. private $userManager;
  61. /** @var IThrottler|MockObject */
  62. private $throttler;
  63. /** @var AppPasswordController */
  64. private $controller;
  65. protected function setUp(): void {
  66. parent::setUp();
  67. $this->session = $this->createMock(ISession::class);
  68. $this->random = $this->createMock(ISecureRandom::class);
  69. $this->tokenProvider = $this->createMock(IProvider::class);
  70. $this->credentialStore = $this->createMock(IStore::class);
  71. $this->request = $this->createMock(IRequest::class);
  72. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  73. $this->userSession = $this->createMock(Session::class);
  74. $this->userManager = $this->createMock(IUserManager::class);
  75. $this->throttler = $this->createMock(IThrottler::class);
  76. $this->controller = new AppPasswordController(
  77. 'core',
  78. $this->request,
  79. $this->session,
  80. $this->random,
  81. $this->tokenProvider,
  82. $this->credentialStore,
  83. $this->eventDispatcher,
  84. $this->userSession,
  85. $this->userManager,
  86. $this->throttler
  87. );
  88. }
  89. public function testGetAppPasswordWithAppPassword() {
  90. $this->session->method('exists')
  91. ->with('app_password')
  92. ->willReturn(true);
  93. $this->expectException(OCSForbiddenException::class);
  94. $this->controller->getAppPassword();
  95. }
  96. public function testGetAppPasswordNoLoginCreds() {
  97. $this->session->method('exists')
  98. ->with('app_password')
  99. ->willReturn(false);
  100. $this->credentialStore->method('getLoginCredentials')
  101. ->willThrowException(new CredentialsUnavailableException());
  102. $this->expectException(OCSForbiddenException::class);
  103. $this->controller->getAppPassword();
  104. }
  105. public function testGetAppPassword() {
  106. $credentials = $this->createMock(ICredentials::class);
  107. $this->session->method('exists')
  108. ->with('app_password')
  109. ->willReturn(false);
  110. $this->credentialStore->method('getLoginCredentials')
  111. ->willReturn($credentials);
  112. $credentials->method('getUid')
  113. ->willReturn('myUID');
  114. $credentials->method('getPassword')
  115. ->willReturn('myPassword');
  116. $credentials->method('getLoginName')
  117. ->willReturn('myLoginName');
  118. $this->request->method('getHeader')
  119. ->with('USER_AGENT')
  120. ->willReturn('myUA');
  121. $this->random->method('generate')
  122. ->with(
  123. 72,
  124. ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
  125. )->willReturn('myToken');
  126. $this->tokenProvider->expects($this->once())
  127. ->method('generateToken')
  128. ->with(
  129. 'myToken',
  130. 'myUID',
  131. 'myLoginName',
  132. 'myPassword',
  133. 'myUA',
  134. IToken::PERMANENT_TOKEN,
  135. IToken::DO_NOT_REMEMBER
  136. );
  137. $this->eventDispatcher->expects($this->once())
  138. ->method('dispatchTyped');
  139. $this->controller->getAppPassword();
  140. }
  141. public function testGetAppPasswordNoPassword() {
  142. $credentials = $this->createMock(ICredentials::class);
  143. $this->session->method('exists')
  144. ->with('app_password')
  145. ->willReturn(false);
  146. $this->credentialStore->method('getLoginCredentials')
  147. ->willReturn($credentials);
  148. $credentials->method('getUid')
  149. ->willReturn('myUID');
  150. $credentials->method('getPassword')
  151. ->willThrowException(new PasswordUnavailableException());
  152. $credentials->method('getLoginName')
  153. ->willReturn('myLoginName');
  154. $this->request->method('getHeader')
  155. ->with('USER_AGENT')
  156. ->willReturn('myUA');
  157. $this->random->method('generate')
  158. ->with(
  159. 72,
  160. ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS
  161. )->willReturn('myToken');
  162. $this->tokenProvider->expects($this->once())
  163. ->method('generateToken')
  164. ->with(
  165. 'myToken',
  166. 'myUID',
  167. 'myLoginName',
  168. null,
  169. 'myUA',
  170. IToken::PERMANENT_TOKEN,
  171. IToken::DO_NOT_REMEMBER
  172. );
  173. $this->eventDispatcher->expects($this->once())
  174. ->method('dispatchTyped');
  175. $this->controller->getAppPassword();
  176. }
  177. public function testDeleteAppPasswordNoAppPassword() {
  178. $this->session->method('exists')
  179. ->with('app_password')
  180. ->willReturn(false);
  181. $this->expectException(OCSForbiddenException::class);
  182. $this->controller->deleteAppPassword();
  183. }
  184. public function testDeleteAppPasswordFails() {
  185. $this->session->method('exists')
  186. ->with('app_password')
  187. ->willReturn(true);
  188. $this->session->method('get')
  189. ->with('app_password')
  190. ->willReturn('myAppPassword');
  191. $this->tokenProvider->method('getToken')
  192. ->with('myAppPassword')
  193. ->willThrowException(new InvalidTokenException());
  194. $this->expectException(OCSForbiddenException::class);
  195. $this->controller->deleteAppPassword();
  196. }
  197. public function testDeleteAppPasswordSuccess() {
  198. $this->session->method('exists')
  199. ->with('app_password')
  200. ->willReturn(true);
  201. $this->session->method('get')
  202. ->with('app_password')
  203. ->willReturn('myAppPassword');
  204. $token = $this->createMock(IToken::class);
  205. $this->tokenProvider->method('getToken')
  206. ->with('myAppPassword')
  207. ->willReturn($token);
  208. $token->method('getUID')
  209. ->willReturn('myUID');
  210. $token->method('getId')
  211. ->willReturn(42);
  212. $this->tokenProvider->expects($this->once())
  213. ->method('invalidateTokenById')
  214. ->with(
  215. 'myUID',
  216. 42
  217. );
  218. $result = $this->controller->deleteAppPassword();
  219. $this->assertEquals(new DataResponse(), $result);
  220. }
  221. }