Skip to content

Commit 8926af3

Browse files
committed
client_live_key, client_test_key fixtures: provide client_id for correlation
functioning similarly to driver_id and included in User-agent, this should make it possible to correlate requests from a particular FT process with its corresponding logs server-side
1 parent 7e5ebc7 commit 8926af3

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

tests/conftest.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44

55
import pytest
6-
from notifications_python_client import NotificationsAPIClient
76
from selenium import webdriver
87
from selenium.webdriver.chrome.service import Service as ChromeService
98
from selenium.webdriver.support.events import EventFiringWebDriver
@@ -12,6 +11,7 @@
1211
from tests.event_listener import LoggingEventListener
1312
from tests.pages.pages import HomePage
1413
from tests.pages.rollups import sign_in, sign_in_email_auth
14+
from tests.test_utils import FTNotificationsAPIClient
1515

1616

1717
def pytest_addoption(parser):
@@ -124,12 +124,28 @@ def login_seeded_user(_driver, request: pytest.FixtureRequest):
124124

125125

126126
@pytest.fixture(scope="module")
127-
def client_live_key():
128-
client = NotificationsAPIClient(base_url=config["notify_api_url"], api_key=config["service"]["api_live_key"])
127+
def _client_live_key():
128+
client = FTNotificationsAPIClient(base_url=config["notify_api_url"], api_key=config["service"]["api_live_key"])
129129
return client
130130

131131

132132
@pytest.fixture(scope="module")
133-
def client_test_key():
134-
client = NotificationsAPIClient(base_url=config["notify_api_url"], api_key=config["service"]["api_test_key"])
133+
def _client_test_key():
134+
client = FTNotificationsAPIClient(base_url=config["notify_api_url"], api_key=config["service"]["api_test_key"])
135135
return client
136+
137+
138+
@pytest.fixture(scope="function")
139+
def client_live_key(_client_live_key, request):
140+
prev_failed_tests = request.session.testsfailed
141+
yield _client_live_key
142+
if prev_failed_tests != request.session.testsfailed:
143+
print("client_live_key client id:", str(getattr(_client_live_key, "_client_id", None))) # noqa: T201
144+
145+
146+
@pytest.fixture(scope="function")
147+
def client_test_key(_client_test_key, request):
148+
prev_failed_tests = request.session.testsfailed
149+
yield _client_test_key
150+
if prev_failed_tests != request.session.testsfailed:
151+
print("client_test_key client id:", str(getattr(_client_test_key, "_client_id", None))) # noqa: T201

tests/test_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ class NotificationStatuses:
5959
SENT = RECEIVED | DELIVERED | {"sending", "pending"}
6060

6161

62+
class FTNotificationsAPIClient(NotificationsAPIClient):
63+
_client_id: uuid.UUID
64+
65+
def __init__(self, *args, **kwargs):
66+
self._client_id = uuid.uuid4()
67+
super().__init__(*args, **kwargs)
68+
69+
def generate_headers(self, api_token):
70+
r = super().generate_headers(api_token)
71+
r["User-agent"] = f"{r["User-agent"]}/client-{self._client_id}"
72+
return r
73+
74+
6275
def create_temp_csv(fields: dict[str, Any], include_build_id: bool = True) -> tuple[list, str, str]:
6376
directory_name = tempfile.mkdtemp()
6477
csv_filename = f"{uuid.uuid4()}-sample.csv"
@@ -540,7 +553,7 @@ def _assert_one_off_email_filled_in_properly(driver, template_name, test, recipi
540553

541554

542555
def get_notification_by_to_field(template_id, api_key, sent_to, statuses=None):
543-
client = NotificationsAPIClient(base_url=config["notify_api_url"], api_key=api_key)
556+
client = FTNotificationsAPIClient(base_url=config["notify_api_url"], api_key=api_key)
544557
resp = client.get("v2/notifications")
545558
for notification in resp["notifications"]:
546559
t_id = notification["template"]["id"]

0 commit comments

Comments
 (0)