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/db.sqlite3
|
||||||
src/functional_tests/management
|
|
||||||
|
|||||||
Binary file not shown.
@@ -78,7 +78,7 @@
|
|||||||
env:
|
env:
|
||||||
DJANGO_DEBUG_FALSE: "1"
|
DJANGO_DEBUG_FALSE: "1"
|
||||||
DJANGO_SECRET_KEY: "{{ secret_key.content | b64decode }}"
|
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"
|
DJANGO_DB_PATH: "/home/nonroot/db.sqlite3"
|
||||||
EMAIL_HOST_PASSWORD: "{{ lookup('env', 'EMAIL_HOST_PASSWORD') }}"
|
EMAIL_HOST_PASSWORD: "{{ lookup('env', 'EMAIL_HOST_PASSWORD') }}"
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -48,11 +48,12 @@ INSTALLED_APPS = [
|
|||||||
# Custom apps
|
# Custom apps
|
||||||
'apps.dashboard',
|
'apps.dashboard',
|
||||||
'apps.lyric',
|
'apps.lyric',
|
||||||
|
'functional_tests',
|
||||||
# Depend apps
|
# Depend apps
|
||||||
]
|
]
|
||||||
|
|
||||||
if 'DJANGO_DEBUG_FALSE' not in os.environ:
|
# if 'DJANGO_DEBUG_FALSE' not in os.environ:
|
||||||
INSTALLED_APPS += ['functional_tests']
|
# INSTALLED_APPS += ['functional_tests']
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ from selenium import webdriver
|
|||||||
from selenium.common.exceptions import WebDriverException
|
from selenium.common.exceptions import WebDriverException
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
|
||||||
|
from .container_commands import reset_database
|
||||||
|
|
||||||
MAX_WAIT = 5
|
MAX_WAIT = 5
|
||||||
|
|
||||||
|
|
||||||
@@ -30,6 +32,7 @@ class FunctionalTest(StaticLiveServerTestCase):
|
|||||||
self.test_server = os.environ.get("TEST_SERVER")
|
self.test_server = os.environ.get("TEST_SERVER")
|
||||||
if self.test_server:
|
if self.test_server:
|
||||||
self.live_server_url = 'http://' + self.test_server
|
self.live_server_url = 'http://' + self.test_server
|
||||||
|
reset_database(self.test_server)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.browser.quit()
|
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.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
|
||||||
from django.contrib.sessions.backends.db import SessionStore
|
from django.contrib.sessions.backends.db import SessionStore
|
||||||
from .base import FunctionalTest
|
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()
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
class MyListsTest(FunctionalTest):
|
class MyListsTest(FunctionalTest):
|
||||||
def create_pre_authenticated_session(self, email):
|
def create_pre_authenticated_session(self, email):
|
||||||
user = User.objects.create(email=email)
|
if self.test_server:
|
||||||
session = SessionStore()
|
session_key = create_session_on_server(self.test_server, email)
|
||||||
session[SESSION_KEY] = user.pk
|
else:
|
||||||
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
|
session_key = create_pre_authenticated_session(email)
|
||||||
session.save()
|
|
||||||
## to set a cookie we need to first visit the domain
|
## to set a cookie we need to first visit the domain
|
||||||
## 404 pages load the quickest!
|
## 404 pages load the quickest!
|
||||||
self.browser.get(self.live_server_url + "/404_no_such_url/")
|
self.browser.get(self.live_server_url + "/404_no_such_url/")
|
||||||
self.browser.add_cookie(
|
self.browser.add_cookie(
|
||||||
dict(
|
dict(
|
||||||
name=settings.SESSION_COOKIE_NAME,
|
name=settings.SESSION_COOKIE_NAME,
|
||||||
value=session.session_key,
|
value=session_key,
|
||||||
path="/",
|
path="/",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user