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
This commit is contained in:
@@ -1,2 +1 @@
|
||||
src/db.sqlite3
|
||||
src/functional_tests/management
|
||||
|
||||
Binary file not shown.
@@ -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:
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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()
|
||||
|
||||
56
src/functional_tests/container_commands.py
Normal file
56
src/functional_tests/container_commands.py
Normal file
@@ -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()
|
||||
@@ -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="/",
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user