Skip to content

Commit 2ed44ab

Browse files
authored
fix: add token_prefix option to accomodate be_next (#442)
* fix: add token_prefix option to accomodate be_next * Add tests
1 parent 38f8731 commit 2ed44ab

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

importTools/update_locations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def __init__(self, name):
151151
"login_path": "/".join(
152152
[self.address.strip("/"), os.environ[f"{name}_LOGIN"].strip("/")]
153153
),
154+
"token_prefix": os.environ.get(f"{name}_TOKEN_PREFIX"),
154155
}
155156

156157

sdk/python/scilog/authmixin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, address, options=None):
2222
self._username = options.get("username")
2323
self._password = options.get("password")
2424
self._login_path = options.get("login_path")
25+
self.options = options
2526

2627
def __repr__(self):
2728
tn = typename(self)

sdk/python/scilog/scicat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
from .authmixin import HEADER_JSON, AuthError, AuthMixin
1+
from .authmixin import HEADER_JSON, AuthError
22
from .httpclient import HttpClient
33

44

55
class SciCatRestAPI(HttpClient):
66
def __init__(self, *args, **kwargs):
77
super().__init__(*args, **kwargs)
88
self.login_path = self._login_path or "https://dacat.psi.ch/auth/msad"
9+
self.token_prefix = self.options.get("token_prefix", "")
910

1011
def authenticate(self, username, password):
1112
auth_payload = {"username": username, "password": password}
1213
res = self._login(auth_payload, HEADER_JSON)
1314
try:
14-
token = res["id"]
15+
token = f"{self.token_prefix}{res['id']}"
1516
except KeyError as e:
1617
raise SciCatAuthError(res) from e
1718
else:

sdk/python/tests/test_scicat.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from unittest.mock import ANY, Mock, patch
2+
3+
import pytest
4+
5+
from scilog import SciCat
6+
7+
8+
@patch("requests.post")
9+
@patch("requests.get")
10+
@pytest.mark.parametrize(
11+
"token_prefix",
12+
[
13+
"",
14+
"Bearer ",
15+
],
16+
)
17+
def test_get_proposals(mock_post, mock_get, token_prefix):
18+
address = "http://scicat"
19+
options = {
20+
"username": f"username{token_prefix}",
21+
"password": "password",
22+
"login_path": f"{address}/login",
23+
"token_prefix": token_prefix,
24+
}
25+
headers = {"Content-type": "application/json", "Accept": "application/json"}
26+
token = "token123"
27+
28+
scicat = SciCat(address, options=options)
29+
mock_response = Mock()
30+
mock_response.json.return_value = {"id": token}
31+
mock_get.return_value = mock_response
32+
scicat.http_client.config = {}
33+
scicat.proposals
34+
mock_get.assert_called_with(
35+
options["login_path"],
36+
json={"username": options["username"], "password": options["password"]},
37+
headers=headers,
38+
timeout=ANY,
39+
verify=True,
40+
)
41+
mock_post.assert_called_with(
42+
f"{address}/proposals",
43+
params=None,
44+
headers={**headers, "Authorization": f"{token_prefix}{token}"},
45+
timeout=ANY,
46+
verify=True,
47+
)

0 commit comments

Comments
 (0)