Skip to content

Commit

Permalink
Test with headless chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Nov 18, 2024
1 parent 495cb75 commit c17f997
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:
uses: actions/checkout@v4

- name: lint+test
working-directory: tests
run: |
docker run \
--env ENABLE_MODULES=turnstile_protect \
-v $(pwd):/var/www/drupal/web/modules/contrib/turnstile_protect \
lehighlts/drupal-ci:${DRUPAL_VERSION}-php${PHP_VERSION}
export MODULE_DIRECTORY=$(pwd | xargs dirname)
docker compose up --quiet-pull --abort-on-container-exit
env:
DRUPAL_VERSION: ${{ matrix.drupal-version }}
PHP_VERSION: ${{ matrix.php-version }}
ENABLE_MODULES: turnstile_protect
22 changes: 22 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
networks:
default:
services:
chromedriver:
image: drupalci/webdriver-chromedriver:production
entrypoint:
- chromedriver
- "--log-path=/dev/null"
- "--verbose"
- "--allowed-ips="
- "--allowed-origins=*"
drupal:
image: lehighlts/drupal-ci:${DRUPAL_VERSION}-php${PHP_VERSION}
volumes:
- ${MODULE_DIRECTORY}:/var/www/drupal/web/modules/contrib/${ENABLE_MODULES}
environment:
SIMPLETEST_BASE_URL: http://drupal:8282
ENABLE_MODULES: ${ENABLE_MODULES}
MINK_DRIVER_ARGS_WEBDRIVER: '["chrome", {"browserName":"chrome","goog:chromeOptions":{"args":["--disable-gpu","--headless", "--no-sandbox", "--disable-dev-shm-usage"]}}, "http://chromedriver:9515"]'
SYMFONY_DEPRECATIONS_HELPER: weak
links:
- chromedriver
25 changes: 18 additions & 7 deletions tests/src/Functional/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Drupal\Tests\turnstile_protect\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
* Tests redirection from /node/1 to /challenge.
*
* @group turnstile_protect
*/
class RedirectTest extends BrowserTestBase {
class RedirectTest extends WebDriverTestBase {

/**
* {@inheritdoc}
Expand All @@ -24,7 +24,7 @@ class RedirectTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'claro';
protected $defaultTheme = 'stark';

/**
* Sets up the test environment.
Expand All @@ -49,26 +49,37 @@ protected function setUp(): void {
->save();

$this->assertEquals("entity.node.canonical", $config->get('routes')[0], 'Routes configuration is set to node.entity.canonical.');
\Drupal::service('cache.config')->deleteAll();

}

/**
* Tests redirection from node to /challenge.
*/
public function testNodeRedirect() {
// Create a node we'll try accessing.
$node = $this->drupalCreateNode(['type' => 'page']);
$nodeUrl = $node->toUrl()->toString();
$this->drupalGet($nodeUrl);

// Since turnstile_protect.settings["threshold"] = 1
// we should be able to view the node once.
$this->drupalGet($nodeUrl);
$url = $this->getSession()->getCurrentUrl();
$components = parse_url($url);
$this->assertEquals($nodeUrl, $components['path'], 'User is not redirected to /challenge.');

// We should be challenged now on the second look.
$this->drupalGet($nodeUrl);
$url = $this->getSession()->getCurrentUrl();
$components = parse_url($url);
$this->assertEquals('/challenge', $components['path'], 'User is redirected to /challenge.');
$this->assertEquals('/challenge', $components['path'], 'User is not redirected to /challenge.');

sleep(15);

// We should be redirected to the node after the turnstile passed
// which it always will in this test with the turnstile site/secret keys
// set to their always pass test.
$url = $this->getSession()->getCurrentUrl();
$components = parse_url($url);
$this->assertEquals($nodeUrl, $components['path'], 'User is redirected back to node.');
}

}

0 comments on commit c17f997

Please sign in to comment.