From 483d1ae621828784bd767b10e2ecde8f452a5886 Mon Sep 17 00:00:00 2001 From: Joey Orlando Date: Sat, 2 Nov 2024 09:15:26 -0400 Subject: [PATCH] wip --- engine/common/api_helpers/custom_fields.py | 2 +- engine/common/tests/test_custom_fields.py | 105 +++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/engine/common/api_helpers/custom_fields.py b/engine/common/api_helpers/custom_fields.py index c845b0a50a..f0ade49461 100644 --- a/engine/common/api_helpers/custom_fields.py +++ b/engine/common/api_helpers/custom_fields.py @@ -139,7 +139,7 @@ def to_internal_value(self, slack_id: str): return self.get_queryset().get(slack_id=slack_id.upper()) except ObjectDoesNotExist: raise ValidationError("Slack channel does not exist") - except (TypeError, ValueError): + except (TypeError, ValueError, AttributeError): raise ValidationError("Invalid Slack channel") def to_representation(self, obj: "SlackChannel") -> str: diff --git a/engine/common/tests/test_custom_fields.py b/engine/common/tests/test_custom_fields.py index b063af0739..484b261bcd 100644 --- a/engine/common/tests/test_custom_fields.py +++ b/engine/common/tests/test_custom_fields.py @@ -6,6 +6,7 @@ from rest_framework import serializers import common.api_helpers.custom_fields as cf +from common.api_helpers.exceptions import BadRequest class TestTimeZoneField: @@ -100,3 +101,107 @@ class MySerializer(serializers.Serializer): else: with pytest.raises(serializers.ValidationError): serializer.is_valid(raise_exception=True) + + +class TestSlackChannelsFilteredByOrganizationSlackWorkspaceField: + class MockRequest: + def __init__(self, user) -> None: + self.user = user + + class MySerializer(serializers.Serializer): + slack_channel_id = cf.SlackChannelsFilteredByOrganizationSlackWorkspaceField() + + @pytest.mark.django_db + def test_org_does_not_have_slack_connected( + self, + make_organization, + make_user_for_organization, + ): + organization = make_organization() + user = make_user_for_organization(organization) + + serializer = self.MySerializer( + data={"slack_channel_id": "abcd"}, + context={"request": self.MockRequest(user)}, + ) + + with pytest.raises(BadRequest) as excinfo: + serializer.is_valid(raise_exception=True) + + assert "Slack isn't connected to this workspace" in str(excinfo.value) + + @pytest.mark.django_db + def test_org_channel_doesnt_belong_to_org( + self, + make_organization, + make_user_for_organization, + make_slack_team_identity, + make_slack_channel, + ): + slack_channel1_id = "FOO" + slack_channel2_id = "BAR" + + slack_team_identity1 = make_slack_team_identity() + make_slack_channel(slack_team_identity1, slack_id=slack_channel1_id) + + slack_team_identity2 = make_slack_team_identity() + make_slack_channel(slack_team_identity2, slack_id=slack_channel2_id) + + organization = make_organization(slack_team_identity=slack_team_identity1) + user = make_user_for_organization(organization) + + serializer = self.MySerializer( + data={"slack_channel_id": slack_channel2_id}, + context={"request": self.MockRequest(user)}, + ) + + with pytest.raises(serializers.ValidationError) as excinfo: + serializer.is_valid(raise_exception=True) + + assert "Slack channel does not exist" in str(excinfo.value) + + @pytest.mark.django_db + def test_invalid_slack_channel( + self, + make_organization, + make_user_for_organization, + make_slack_team_identity, + make_slack_channel, + ): + slack_channel_id = "FOO" + slack_team_identity = make_slack_team_identity() + make_slack_channel(slack_team_identity, slack_id=slack_channel_id) + organization = make_organization(slack_team_identity=slack_team_identity) + user = make_user_for_organization(organization) + + serializer = self.MySerializer( + data={"slack_channel_id": 1}, + context={"request": self.MockRequest(user)}, + ) + + with pytest.raises(serializers.ValidationError) as excinfo: + serializer.is_valid(raise_exception=True) + + assert "Invalid Slack channel" in str(excinfo.value) + + @pytest.mark.django_db + def test_valid( + self, + make_organization, + make_user_for_organization, + make_slack_team_identity, + make_slack_channel, + ): + slack_channel_id = "FOO" + slack_team_identity = make_slack_team_identity() + slack_channel = make_slack_channel(slack_team_identity, slack_id=slack_channel_id) + organization = make_organization(slack_team_identity=slack_team_identity) + user = make_user_for_organization(organization) + + serializer = self.MySerializer( + data={"slack_channel_id": slack_channel_id}, + context={"request": self.MockRequest(user)}, + ) + + serializer.is_valid(raise_exception=True) + assert serializer.validated_data["slack_channel_id"] == slack_channel