PasswordConfirmationMiddlewareTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Controller;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\ISession;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use Test\TestCase;
  33. class PasswordConfirmationMiddlewareTest extends TestCase {
  34. /** @var ControllerMethodReflector */
  35. private $reflector;
  36. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  37. private $session;
  38. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  39. private $userSession;
  40. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */
  41. private $user;
  42. /** @var PasswordConfirmationMiddleware */
  43. private $middleware;
  44. /** @var Controller */
  45. private $contoller;
  46. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  47. private $timeFactory;
  48. protected function setUp() {
  49. $this->reflector = new ControllerMethodReflector();
  50. $this->session = $this->createMock(ISession::class);
  51. $this->userSession = $this->createMock(IUserSession::class);
  52. $this->user = $this->createMock(IUser::class);
  53. $this->contoller = $this->createMock(Controller::class);
  54. $this->timeFactory = $this->createMock(ITimeFactory::class);
  55. $this->middleware = new PasswordConfirmationMiddleware(
  56. $this->reflector,
  57. $this->session,
  58. $this->userSession,
  59. $this->timeFactory
  60. );
  61. }
  62. public function testNoAnnotation() {
  63. $this->reflector->reflect(__CLASS__, __FUNCTION__);
  64. $this->session->expects($this->never())
  65. ->method($this->anything());
  66. $this->userSession->expects($this->never())
  67. ->method($this->anything());
  68. $this->middleware->beforeController($this->contoller, __FUNCTION__);
  69. }
  70. /**
  71. * @TestAnnotation
  72. */
  73. public function testDifferentAnnotation() {
  74. $this->reflector->reflect(__CLASS__, __FUNCTION__);
  75. $this->session->expects($this->never())
  76. ->method($this->anything());
  77. $this->userSession->expects($this->never())
  78. ->method($this->anything());
  79. $this->middleware->beforeController($this->contoller, __FUNCTION__);
  80. }
  81. /**
  82. * @PasswordConfirmationRequired
  83. * @dataProvider dataProvider
  84. */
  85. public function testAnnotation($backend, $lastConfirm, $currentTime, $exception) {
  86. $this->reflector->reflect(__CLASS__, __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->contoller, __FUNCTION__);
  99. } catch (NotConfirmedException $e) {
  100. $thrown = true;
  101. }
  102. $this->assertSame($exception, $thrown);
  103. }
  104. public function dataProvider() {
  105. return [
  106. ['foo', 2000, 4000, true],
  107. ['foo', 2000, 3000, false],
  108. ['user_saml', 2000, 4000, false],
  109. ['user_saml', 2000, 3000, false],
  110. ['foo', 2000, 3815, false],
  111. ['foo', 2000, 3816, true],
  112. ];
  113. }
  114. }