From 2802deb49735f23f95b1fd5d2b16225b6b4fa69b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 23 Mar 2025 19:30:24 +0000 Subject: [PATCH] tests/e2e: don't rely DNS to get host's IP for container access Signed-off-by: Jens Langhammer --- tests/e2e/test_provider_oauth2_github.py | 6 ++++-- tests/e2e/test_provider_oauth2_grafana.py | 8 ++++++-- tests/e2e/utils.py | 21 ++++++++++++--------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/e2e/test_provider_oauth2_github.py b/tests/e2e/test_provider_oauth2_github.py index 977308ead1..81f696645e 100644 --- a/tests/e2e/test_provider_oauth2_github.py +++ b/tests/e2e/test_provider_oauth2_github.py @@ -47,10 +47,12 @@ class TestProviderOAuth2Github(SeleniumTestCase): "GF_AUTH_GITHUB_AUTH_URL": self.url( "authentik_providers_oauth2_root:github-authorize" ), - "GF_AUTH_GITHUB_TOKEN_URL": self.url( + "GF_AUTH_GITHUB_TOKEN_URL": self.host_url( "authentik_providers_oauth2_root:github-access-token" ), - "GF_AUTH_GITHUB_API_URL": self.url("authentik_providers_oauth2_root:github-user"), + "GF_AUTH_GITHUB_API_URL": self.host_url( + "authentik_providers_oauth2_root:github-user" + ), "GF_LOG_LEVEL": "debug", }, ) diff --git a/tests/e2e/test_provider_oauth2_grafana.py b/tests/e2e/test_provider_oauth2_grafana.py index 101c42d48b..6b34edbba6 100644 --- a/tests/e2e/test_provider_oauth2_grafana.py +++ b/tests/e2e/test_provider_oauth2_grafana.py @@ -53,8 +53,12 @@ class TestProviderOAuth2OAuth(SeleniumTestCase): "GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET": self.client_secret, "GF_AUTH_GENERIC_OAUTH_SCOPES": "openid email profile", "GF_AUTH_GENERIC_OAUTH_AUTH_URL": self.url("authentik_providers_oauth2:authorize"), - "GF_AUTH_GENERIC_OAUTH_TOKEN_URL": self.url("authentik_providers_oauth2:token"), - "GF_AUTH_GENERIC_OAUTH_API_URL": self.url("authentik_providers_oauth2:userinfo"), + "GF_AUTH_GENERIC_OAUTH_TOKEN_URL": self.host_url( + "authentik_providers_oauth2:token" + ), + "GF_AUTH_GENERIC_OAUTH_API_URL": self.host_url( + "authentik_providers_oauth2:userinfo" + ), "GF_AUTH_SIGNOUT_REDIRECT_URL": self.url( "authentik_providers_oauth2:end-session", application_slug=self.app_slug, diff --git a/tests/e2e/utils.py b/tests/e2e/utils.py index dda9c374a5..cb15a179b6 100644 --- a/tests/e2e/utils.py +++ b/tests/e2e/utils.py @@ -2,7 +2,6 @@ import json import os -import socket from collections.abc import Callable from functools import lru_cache, wraps from os import environ @@ -51,13 +50,6 @@ def get_docker_tag() -> str: return f"gh-{branch_name}" -def get_local_ip() -> str: - """Get the local machine's IP""" - hostname = socket.gethostname() - ip_addr = socket.gethostbyname(hostname) - return ip_addr - - class DockerTestCase(TestCase): """Mixin for dealing with containers""" @@ -113,6 +105,9 @@ class DockerTestCase(TestCase): specs["network"] = self.__network.name specs["labels"] = self.docker_labels specs["detach"] = True + specs["extra_hosts"] = { + "host.docker.internal": "host-gateway", + } if hasattr(self, "live_server_url"): specs.setdefault("environment", {}) specs["environment"]["AUTHENTIK_HOST"] = self.live_server_url @@ -155,7 +150,6 @@ class DockerTestCase(TestCase): class SeleniumTestCase(DockerTestCase, StaticLiveServerTestCase): """StaticLiveServerTestCase which automatically creates a Webdriver instance""" - host = get_local_ip() wait_timeout: int user: User @@ -210,6 +204,15 @@ class SeleniumTestCase(DockerTestCase, StaticLiveServerTestCase): f"URL {self.driver.current_url} doesn't match expected URL {desired_url}", ) + def host_url(self, view, query: dict | None = None, **kwargs) -> str: + """reverse `view` with `**kwargs` into full URL using live_server_url""" + url = f"http://host.docker.internal:{self.server_thread.port}" + reverse( + view, kwargs=kwargs + ) + if query: + return url + "?" + urlencode(query) + return url + def url(self, view, query: dict | None = None, **kwargs) -> str: """reverse `view` with `**kwargs` into full URL using live_server_url""" url = self.live_server_url + reverse(view, kwargs=kwargs)