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
1 change: 1 addition & 0 deletions importTools/update_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def __init__(self, name):
"login_path": "/".join(
[self.address.strip("/"), os.environ[f"{name}_LOGIN"].strip("/")]
),
"token_prefix": os.environ.get(f"{name}_TOKEN_PREFIX"),
}


Expand Down
1 change: 1 addition & 0 deletions sdk/python/scilog/authmixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, address, options=None):
self._username = options.get("username")
self._password = options.get("password")
self._login_path = options.get("login_path")
self.options = options

def __repr__(self):
tn = typename(self)
Expand Down
5 changes: 3 additions & 2 deletions sdk/python/scilog/scicat.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from .authmixin import HEADER_JSON, AuthError, AuthMixin
from .authmixin import HEADER_JSON, AuthError
from .httpclient import HttpClient


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

def authenticate(self, username, password):
auth_payload = {"username": username, "password": password}
res = self._login(auth_payload, HEADER_JSON)
try:
token = res["id"]
token = f"{self.token_prefix}{res['id']}"
except KeyError as e:
raise SciCatAuthError(res) from e
else:
Expand Down
47 changes: 47 additions & 0 deletions sdk/python/tests/test_scicat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from unittest.mock import ANY, Mock, patch

import pytest

from scilog import SciCat


@patch("requests.post")
@patch("requests.get")
@pytest.mark.parametrize(
"token_prefix",
[
"",
"Bearer ",
],
)
def test_get_proposals(mock_post, mock_get, token_prefix):
address = "http://scicat"
options = {
"username": f"username{token_prefix}",
"password": "password",
"login_path": f"{address}/login",
"token_prefix": token_prefix,
}
headers = {"Content-type": "application/json", "Accept": "application/json"}
token = "token123"

scicat = SciCat(address, options=options)
mock_response = Mock()
mock_response.json.return_value = {"id": token}
mock_get.return_value = mock_response
scicat.http_client.config = {}
scicat.proposals
mock_get.assert_called_with(
options["login_path"],
json={"username": options["username"], "password": options["password"]},
headers=headers,
timeout=ANY,
verify=True,
)
mock_post.assert_called_with(
f"{address}/proposals",
params=None,
headers={**headers, "Authorization": f"{token_prefix}{token}"},
timeout=ANY,
verify=True,
)