From 449b40e12ef616ccf1da0ce2264cd254fb17d4f8 Mon Sep 17 00:00:00 2001 From: Disco DeDisco Date: Tue, 3 Feb 2026 22:14:55 -0500 Subject: [PATCH] new functional_tests.container_commands file discerns local from server containers; .base calls now calls reset_database() therefrom; .test_my_lists also discerns test server location for different session keys; functional_tests restored as app in core.settings --- .dockerignore | 1 - container.db.sqlite3 | Bin 106496 -> 106496 bytes infra/deploy-playbook.yaml | 2 +- src/core/settings.py | 5 +- src/functional_tests/base.py | 3 ++ src/functional_tests/container_commands.py | 56 +++++++++++++++++++++ src/functional_tests/test_my_lists.py | 13 ++--- 7 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/functional_tests/container_commands.py diff --git a/.dockerignore b/.dockerignore index 2e27272..ea2e09c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1 @@ src/db.sqlite3 -src/functional_tests/management diff --git a/container.db.sqlite3 b/container.db.sqlite3 index 226a71f51f3bc7014cd62e158ae9cf467b9d12ac..d95a78b3e739862653374aec1ce3a52fb4cf4a3d 100644 GIT binary patch delta 538 zcmZoTz}9epZGtqT_Cy(HM(vFWi{yp67})p=nfQ0}7xKU4U&B9>|K`TRSpLa{`jY(o zml*gN5Mc65eH#&WAfJz&S&=cPvM4h-zO*>Ch-vdned!MzOnmwb`~rOX8w-{B>Ko&j zSbdwk9g|A(iqZ{AjVsIxEzQ#`%Z)1YjWUuG3oMLF46CxUE%Z{oDvQGM3j%zcjlD9R zElNT%jl8Q2DuRl$e1ZdmqP&8$tNcTX4P2Zvi*wvE0z=Jo!$RG1Lvzhc+_OXD{k(k( zd_#h(e5x$s1D##-OMNQi{e437lDvX5O^Q=O6N8iUeF};k6U_>|%8IQF%kumR46VG< zJxhF&OAC!E3ys~4gPqJPjmo{8Ej=BJN+WWM%v}QH&G%AH*}NfK;Dg;D5+}0;qZq uzoZ1SDI+$;Ow%9MGb#fWA7JdsYR1#>f11iPjnRC{8C@~0~;g100Y0k#zHp!=>qzUUx55O4E%RC z3npCR-+srQQ9}SI@_>Q=0Z`-`|Mmy*j4B|JhYb7=fg;!Wr$4M`R0aw>V&H!S6u7~^ K{ZT)ooB#k-hBR>i diff --git a/infra/deploy-playbook.yaml b/infra/deploy-playbook.yaml index 366a56a..1e7c9ad 100644 --- a/infra/deploy-playbook.yaml +++ b/infra/deploy-playbook.yaml @@ -78,7 +78,7 @@ env: DJANGO_DEBUG_FALSE: "1" DJANGO_SECRET_KEY: "{{ secret_key.content | b64decode }}" - DJANGO_ALLOWED_HOST: "localhost,staging.earthmanrpg.me,www.earthmanrpg.me,dashboard.earthmanrpg.me,earthmanrpg.me,104.131.184.0" + DJANGO_ALLOWED_HOST: "localhost,staging.earthmanrpg.me,www.earthmanrpg.me,dashboard.earthmanrpg.me,earthmanrpg.me,167.172.29.172" DJANGO_DB_PATH: "/home/nonroot/db.sqlite3" EMAIL_HOST_PASSWORD: "{{ lookup('env', 'EMAIL_HOST_PASSWORD') }}" ports: diff --git a/src/core/settings.py b/src/core/settings.py index 4f77465..e590263 100644 --- a/src/core/settings.py +++ b/src/core/settings.py @@ -48,11 +48,12 @@ INSTALLED_APPS = [ # Custom apps 'apps.dashboard', 'apps.lyric', + 'functional_tests', # Depend apps ] -if 'DJANGO_DEBUG_FALSE' not in os.environ: - INSTALLED_APPS += ['functional_tests'] +# if 'DJANGO_DEBUG_FALSE' not in os.environ: +# INSTALLED_APPS += ['functional_tests'] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', diff --git a/src/functional_tests/base.py b/src/functional_tests/base.py index 6c29c5d..a3e5577 100644 --- a/src/functional_tests/base.py +++ b/src/functional_tests/base.py @@ -6,6 +6,8 @@ from selenium import webdriver from selenium.common.exceptions import WebDriverException from selenium.webdriver.common.by import By +from .container_commands import reset_database + MAX_WAIT = 5 @@ -30,6 +32,7 @@ class FunctionalTest(StaticLiveServerTestCase): self.test_server = os.environ.get("TEST_SERVER") if self.test_server: self.live_server_url = 'http://' + self.test_server + reset_database(self.test_server) def tearDown(self): self.browser.quit() diff --git a/src/functional_tests/container_commands.py b/src/functional_tests/container_commands.py new file mode 100644 index 0000000..fb74014 --- /dev/null +++ b/src/functional_tests/container_commands.py @@ -0,0 +1,56 @@ +import subprocess + +USER = "discoman" + + +def reset_database(host): + return _exec_in_container( + host, ["/.venv/bin/python", "/src/manage.py", "flush", "--noinput"] + ) + +def create_session_on_server(host, email): + return _exec_in_container( + host, ["/.venv/bin/python", "/src/manage.py", "create_session", email] + ) + +def _exec_in_container(host, commands): + if "localhost" in host: + return _exec_in_container_locally(commands) + else: + return _exec_in_container_on_server(host, commands) + +def _exec_in_container_locally(commands): + print(f"Running {commands} on inside local docker container") + return _run_commands(["docker", "exec", _get_container_id()] + commands) + +def _exec_in_container_on_server(host, commands): + print(f"Running {commands!r} on {host} inside docker container") + return _run_commands( + [ + "ssh", + "-i", + "c:/Users/adamc/.ssh/digitalocean_keys/id_ed25519_wsl_python-tdd", + f"{USER}@{host}", + "docker", + "exec", + "gamearray", + ] + commands + ) + +def _get_container_id(): + return subprocess.check_output( + ["docker", "ps", "-q", "--filter", "ancestor=gamearray"] + ).strip() + +def _run_commands(commands): + process = subprocess.run( + commands, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False, + ) + result = process.stdout.decode() + if process.returncode != 0: + raise Exception(result) + print(f"Result: {result!r}") + return result.strip() diff --git a/src/functional_tests/test_my_lists.py b/src/functional_tests/test_my_lists.py index b65f644..11f7288 100644 --- a/src/functional_tests/test_my_lists.py +++ b/src/functional_tests/test_my_lists.py @@ -2,24 +2,25 @@ from django.conf import settings from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model from django.contrib.sessions.backends.db import SessionStore from .base import FunctionalTest +from .container_commands import create_session_on_server +from .management.commands.create_session import create_pre_authenticated_session User = get_user_model() class MyListsTest(FunctionalTest): def create_pre_authenticated_session(self, email): - user = User.objects.create(email=email) - session = SessionStore() - session[SESSION_KEY] = user.pk - session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] - session.save() + if self.test_server: + session_key = create_session_on_server(self.test_server, email) + else: + session_key = create_pre_authenticated_session(email) ## to set a cookie we need to first visit the domain ## 404 pages load the quickest! self.browser.get(self.live_server_url + "/404_no_such_url/") self.browser.add_cookie( dict( name=settings.SESSION_COOKIE_NAME, - value=session.session_key, + value=session_key, path="/", ) )