EmailLoginCommandTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\EmailLoginCommand;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. class EmailLoginCommandTest extends ALoginCommandTest {
  13. /** @var IUserManager|MockObject */
  14. private $userManager;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->userManager = $this->createMock(IUserManager::class);
  18. $this->cmd = new EmailLoginCommand(
  19. $this->userManager
  20. );
  21. }
  22. public function testProcessAlreadyLoggedIn(): void {
  23. $data = $this->getLoggedInLoginData();
  24. $result = $this->cmd->process($data);
  25. $this->assertTrue($result->isSuccess());
  26. }
  27. public function testProcessNotAnEmailLogin(): void {
  28. $data = $this->getFailedLoginData();
  29. $this->userManager->expects($this->never())
  30. ->method('getByEmail')
  31. ->with($this->username)
  32. ->willReturn([]);
  33. $result = $this->cmd->process($data);
  34. $this->assertTrue($result->isSuccess());
  35. }
  36. public function testProcessDuplicateEmailLogin(): void {
  37. $data = $this->getFailedLoginData();
  38. $data->setUsername('user@example.com');
  39. $this->userManager->expects($this->once())
  40. ->method('getByEmail')
  41. ->with('user@example.com')
  42. ->willReturn([
  43. $this->createMock(IUser::class),
  44. $this->createMock(IUser::class),
  45. ]);
  46. $result = $this->cmd->process($data);
  47. $this->assertTrue($result->isSuccess());
  48. }
  49. public function testProcessUidIsEmail(): void {
  50. $email = 'user@domain.com';
  51. $data = $this->getFailedLoginData();
  52. $data->setUsername($email);
  53. $emailUser = $this->createMock(IUser::class);
  54. $emailUser->expects($this->any())
  55. ->method('getUID')
  56. ->willReturn($email);
  57. $this->userManager->expects($this->once())
  58. ->method('getByEmail')
  59. ->with($email)
  60. ->willReturn([
  61. $emailUser,
  62. ]);
  63. $this->userManager->expects($this->never())
  64. ->method('checkPassword');
  65. $result = $this->cmd->process($data);
  66. $this->assertTrue($result->isSuccess());
  67. $this->assertFalse($data->getUser());
  68. $this->assertEquals($email, $data->getUsername());
  69. }
  70. public function testProcessWrongPassword(): void {
  71. $email = 'user@domain.com';
  72. $data = $this->getFailedLoginData();
  73. $data->setUsername($email);
  74. $emailUser = $this->createMock(IUser::class);
  75. $emailUser->expects($this->any())
  76. ->method('getUID')
  77. ->willReturn('user2');
  78. $this->userManager->expects($this->once())
  79. ->method('getByEmail')
  80. ->with($email)
  81. ->willReturn([
  82. $emailUser,
  83. ]);
  84. $this->userManager->expects($this->once())
  85. ->method('checkPassword')
  86. ->with(
  87. 'user2',
  88. $this->password
  89. )
  90. ->willReturn(false);
  91. $result = $this->cmd->process($data);
  92. $this->assertTrue($result->isSuccess());
  93. $this->assertFalse($data->getUser());
  94. $this->assertEquals($email, $data->getUsername());
  95. }
  96. public function testProcess(): void {
  97. $email = 'user@domain.com';
  98. $data = $this->getFailedLoginData();
  99. $data->setUsername($email);
  100. $emailUser = $this->createMock(IUser::class);
  101. $emailUser->expects($this->any())
  102. ->method('getUID')
  103. ->willReturn('user2');
  104. $this->userManager->expects($this->once())
  105. ->method('getByEmail')
  106. ->with($email)
  107. ->willReturn([
  108. $emailUser,
  109. ]);
  110. $this->userManager->expects($this->once())
  111. ->method('checkPassword')
  112. ->with(
  113. 'user2',
  114. $this->password
  115. )
  116. ->willReturn($emailUser);
  117. $result = $this->cmd->process($data);
  118. $this->assertTrue($result->isSuccess());
  119. $this->assertEquals($emailUser, $data->getUser());
  120. $this->assertEquals('user2', $data->getUsername());
  121. }
  122. }