Skip to content

Commit

Permalink
Add e2e tests to Web Browsable API
Browse files Browse the repository at this point in the history
It's very important to provide a safe way to change
the Web Browsable API in the next tasks.

See also: #378, #377, #376, #374, #373, and #370
  • Loading branch information
nycholas committed Mar 27, 2023
1 parent 541315d commit 9e19ba3
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 77 deletions.
15 changes: 13 additions & 2 deletions Dockerfile.it
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN apk add --no-cache --update --virtual .build-deps \
linux-headers \
ca-certificates \
python3-dev \
git \
&& rm -rf /var/cache/* \
&& mkdir /var/cache/apk \
&& ln -sf /lib/ld-musl-x86_64.so.1 /usr/bin/ldd \
Expand All @@ -14,16 +15,24 @@ WORKDIR /svc

COPY requirements/test.txt /svc/
RUN pip install pip setuptools wheel --upgrade \
&& pip wheel --wheel-dir=/svc/wheels -r test.txt
&& pip wheel --wheel-dir=/svc/wheels -r test.txt \
poetry-core>=1.0.0

FROM python:3.10-alpine

ENV PYTHONUNBUFFERED=1 \
DEBUG=0 \
PYTEST_CACHE_DIR=".pytest_cache" \
SITE_DOMAIN="app" \
SITE_PORT=5000
SITE_PORT=5000 \
WEB_URL="http://app:5000" \
API_URL="http://app:5000/api" \
BROWSABLE_API_URL="http://app:5000/api/browse"

RUN apk add --no-cache --update \
git \
chromium \
chromium-chromedriver \
&& rm -rf /var/cache/* \
&& mkdir /var/cache/apk \
&& ln -sf /lib/ld-musl-x86_64.so.1 /usr/bin/ldd \
Expand Down Expand Up @@ -52,6 +61,8 @@ RUN pip install pip setuptools wheel --upgrade \
--no-create-home \
-s /bin/false \
flask_user \
&& mkdir -p /app/test-results \
&& mkdir -p /app/.pytest_cache/screenshots \
&& chown flask_user:flask_user -R /app

USER flask_user
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.local
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ RUN pip install pip setuptools wheel --upgrade \
FROM python:3.10-alpine

ENV PYTHONUNBUFFERED=1 \
DEBUG=0
DEBUG=0 \
FLASK_SERVER_NAME="app:5000"

RUN apk add --no-cache --update \
&& rm -rf /var/cache/* \
Expand Down
12 changes: 8 additions & 4 deletions docker-compose.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ services:
context: .
dockerfile: Dockerfile.it
environment:
- FLASK_ENV=TESTING
- PYTEST_CACHE_DIR=.pytest_cache
- SITE_DOMAIN=async_app
- SITE_PORT=5000
- WEB_URL=http://async_app:5000
- API_URL=http://async_app:5000/api
- BROWSABLE_API_URL=http://async_app:5000/api/browse
user: ${UID:-0}:${GID:-0}
volumes:
- .pytest_cache/test-results/async_app:/app/test-results
- .pytest_cache/screnshots/async_app:/app/.pytest_cache/screnshots
depends_on:
- async_app

Expand All @@ -23,7 +26,6 @@ services:
args:
- FLASK_ASYNC=1
environment:
- FLASK_ENV=TESTING
- FLASK_SERVER_NAME=async_app:5000
user: ${UID:-0}:${GID:-0}
command: >
Expand All @@ -36,13 +38,16 @@ services:
context: .
dockerfile: Dockerfile.it
environment:
- FLASK_ENV=TESTING
- PYTEST_CACHE_DIR=.pytest_cache
- SITE_DOMAIN=app
- SITE_PORT=5000
- WEB_URL=http://app:5000
- API_URL=http://app:5000/api
- BROWSABLE_API_URL=http://app:5000/api/browse
user: ${UID:-0}:${GID:-0}
volumes:
- .pytest_cache/test-results/app:/app/test-results
- .pytest_cache/screnshots/app:/app/.pytest_cache/screnshots
depends_on:
- app

Expand All @@ -51,7 +56,6 @@ services:
context: .
dockerfile: Dockerfile.local
environment:
- FLASK_ENV=TESTING
- FLASK_SERVER_NAME=app:5000
user: ${UID:-0}:${GID:-0}
command: >
Expand Down
4 changes: 0 additions & 4 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ services:
context: .
dockerfile: Dockerfile.py310.test
environment:
- FLASK_ENV=TESTING
- PRAGMA_VERSION=py3.10
command: >
sh -c "flake8 src/ tests/ &&
Expand All @@ -21,7 +20,6 @@ services:
context: .
dockerfile: Dockerfile.py39.test
environment:
- FLASK_ENV=TESTING
- PRAGMA_VERSION=py3.9
command: >
sh -c "flake8 src/ tests/ &&
Expand All @@ -33,7 +31,6 @@ services:
context: .
dockerfile: Dockerfile.py38.test
environment:
- FLASK_ENV=TESTING
- PRAGMA_VERSION=py3.8
command: >
sh -c "flake8 src/ tests/ &&
Expand All @@ -45,7 +42,6 @@ services:
context: .
dockerfile: Dockerfile.py37.test
environment:
- FLASK_ENV=TESTING
- PRAGMA_VERSION=py3.7
command: >
sh -c "flake8 src/ tests/ &&
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pytest-xdist<=3.0.2;python_version<="3.6"
pytest-sugar==0.9.6 # https://github.com/Frozenball/pytest-sugar
pytest-env==0.8.1;python_version>"3.6" # https://github.com/MobileDynasty/pytest-env
pytest-env<0.7.0;python_version<="3.6"
pytest-selenium@git+https://github.com/pytest-dev/pytest-selenium.git;python_version>"3.7" # https://github.com/pytest-dev/pytest-selenium
mock==5.0.1 # https://github.com/testing-cabal/mock

# Type check
Expand Down
28 changes: 26 additions & 2 deletions tests/test_apps/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,42 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import time
import unittest
from pathlib import Path

import requests
from selenium import webdriver

WEB_DRIVER_SCREENSHOT_DIR = Path(os.environ['PYTEST_CACHE_DIR']) / 'screenshots'


class APITestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
session = requests.Session()
session.headers.update(
{
'Content-Type': 'application/json',
}
)
self.requests = session
self.api_url = os.environ['API_URL']


class WebDriverTestCase(unittest.TestCase):
def setUp(self) -> None:
chrome_prefs = {}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_experimental_option('prefs', chrome_prefs)
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.delete_all_cookies()
self.driver.implicitly_wait(10)
self.addCleanup(self.driver.quit)
self.addCleanup(self.take_screenshot)
if not WEB_DRIVER_SCREENSHOT_DIR.exists():
WEB_DRIVER_SCREENSHOT_DIR.mkdir(parents=True)

def take_screenshot(self):
self.driver.get_screenshot_as_file(str(WEB_DRIVER_SCREENSHOT_DIR / f'{self.id()}-{time.time()}.png'))
2 changes: 0 additions & 2 deletions tests/test_apps/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ required_plugins =
pytest-xdist
pytest-sugar
pytest-env
env =
FLASK_ENV=TESTING
2 changes: 1 addition & 1 deletion tests/test_apps/pytest.local.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ required_plugins =
pytest-sugar
pytest-env
env =
FLASK_ENV=TESTING
PYTEST_CACHE_DIR=.pytest_cache
SITE_DOMAIN=localhost
SITE_PORT=5000
WEB_URL=http://localhost:5000
Expand Down
Loading

0 comments on commit 9e19ba3

Please sign in to comment.