Skip to content
Snippets Groups Projects
Commit f0c06795 authored by Jeremy Declerck's avatar Jeremy Declerck
Browse files

Issue #3484359 by jeremy.declerck, robindh: Add test coverage to the current functionality

parent 1e6787dc
No related branches found
No related tags found
1 merge request!1#3484359: Add basic test coverage.
Pipeline #326059 passed
......@@ -8,4 +8,5 @@ parameters:
- .
ignoreErrors:
# new static() is a best practice in Drupal, so we cannot fix that.
- "#^Unsafe usage of new static#"
\ No newline at end of file
- "#^Unsafe usage of new static#"
- '#Call to an undefined method Drupal\\Tests\\WebAssert\:\:assertExpectedAjaxRequest\(\).#'
\ No newline at end of file
<?php
namespace Drupal\Tests\altcha\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
/**
* Test basic functionality for the Altcha module.
*
* @group altcha
*
* @dependencies captcha
*/
class AltchaBasicTest extends BrowserTestBase {
use StringTranslationTrait;
/**
* A normal user.
*
* @var \Drupal\user\UserInterface
*/
protected UserInterface $normalUser;
/**
* An admin user.
*
* @var \Drupal\user\UserInterface
*/
protected UserInterface $adminUser;
/**
* The hmac key.
*
* @var string
*/
protected string $secretKey;
/**
* Modules to enable.
*
* @var string[]
*/
protected static $modules = ['altcha', 'captcha'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
\Drupal::moduleHandler()->loadInclude('captcha', 'inc');
// Create a normal user.
$this->normalUser = $this->drupalCreateUser();
// Create an admin user.
$permissions = [
'administer CAPTCHA settings',
'skip CAPTCHA',
'administer permissions',
'administer altcha',
];
$this->adminUser = $this->drupalCreateUser($permissions);
}
/**
* The hmac secret key.
*/
protected function testInstallation(): void {
$this->assertNotEmpty($this->secretKey);
$this->assertEquals(64, strlen($this->secretKey));
}
/**
* Test access to the administration page.
*/
public function testAdminAccess(): void {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/config/people/captcha/altcha');
$this->assertSession()->pageTextNotContains($this->t('Access denied'));
$this->drupalLogout();
}
/**
* Test the Altcha settings form.
*/
public function testSettingsForm(): void {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/config/people/captcha/altcha');
$this->drupalLogout();
}
/**
* Testing the protection of the user login form.
*/
public function testLoginForm(): void {
$altcha_selector = '//input[@name="captcha_token"]';
// Validate login process.
$this->drupalLogin($this->normalUser);
$this->drupalLogout();
$this->drupalGet('user/login');
// Altcha should not be configured yet.
$this->assertSession()->elementNotExists('xpath', $altcha_selector);
// Enable 'altcha/ALTCHA' on login form.
captcha_set_form_id_setting('user_login_form', 'altcha/ALTCHA');
$result = captcha_get_form_id_setting('user_login_form');
// Altcha should be configured.
$this->assertNotNull($result, 'A configuration has been found for CAPTCHA point: user_login_form');
$this->assertEquals($result->getCaptchaType(), 'altcha/ALTCHA', 'Altcha type has been configured for CAPTCHA point: user_login_form');
// Test the API SAAS version.
$this->config('altcha.settings')
->set('integration_type', 'saas_api')
->save();
$this->config('altcha.settings')->set('saas_api_key', 'test')->save();
$this->config('altcha.settings')->set('saas_api_region', 'eu')->save();
$this->config('altcha.settings')->set('max_number', 20000)->save();
$options = [
'query' => [
'apiKey' => 'test',
'maxnumber' => 20000,
],
];
$this->drupalGet('user/login');
// An Altcha should exist with challengeurl matching the configuration.
$this->assertSession()->elementExists('xpath', $altcha_selector);
$this->assertSession()
->responseContains(Html::escape(Url::fromUri('https://eu.altcha.org/api/v1/challenge', $options)
->toString()));
// Test the Self-hosted version.
$this->config('altcha.settings')
->set('integration_type', 'self_hosted')
->save();
\Drupal::service('altcha.secret_manager')
->generateSecretKey();
$this->drupalGet('user/login');
$this->assertSession()->elementExists('xpath', $altcha_selector);
$this->assertSession()
->responseContains(Html::escape(Url::fromRoute('altcha.challenge')
->toString()));
// Check auto verification attribute.
$this->config('altcha.settings')
->set('auto_verification', 'onsubmit')
->save();
$this->drupalGet('user/login');
$element = $this->xpath('//altcha-widget[@auto="onsubmit"]');
$this->assertNotEmpty($element, 'auto verification should be enabled and onsubmit.');
// Check the maxnumber attribute.
$this->config('altcha.settings')->set('max_number', 10000)->save();
$this->drupalGet('user/login');
$element = $this->xpath('//altcha-widget[@maxnumber="10000"]');
$this->assertNotEmpty($element, 'maxnumber should be enabled and equal to 10000');
// Validate that the login attempt fails.
$edit['name'] = $this->normalUser->getAccountName();
$edit['pass'] = $this->normalUser->getPassword();
$this->drupalGet('user/login');
$this->submitForm($edit, $this->t('Log in'));
$this->assertSession()
->pageTextContains($this->t('The answer you entered for the CAPTCHA was not correct.'));
// Make sure the user did not start a session.
$this->assertFalse($this->drupalUserIsLoggedIn($this->normalUser));
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\altcha\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Tests Altcha javascript functionalities.
*
* @group altcha
*
* @dependencies captcha
*/
class AltchaJavascriptTest extends WebDriverTestBase {
/**
* Modules to enable.
*
* @var string[]
*/
protected static $modules = ['altcha', 'captcha'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
\Drupal::moduleHandler()->loadInclude('captcha', 'inc');
}
/**
* Click the captcha checkbox element and wait for it to be validated.
*/
public function testClickAltcha(): void {
captcha_set_form_id_setting('user_login_form', 'altcha/ALTCHA');
$this->drupalGet('user/login');
$this->assertSession()
->elementExists('xpath', '//div[@data-state="unverified"]');
$this->click('#altcha_checkbox');
// Verify that the state of the element switched to verifying after click.
$this->assertSession()
->elementExists('xpath', '//div[@data-state="verifying"]');
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\altcha\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\user\UserInterface;
/**
* Tests for ajax secret key generation.
*
* @group altcha
*
* @dependencies captcha
*/
class SecretKeyTest extends WebDriverTestBase {
/**
* An admin user.
*
* @var \Drupal\user\UserInterface
*/
protected UserInterface $adminUser;
/**
* The Altcha secret hmac key.
*
* @var string
*/
protected string $secretKey;
/**
* Modules to enable.
*
* @var string[]
*/
protected static $modules = ['altcha', 'captcha'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create an admin user.
$permissions = [
'administer CAPTCHA settings',
'administer altcha',
];
$this->adminUser = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->adminUser);
$this->secretKey = \Drupal::service('altcha.secret_manager')->getSecretKey();
}
/**
* Test Altcha hmac key generation.
*/
public function testSecretKeyGeneration(): void {
$this->drupalGet('admin/config/people/captcha/altcha');
$this->click('#edit-secret-key-regenerate');
$this->assertSession()->assertExpectedAjaxRequest(1);
$this->assertSession()
->pageTextContains('Secret key was successfully updated!');
$this->assertNotEquals(\Drupal::service('altcha.secret_manager')
->getSecretKey(), $this->secretKey, 'The hmac key did not update.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment