Skip to content

Commit 0e53597

Browse files
committed
fix(skore-hub-project/login): Ignore token URI when using API key
1 parent 2f7bd86 commit 0e53597

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

skore-hub-project/src/skore_hub_project/authentication/uri.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ def persist(uri: str) -> None:
2525

2626
def URI() -> str:
2727
"""URI used for ``skore hub`` authentication."""
28+
uri_from_environment = environ.get(ENVARNAME)
29+
30+
if environ.get("SKORE_HUB_API_KEY"):
31+
return uri_from_environment or DEFAULT
32+
2833
filepath = Filepath()
2934
uri_from_persistence = filepath.read_text() if filepath.exists() else None
30-
uri_from_environment = environ.get(ENVARNAME)
3135

3236
if (
3337
uri_from_persistence

skore-hub-project/tests/unit/client/test_client.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pytest import mark, raises
1212

1313
from skore_hub_project.authentication import token, uri
14-
from skore_hub_project.authentication.uri import DEFAULT as URI
14+
from skore_hub_project.authentication.uri import DEFAULT as DEFAULT_URI
1515
from skore_hub_project.client.client import Client, HUBClient
1616

1717
DATETIME_MIN = datetime.min.replace(tzinfo=timezone.utc).isoformat()
@@ -123,7 +123,7 @@ def sleep(timeout):
123123
class TestHUBClient:
124124
def test_request_with_api_key(self, monkeypatch, respx_mock):
125125
monkeypatch.setenv("SKORE_HUB_API_KEY", "<api-key>")
126-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
126+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
127127

128128
with HUBClient() as client:
129129
client.get("foo")
@@ -132,7 +132,7 @@ def test_request_with_api_key(self, monkeypatch, respx_mock):
132132
assert respx_mock.calls.last.request.headers["X-API-Key"] == "<api-key>"
133133

134134
def test_request_with_token(self, respx_mock):
135-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
135+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
136136

137137
assert not token.Filepath().exists()
138138

@@ -150,12 +150,12 @@ def test_request_with_token(self, respx_mock):
150150
assert respx_mock.calls.last.request.headers["authorization"] == "Bearer A"
151151

152152
def test_request_with_token_and_uri(self, respx_mock):
153-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
153+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
154154

155155
assert not token.Filepath().exists()
156156

157157
token.persist("A", "B", DATETIME_MAX)
158-
uri.persist(URI)
158+
uri.persist(DEFAULT_URI)
159159

160160
assert token.Filepath().exists()
161161
assert token.access(refresh=False) == "A"
@@ -165,10 +165,48 @@ def test_request_with_token_and_uri(self, respx_mock):
165165

166166
assert token.Filepath().exists()
167167
assert token.access(refresh=False) == "A"
168-
assert uri.URI() == URI
168+
assert uri.URI() == DEFAULT_URI
169169
assert "X-API-Key" not in respx_mock.calls.last.request.headers
170170
assert respx_mock.calls.last.request.headers["authorization"] == "Bearer A"
171171

172+
def test_request_with_api_key_and_token_and_uri(self, monkeypatch, respx_mock):
173+
TOKEN_URI = "https://token.com"
174+
API_KEY_URI = "https://apikey.com"
175+
176+
assert not uri.Filepath().exists()
177+
178+
# simulate user with token
179+
uri.persist(TOKEN_URI)
180+
181+
assert uri.URI() == TOKEN_URI
182+
183+
# simulate user with token and API key
184+
monkeypatch.setenv("SKORE_HUB_API_KEY", "<api-key>")
185+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
186+
187+
assert uri.URI() == DEFAULT_URI
188+
189+
with HUBClient() as client:
190+
client.get("foo")
191+
192+
assert respx_mock.calls.last.request.url == urljoin(DEFAULT_URI, "foo")
193+
assert "authorization" not in respx_mock.calls.last.request.headers
194+
assert respx_mock.calls.last.request.headers["X-API-Key"] == "<api-key>"
195+
196+
# simulate user with token, API key and URI in environment
197+
monkeypatch.setenv("SKORE_HUB_API_KEY", "<api-key>")
198+
monkeypatch.setenv("SKORE_HUB_URI", API_KEY_URI)
199+
respx_mock.get(urljoin(API_KEY_URI, "foo")).mock(Response(200))
200+
201+
assert uri.URI() == API_KEY_URI
202+
203+
with HUBClient() as client:
204+
client.get("foo")
205+
206+
assert respx_mock.calls.last.request.url == urljoin(API_KEY_URI, "foo")
207+
assert "authorization" not in respx_mock.calls.last.request.headers
208+
assert respx_mock.calls.last.request.headers["X-API-Key"] == "<api-key>"
209+
172210
@mark.respx(assert_all_mocked=False)
173211
def test_request_with_invalid_token_raises(self, respx_mock):
174212
with raises(token.TokenError, match="not logged in"), HUBClient() as client:
@@ -179,7 +217,7 @@ def test_request_with_invalid_token_raises(self, respx_mock):
179217
@mark.respx(assert_all_mocked=False)
180218
def test_request_with_conflicting_uri_raises(self, respx_mock, monkeypatch):
181219
token.persist("A", "B", DATETIME_MAX)
182-
uri.persist(URI)
220+
uri.persist(DEFAULT_URI)
183221
monkeypatch.setenv(uri.ENVARNAME, "https://my-conflicting-uri")
184222

185223
with (
@@ -193,7 +231,7 @@ def test_request_with_conflicting_uri_raises(self, respx_mock, monkeypatch):
193231
assert not respx_mock.calls
194232

195233
def test_request_with_expired_token(self, tmp_path, respx_mock):
196-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
234+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
197235
respx_mock.post(REFRESH_URL).mock(
198236
Response(
199237
200,
@@ -221,7 +259,7 @@ def test_request_with_expired_token(self, tmp_path, respx_mock):
221259
assert respx_mock.calls.last.request.headers["authorization"] == "Bearer D"
222260

223261
def test_request_raises(self, tmp_path, respx_mock):
224-
respx_mock.get(urljoin(URI, "foo")).mock(Response(404))
262+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(404))
225263

226264
assert not token.Filepath().exists()
227265

@@ -244,7 +282,7 @@ def test_request_without_package_version(self, respx_mock):
244282
assert version("skore-hub-project") == "0.0.0+unknown"
245283
assert PACKAGE_VERSION is None
246284

247-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
285+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
248286

249287
with HUBClient(authenticated=False) as client:
250288
client.get("foo")
@@ -253,7 +291,7 @@ def test_request_without_package_version(self, respx_mock):
253291

254292
def test_request_with_package_version(self, monkeypatch, respx_mock):
255293
monkeypatch.setattr("skore_hub_project.client.client.PACKAGE_VERSION", "1.0.0")
256-
respx_mock.get(urljoin(URI, "foo")).mock(Response(200))
294+
respx_mock.get(urljoin(DEFAULT_URI, "foo")).mock(Response(200))
257295

258296
with HUBClient(authenticated=False) as client:
259297
client.get("foo")

0 commit comments

Comments
 (0)