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)) diff --git a/dds_cli/__main__.py b/dds_cli/__main__.py index 63d01db3..ebd533a2 100644 --- a/dds_cli/__main__.py +++ b/dds_cli/__main__.py @@ -2070,15 +2070,22 @@ 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", ) 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