Skip to content

Commit 350cb0a

Browse files
committed
Add e2e tests to Web Browsable API
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
1 parent 541315d commit 350cb0a

14 files changed

+167
-78
lines changed

Dockerfile.it

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ RUN apk add --no-cache --update --virtual .build-deps \
55
linux-headers \
66
ca-certificates \
77
python3-dev \
8+
git \
89
&& rm -rf /var/cache/* \
910
&& mkdir /var/cache/apk \
1011
&& ln -sf /lib/ld-musl-x86_64.so.1 /usr/bin/ldd \
@@ -14,16 +15,24 @@ WORKDIR /svc
1415

1516
COPY requirements/test.txt /svc/
1617
RUN pip install pip setuptools wheel --upgrade \
17-
&& pip wheel --wheel-dir=/svc/wheels -r test.txt
18+
&& pip wheel --wheel-dir=/svc/wheels -r test.txt \
19+
poetry-core>=1.0.0
1820

1921
FROM python:3.10-alpine
2022

2123
ENV PYTHONUNBUFFERED=1 \
2224
DEBUG=0 \
25+
PYTEST_CACHE_DIR=".pytest_cache" \
2326
SITE_DOMAIN="app" \
24-
SITE_PORT=5000
27+
SITE_PORT=5000 \
28+
WEB_URL="http://app:5000" \
29+
API_URL="http://app:5000/api" \
30+
BROWSABLE_API_URL="http://app:5000/api/browse"
2531

2632
RUN apk add --no-cache --update \
33+
git \
34+
chromium \
35+
chromium-chromedriver \
2736
&& rm -rf /var/cache/* \
2837
&& mkdir /var/cache/apk \
2938
&& ln -sf /lib/ld-musl-x86_64.so.1 /usr/bin/ldd \
@@ -52,6 +61,8 @@ RUN pip install pip setuptools wheel --upgrade \
5261
--no-create-home \
5362
-s /bin/false \
5463
flask_user \
64+
&& mkdir -p /app/test-results \
65+
&& mkdir -p /app/.pytest_cache/screenshots \
5566
&& chown flask_user:flask_user -R /app
5667

5768
USER flask_user

Dockerfile.local

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ RUN pip install pip setuptools wheel --upgrade \
2626
FROM python:3.10-alpine
2727

2828
ENV PYTHONUNBUFFERED=1 \
29-
DEBUG=0
29+
DEBUG=0 \
30+
FLASK_SERVER_NAME="app:5000"
3031

3132
RUN apk add --no-cache --update \
3233
&& rm -rf /var/cache/* \

Dockerfile.py310.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ COPY requirements/ /code/requirements/
1111
RUN set -ex \
1212
&& apk add --no-cache --virtual .build-deps \
1313
gcc \
14+
g++ \
15+
cmake \
16+
rust \
17+
cargo \
1418
musl-dev \
1519
python3-dev \
16-
&& pip install pip setuptools wheel --upgrade \
20+
git \
21+
&& pip install pip setuptools --upgrade \
1722
&& pip install -r requirements/base.txt \
1823
&& pip install -r requirements/test.txt \
24+
poetry-core>=1.0.0 \
1925
&& pip install -r requirements/ci.txt \
2026
&& apk del .build-deps \
2127
&& addgroup -S kuchulu \

Dockerfile.py37.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ COPY requirements/ /code/requirements/
1111
RUN set -ex \
1212
&& apk add --no-cache --virtual .build-deps \
1313
gcc \
14+
g++ \
15+
cmake \
16+
rust \
17+
cargo \
1418
musl-dev \
1519
python3-dev \
20+
git \
1621
&& pip install pip setuptools wheel --upgrade \
1722
&& pip install -r requirements/base.txt \
1823
&& pip install -r requirements/test.txt \
24+
poetry-core>=1.0.0 \
1925
&& pip install -r requirements/ci.txt \
2026
&& apk del .build-deps \
2127
&& addgroup -S kuchulu \

Dockerfile.py38.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ COPY requirements/ /code/requirements/
1111
RUN set -ex \
1212
&& apk add --no-cache --virtual .build-deps \
1313
gcc \
14+
g++ \
15+
cmake \
16+
rust \
17+
cargo \
1418
musl-dev \
1519
python3-dev \
20+
git \
1621
&& pip install pip setuptools wheel --upgrade \
1722
&& pip install -r requirements/base.txt \
1823
&& pip install -r requirements/test.txt \
24+
poetry-core>=1.0.0 \
1925
&& pip install -r requirements/ci.txt \
2026
&& apk del .build-deps \
2127
&& addgroup -S kuchulu \

Dockerfile.py39.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ COPY requirements/ /code/requirements/
1111
RUN set -ex \
1212
&& apk add --no-cache --virtual .build-deps \
1313
gcc \
14+
g++ \
15+
cmake \
16+
rust \
17+
cargo \
1418
musl-dev \
1519
python3-dev \
20+
git \
1621
&& pip install pip setuptools wheel --upgrade \
1722
&& pip install -r requirements/base.txt \
1823
&& pip install -r requirements/test.txt \
24+
poetry-core>=1.0.0 \
1925
&& pip install -r requirements/ci.txt \
2026
&& apk del .build-deps \
2127
&& addgroup -S kuchulu \

docker-compose.it.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ services:
66
context: .
77
dockerfile: Dockerfile.it
88
environment:
9-
- FLASK_ENV=TESTING
9+
- PYTEST_CACHE_DIR=.pytest_cache
1010
- SITE_DOMAIN=async_app
1111
- SITE_PORT=5000
1212
- WEB_URL=http://async_app:5000
1313
- API_URL=http://async_app:5000/api
1414
- BROWSABLE_API_URL=http://async_app:5000/api/browse
1515
user: ${UID:-0}:${GID:-0}
16+
volumes:
17+
- .pytest_cache/test-results/async_app:/app/test-results
18+
- .pytest_cache/screnshots/async_app:/app/.pytest_cache/screnshots
1619
depends_on:
1720
- async_app
1821

@@ -23,7 +26,6 @@ services:
2326
args:
2427
- FLASK_ASYNC=1
2528
environment:
26-
- FLASK_ENV=TESTING
2729
- FLASK_SERVER_NAME=async_app:5000
2830
user: ${UID:-0}:${GID:-0}
2931
command: >
@@ -36,13 +38,16 @@ services:
3638
context: .
3739
dockerfile: Dockerfile.it
3840
environment:
39-
- FLASK_ENV=TESTING
41+
- PYTEST_CACHE_DIR=.pytest_cache
4042
- SITE_DOMAIN=app
4143
- SITE_PORT=5000
4244
- WEB_URL=http://app:5000
4345
- API_URL=http://app:5000/api
4446
- BROWSABLE_API_URL=http://app:5000/api/browse
4547
user: ${UID:-0}:${GID:-0}
48+
volumes:
49+
- .pytest_cache/test-results/app:/app/test-results
50+
- .pytest_cache/screnshots/app:/app/.pytest_cache/screnshots
4651
depends_on:
4752
- app
4853

@@ -51,7 +56,6 @@ services:
5156
context: .
5257
dockerfile: Dockerfile.local
5358
environment:
54-
- FLASK_ENV=TESTING
5559
- FLASK_SERVER_NAME=app:5000
5660
user: ${UID:-0}:${GID:-0}
5761
command: >

docker-compose.test.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ services:
66
context: .
77
dockerfile: Dockerfile.py310.test
88
environment:
9-
- FLASK_ENV=TESTING
109
- PRAGMA_VERSION=py3.10
1110
command: >
1211
sh -c "flake8 src/ tests/ &&
@@ -21,7 +20,6 @@ services:
2120
context: .
2221
dockerfile: Dockerfile.py39.test
2322
environment:
24-
- FLASK_ENV=TESTING
2523
- PRAGMA_VERSION=py3.9
2624
command: >
2725
sh -c "flake8 src/ tests/ &&
@@ -33,7 +31,6 @@ services:
3331
context: .
3432
dockerfile: Dockerfile.py38.test
3533
environment:
36-
- FLASK_ENV=TESTING
3734
- PRAGMA_VERSION=py3.8
3835
command: >
3936
sh -c "flake8 src/ tests/ &&
@@ -45,7 +42,6 @@ services:
4542
context: .
4643
dockerfile: Dockerfile.py37.test
4744
environment:
48-
- FLASK_ENV=TESTING
4945
- PRAGMA_VERSION=py3.7
5046
command: >
5147
sh -c "flake8 src/ tests/ &&

requirements/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pytest-xdist<=3.0.2;python_version<="3.6"
88
pytest-sugar==0.9.6 # https://github.com/Frozenball/pytest-sugar
99
pytest-env==0.8.1;python_version>"3.6" # https://github.com/MobileDynasty/pytest-env
1010
pytest-env<0.7.0;python_version<="3.6"
11+
pytest-selenium@git+https://github.com/pytest-dev/pytest-selenium.git;python_version>"3.7" # https://github.com/pytest-dev/pytest-selenium
1112
mock==5.0.1 # https://github.com/testing-cabal/mock
1213

1314
# Type check

tests/test_apps/conftest.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,42 @@
2525
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
# POSSIBILITY OF SUCH DAMAGE.
2727
import os
28+
import time
2829
import unittest
30+
from pathlib import Path
2931

3032
import requests
33+
from selenium import webdriver
34+
35+
WEB_DRIVER_SCREENSHOT_DIR = Path(os.environ['PYTEST_CACHE_DIR']) / 'screenshots'
3136

3237

3338
class APITestCase(unittest.TestCase):
34-
def setUp(self):
39+
def setUp(self) -> None:
3540
session = requests.Session()
3641
session.headers.update(
3742
{
3843
'Content-Type': 'application/json',
3944
}
4045
)
4146
self.requests = session
42-
self.api_url = os.environ['API_URL']
47+
48+
49+
class WebDriverTestCase(unittest.TestCase):
50+
def setUp(self) -> None:
51+
chrome_prefs = {}
52+
chrome_options = webdriver.ChromeOptions()
53+
chrome_options.add_argument('--no-sandbox')
54+
chrome_options.add_argument('--headless')
55+
chrome_options.add_argument('--disable-gpu')
56+
chrome_options.add_experimental_option('prefs', chrome_prefs)
57+
self.driver = webdriver.Chrome(options=chrome_options)
58+
self.driver.delete_all_cookies()
59+
self.driver.implicitly_wait(10)
60+
self.addCleanup(self.driver.quit)
61+
self.addCleanup(self.take_screenshot)
62+
if not WEB_DRIVER_SCREENSHOT_DIR.exists():
63+
WEB_DRIVER_SCREENSHOT_DIR.mkdir(parents=True)
64+
65+
def take_screenshot(self):
66+
self.driver.get_screenshot_as_file(str(WEB_DRIVER_SCREENSHOT_DIR / f'{self.id()}-{time.time()}.png'))

tests/test_apps/pytest.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ required_plugins =
99
pytest-xdist
1010
pytest-sugar
1111
pytest-env
12-
env =
13-
FLASK_ENV=TESTING

tests/test_apps/pytest.local.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ required_plugins =
1010
pytest-sugar
1111
pytest-env
1212
env =
13-
FLASK_ENV=TESTING
13+
PYTEST_CACHE_DIR=.pytest_cache
1414
SITE_DOMAIN=localhost
1515
SITE_PORT=5000
1616
WEB_URL=http://localhost:5000

0 commit comments

Comments
 (0)