session.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\User;
  9. class Session extends \PHPUnit_Framework_TestCase {
  10. public function testGetUser() {
  11. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  12. $session->expects($this->once())
  13. ->method('get')
  14. ->with('user_id')
  15. ->will($this->returnValue('foo'));
  16. $backend = $this->getMock('OC_User_Dummy');
  17. $backend->expects($this->once())
  18. ->method('userExists')
  19. ->with('foo')
  20. ->will($this->returnValue(true));
  21. $manager = new \OC\User\Manager();
  22. $manager->registerBackend($backend);
  23. $userSession = new \OC\User\Session($manager, $session);
  24. $user = $userSession->getUser();
  25. $this->assertEquals('foo', $user->getUID());
  26. }
  27. public function testSetUser() {
  28. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  29. $session->expects($this->once())
  30. ->method('set')
  31. ->with('user_id', 'foo');
  32. $manager = $this->getMock('\OC\User\Manager');
  33. $backend = $this->getMock('OC_User_Dummy');
  34. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  35. $user->expects($this->once())
  36. ->method('getUID')
  37. ->will($this->returnValue('foo'));
  38. $userSession = new \OC\User\Session($manager, $session);
  39. $userSession->setUser($user);
  40. }
  41. public function testLoginValidPasswordEnabled() {
  42. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  43. $session->expects($this->once())
  44. ->method('set')
  45. ->with('user_id', 'foo');
  46. $manager = $this->getMock('\OC\User\Manager');
  47. $backend = $this->getMock('OC_User_Dummy');
  48. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  49. $user->expects($this->once())
  50. ->method('isEnabled')
  51. ->will($this->returnValue(true));
  52. $user->expects($this->any())
  53. ->method('getUID')
  54. ->will($this->returnValue('foo'));
  55. $manager->expects($this->once())
  56. ->method('checkPassword')
  57. ->with('foo', 'bar')
  58. ->will($this->returnValue($user));
  59. $userSession = new \OC\User\Session($manager, $session);
  60. $userSession->login('foo', 'bar');
  61. $this->assertEquals($user, $userSession->getUser());
  62. }
  63. public function testLoginValidPasswordDisabled() {
  64. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  65. $session->expects($this->never())
  66. ->method('set');
  67. $manager = $this->getMock('\OC\User\Manager');
  68. $backend = $this->getMock('OC_User_Dummy');
  69. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  70. $user->expects($this->once())
  71. ->method('isEnabled')
  72. ->will($this->returnValue(false));
  73. $manager->expects($this->once())
  74. ->method('checkPassword')
  75. ->with('foo', 'bar')
  76. ->will($this->returnValue($user));
  77. $userSession = new \OC\User\Session($manager, $session);
  78. $userSession->login('foo', 'bar');
  79. }
  80. public function testLoginInValidPassword() {
  81. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  82. $session->expects($this->never())
  83. ->method('set');
  84. $manager = $this->getMock('\OC\User\Manager');
  85. $backend = $this->getMock('OC_User_Dummy');
  86. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  87. $user->expects($this->never())
  88. ->method('isEnabled');
  89. $manager->expects($this->once())
  90. ->method('checkPassword')
  91. ->with('foo', 'bar')
  92. ->will($this->returnValue(false));
  93. $userSession = new \OC\User\Session($manager, $session);
  94. $userSession->login('foo', 'bar');
  95. }
  96. public function testLoginNonExisting() {
  97. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  98. $session->expects($this->never())
  99. ->method('set');
  100. $manager = $this->getMock('\OC\User\Manager');
  101. $backend = $this->getMock('OC_User_Dummy');
  102. $manager->expects($this->once())
  103. ->method('checkPassword')
  104. ->with('foo', 'bar')
  105. ->will($this->returnValue(false));
  106. $userSession = new \OC\User\Session($manager, $session);
  107. $userSession->login('foo', 'bar');
  108. }
  109. }