PasswordConfirmationMiddlewareTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Middleware\Security;
  24. use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
  25. use OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware;
  26. use OC\AppFramework\Utility\ControllerMethodReflector;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IRequest;
  29. use OCP\ISession;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use Test\AppFramework\Middleware\Security\Mock\PasswordConfirmationMiddlewareController;
  33. use Test\TestCase;
  34. class PasswordConfirmationMiddlewareTest extends TestCase {
  35. /** @var ControllerMethodReflector */
  36. private $reflector;
  37. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  38. private $session;
  39. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. private $userSession;
  41. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  42. private $user;
  43. /** @var PasswordConfirmationMiddleware */
  44. private $middleware;
  45. /** @var PasswordConfirmationMiddlewareController */
  46. private $controller;
  47. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  48. private $timeFactory;
  49. protected function setUp(): void {
  50. $this->reflector = new ControllerMethodReflector();
  51. $this->session = $this->createMock(ISession::class);
  52. $this->userSession = $this->createMock(IUserSession::class);
  53. $this->user = $this->createMock(IUser::class);
  54. $this->timeFactory = $this->createMock(ITimeFactory::class);
  55. $this->controller = new PasswordConfirmationMiddlewareController(
  56. 'test',
  57. $this->createMock(IRequest::class)
  58. );
  59. $this->middleware = new PasswordConfirmationMiddleware(
  60. $this->reflector,
  61. $this->session,
  62. $this->userSession,
  63. $this->timeFactory
  64. );
  65. }
  66. public function testNoAnnotationNorAttribute() {
  67. $this->reflector->reflect($this->controller, __FUNCTION__);
  68. $this->session->expects($this->never())
  69. ->method($this->anything());
  70. $this->userSession->expects($this->never())
  71. ->method($this->anything());
  72. $this->middleware->beforeController($this->controller, __FUNCTION__);
  73. }
  74. public function testDifferentAnnotation() {
  75. $this->reflector->reflect($this->controller, __FUNCTION__);
  76. $this->session->expects($this->never())
  77. ->method($this->anything());
  78. $this->userSession->expects($this->never())
  79. ->method($this->anything());
  80. $this->middleware->beforeController($this->controller, __FUNCTION__);
  81. }
  82. /**
  83. * @dataProvider dataProvider
  84. */
  85. public function testAnnotation($backend, $lastConfirm, $currentTime, $exception) {
  86. $this->reflector->reflect($this->controller, __FUNCTION__);
  87. $this->user->method('getBackendClassName')
  88. ->willReturn($backend);
  89. $this->userSession->method('getUser')
  90. ->willReturn($this->user);
  91. $this->session->method('get')
  92. ->with('last-password-confirm')
  93. ->willReturn($lastConfirm);
  94. $this->timeFactory->method('getTime')
  95. ->willReturn($currentTime);
  96. $thrown = false;
  97. try {
  98. $this->middleware->beforeController($this->controller, __FUNCTION__);
  99. } catch (NotConfirmedException $e) {
  100. $thrown = true;
  101. }
  102. $this->assertSame($exception, $thrown);
  103. }
  104. /**
  105. * @dataProvider dataProvider
  106. */
  107. public function testAttribute($backend, $lastConfirm, $currentTime, $exception) {
  108. $this->reflector->reflect($this->controller, __FUNCTION__);
  109. $this->user->method('getBackendClassName')
  110. ->willReturn($backend);
  111. $this->userSession->method('getUser')
  112. ->willReturn($this->user);
  113. $this->session->method('get')
  114. ->with('last-password-confirm')
  115. ->willReturn($lastConfirm);
  116. $this->timeFactory->method('getTime')
  117. ->willReturn($currentTime);
  118. $thrown = false;
  119. try {
  120. $this->middleware->beforeController($this->controller, __FUNCTION__);
  121. } catch (NotConfirmedException $e) {
  122. $thrown = true;
  123. }
  124. $this->assertSame($exception, $thrown);
  125. }
  126. public function dataProvider() {
  127. return [
  128. ['foo', 2000, 4000, true],
  129. ['foo', 2000, 3000, false],
  130. ['user_saml', 2000, 4000, false],
  131. ['user_saml', 2000, 3000, false],
  132. ['foo', 2000, 3815, false],
  133. ['foo', 2000, 3816, true],
  134. ];
  135. }
  136. }