diff --git a/api-openbis-python3-pybis/src/python/pybis/pybis.py b/api-openbis-python3-pybis/src/python/pybis/pybis.py index a9ebfc8abe15f6e57a763fa1f652e868ec430576..1d1f13cd2be4f06ccfe63d2caf56892abd3db772 100644 --- a/api-openbis-python3-pybis/src/python/pybis/pybis.py +++ b/api-openbis-python3-pybis/src/python/pybis/pybis.py @@ -32,7 +32,7 @@ import zlib from datetime import datetime from pathlib import Path from typing import List -from urllib.parse import quote, urljoin, urlparse +from urllib.parse import urljoin, urlparse import requests import urllib3 @@ -56,9 +56,9 @@ from .entity_type import ( MaterialType, SampleType, ) +from .experiment import Experiment from .group import Group from .openbis_object import OpenBisObject, Transaction -from .experiment import Experiment from .person import Person from .project import Project from .role_assignment import RoleAssignment @@ -81,7 +81,6 @@ from .utils import ( extract_permid, extract_person, extract_userId, - extract_username_from_token, format_timestamp, is_identifier, is_number, @@ -4404,6 +4403,8 @@ class Openbis: """ if not token: return + if type(token) is PersonalAccessToken: + token = token.permId if not self.is_token_valid(token): raise ValueError("Session is no longer valid. Please log in again.") else: diff --git a/api-openbis-python3-pybis/src/python/tests/test_openbis.py b/api-openbis-python3-pybis/src/python/tests/test_openbis.py index 94882a43ba253c840188e1ba12b3ad351f846d64..3c59ee469e83071e5f6e84ec86e1ad2e595682f0 100644 --- a/api-openbis-python3-pybis/src/python/tests/test_openbis.py +++ b/api-openbis-python3-pybis/src/python/tests/test_openbis.py @@ -12,13 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # -import json -import random import re +import time import pytest -import time -from pybis import DataSet from pybis import Openbis @@ -47,7 +44,7 @@ def test_cached_token(other_openbis_instance): assert other_openbis_instance.is_token_valid() is False -def test_create_permId(openbis_instance): +def test_create_perm_id(openbis_instance): permId = openbis_instance.create_permId() assert permId is not None m = re.search("([0-9]){17}-([0-9]*)", permId) @@ -58,9 +55,9 @@ def test_create_permId(openbis_instance): def test_get_samples_update_in_transaction(openbis_instance): - ''' + """ Update samples in transaction without overriding parents/children - ''' + """ name_suffix = str(time.time()) # Create new space space = openbis_instance.new_space(code='space_name' + name_suffix, description='') @@ -213,9 +210,9 @@ def test_get_samples_update_in_transaction(openbis_instance): def test_failed_second_login_raises_exception(openbis_instance): - ''' + """ Logins to openBIS using wrong username/password, PyBIS should raise exception - ''' + """ assert openbis_instance.is_session_active() is True try: @@ -224,3 +221,15 @@ def test_failed_second_login_raises_exception(openbis_instance): assert False except ValueError as e: assert str(e) == "login to openBIS failed" + + +def test_set_token_accepts_personal_access_token_object(openbis_instance): + """ + Verifies that set_token method accepts both permId and PersonalAccessToken object + """ + assert openbis_instance.is_session_active() is True + + pat = openbis_instance.get_or_create_personal_access_token(sessionName="Project A") + + openbis_instance.set_token(pat, save_token=True) + openbis_instance.set_token(pat.permId, save_token=True)