Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/flareio/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
tenant_id: t.Optional[int] = None,
session: t.Optional[requests.Session] = None,
api_domain: t.Optional[str] = None,
_disable_auth: bool = False,
_enable_beta_features: bool = False,
) -> None:
if not api_key:
Expand All @@ -53,6 +54,7 @@ def __init__(

self._api_token: t.Optional[str] = None
self._api_token_exp: t.Optional[datetime] = None
self._disable_auth: bool = _disable_auth
self._session = session or self._create_session()

@classmethod
Expand Down Expand Up @@ -134,6 +136,8 @@ def generate_token(self) -> str:
return token

def _auth_headers(self) -> dict:
if self._disable_auth:
return dict()
api_token: t.Optional[str] = self._api_token
if not api_token or (
self._api_token_exp and self._api_token_exp < datetime.now()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_api_client_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,22 @@ def test_bad_domain() -> None:
match="Client was used to access netloc='bad.com' at url='https://bad.com/hello-post'. Only the domain api.flare.io is supported.",
):
client.post("https://bad.com/hello-post")


def test_disable_auth_does_not_call_generate() -> None:
client = get_test_client(
authenticated=False,
_disable_auth=True,
)
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"POST",
"https://api.flare.io/hello-post",
status_code=200,
)
client.post("https://api.flare.io/hello-post", json={"foo": "bar"})
assert mocker.last_request.url == "https://api.flare.io/hello-post"
assert mocker.last_request.json() == {"foo": "bar"}

# Authorization header should not be present when auth is disabled
assert not mocker.last_request.headers.get("Authorization")
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ def get_test_client(
authenticated: bool = True,
api_domain: t.Optional[str] = None,
_enable_beta_features: bool = False,
_disable_auth: bool = False,
) -> FlareApiClient:
client = FlareApiClient(
api_key="test-api-key",
tenant_id=tenant_id,
api_domain=api_domain,
_enable_beta_features=_enable_beta_features,
_disable_auth=_disable_auth,
)

if authenticated:
Expand Down