View source
<?php
namespace Drupal\basic_auth\Tests\Authentication;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Url;
use Drupal\basic_auth\Tests\BasicAuthTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
class BasicAuthTest extends WebTestBase {
use BasicAuthTestTrait;
public static $modules = array(
'basic_auth',
'router_test',
'locale',
);
public function testBasicAuth() {
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
$account = $this
->drupalCreateUser();
$url = Url::fromRoute('router_test.11');
$this
->basicAuthGet($url, $account
->getUsername(), $account->pass_raw);
$this
->assertText($account
->getUsername(), 'Account name is displayed.');
$this
->assertResponse('200', 'HTTP response is OK');
$this
->curlClose();
$this
->assertFalse($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->assertIdentical(strpos($this
->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'Cache-Control is not set to public');
$this
->basicAuthGet($url, $account
->getUsername(), $this
->randomMachineName());
$this
->assertNoText($account
->getUsername(), 'Bad basic auth credentials do not authenticate the user.');
$this
->assertResponse('403', 'Access is not granted.');
$this
->curlClose();
$this
->drupalGet($url);
$this
->assertEqual($this
->drupalGetHeader('WWW-Authenticate'), SafeMarkup::format('Basic realm="@realm"', [
'@realm' => \Drupal::config('system.site')
->get('name'),
]));
$this
->assertResponse('401', 'Not authenticated on the route that allows only basic_auth. Prompt to authenticate received.');
$this
->drupalGet('admin');
$this
->assertResponse('403', 'No authentication prompt for routes not explicitly defining authentication providers.');
$account = $this
->drupalCreateUser(array(
'access administration pages',
));
$this
->basicAuthGet(Url::fromRoute('system.admin'), $account
->getUsername(), $account->pass_raw);
$this
->assertNoLink('Log out', 'User is not logged in');
$this
->assertResponse('403', 'No basic authentication for routes not explicitly defining authentication providers.');
$this
->curlClose();
$url = Url::fromRoute('router_test.10');
$this
->drupalGet($url);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->basicAuthGet($url, $account
->getUsername(), $account->pass_raw);
$this
->assertFalse($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->assertIdentical(strpos($this
->drupalGetHeader('Cache-Control'), 'public'), FALSE, 'No page cache response when requesting a cached page with basic auth credentials.');
}
function testGlobalLoginFloodControl() {
$this
->config('user.flood')
->set('ip_limit', 2)
->set('user_limit', 4000)
->save();
$user = $this
->drupalCreateUser(array());
$incorrect_user = clone $user;
$incorrect_user->pass_raw .= 'incorrect';
$url = Url::fromRoute('router_test.11');
for ($i = 0; $i < 2; $i++) {
$this
->basicAuthGet($url, $incorrect_user
->getUsername(), $incorrect_user->pass_raw);
}
$this
->basicAuthGet($url, $user
->getUsername(), $user->pass_raw);
$this
->assertResponse('403', 'Access is blocked because of IP based flood prevention.');
}
function testPerUserLoginFloodControl() {
$this
->config('user.flood')
->set('ip_limit', 4000)
->set('user_limit', 2)
->save();
$user = $this
->drupalCreateUser(array());
$incorrect_user = clone $user;
$incorrect_user->pass_raw .= 'incorrect';
$user2 = $this
->drupalCreateUser(array());
$url = Url::fromRoute('router_test.11');
$this
->basicAuthGet($url, $incorrect_user
->getUsername(), $incorrect_user->pass_raw);
$this
->basicAuthGet($url, $user
->getUsername(), $user->pass_raw);
$this
->assertResponse('200', 'Per user flood prevention gets reset on a successful login.');
for ($i = 0; $i < 2; $i++) {
$this
->basicAuthGet($url, $incorrect_user
->getUsername(), $incorrect_user->pass_raw);
}
$this
->basicAuthGet($url, $user
->getUsername(), $user->pass_raw);
$this
->assertResponse('403', 'The user account is blocked due to per user flood prevention.');
$this
->basicAuthGet($url, $user2
->getUsername(), $user2->pass_raw);
$this
->assertResponse('200', 'Per user flood prevention does not block access for other users.');
}
function testLocale() {
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->config('system.site')
->set('default_langcode', 'de')
->save();
$account = $this
->drupalCreateUser();
$url = Url::fromRoute('router_test.11');
$this
->basicAuthGet($url, $account
->getUsername(), $account->pass_raw);
$this
->assertText($account
->getUsername(), 'Account name is displayed.');
$this
->assertResponse('200', 'HTTP response is OK');
$this
->curlClose();
}
function testUnauthorizedErrorMessage() {
$account = $this
->drupalCreateUser();
$url = Url::fromRoute('router_test.11');
$this
->drupalGet($url);
$this
->assertResponse('401', 'The user is blocked when no credentials are passed.');
$this
->assertNoText('Exception', "No raw exception is displayed on the page.");
$this
->assertText('Please log in to access this page.', "A user friendly access unauthorized message is displayed.");
$this
->basicAuthGet($url, NULL, NULL);
$this
->assertResponse('403', 'The user is blocked when empty credentials are passed.');
$this
->assertText('Access denied', "A user friendly access denied message is displayed");
$this
->basicAuthGet($url, $account
->getUsername(), $this
->randomMachineName());
$this
->assertResponse('403', 'The user is blocked when wrong credentials are passed.');
$this
->assertText('Access denied', "A user friendly access denied message is displayed");
}
}