EmailLoginCommandTest.php 4.3 KB

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