-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
55 lines (45 loc) · 1.55 KB
/
conftest.py
File metadata and controls
55 lines (45 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import abc
from dataclasses import dataclass, field
from typing import Any, Generator
import pytest
from fastapi.testclient import TestClient
from workerfacing_api.dependencies import current_user_dep
from workerfacing_api.main import workerfacing_app
@pytest.fixture
def client() -> Generator[TestClient, None, None]:
# run everything in lifespan context
with TestClient(workerfacing_app) as client:
yield client
@dataclass
class EndpointParams:
method: str
path_params: str = ""
params: dict[str, str | int] = field(default_factory=dict)
class _TestEndpoint(abc.ABC):
endpoint = ""
@abc.abstractmethod
@pytest.fixture(scope="session")
def passing_params(self, *args: Any, **kwargs: Any) -> list[EndpointParams]:
raise NotImplementedError
def test_required_auth(
self,
monkeypatch: pytest.MonkeyPatch,
client: TestClient,
passing_params: list[EndpointParams],
) -> None:
for param in passing_params:
response = client.request(
method=param.method,
url=f"{self.endpoint}/{param.path_params}",
params=param.params,
)
response.raise_for_status()
monkeypatch.delitem(
workerfacing_app.dependency_overrides, # type: ignore
current_user_dep,
)
for param in passing_params:
response = client.request(
method=param.method, url=f"{self.endpoint}/{param.path_params}"
)
assert response.status_code == 401