AuthTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\security;
  9. use lithium\security\Auth;
  10. use lithium\storage\Session;
  11. class AuthTest extends \lithium\test\Unit {
  12. protected $_classes = array(
  13. 'mockAuthAdapter' => 'lithium\tests\mocks\security\auth\adapter\MockAuthAdapter'
  14. );
  15. public function setUp() {
  16. Session::config(array(
  17. 'test' => array('adapter' => 'Memory')
  18. ));
  19. Auth::config(array(
  20. 'test' => array(
  21. 'adapter' => $this->_classes['mockAuthAdapter']
  22. )
  23. ));
  24. }
  25. public function testBasicAuthCheck() {
  26. $this->assertFalse(Auth::check('test'));
  27. $user = array('user' => 'bob');
  28. $result = Auth::check('test', $user, array('success' => true));
  29. $this->assertEqual($user, $result);
  30. $result = Session::read('test');
  31. $this->assertEqual($user, $result);
  32. $result = Auth::check('test');
  33. $this->assertEqual($user, $result);
  34. }
  35. public function testAuthLogout() {
  36. $user = array('user' => 'bob');
  37. $result = Auth::check('test', $user, array('success' => true));
  38. $this->assertEqual($user, $result);
  39. $result = Auth::check('test');
  40. $this->assertEqual($user, $result);
  41. Auth::clear('test');
  42. $this->assertFalse(Auth::check('test'));
  43. }
  44. public function testManualSessionInitialization() {
  45. $this->assertFalse(Auth::check('test'));
  46. $user = array('id' => 13, 'user' => 'bob');
  47. $this->assertTrue(Auth::set('test', $user));
  48. $result = Auth::check('test');
  49. $this->assertEqual($user, $result);
  50. }
  51. public function testManualSessionFail() {
  52. $this->assertFalse(Auth::check('test'));
  53. $user = array('id' => 13, 'user' => 'bob');
  54. $this->assertFalse(Auth::set('test', $user, array('fail' => true)));
  55. $this->assertFalse(Auth::check('test'));
  56. }
  57. public function testNoConfigurations() {
  58. Auth::reset();
  59. $this->assertIdentical(array(), Auth::config());
  60. $this->expectException("Configuration `user` has not been defined.");
  61. Auth::check('user');
  62. }
  63. public function testAuthPersist() {
  64. Auth::reset();
  65. Auth::config(array(
  66. 'test' => array(
  67. 'adapter' => $this->_classes['mockAuthAdapter'],
  68. )
  69. ));
  70. $config = Auth::config();
  71. $this->assertTrue(isset($config['test']['session']['persist']));
  72. $this->assertTrue(empty($config['test']['session']['persist']));
  73. $user = array('username' => 'foo', 'password' => 'bar');
  74. $result = Auth::check('test', $user, array('success' => true));
  75. $this->assertTrue(isset($result['username']));
  76. $this->assertFalse(isset($result['password']));
  77. Auth::reset();
  78. Auth::config(array(
  79. 'test' => array(
  80. 'adapter' => $this->_classes['mockAuthAdapter'],
  81. 'session' => array(
  82. 'persist' => array('username', 'email')
  83. )
  84. )
  85. ));
  86. $user = array(
  87. 'username' => 'foobar',
  88. 'password' => 'not!important',
  89. 'email' => '[email protected]',
  90. 'insuranceNumer' => 1234567
  91. );
  92. $expected = array(
  93. 'username' => 'foobar',
  94. 'email' => '[email protected]'
  95. );
  96. $result = Auth::check('test', $user, array('success' => true, 'checkSession' => false));
  97. $this->assertEqual($expected, $result);
  98. $this->assertEqual($expected, Session::read('test'));
  99. Auth::reset();
  100. Auth::config(array(
  101. 'test' => array(
  102. 'adapter' => $this->_classes['mockAuthAdapter'],
  103. )
  104. ));
  105. $user = array(
  106. 'id' => '123',
  107. 'username' => 'foobar',
  108. 'password' => 'not!important',
  109. 'email' => '[email protected]',
  110. 'insuranceNumer' => 1234567
  111. );
  112. $expected = 123;
  113. $result = Auth::check('test', $user, array('keyOnly' => true, 'checkSession' => false));
  114. $this->assertEqual($expected, $result);
  115. $this->assertEqual($expected, Session::read('test'));
  116. }
  117. }
  118. ?>