CreateSessionTokenCommandTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  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. declare(strict_types=1);
  23. namespace Test\Authentication\Login;
  24. use OC\Authentication\Login\CreateSessionTokenCommand;
  25. use OC\Authentication\Token\IToken;
  26. use OC\User\Session;
  27. use OCP\IConfig;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. class CreateSessionTokenCommandTest extends ALoginCommandTest {
  30. /** @var IConfig|MockObject */
  31. private $config;
  32. /** @var Session|MockObject */
  33. private $userSession;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->config = $this->createMock(IConfig::class);
  37. $this->userSession = $this->createMock(Session::class);
  38. $this->cmd = new CreateSessionTokenCommand(
  39. $this->config,
  40. $this->userSession
  41. );
  42. }
  43. public function testProcess() {
  44. $data = $this->getLoggedInLoginData();
  45. $this->config->expects($this->once())
  46. ->method('getSystemValueInt')
  47. ->with(
  48. 'remember_login_cookie_lifetime',
  49. 60 * 60 * 24 * 15
  50. )
  51. ->willReturn(100);
  52. $this->user->expects($this->any())
  53. ->method('getUID')
  54. ->willReturn($this->username);
  55. $this->userSession->expects($this->once())
  56. ->method('createSessionToken')
  57. ->with(
  58. $this->request,
  59. $this->username,
  60. $this->username,
  61. $this->password,
  62. IToken::REMEMBER
  63. );
  64. $this->userSession->expects($this->once())
  65. ->method('updateTokens')
  66. ->with(
  67. $this->username,
  68. $this->password
  69. );
  70. $result = $this->cmd->process($data);
  71. $this->assertTrue($result->isSuccess());
  72. }
  73. public function testProcessDoNotRemember() {
  74. $data = $this->getLoggedInLoginData();
  75. $this->config->expects($this->once())
  76. ->method('getSystemValueInt')
  77. ->with(
  78. 'remember_login_cookie_lifetime',
  79. 60 * 60 * 24 * 15
  80. )
  81. ->willReturn(0);
  82. $this->user->expects($this->any())
  83. ->method('getUID')
  84. ->willReturn($this->username);
  85. $this->userSession->expects($this->once())
  86. ->method('createSessionToken')
  87. ->with(
  88. $this->request,
  89. $this->username,
  90. $this->username,
  91. $this->password,
  92. IToken::DO_NOT_REMEMBER
  93. );
  94. $this->userSession->expects($this->once())
  95. ->method('updateTokens')
  96. ->with(
  97. $this->username,
  98. $this->password
  99. );
  100. $result = $this->cmd->process($data);
  101. $this->assertTrue($result->isSuccess());
  102. $this->assertFalse($data->isRememberLogin());
  103. }
  104. }