diff --git a/api-openbis-python3-pybis/src/python/pybis/pybis.py b/api-openbis-python3-pybis/src/python/pybis/pybis.py index f8e118db98c5b31661082720fe34f46bc5f67aa9..81360b6c63c8acdafa06ee01e9fe6662a63d1bf7 100644 --- a/api-openbis-python3-pybis/src/python/pybis/pybis.py +++ b/api-openbis-python3-pybis/src/python/pybis/pybis.py @@ -1310,16 +1310,19 @@ class Openbis: if password is None: import getpass - password = getpass.getpass() + def is_different_login(): + return username != self._get_username() + login_request = { "method": "login", "params": [username, password], } - self.token = self._post_request(self.as_v3, login_request) - if self.token is None: + token = self._post_request(self.as_v3, login_request) + if token is None or (is_different_login() and token == self.token): raise ValueError("login to openBIS failed") + self.token = token if save_token: self._save_token_to_disk() self._password(password) 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 8db5be30fbb973acf22d7ee6840321a80ce55a6f..c61c760035525200d162f680cb9bfa324f24b357 100644 --- a/api-openbis-python3-pybis/src/python/tests/test_openbis.py +++ b/api-openbis-python3-pybis/src/python/tests/test_openbis.py @@ -196,3 +196,17 @@ def test_get_samples_update_in_transaction(openbis_instance): assert sample1.parents == [sample3.identifier] assert sample2.parents == [] assert sample3.children == [sample1.identifier] + + +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: + openbis_instance.login('non_existing_username_for_test', 'abcdef') + # Login should fail at this point + assert False + except ValueError as e: + assert str(e) == "login to openBIS failed"