From c17f99757aea710cb6a07f870dc3c9bde4c325d1 Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Mon, 18 Nov 2024 10:57:35 -0500 Subject: [PATCH] Test with headless chrome --- .github/workflows/lint-test.yml | 8 ++++---- tests/docker-compose.yml | 22 ++++++++++++++++++++++ tests/src/Functional/RedirectTest.php | 25 ++++++++++++++++++------- 3 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 tests/docker-compose.yml diff --git a/.github/workflows/lint-test.yml b/.github/workflows/lint-test.yml index 6d667fa..20eb766 100644 --- a/.github/workflows/lint-test.yml +++ b/.github/workflows/lint-test.yml @@ -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 diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..3adcfd8 --- /dev/null +++ b/tests/docker-compose.yml @@ -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 diff --git a/tests/src/Functional/RedirectTest.php b/tests/src/Functional/RedirectTest.php index 75fc3b4..8fd0137 100644 --- a/tests/src/Functional/RedirectTest.php +++ b/tests/src/Functional/RedirectTest.php @@ -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} @@ -24,7 +24,7 @@ class RedirectTest extends BrowserTestBase { /** * {@inheritdoc} */ - protected $defaultTheme = 'claro'; + protected $defaultTheme = 'stark'; /** * Sets up the test environment. @@ -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.'); } }