StoreTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Authentication\LoginCredentials;
  7. use OC\Authentication\Exceptions\InvalidTokenException;
  8. use OC\Authentication\Exceptions\PasswordlessTokenException;
  9. use OC\Authentication\LoginCredentials\Credentials;
  10. use OC\Authentication\LoginCredentials\Store;
  11. use OC\Authentication\Token\IProvider;
  12. use OC\Authentication\Token\IToken;
  13. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  14. use OCP\ISession;
  15. use OCP\Session\Exceptions\SessionNotAvailableException;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. use function json_encode;
  19. class StoreTest extends TestCase {
  20. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  21. private $session;
  22. /** @var IProvider|\PHPUnit\Framework\MockObject\MockObject */
  23. private $tokenProvider;
  24. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  25. private $logger;
  26. /** @var Store */
  27. private $store;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->session = $this->createMock(ISession::class);
  31. $this->tokenProvider = $this->createMock(IProvider::class);
  32. $this->logger = $this->createMock(LoggerInterface::class);
  33. $this->store = new Store($this->session, $this->logger, $this->tokenProvider);
  34. }
  35. public function testAuthenticate() {
  36. $params = [
  37. 'run' => true,
  38. 'uid' => 'user123',
  39. 'password' => 123456,
  40. ];
  41. $this->session->expects($this->once())
  42. ->method('set')
  43. ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params)));
  44. $this->store->authenticate($params);
  45. }
  46. public function testSetSession() {
  47. $session = $this->createMock(ISession::class);
  48. $this->store->setSession($session);
  49. $this->addToAssertionCount(1);
  50. }
  51. public function testGetLoginCredentialsNoTokenProvider() {
  52. $this->store = new Store($this->session, $this->logger, null);
  53. $this->expectException(CredentialsUnavailableException::class);
  54. $this->store->getLoginCredentials();
  55. }
  56. public function testGetLoginCredentials() {
  57. $uid = 'uid';
  58. $user = 'user123';
  59. $password = 'passme';
  60. $token = $this->createMock(IToken::class);
  61. $this->session->expects($this->once())
  62. ->method('getId')
  63. ->willReturn('sess2233');
  64. $this->tokenProvider->expects($this->once())
  65. ->method('getToken')
  66. ->with('sess2233')
  67. ->willReturn($token);
  68. $token->expects($this->once())
  69. ->method('getUID')
  70. ->willReturn($uid);
  71. $token->expects($this->once())
  72. ->method('getLoginName')
  73. ->willReturn($user);
  74. $this->tokenProvider->expects($this->once())
  75. ->method('getPassword')
  76. ->with($token, 'sess2233')
  77. ->willReturn($password);
  78. $expected = new Credentials($uid, $user, $password);
  79. $creds = $this->store->getLoginCredentials();
  80. $this->assertEquals($expected, $creds);
  81. }
  82. public function testGetLoginCredentialsSessionNotAvailable() {
  83. $this->session->expects($this->once())
  84. ->method('getId')
  85. ->will($this->throwException(new SessionNotAvailableException()));
  86. $this->expectException(CredentialsUnavailableException::class);
  87. $this->store->getLoginCredentials();
  88. }
  89. public function testGetLoginCredentialsInvalidToken() {
  90. $this->session->expects($this->once())
  91. ->method('getId')
  92. ->willReturn('sess2233');
  93. $this->tokenProvider->expects($this->once())
  94. ->method('getToken')
  95. ->with('sess2233')
  96. ->will($this->throwException(new InvalidTokenException()));
  97. $this->expectException(CredentialsUnavailableException::class);
  98. $this->store->getLoginCredentials();
  99. }
  100. public function testGetLoginCredentialsPartialCredentialsAndSessionName() {
  101. $uid = 'id987';
  102. $user = 'user987';
  103. $password = '7389374';
  104. $this->session->expects($this->once())
  105. ->method('getId')
  106. ->willReturn('sess2233');
  107. $this->tokenProvider->expects($this->once())
  108. ->method('getToken')
  109. ->with('sess2233')
  110. ->will($this->throwException(new InvalidTokenException()));
  111. $this->session->expects($this->once())
  112. ->method('exists')
  113. ->with($this->equalTo('login_credentials'))
  114. ->willReturn(true);
  115. $this->session->expects($this->exactly(2))
  116. ->method('get')
  117. ->willReturnMap([
  118. [
  119. 'login_credentials',
  120. json_encode([
  121. 'uid' => $uid,
  122. 'password' => $password,
  123. ])
  124. ],
  125. [
  126. 'loginname',
  127. $user,
  128. ],
  129. ]);
  130. $expected = new Credentials($uid, $user, $password);
  131. $actual = $this->store->getLoginCredentials();
  132. $this->assertEquals($expected, $actual);
  133. }
  134. public function testGetLoginCredentialsPartialCredentials() {
  135. $uid = 'id987';
  136. $password = '7389374';
  137. $this->session->expects($this->once())
  138. ->method('getId')
  139. ->willReturn('sess2233');
  140. $this->tokenProvider->expects($this->once())
  141. ->method('getToken')
  142. ->with('sess2233')
  143. ->will($this->throwException(new InvalidTokenException()));
  144. $this->session->expects($this->once())
  145. ->method('exists')
  146. ->with($this->equalTo('login_credentials'))
  147. ->willReturn(true);
  148. $this->session->expects($this->exactly(2))
  149. ->method('get')
  150. ->willReturnMap([
  151. [
  152. 'login_credentials',
  153. json_encode([
  154. 'uid' => $uid,
  155. 'password' => $password,
  156. ])
  157. ],
  158. [
  159. 'loginname',
  160. null,
  161. ],
  162. ]);
  163. $expected = new Credentials($uid, $uid, $password);
  164. $actual = $this->store->getLoginCredentials();
  165. $this->assertEquals($expected, $actual);
  166. }
  167. public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
  168. $uid = 'id987';
  169. $user = 'user987';
  170. $password = '7389374';
  171. $this->session->expects($this->once())
  172. ->method('getId')
  173. ->willReturn('sess2233');
  174. $this->tokenProvider->expects($this->once())
  175. ->method('getToken')
  176. ->with('sess2233')
  177. ->will($this->throwException(new InvalidTokenException()));
  178. $this->session->expects($this->once())
  179. ->method('exists')
  180. ->with($this->equalTo('login_credentials'))
  181. ->willReturn(true);
  182. $this->session->expects($this->once())
  183. ->method('get')
  184. ->with($this->equalTo('login_credentials'))
  185. ->willReturn('{"run":true,"uid":"id987","loginName":"user987","password":"7389374"}');
  186. $expected = new Credentials($uid, $user, $password);
  187. $actual = $this->store->getLoginCredentials();
  188. $this->assertEquals($expected, $actual);
  189. }
  190. public function testGetLoginCredentialsPasswordlessToken() {
  191. $this->session->expects($this->once())
  192. ->method('getId')
  193. ->willReturn('sess2233');
  194. $this->tokenProvider->expects($this->once())
  195. ->method('getToken')
  196. ->with('sess2233')
  197. ->will($this->throwException(new PasswordlessTokenException()));
  198. $this->expectException(CredentialsUnavailableException::class);
  199. $this->store->getLoginCredentials();
  200. }
  201. }