Skip to content

Commit

Permalink
Merge pull request #217 from novonordisk-research/manual_login_context
Browse files Browse the repository at this point in the history
fix: Manual login within a context manager works
  • Loading branch information
SRFU-NN authored Aug 19, 2024
2 parents d66b3e6 + 63bc16d commit 072cd3e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/OptiHPLCHandler/empower_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def __init__(

def __enter__(self):
"""Start the context manager."""
if self.auto_login:
self.login(has_context=True)
self._has_context = True
if self.auto_login:
self.login()
return self

def __exit__(self, exc_type, exc_value, traceback):
Expand Down Expand Up @@ -131,7 +131,6 @@ def login(
self,
username: Optional[str] = None,
password: Optional[str] = None,
has_context: bool = False,
):
"""
Log into Empower.
Expand All @@ -145,7 +144,7 @@ def login(
login is called with a different username, the default username is changed
to the given username.
"""
if not has_context:
if not self._has_context:
# If the login is not done in a context manager, it is only allowed if
# `run_without_context` is True, and even there, it psots a warning.
if self.allow_login_without_context_manager:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_proxy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ def test_no_autologin(self):
pass
assert self.handler.connection.login.call_count == 0

def test_context_no_autologin(self):
self.handler.auto_login = False
with self.handler:
self.handler.login()


class TestUsername(unittest.TestCase):
# We need to patch the EmpowerConnection class, because it is used in the
Expand Down

0 comments on commit 072cd3e

Please sign in to comment.