From 668a5253c4f4184a9130b686a663fd8f53f4de61 Mon Sep 17 00:00:00 2001 From: rv0lt Date: Thu, 3 Oct 2024 11:12:40 +0200 Subject: [PATCH 1/4] new option to send motd to only unit personnel --- dds_cli/__main__.py | 5 +++-- dds_cli/motd_manager.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/dds_cli/__main__.py b/dds_cli/__main__.py index 63d01db3..0ab9133e 100644 --- a/dds_cli/__main__.py +++ b/dds_cli/__main__.py @@ -2070,15 +2070,16 @@ def deactivate_motd(click_ctx, motd_id): # -- dds motd send -- # @motd_group_command.command(name="send") @click.argument("motd_id", metavar="[MOTD_ID]", nargs=1, type=int, required=True) +@click.option("--unit-personnel-only", is_flag=True, required=False, default=False, help="Send MOTD to unit personnel only.") @click.pass_obj -def send_motd(click_ctx, motd_id): +def send_motd(click_ctx, motd_id, unit_personnel_only): """Send motd as email to all users.""" try: with dds_cli.motd_manager.MotdManager( no_prompt=click_ctx.get("NO_PROMPT", False), token_path=click_ctx.get("TOKEN_PATH"), ) as sender: - sender.send_motd(motd_id=motd_id) + sender.send_motd(motd_id=motd_id, unit_personnel_only=unit_personnel_only) except ( dds_cli.exceptions.AuthenticationError, dds_cli.exceptions.ApiResponseError, diff --git a/dds_cli/motd_manager.py b/dds_cli/motd_manager.py index f1aea17d..4e746179 100644 --- a/dds_cli/motd_manager.py +++ b/dds_cli/motd_manager.py @@ -126,13 +126,13 @@ def deactivate_motd(self, motd_id) -> None: ) LOG.info(response_message) - def send_motd(self, motd_id: int) -> None: + def send_motd(self, motd_id: int, unit_personnel_only=False) -> None: """Send specific MOTD to users.""" response_json, _ = dds_cli.utils.perform_request( endpoint=DDSEndpoint.MOTD_SEND, headers=self.token, method="post", - json={"motd_id": motd_id}, + json={"motd_id": motd_id, "unit_personnel_only": unit_personnel_only}, error_message="Failed sending the MOTD to users", ) From 77a279b89acef0b7130fbb5077951e9144b06791 Mon Sep 17 00:00:00 2001 From: rv0lt Date: Thu, 3 Oct 2024 11:13:08 +0200 Subject: [PATCH 2/4] black --- dds_cli/__main__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dds_cli/__main__.py b/dds_cli/__main__.py index 0ab9133e..ebd533a2 100644 --- a/dds_cli/__main__.py +++ b/dds_cli/__main__.py @@ -2070,7 +2070,13 @@ def deactivate_motd(click_ctx, motd_id): # -- dds motd send -- # @motd_group_command.command(name="send") @click.argument("motd_id", metavar="[MOTD_ID]", nargs=1, type=int, required=True) -@click.option("--unit-personnel-only", is_flag=True, required=False, default=False, help="Send MOTD to unit personnel only.") +@click.option( + "--unit-personnel-only", + is_flag=True, + required=False, + default=False, + help="Send MOTD to unit personnel only.", +) @click.pass_obj def send_motd(click_ctx, motd_id, unit_personnel_only): """Send motd as email to all users.""" From 09ab317aff0f22fd5f5e8547a676f20374a327e4 Mon Sep 17 00:00:00 2001 From: rv0lt Date: Thu, 3 Oct 2024 11:28:52 +0200 Subject: [PATCH 3/4] sprintlog --- SPRINTLOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/SPRINTLOG.md b/SPRINTLOG.md index b3ca0baf..eb204097 100644 --- a/SPRINTLOG.md +++ b/SPRINTLOG.md @@ -383,3 +383,4 @@ _Empty sprint_ # 2024-09-24 - 2024-10-04 - New version and changelog([#714](https://github.com/ScilifelabDataCentre/dds_cli/pull/714)) +- Added a new option to the motd send command to allow sending to unit personnel only ([#717](https://github.com/ScilifelabDataCentre/dds_cli/pull/717)) From 3134a77705a1ffd22afe801b65d5217fcf814478 Mon Sep 17 00:00:00 2001 From: rv0lt Date: Thu, 3 Oct 2024 11:43:39 +0200 Subject: [PATCH 4/4] added test --- tests/test_motd_manager.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_motd_manager.py b/tests/test_motd_manager.py index 335c7b6b..b3610df3 100644 --- a/tests/test_motd_manager.py +++ b/tests/test_motd_manager.py @@ -222,3 +222,30 @@ def test_add_new_motd_ok(caplog: LogCaptureFixture): logging.INFO, "Response from API about adding a MOTD.", ) in caplog.record_tuples + + +# send_motd + + +def test_send_motd_all(caplog: LogCaptureFixture): + """Send a MOTD to all users.""" + + motd_id = 1 + response_message = "Response from API about sending a MOTD." + returned_response: Dict = {"message": response_message} + + with caplog.at_level(logging.INFO): + # Create mocker + with Mocker() as mock: + # Create mocked request - real request not executed + mock.post(DDSEndpoint.MOTD_SEND, status_code=200, json=returned_response) + + with motd_manager.MotdManager(authenticate=False, no_prompt=True) as mtdm: + mtdm.token = {} # required, otherwise none + mtdm.send_motd(motd_id=motd_id, unit_personnel_only=False) # Send motd + + assert ( + "dds_cli.motd_manager", + logging.INFO, + response_message, + ) in caplog.record_tuples