From b3a04794ea557e087703227bc152c811d50c3546 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 09:20:00 -0500 Subject: [PATCH 1/7] client id parameters added with tests --- src/pyoncatqt/login.py | 25 +++++++++++---- tests/test_login_dialog.py | 66 +++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index 0be251c..694e138 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -151,7 +151,9 @@ class ONCatLogin(QGroupBox): Params ------ - key : str, required + client_id : str, optional + The client_id is an ONCat client ID. Defaults to None. client_id is required or key is required + key : str, optional The key used to retrieve ONCat client ID from configuration. Defaults to None. parent : QWidget, optional The parent widget. @@ -181,12 +183,16 @@ class ONCatLogin(QGroupBox): connection_updated = Signal(bool) - def __init__(self: QGroupBox, key: str = None, parent: QWidget = None, **kwargs: Dict[str, Any]) -> None: + def __init__( + self: QGroupBox, client_id: str = None, key: str = None, parent: QWidget = None, **kwargs: Dict[str, Any] + ) -> None: """ Initialize the ONCatLogin widget. Params ------ + client_id : str, optional + The client_id is an ONCat client ID. Defaults to None. key : str, optional The key used to retrieve ONCat client ID from configuration. Defaults to None. parent : QWidget, optional @@ -215,12 +221,17 @@ def __init__(self: QGroupBox, key: str = None, parent: QWidget = None, **kwargs: # OnCat agent self.oncat_url = get_data("login.oncat", "oncat_url") - self.client_id = get_data("login.oncat", f"{key}_id") + self.client_id = client_id or get_data("login.oncat", f"{key}_id") if not self.client_id: - raise ValueError(f"Invalid module {key}. No OnCat client Id is found for this application.") - - self.token_path = os.path.abspath(f"{os.path.expanduser('~')}/.pyoncatqt/{key}_token.json") - + raise ValueError(f"Invalid module {key}. No OnCat client Id is found or provided for this application.") + + # use the partial client id to generate the filename + token_filename = f"{self.client_id[0:8]}_token.json" + if key: + token_filename = f"{key}_token.json" + self.token_path = os.path.abspath(f"{os.path.expanduser('~')}/.pyoncatqt/{token_filename}") + print(" self.client_id", self.client_id) + print(" self.token_path", self.token_path) self.agent = pyoncat.ONCat( self.oncat_url, client_id=self.client_id, diff --git a/tests/test_login_dialog.py b/tests/test_login_dialog.py index 54e5ee9..236d136 100644 --- a/tests/test_login_dialog.py +++ b/tests/test_login_dialog.py @@ -29,7 +29,7 @@ def test_login_dialog_creation() -> None: assert isinstance(dialog.button_cancel, QPushButton) -def test_login(qtbot: pytest.fixture) -> None: +def test_login_key(qtbot: pytest.fixture) -> None: dialog = ONCatLogin(key="test") dialog.login_dialog = ONCatLoginDialog(agent=MagicMock(), parent=dialog) dialog.login_dialog.login_status.connect(check_status) @@ -56,6 +56,70 @@ def dialog_completed() -> None: qtbot.waitUntil(dialog_completed, timeout=5000) +def test_login_client_id(qtbot: pytest.fixture) -> None: + client_id = "12cnfjejsfsdf3456789ab" + dialog = ONCatLogin(client_id=client_id) + dialog.login_dialog = ONCatLoginDialog(agent=MagicMock(), parent=dialog) + dialog.login_dialog.login_status.connect(check_status) + qtbot.addWidget(dialog) + dialog.show() + + assert dialog.client_id == client_id + # token_12cnf.json + assert dialog.token_path.endswith(f"{client_id[0:8]}_token.json") + completed = False + + def handle_dialog() -> None: + nonlocal completed + + qtbot.keyClicks(dialog.login_dialog.user_pwd, "password") + qtbot.wait(2000) + qtbot.mouseClick(dialog.login_dialog.button_login, QtCore.Qt.LeftButton) + completed = True + + def dialog_completed() -> None: + nonlocal completed + assert completed is True + + QtCore.QTimer.singleShot(500, functools.partial(handle_dialog)) + qtbot.mouseClick(dialog.oncat_button, QtCore.Qt.LeftButton) + + qtbot.waitUntil(dialog_completed, timeout=5000) + + +def test_login_client_id_key(qtbot: pytest.fixture) -> None: + client_id = "12cnfjejsfsdf3456789ab" + key = "shiver" + + dialog = ONCatLogin(client_id=client_id, key=key) + dialog.login_dialog = ONCatLoginDialog(agent=MagicMock(), parent=dialog) + dialog.login_dialog.login_status.connect(check_status) + qtbot.addWidget(dialog) + dialog.show() + + assert dialog.client_id == client_id + # token_shiver.json + assert dialog.token_path.endswith(f"{key}_token.json") + completed = False + + def handle_dialog() -> None: + nonlocal completed + + qtbot.keyClicks(dialog.login_dialog.user_pwd, "password") + qtbot.wait(2000) + qtbot.mouseClick(dialog.login_dialog.button_login, QtCore.Qt.LeftButton) + completed = True + + def dialog_completed() -> None: + nonlocal completed + assert completed is True + + QtCore.QTimer.singleShot(500, functools.partial(handle_dialog)) + qtbot.mouseClick(dialog.oncat_button, QtCore.Qt.LeftButton) + + qtbot.waitUntil(dialog_completed, timeout=5000) + + def test_get_agent() -> None: dialog = ONCatLogin(key="test") dialog.agent = MagicMock() From c9fd5ce4e8477712f37f311889978279cbcfe6b1 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 12:25:59 -0500 Subject: [PATCH 2/7] documentation and tests updated --- docs/source/sample.rst | 6 ++++++ tests/test_login_dialog.py | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/source/sample.rst b/docs/source/sample.rst index f99d9a5..d6e071c 100644 --- a/docs/source/sample.rst +++ b/docs/source/sample.rst @@ -72,7 +72,13 @@ ONCatLoginDialog The `ONCatLoginDialog` can be imported and used separate from the `ONCatLogin` widget to give developers the ability to customize the login process. In order to use the `ONCatLoginDialog`, you must have an instance of the `pyoncat.ONCat` agent. + If you chose to use the `ONCatLogin` widget instead, the agent is already created for you. +In this case, a `key` or `client_id` are required to be passed by the user application. If `key` (application name) +is passed, the client_id is retrieved from the configuration, provided it exists. If the client_id is +provided, it uses this instead. If both are provided, client_id is used for oncat client id tasks, e.g. agent creation, +and the key is only used to create a human-readbale filename for saving the user's authentication token. + The `ONCatLoginDialog` requires the agent to be passed in as an argument. The agent is used to authenticate the user and manage the connection to the ONCat server. At a minimum the agent must be initialized with the ONCat server URL, flow, and the client ID. diff --git a/tests/test_login_dialog.py b/tests/test_login_dialog.py index 236d136..435182c 100644 --- a/tests/test_login_dialog.py +++ b/tests/test_login_dialog.py @@ -36,6 +36,9 @@ def test_login_key(qtbot: pytest.fixture) -> None: qtbot.addWidget(dialog) dialog.show() + assert dialog.client_id == "0123456489" + assert dialog.token_path.endswith("test_token.json") + completed = False def handle_dialog() -> None: @@ -89,7 +92,7 @@ def dialog_completed() -> None: def test_login_client_id_key(qtbot: pytest.fixture) -> None: client_id = "12cnfjejsfsdf3456789ab" - key = "shiver" + key = "test" dialog = ONCatLogin(client_id=client_id, key=key) dialog.login_dialog = ONCatLoginDialog(agent=MagicMock(), parent=dialog) @@ -97,8 +100,9 @@ def test_login_client_id_key(qtbot: pytest.fixture) -> None: qtbot.addWidget(dialog) dialog.show() + # client id provided as parameter assert dialog.client_id == client_id - # token_shiver.json + # key used for filename only token_shiver.json assert dialog.token_path.endswith(f"{key}_token.json") completed = False From 0fe35a4024bc00cee045ad4fbb8fe5dfedb98f0b Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 13:52:27 -0500 Subject: [PATCH 3/7] prints removed --- src/pyoncatqt/login.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index 694e138..a268f94 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -230,8 +230,6 @@ def __init__( if key: token_filename = f"{key}_token.json" self.token_path = os.path.abspath(f"{os.path.expanduser('~')}/.pyoncatqt/{token_filename}") - print(" self.client_id", self.client_id) - print(" self.token_path", self.token_path) self.agent = pyoncat.ONCat( self.oncat_url, client_id=self.client_id, From cb4f4f980b7405ced5c8b55c2af7ddafab3c85b7 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 14:19:54 -0500 Subject: [PATCH 4/7] space added --- src/pyoncatqt/login.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index a268f94..65b90d8 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -230,6 +230,7 @@ def __init__( if key: token_filename = f"{key}_token.json" self.token_path = os.path.abspath(f"{os.path.expanduser('~')}/.pyoncatqt/{token_filename}") + self.agent = pyoncat.ONCat( self.oncat_url, client_id=self.client_id, From fdbd8fc607ed65aadb475b2b1d79e3ac2f41f967 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 18:02:34 -0500 Subject: [PATCH 5/7] additional emit in comments, * to enforce kweyword arguments only --- src/pyoncatqt/login.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index 65b90d8..ccfae09 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -184,7 +184,7 @@ class ONCatLogin(QGroupBox): connection_updated = Signal(bool) def __init__( - self: QGroupBox, client_id: str = None, key: str = None, parent: QWidget = None, **kwargs: Dict[str, Any] + self: QGroupBox, *, client_id: str = None, key: str = None, parent: QWidget = None, **kwargs: Dict[str, Any] ) -> None: """ Initialize the ONCatLogin widget. @@ -252,7 +252,7 @@ def update_connection_status(self: QGroupBox) -> None: self.status_label.setText("ONCat: Disconnected") self.status_label.setStyleSheet("color: red") - self.connection_updated.emit(self.is_connected) + # self.connection_updated.emit(self.is_connected) @property def is_connected(self: QGroupBox) -> bool: @@ -264,6 +264,7 @@ def is_connected(self: QGroupBox) -> bool: bool True if connected, False otherwise. """ + try: self.agent.Facility.list() return True From d2be548b6d75868e237ca5c7b02bcc7e0a302e47 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 20:19:07 -0500 Subject: [PATCH 6/7] if else updated from review and. removed comment --- src/pyoncatqt/login.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index ccfae09..9ac4dac 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -221,8 +221,11 @@ def __init__( # OnCat agent self.oncat_url = get_data("login.oncat", "oncat_url") - self.client_id = client_id or get_data("login.oncat", f"{key}_id") - if not self.client_id: + if client_id is not None: + self.client_id = client_id + elif key is not None: + self.client_id = get_data("login.oncat", f"{key}_id") + else: raise ValueError(f"Invalid module {key}. No OnCat client Id is found or provided for this application.") # use the partial client id to generate the filename @@ -252,8 +255,6 @@ def update_connection_status(self: QGroupBox) -> None: self.status_label.setText("ONCat: Disconnected") self.status_label.setStyleSheet("color: red") - # self.connection_updated.emit(self.is_connected) - @property def is_connected(self: QGroupBox) -> bool: """ From d1c585128b01e9cbb16288de617591fe671976a8 Mon Sep 17 00:00:00 2001 From: "Patrou, Maria" Date: Thu, 16 Jan 2025 20:44:02 -0500 Subject: [PATCH 7/7] emit put back --- src/pyoncatqt/login.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pyoncatqt/login.py b/src/pyoncatqt/login.py index 9ac4dac..23de574 100644 --- a/src/pyoncatqt/login.py +++ b/src/pyoncatqt/login.py @@ -254,6 +254,7 @@ def update_connection_status(self: QGroupBox) -> None: else: self.status_label.setText("ONCat: Disconnected") self.status_label.setStyleSheet("color: red") + self.connection_updated.emit(self.is_connected) @property def is_connected(self: QGroupBox) -> bool: