Mercurial > libervia-backend
annotate tests/e2e/libervia/conftest.py @ 3436:f9011d62a87a
tests (e2e/conftest): check new exit code for conflict to avoid crashing if test accounts already exist
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 04 Dec 2020 12:34:24 +0100 |
parents | d4558f3cbf13 |
children | 84fb41b515a1 |
rev | line source |
---|---|
3429
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # SàT: an XMPP client |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 import sys |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 import os |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 import socket |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import pytest |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import time |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from datetime import datetime |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from pathlib import Path |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import helium |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 LIBERVIA_HOST = "libervia.test" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 LIBERVIA_PORT_HTTPS = 8443 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 BASE_URL = f"https://{LIBERVIA_HOST}:{LIBERVIA_PORT_HTTPS}" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 SIZE_DESKTOP = (1024, 728) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 SIZE_MOBILE = (380, 640) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 account_1_cookies = None |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 @pytest.hookimpl(tryfirst=True, hookwrapper=True) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 def pytest_runtest_makereport(item, call): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 # needed to get test results in request fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 # cf. https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 outcome = yield |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 rep = outcome.get_result() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 setattr(item, "rep_" + rep.when, rep) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 def screenshot_on_failure(request): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 yield |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 if request.node.rep_setup.passed: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 if request.node.rep_call.failed: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 report_dir = Path(os.getenv("SAT_TEST_REPORT_DIR", "/tmp/tests_report")) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 dest_dir = report_dir/"screenshots" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 dest_dir.mkdir(parents=True, exist_ok=True) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 filename = f"{datetime.now().isoformat()}_{request.node.name}.png" |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 dest_path = dest_dir/filename |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 helium.get_driver().save_screenshot(str(dest_path)) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 print(f"screenshot saved to {dest_path}") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def wait_for_socket(host, port, retries=30): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 sock = socket.socket() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 while True: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 try: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 sock.connect((host, port)) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 except ConnectionRefusedError as e: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 retries -= 1 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 if retries < 0: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 print(f"Can't access server at {host}:{port}", file=sys.stderr) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 raise e |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 else: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 print(f"Can't connect to {host}:{port}, retrying ({retries})") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 time.sleep(1) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 else: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 break |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @pytest.fixture(scope="session") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 def browser(): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 if os.getenv("SAT_TEST_E2E_LIBERVIA_NO_HEADLESS") is not None: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 kwargs = {} |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 else: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 kwargs = {"headless": True} |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 driver = helium.start_firefox(**kwargs) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 driver.set_window_size(*SIZE_DESKTOP) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 wait_for_socket(LIBERVIA_HOST, LIBERVIA_PORT_HTTPS) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 yield helium |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 if os.getenv("SAT_TEST_E2E_LIBERVIA_KEEP_BROWSER") is None: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 helium.kill_browser() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 def nobody_logged_in(browser): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 browser.get_driver().delete_all_cookies() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 def log_in_account1(browser): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 global account_1_cookies |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 if account_1_cookies is None: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 browser.get_driver().delete_all_cookies() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 browser.go_to("https://libervia.test:8443/login") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 browser.write("account1", into="login") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 browser.write("test", into="password") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 browser.click("log in") |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 account_1_cookies = browser.get_driver().get_cookies() |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 else: |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 browser.get_driver().add_cookie(account_1_cookies) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 def mobile_screen(browser): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 browser.get_driver().set_window_size(*SIZE_MOBILE) |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 @pytest.fixture |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def desktop_screen(browser): |
d4558f3cbf13
tests, docker(e2e): added e2e tests for Libervia:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 browser.get_driver().set_window_size(*SIZE_DESKTOP) |