Skip to content

Commit e43d8d2

Browse files
authored
allow disabling auth (#28)
1 parent d07d2c1 commit e43d8d2

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/flareio/api_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
tenant_id: t.Optional[int] = None,
3535
session: t.Optional[requests.Session] = None,
3636
api_domain: t.Optional[str] = None,
37+
_disable_auth: bool = False,
3738
_enable_beta_features: bool = False,
3839
) -> None:
3940
if not api_key:
@@ -53,6 +54,7 @@ def __init__(
5354

5455
self._api_token: t.Optional[str] = None
5556
self._api_token_exp: t.Optional[datetime] = None
57+
self._disable_auth: bool = _disable_auth
5658
self._session = session or self._create_session()
5759

5860
@classmethod
@@ -134,6 +136,8 @@ def generate_token(self) -> str:
134136
return token
135137

136138
def _auth_headers(self) -> dict:
139+
if self._disable_auth:
140+
return dict()
137141
api_token: t.Optional[str] = self._api_token
138142
if not api_token or (
139143
self._api_token_exp and self._api_token_exp < datetime.now()

tests/test_api_client_endpoints.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,22 @@ def test_bad_domain() -> None:
121121
match="Client was used to access netloc='bad.com' at url='https://bad.com/hello-post'. Only the domain api.flare.io is supported.",
122122
):
123123
client.post("https://bad.com/hello-post")
124+
125+
126+
def test_disable_auth_does_not_call_generate() -> None:
127+
client = get_test_client(
128+
authenticated=False,
129+
_disable_auth=True,
130+
)
131+
with requests_mock.Mocker() as mocker:
132+
mocker.register_uri(
133+
"POST",
134+
"https://api.flare.io/hello-post",
135+
status_code=200,
136+
)
137+
client.post("https://api.flare.io/hello-post", json={"foo": "bar"})
138+
assert mocker.last_request.url == "https://api.flare.io/hello-post"
139+
assert mocker.last_request.json() == {"foo": "bar"}
140+
141+
# Authorization header should not be present when auth is disabled
142+
assert not mocker.last_request.headers.get("Authorization")

tests/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ def get_test_client(
1111
authenticated: bool = True,
1212
api_domain: t.Optional[str] = None,
1313
_enable_beta_features: bool = False,
14+
_disable_auth: bool = False,
1415
) -> FlareApiClient:
1516
client = FlareApiClient(
1617
api_key="test-api-key",
1718
tenant_id=tenant_id,
1819
api_domain=api_domain,
1920
_enable_beta_features=_enable_beta_features,
21+
_disable_auth=_disable_auth,
2022
)
2123

2224
if authenticated:

0 commit comments

Comments
 (0)