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 226a71f..d95a78b 100644 Binary files a/container.db.sqlite3 and b/container.db.sqlite3 differ 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="/", ) )