|
| 1 | +import os |
| 2 | +from typing import Dict, List |
| 3 | +from uuid import uuid4 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from nylas import Client |
| 8 | + |
| 9 | + |
| 10 | +_E2E_API_KEY_ENV = "NYLAS_E2E_API_KEY" |
| 11 | +_E2E_API_URI_ENV = "NYLAS_E2E_API_URI" |
| 12 | +_E2E_RUN_ENV = "NYLAS_E2E_RUN" |
| 13 | + |
| 14 | + |
| 15 | +def _is_truthy(value: str) -> bool: |
| 16 | + return value.lower() in {"1", "true", "yes", "on"} |
| 17 | + |
| 18 | + |
| 19 | +def pytest_addoption(parser): |
| 20 | + parser.addoption( |
| 21 | + "--run-e2e", |
| 22 | + action="store_true", |
| 23 | + default=False, |
| 24 | + help="Run live E2E tests that call Nylas APIs.", |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def pytest_collection_modifyitems(config, items): |
| 29 | + run_e2e = config.getoption("--run-e2e") or _is_truthy(os.getenv(_E2E_RUN_ENV, "")) |
| 30 | + if run_e2e: |
| 31 | + return |
| 32 | + |
| 33 | + skip_e2e = pytest.mark.skip( |
| 34 | + reason=( |
| 35 | + "E2E tests are opt-in. Set NYLAS_E2E_RUN=1 or pass --run-e2e to execute." |
| 36 | + ) |
| 37 | + ) |
| 38 | + for item in items: |
| 39 | + if "e2e" in item.keywords: |
| 40 | + item.add_marker(skip_e2e) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def paginated_list_contains_id(): |
| 45 | + def _contains_id(list_method, resource_id: str, limit: int = 100, max_pages: int = 20) -> bool: |
| 46 | + next_cursor = None |
| 47 | + seen_cursors = set() |
| 48 | + |
| 49 | + for _ in range(max_pages): |
| 50 | + query_params = {"limit": limit} |
| 51 | + if next_cursor: |
| 52 | + query_params["page_token"] = next_cursor |
| 53 | + |
| 54 | + response = list_method(query_params=query_params) |
| 55 | + if any(item.id == resource_id for item in response.data if item and item.id): |
| 56 | + return True |
| 57 | + |
| 58 | + if not response.next_cursor or response.next_cursor in seen_cursors: |
| 59 | + return False |
| 60 | + |
| 61 | + seen_cursors.add(response.next_cursor) |
| 62 | + next_cursor = response.next_cursor |
| 63 | + |
| 64 | + return False |
| 65 | + |
| 66 | + return _contains_id |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture(scope="session") |
| 70 | +def e2e_client() -> Client: |
| 71 | + api_key = os.getenv(_E2E_API_KEY_ENV, "") |
| 72 | + if not api_key: |
| 73 | + pytest.skip( |
| 74 | + "E2E tests require NYLAS_E2E_API_KEY to be set." |
| 75 | + ) |
| 76 | + |
| 77 | + api_uri = os.getenv(_E2E_API_URI_ENV, "") |
| 78 | + timeout = int(os.getenv("NYLAS_E2E_TIMEOUT", "90")) |
| 79 | + if api_uri: |
| 80 | + return Client(api_key=api_key, api_uri=api_uri, timeout=timeout) |
| 81 | + return Client(api_key=api_key, timeout=timeout) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def unique_name(): |
| 86 | + def _build(prefix: str) -> str: |
| 87 | + return f"{prefix}-{uuid4().hex[:10]}" |
| 88 | + |
| 89 | + return _build |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture |
| 93 | +def e2e_resource_registry(e2e_client): |
| 94 | + registry: Dict[str, List[str]] = { |
| 95 | + "policies": [], |
| 96 | + "rules": [], |
| 97 | + "lists": [], |
| 98 | + } |
| 99 | + yield registry |
| 100 | + |
| 101 | + for policy_id in reversed(registry["policies"]): |
| 102 | + try: |
| 103 | + e2e_client.policies.destroy(policy_id) |
| 104 | + except Exception: |
| 105 | + pass |
| 106 | + |
| 107 | + for rule_id in reversed(registry["rules"]): |
| 108 | + try: |
| 109 | + e2e_client.rules.destroy(rule_id) |
| 110 | + except Exception: |
| 111 | + pass |
| 112 | + |
| 113 | + for list_id in reversed(registry["lists"]): |
| 114 | + try: |
| 115 | + e2e_client.lists.destroy(list_id) |
| 116 | + except Exception: |
| 117 | + pass |
| 118 | + |
0 commit comments