/lib/tests/authlib_test.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 194 lines · 115 code · 47 blank · 32 comment · 1 complexity · 2574ee47c6649f1a0f710467dbd419f3 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Authentication related tests.
  18. *
  19. * @package core_auth
  20. * @category phpunit
  21. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Functional test for authentication related APIs.
  27. */
  28. class authlib_testcase extends advanced_testcase {
  29. public function test_lockout() {
  30. global $CFG;
  31. require_once("$CFG->libdir/authlib.php");
  32. $this->resetAfterTest();
  33. $oldlog = ini_get('error_log');
  34. ini_set('error_log', "$CFG->dataroot/testlog.log"); // Prevent standard logging.
  35. set_config('lockoutthreshold', 0);
  36. set_config('lockoutwindow', 60*20);
  37. set_config('lockoutduration', 60*30);
  38. $user = $this->getDataGenerator()->create_user();
  39. // Test lockout is disabled when threshold not set.
  40. $this->assertFalse(login_is_lockedout($user));
  41. login_attempt_failed($user);
  42. login_attempt_failed($user);
  43. login_attempt_failed($user);
  44. login_attempt_failed($user);
  45. $this->assertFalse(login_is_lockedout($user));
  46. // Test lockout threshold works.
  47. set_config('lockoutthreshold', 3);
  48. login_attempt_failed($user);
  49. login_attempt_failed($user);
  50. $this->assertFalse(login_is_lockedout($user));
  51. ob_start();
  52. login_attempt_failed($user);
  53. $output = ob_get_clean();
  54. $this->assertContains('noemailever', $output);
  55. $this->assertTrue(login_is_lockedout($user));
  56. // Test unlock works.
  57. login_unlock_account($user);
  58. $this->assertFalse(login_is_lockedout($user));
  59. // Test lockout window works.
  60. login_attempt_failed($user);
  61. login_attempt_failed($user);
  62. $this->assertFalse(login_is_lockedout($user));
  63. set_user_preference('login_failed_last', time()-60*20-10, $user);
  64. login_attempt_failed($user);
  65. $this->assertFalse(login_is_lockedout($user));
  66. // Test valid login resets window.
  67. login_attempt_valid($user);
  68. $this->assertFalse(login_is_lockedout($user));
  69. login_attempt_failed($user);
  70. login_attempt_failed($user);
  71. $this->assertFalse(login_is_lockedout($user));
  72. // Test lock duration works.
  73. ob_start(); // Prevent nomailever notice.
  74. login_attempt_failed($user);
  75. $output = ob_get_clean();
  76. $this->assertContains('noemailever', $output);
  77. $this->assertTrue(login_is_lockedout($user));
  78. set_user_preference('login_lockout', time()-60*30+10, $user);
  79. $this->assertTrue(login_is_lockedout($user));
  80. set_user_preference('login_lockout', time()-60*30-10, $user);
  81. $this->assertFalse(login_is_lockedout($user));
  82. // Test lockout ignored pref works.
  83. set_user_preference('login_lockout_ignored', 1, $user);
  84. login_attempt_failed($user);
  85. login_attempt_failed($user);
  86. login_attempt_failed($user);
  87. login_attempt_failed($user);
  88. $this->assertFalse(login_is_lockedout($user));
  89. ini_set('error_log', $oldlog);
  90. }
  91. public function test_authenticate_user_login() {
  92. global $CFG;
  93. $this->resetAfterTest();
  94. $oldlog = ini_get('error_log');
  95. ini_set('error_log', "$CFG->dataroot/testlog.log"); // Prevent standard logging.
  96. set_config('lockoutthreshold', 0);
  97. set_config('lockoutwindow', 60*20);
  98. set_config('lockoutduration', 60*30);
  99. $_SERVER['HTTP_USER_AGENT'] = 'no browser'; // Hack around missing user agent in CLI scripts.
  100. $user1 = $this->getDataGenerator()->create_user(array('username'=>'username1', 'password'=>'password1'));
  101. $user2 = $this->getDataGenerator()->create_user(array('username'=>'username2', 'password'=>'password2', 'suspended'=>1));
  102. $user3 = $this->getDataGenerator()->create_user(array('username'=>'username3', 'password'=>'password3', 'auth'=>'nologin'));
  103. $result = authenticate_user_login('username1', 'password1');
  104. $this->assertInstanceOf('stdClass', $result);
  105. $this->assertEquals($user1->id, $result->id);
  106. $reason = null;
  107. $result = authenticate_user_login('username1', 'password1', false, $reason);
  108. $this->assertInstanceOf('stdClass', $result);
  109. $this->assertEquals(AUTH_LOGIN_OK, $reason);
  110. $reason = null;
  111. $result = authenticate_user_login('username1', 'nopass', false, $reason);
  112. $this->assertFalse($result);
  113. $this->assertEquals(AUTH_LOGIN_FAILED, $reason);
  114. $reason = null;
  115. $result = authenticate_user_login('username2', 'password2', false, $reason);
  116. $this->assertFalse($result);
  117. $this->assertEquals(AUTH_LOGIN_SUSPENDED, $reason);
  118. $reason = null;
  119. $result = authenticate_user_login('username3', 'password3', false, $reason);
  120. $this->assertFalse($result);
  121. $this->assertEquals(AUTH_LOGIN_SUSPENDED, $reason);
  122. $reason = null;
  123. $result = authenticate_user_login('username4', 'password3', false, $reason);
  124. $this->assertFalse($result);
  125. $this->assertEquals(AUTH_LOGIN_NOUSER, $reason);
  126. set_config('lockoutthreshold', 3);
  127. $reason = null;
  128. $result = authenticate_user_login('username1', 'nopass', false, $reason);
  129. $this->assertFalse($result);
  130. $this->assertEquals(AUTH_LOGIN_FAILED, $reason);
  131. $result = authenticate_user_login('username1', 'nopass', false, $reason);
  132. $this->assertFalse($result);
  133. $this->assertEquals(AUTH_LOGIN_FAILED, $reason);
  134. ob_start(); // Prevent nomailever notice.
  135. $result = authenticate_user_login('username1', 'nopass', false, $reason);
  136. ob_end_clean();
  137. $this->assertFalse($result);
  138. $this->assertEquals(AUTH_LOGIN_FAILED, $reason);
  139. $result = authenticate_user_login('username1', 'password1', false, $reason);
  140. $this->assertFalse($result);
  141. $this->assertEquals(AUTH_LOGIN_LOCKOUT, $reason);
  142. $result = authenticate_user_login('username1', 'password1', true, $reason);
  143. $this->assertInstanceOf('stdClass', $result);
  144. $this->assertEquals(AUTH_LOGIN_OK, $reason);
  145. ini_set('error_log', $oldlog);
  146. }
  147. }