StoreTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2016 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. */
  23. namespace Test\Authentication\LoginCredentials;
  24. use OC\Authentication\Exceptions\InvalidTokenException;
  25. use OC\Authentication\Exceptions\PasswordlessTokenException;
  26. use OC\Authentication\LoginCredentials\Credentials;
  27. use OC\Authentication\LoginCredentials\Store;
  28. use OC\Authentication\Token\IProvider;
  29. use OC\Authentication\Token\IToken;
  30. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  31. use OCP\ILogger;
  32. use OCP\ISession;
  33. use OCP\Session\Exceptions\SessionNotAvailableException;
  34. use PHPUnit_Framework_MockObject_MockObject;
  35. use Test\TestCase;
  36. class StoreTest extends TestCase {
  37. /** @var ISession|PHPUnit_Framework_MockObject_MockObject */
  38. private $session;
  39. /** @var IProvider|PHPUnit_Framework_MockObject_MockObject */
  40. private $tokenProvider;
  41. /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
  42. private $logger;
  43. /** @var Store */
  44. private $store;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->session = $this->createMock(ISession::class);
  48. $this->tokenProvider = $this->createMock(IProvider::class);
  49. $this->logger = $this->createMock(ILogger::class);
  50. $this->store = new Store($this->session, $this->logger, $this->tokenProvider);
  51. }
  52. public function testAuthenticate() {
  53. $params = [
  54. 'run' => true,
  55. 'uid' => 'user123',
  56. 'password' => 123456,
  57. ];
  58. $this->session->expects($this->once())
  59. ->method('set')
  60. ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params)));
  61. $this->store->authenticate($params);
  62. }
  63. public function testSetSession() {
  64. $session = $this->createMock(ISession::class);
  65. $this->store->setSession($session);
  66. $this->addToAssertionCount(1);
  67. }
  68. public function testGetLoginCredentialsNoTokenProvider() {
  69. $this->store = new Store($this->session, $this->logger, null);
  70. $this->expectException(CredentialsUnavailableException::class);
  71. $this->store->getLoginCredentials();
  72. }
  73. public function testGetLoginCredentials() {
  74. $uid = 'uid';
  75. $user = 'user123';
  76. $password = 'passme';
  77. $token = $this->createMock(IToken::class);
  78. $this->session->expects($this->once())
  79. ->method('getId')
  80. ->willReturn('sess2233');
  81. $this->tokenProvider->expects($this->once())
  82. ->method('getToken')
  83. ->with('sess2233')
  84. ->willReturn($token);
  85. $token->expects($this->once())
  86. ->method('getUID')
  87. ->willReturn($uid);
  88. $token->expects($this->once())
  89. ->method('getLoginName')
  90. ->willReturn($user);
  91. $this->tokenProvider->expects($this->once())
  92. ->method('getPassword')
  93. ->with($token, 'sess2233')
  94. ->willReturn($password);
  95. $expected = new Credentials($uid, $user, $password);
  96. $creds = $this->store->getLoginCredentials();
  97. $this->assertEquals($expected, $creds);
  98. }
  99. public function testGetLoginCredentialsSessionNotAvailable() {
  100. $this->session->expects($this->once())
  101. ->method('getId')
  102. ->will($this->throwException(new SessionNotAvailableException()));
  103. $this->expectException(CredentialsUnavailableException::class);
  104. $this->store->getLoginCredentials();
  105. }
  106. public function testGetLoginCredentialsInvalidToken() {
  107. $this->session->expects($this->once())
  108. ->method('getId')
  109. ->willReturn('sess2233');
  110. $this->tokenProvider->expects($this->once())
  111. ->method('getToken')
  112. ->with('sess2233')
  113. ->will($this->throwException(new InvalidTokenException()));
  114. $this->expectException(CredentialsUnavailableException::class);
  115. $this->store->getLoginCredentials();
  116. }
  117. public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
  118. $uid = 'user987';
  119. $password = '7389374';
  120. $this->session->expects($this->once())
  121. ->method('getId')
  122. ->willReturn('sess2233');
  123. $this->tokenProvider->expects($this->once())
  124. ->method('getToken')
  125. ->with('sess2233')
  126. ->will($this->throwException(new InvalidTokenException()));
  127. $this->session->expects($this->once())
  128. ->method('exists')
  129. ->with($this->equalTo('login_credentials'))
  130. ->willReturn(true);
  131. $this->session->expects($this->once())
  132. ->method('get')
  133. ->with($this->equalTo('login_credentials'))
  134. ->willReturn('{"run":true,"uid":"user987","password":"7389374"}');
  135. $expected = new Credentials('user987', 'user987', '7389374');
  136. $actual = $this->store->getLoginCredentials();
  137. $this->assertEquals($expected, $actual);
  138. }
  139. public function testGetLoginCredentialsPasswordlessToken() {
  140. $this->session->expects($this->once())
  141. ->method('getId')
  142. ->willReturn('sess2233');
  143. $this->tokenProvider->expects($this->once())
  144. ->method('getToken')
  145. ->with('sess2233')
  146. ->will($this->throwException(new PasswordlessTokenException()));
  147. $this->expectException(CredentialsUnavailableException::class);
  148. $this->store->getLoginCredentials();
  149. }
  150. }