Skip to content

Commit

Permalink
Merge pull request #717 from ScilifelabDataCentre/DDS-2143-Modify-the…
Browse files Browse the repository at this point in the history
…-command-dds-motd-send-in-CLI

Modify the command dds motd send to allow the option to send only to unit personnel
  • Loading branch information
rv0lt authored Oct 3, 2024
2 parents 3a6f3e8 + 3134a77 commit 0304a1e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
11 changes: 9 additions & 2 deletions dds_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions dds_cli/motd_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_motd_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0304a1e

Please sign in to comment.