Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyorlando committed Nov 2, 2024
1 parent 89356e0 commit 483d1ae
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engine/common/api_helpers/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
105 changes: 105 additions & 0 deletions engine/common/tests/test_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 483d1ae

Please sign in to comment.