StoreTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  67. public function testGetLoginCredentialsNoTokenProvider() {
  68. $this->store = new Store($this->session, $this->logger, null);
  69. $this->expectException(CredentialsUnavailableException::class);
  70. $this->store->getLoginCredentials();
  71. }
  72. public function testGetLoginCredentials() {
  73. $uid = 'uid';
  74. $user = 'user123';
  75. $password = 'passme';
  76. $token = $this->createMock(IToken::class);
  77. $this->session->expects($this->once())
  78. ->method('getId')
  79. ->willReturn('sess2233');
  80. $this->tokenProvider->expects($this->once())
  81. ->method('getToken')
  82. ->with('sess2233')
  83. ->willReturn($token);
  84. $token->expects($this->once())
  85. ->method('getUID')
  86. ->willReturn($uid);
  87. $token->expects($this->once())
  88. ->method('getLoginName')
  89. ->willReturn($user);
  90. $this->tokenProvider->expects($this->once())
  91. ->method('getPassword')
  92. ->with($token, 'sess2233')
  93. ->willReturn($password);
  94. $expected = new Credentials($uid, $user, $password);
  95. $creds = $this->store->getLoginCredentials();
  96. $this->assertEquals($expected, $creds);
  97. }
  98. public function testGetLoginCredentialsSessionNotAvailable() {
  99. $this->session->expects($this->once())
  100. ->method('getId')
  101. ->will($this->throwException(new SessionNotAvailableException()));
  102. $this->expectException(CredentialsUnavailableException::class);
  103. $this->store->getLoginCredentials();
  104. }
  105. public function testGetLoginCredentialsInvalidToken() {
  106. $this->session->expects($this->once())
  107. ->method('getId')
  108. ->willReturn('sess2233');
  109. $this->tokenProvider->expects($this->once())
  110. ->method('getToken')
  111. ->with('sess2233')
  112. ->will($this->throwException(new InvalidTokenException()));
  113. $this->expectException(CredentialsUnavailableException::class);
  114. $this->store->getLoginCredentials();
  115. }
  116. public function testGetLoginCredentialsInvalidTokenLoginCredentials() {
  117. $uid = 'user987';
  118. $password = '7389374';
  119. $this->session->expects($this->once())
  120. ->method('getId')
  121. ->willReturn('sess2233');
  122. $this->tokenProvider->expects($this->once())
  123. ->method('getToken')
  124. ->with('sess2233')
  125. ->will($this->throwException(new InvalidTokenException()));
  126. $this->session->expects($this->once())
  127. ->method('exists')
  128. ->with($this->equalTo('login_credentials'))
  129. ->willReturn(true);
  130. $this->session->expects($this->once())
  131. ->method('get')
  132. ->with($this->equalTo('login_credentials'))
  133. ->willReturn('{"run":true,"uid":"user987","password":"7389374"}');
  134. $expected = new Credentials('user987', 'user987', '7389374');
  135. $actual = $this->store->getLoginCredentials();
  136. $this->assertEquals($expected, $actual);
  137. }
  138. public function testGetLoginCredentialsPasswordlessToken() {
  139. $this->session->expects($this->once())
  140. ->method('getId')
  141. ->willReturn('sess2233');
  142. $this->tokenProvider->expects($this->once())
  143. ->method('getToken')
  144. ->with('sess2233')
  145. ->will($this->throwException(new PasswordlessTokenException()));
  146. $this->expectException(CredentialsUnavailableException::class);
  147. $this->store->getLoginCredentials();
  148. }
  149. }