EmailLoginCommandTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 lib\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() {
  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->once())
  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. $this->userManager->expects($this->once())
  55. ->method('getByEmail')
  56. ->with($this->username)
  57. ->willReturn([
  58. $this->createMock(IUser::class),
  59. $this->createMock(IUser::class),
  60. ]);
  61. $result = $this->cmd->process($data);
  62. $this->assertTrue($result->isSuccess());
  63. }
  64. public function testProcessUidIsEmail() {
  65. $email = 'user@domain.com';
  66. $data = $this->getFailedLoginData();
  67. $data->setUsername($email);
  68. $emailUser = $this->createMock(IUser::class);
  69. $emailUser->expects($this->any())
  70. ->method('getUID')
  71. ->willReturn($email);
  72. $this->userManager->expects($this->once())
  73. ->method('getByEmail')
  74. ->with($email)
  75. ->willReturn([
  76. $emailUser,
  77. ]);
  78. $this->userManager->expects($this->never())
  79. ->method('checkPassword');
  80. $result = $this->cmd->process($data);
  81. $this->assertTrue($result->isSuccess());
  82. $this->assertFalse($data->getUser());
  83. $this->assertEquals($email, $data->getUsername());
  84. }
  85. public function testProcessWrongPassword() {
  86. $email = 'user@domain.com';
  87. $data = $this->getFailedLoginData();
  88. $data->setUsername($email);
  89. $emailUser = $this->createMock(IUser::class);
  90. $emailUser->expects($this->any())
  91. ->method('getUID')
  92. ->willReturn('user2');
  93. $this->userManager->expects($this->once())
  94. ->method('getByEmail')
  95. ->with($email)
  96. ->willReturn([
  97. $emailUser,
  98. ]);
  99. $this->userManager->expects($this->once())
  100. ->method('checkPassword')
  101. ->with(
  102. 'user2',
  103. $this->password
  104. )
  105. ->willReturn(false);
  106. $result = $this->cmd->process($data);
  107. $this->assertTrue($result->isSuccess());
  108. $this->assertFalse($data->getUser());
  109. $this->assertEquals($email, $data->getUsername());
  110. }
  111. public function testProcess() {
  112. $email = 'user@domain.com';
  113. $data = $this->getFailedLoginData();
  114. $data->setUsername($email);
  115. $emailUser = $this->createMock(IUser::class);
  116. $emailUser->expects($this->any())
  117. ->method('getUID')
  118. ->willReturn('user2');
  119. $this->userManager->expects($this->once())
  120. ->method('getByEmail')
  121. ->with($email)
  122. ->willReturn([
  123. $emailUser,
  124. ]);
  125. $this->userManager->expects($this->once())
  126. ->method('checkPassword')
  127. ->with(
  128. 'user2',
  129. $this->password
  130. )
  131. ->willReturn($emailUser);
  132. $result = $this->cmd->process($data);
  133. $this->assertTrue($result->isSuccess());
  134. $this->assertEquals($emailUser, $data->getUser());
  135. $this->assertEquals('user2', $data->getUsername());
  136. }
  137. }