CreateSessionTokenCommandTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. namespace Test\Authentication\Login;
  8. use OC\Authentication\Login\CreateSessionTokenCommand;
  9. use OC\Authentication\Token\IToken;
  10. use OC\User\Session;
  11. use OCP\IConfig;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. class CreateSessionTokenCommandTest extends ALoginCommandTest {
  14. /** @var IConfig|MockObject */
  15. private $config;
  16. /** @var Session|MockObject */
  17. private $userSession;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->config = $this->createMock(IConfig::class);
  21. $this->userSession = $this->createMock(Session::class);
  22. $this->cmd = new CreateSessionTokenCommand(
  23. $this->config,
  24. $this->userSession
  25. );
  26. }
  27. public function testProcess(): void {
  28. $data = $this->getLoggedInLoginData();
  29. $this->config->expects($this->once())
  30. ->method('getSystemValueInt')
  31. ->with(
  32. 'remember_login_cookie_lifetime',
  33. 60 * 60 * 24 * 15
  34. )
  35. ->willReturn(100);
  36. $this->user->expects($this->any())
  37. ->method('getUID')
  38. ->willReturn($this->username);
  39. $this->userSession->expects($this->once())
  40. ->method('createSessionToken')
  41. ->with(
  42. $this->request,
  43. $this->username,
  44. $this->username,
  45. $this->password,
  46. IToken::REMEMBER
  47. );
  48. $this->userSession->expects($this->once())
  49. ->method('updateTokens')
  50. ->with(
  51. $this->username,
  52. $this->password
  53. );
  54. $result = $this->cmd->process($data);
  55. $this->assertTrue($result->isSuccess());
  56. }
  57. public function testProcessDoNotRemember(): void {
  58. $data = $this->getLoggedInLoginData();
  59. $this->config->expects($this->once())
  60. ->method('getSystemValueInt')
  61. ->with(
  62. 'remember_login_cookie_lifetime',
  63. 60 * 60 * 24 * 15
  64. )
  65. ->willReturn(0);
  66. $this->user->expects($this->any())
  67. ->method('getUID')
  68. ->willReturn($this->username);
  69. $this->userSession->expects($this->once())
  70. ->method('createSessionToken')
  71. ->with(
  72. $this->request,
  73. $this->username,
  74. $this->username,
  75. $this->password,
  76. IToken::DO_NOT_REMEMBER
  77. );
  78. $this->userSession->expects($this->once())
  79. ->method('updateTokens')
  80. ->with(
  81. $this->username,
  82. $this->password
  83. );
  84. $result = $this->cmd->process($data);
  85. $this->assertTrue($result->isSuccess());
  86. $this->assertFalse($data->isRememberLogin());
  87. }
  88. }