View source
<?php
namespace Drupal\auto_login_url\Tests;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
class AutoLoginUrlTest extends BrowserTestBase {
protected static $modules = [
'auto_login_url',
];
protected $profile = 'minimal';
protected function setUp() : void {
parent::setUp();
$role = Role::load('authenticated');
$role
->grantPermission('use auto login url');
$role
->save();
$role = Role::load('anonymous');
$role
->grantPermission('use auto login url');
$role
->save();
$this->additionalCurlOptions = [
CURLOPT_FOLLOWLOCATION => TRUE,
];
}
public function testAluTokenGenerationCheck() {
$user = $this
->createUser([
'use auto login url',
]);
$url = auto_login_url_create($user
->get('uid')->value, 'user/' . $user
->get('uid')->value);
$this
->drupalGet($url);
$this
->assertResponse(200, t('User logged in successfully.'));
$this
->assertText($user
->get('name')->value, t('User name is visible, hence user is logged in.'));
$user2 = $this
->createUser([
'use auto login url',
]);
$url = auto_login_url_create($user2
->get('uid')->value, 'user/' . $user2
->get('uid')->value);
$this
->drupalGet($url);
$this
->assertResponse(200, t('User 2 logged in successfully.'));
$this
->assertText($user2
->get('name')->value, t('User 2 name is visible, hence user is logged in.'));
}
public function testAluSettingsCheck() {
$config = $this
->config('auto_login_url.settings');
$config
->set('secret', 'new secret')
->save();
$config
->set('token_length', 8)
->save();
$user = $this
->createUser([
'use auto login url',
]);
$url = auto_login_url_create($user
->get('uid')->value, 'user/' . $user
->get('uid')->value);
$this
->drupalGet($url);
$this
->assertResponse(200, t('User logged in successfully.'));
$this
->assertText($user
->get('name')->value, t('User name is visible, hence user is logged in.'));
}
public function testAluFloodCheck() {
$flood_config = $this
->config('user.flood');
$flood_config
->set('ip_limit', 5)
->save();
$user = $this
->createUser([
'use auto login url',
]);
for ($i = 1; $i < 6; $i++) {
$this
->drupalGet('autologinurl/' . $i . '/some-token' . $i);
$this
->assertResponse(403, t('Got access denied page.'));
}
$url = auto_login_url_create($user
->get('uid')->value, 'user/' . $user
->get('uid')->value);
$this
->drupalGet($url);
$this
->assertResponse(403, t('Got access denied page.'));
$this
->assertText(t('Sorry, too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later.'), t('Cannot login message visible.'));
$connection = \Drupal::database();
$connection
->truncate('flood')
->execute();
$this
->drupalGet($url);
$this
->assertResponse(200, t('User logged in successfully.'));
$this
->assertText($user
->get('name')->value, t('User name is visible, hence user is logged in.'));
}
}