From df25ab8b9194e32b96e2e56b3f74004b65c93403 Mon Sep 17 00:00:00 2001 From: Daniel Draper Date: Thu, 7 Sep 2023 07:52:50 +0700 Subject: [PATCH] try different permissions, better tests --- .github/workflows/ci.yml | 2 +- sched_slack_bot/model/schedule.py | 6 +++-- test_sched_slack_bot/model/test_schedule.py | 28 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 58b0536..dad0d80 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,7 +86,7 @@ jobs: build_and_push_docker_image: permissions: - packages: write + packages: write-all runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 diff --git a/sched_slack_bot/model/schedule.py b/sched_slack_bot/model/schedule.py index 727eaaf..49946c4 100644 --- a/sched_slack_bot/model/schedule.py +++ b/sched_slack_bot/model/schedule.py @@ -151,9 +151,11 @@ def from_modal_submission(cls, submission_body: SlackBody) -> Schedule: if schedule_id.startswith(CREATE_NEW_SCHEDULE_VIEW_ID_PREFIX): schedule_id = str(uuid.uuid4()) - - if SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER in schedule_id: + elif SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER in schedule_id: schedule_id = schedule_id.split(SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER)[0] + else: + raise ValueError(f"external id of schedule doesn't contain delimiter '{SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER}'" + f"nor prefix '{CREATE_NEW_SCHEDULE_VIEW_ID_PREFIX}', received instead: '{schedule_id}'") return Schedule( id=schedule_id, diff --git a/test_sched_slack_bot/model/test_schedule.py b/test_sched_slack_bot/model/test_schedule.py index c501f96..1f661e0 100644 --- a/test_sched_slack_bot/model/test_schedule.py +++ b/test_sched_slack_bot/model/test_schedule.py @@ -17,6 +17,8 @@ get_datetime_block_ids, FIRST_ROTATION_LABEL, SECOND_ROTATION_LABEL, + SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER, + CREATE_NEW_SCHEDULE_VIEW_ID_PREFIX, ) @@ -81,7 +83,7 @@ def _add_valid_datetime_values( def valid_slack_body(minimum_slack_body: SlackBody, schedule: Schedule) -> SlackBody: valid_slack_body = copy.deepcopy(minimum_slack_body) - valid_slack_body["view"]["external_id"] = schedule.id + valid_slack_body["view"]["external_id"] = f"{schedule.id}{SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER}{str(uuid.uuid4())}" # mypy cant deal with dynamic typed dicts valid_slack_body["view"]["state"]["values"][DISPLAY_NAME_BLOCK_ID] = { @@ -231,7 +233,7 @@ def test_schedule_from_modal_submission_raises_with_missing_values( Schedule.from_modal_submission(submission_body=valid_slack_body) -def test_schedule_from_modal_submission_works(schedule: Schedule, valid_slack_body: SlackBody) -> None: +def test_schedule_from_modal_submission_works_for_edit(schedule: Schedule, valid_slack_body: SlackBody) -> None: from_modal = Schedule.from_modal_submission(submission_body=valid_slack_body) assert from_modal.display_name == schedule.display_name @@ -241,3 +243,25 @@ def test_schedule_from_modal_submission_works(schedule: Schedule, valid_slack_bo assert from_modal.current_index == schedule.current_index assert from_modal.time_between_rotations == schedule.time_between_rotations assert from_modal.id == schedule.id + + +def test_schedule_from_modal_submission_works_for_create(schedule: Schedule, valid_slack_body: SlackBody) -> None: + valid_slack_body["view"][ + "external_id" + ] = f"{CREATE_NEW_SCHEDULE_VIEW_ID_PREFIX}{SCHEDULE_VIEW_ID_SCHEDULE_ID_DELIMITER}{str(uuid.uuid4())}" + + from_modal = Schedule.from_modal_submission(submission_body=valid_slack_body) + + assert from_modal.display_name == schedule.display_name + assert from_modal.members == schedule.members + assert from_modal.channel_id_to_notify_in == schedule.channel_id_to_notify_in + assert from_modal.next_rotation == schedule.next_rotation + assert from_modal.current_index == schedule.current_index + assert from_modal.time_between_rotations == schedule.time_between_rotations + assert from_modal.id != schedule.id + + +def test_schedule_from_modal_submission_raises_with_wrong_external_id(schedule: Schedule, valid_slack_body: SlackBody) -> None: + valid_slack_body["view"]["external_id"] = "broken" + with pytest.raises(ValueError): + Schedule.from_modal_submission(submission_body=valid_slack_body)