def test_unlink_team(self): data = self.send_slack_message( "unlink team", channel_name=self.channel_name, channel_id=self.channel_id, ) assert "Click here to unlink your team from this channel" in get_response_text(data)
def test_user_message_unlink(self): """ Test that when a user types in "unlink" to the DM we reply with the correct response """ idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX1", config={}) Identity.objects.create( external_id="UXXXXXXX1", idp=idp, user=self.user, status=IdentityStatus.VALID, scopes=[], ) responses.add(responses.POST, "https://slack.com/api/chat.postMessage", json={"ok": True}) resp = self.post_webhook( event_data=json.loads(MESSAGE_IM_EVENT_UNLINK)) assert resp.status_code == 200, resp.content request = responses.calls[0].request assert request.headers[ "Authorization"] == "Bearer xoxb-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx" data = json.loads(request.body) assert "Click here to unlink your identity" in get_response_text(data)
def test_link_team_identity_does_not_exist(self): """Test that get_identity fails if the user has no Identity and we reply with the LINK_USER_MESSAGE""" user2 = self.create_user() self.create_member( teams=[self.team], user=user2, role="member", organization=self.organization ) self.login_as(user2) data = self.send_slack_message("link team", user_id=OTHER_SLACK_ID) assert LINK_USER_FIRST_MESSAGE in get_response_text(data)
def test_link_user_identity(self): linking_url = build_linking_url( self.integration, self.external_id, self.channel_id, self.response_url ) response = self.client.post(linking_url) assert response.status_code == 200 assert len(responses.calls) >= 1 data = json.loads(str(responses.calls[0].request.body.decode("utf-8"))) assert SUCCESS_LINKED_MESSAGE in get_response_text(data)
def test_unlink_no_team(self): """ Test for when a user attempts to remove a link between a Slack channel and a Sentry team that does not exist. """ data = self.send_slack_message( "unlink team", channel_name="specific", channel_id=OTHER_SLACK_ID, ) assert TEAM_NOT_LINKED_MESSAGE in get_response_text(data)
def test_link_team_already_linked(self): """Test that if a team has already been linked to a Slack channel when a user tries to link them again, we reject the attempt and reply with the ALREADY_LINKED_MESSAGE""" self.link_team() response = self.get_success_response(data={"team": self.team.id}) self.assertTemplateUsed( response, "sentry/integrations/slack/post-linked-team.html") assert len(responses.calls) >= 1 data = json.loads(str(responses.calls[0].request.body.decode("utf-8"))) assert ( f"The {self.team.slug} team has already been linked to a Slack channel." in get_response_text(data))
def test_link_team_insufficient_role(self): """ Test that when a user whose role is insufficient attempts to link a team, we reject them and reply with the INSUFFICIENT_ROLE_MESSAGE. """ user2 = self.create_user() self.create_member( teams=[self.team], user=user2, role="member", organization=self.organization ) self.login_as(user2) link_user(user2, self.idp, slack_id=OTHER_SLACK_ID) data = self.send_slack_message("link team", user_id=OTHER_SLACK_ID) assert INSUFFICIENT_ROLE_MESSAGE in get_response_text(data)
def test_user_message_already_unlinked(self): """ Test that when a user without an Identity types in "unlink" to the DM we reply with the correct response. """ IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX1", config={}) responses.add(responses.POST, "https://slack.com/api/chat.postMessage", json={"ok": True}) resp = self.post_webhook(event_data=json.loads(MESSAGE_IM_EVENT_UNLINK)) assert resp.status_code == 200, resp.content request = responses.calls[0].request assert request.headers["Authorization"] == "Bearer xoxb-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxxx" data = json.loads(request.body) assert "You do not have a linked identity to unlink" in get_response_text(data)
def test_link_team_from_dm(self): """ Test that if a user types `/sentry link team` from a DM instead of a channel, we reply with an error message. """ response = self.get_slack_response( { "text": "link team", "team_id": self.external_id, "user_id": OTHER_SLACK_ID, "channel_name": "directmessage", } ) data = json.loads(str(response.content.decode("utf-8"))) assert LINK_FROM_CHANNEL_MESSAGE in get_response_text(data)
def test_unlink_multiple_orgs(self): # Create another organization and team for this user that is linked through `self.integration`. organization2 = self.create_organization(owner=self.user) team2 = self.create_team(organization=organization2, members=[self.user]) OrganizationIntegration.objects.create( organization=organization2, integration=self.integration ) self.link_team(team2) data = self.send_slack_message( "unlink team", channel_name=self.channel_name, channel_id=self.channel_id, ) assert "Click here to unlink your team from this channel" in get_response_text(data)
def test_link_another_team_to_channel(self): """ Test that we block a user who tries to link a second team to a channel that already has a team linked to it. """ self.link_team() response = self.get_slack_response( { "text": "link team", "team_id": self.external_id, "user_id": self.slack_id, "channel_name": self.channel_name, "channel_id": self.channel_id, } ) data = json.loads(str(response.content.decode("utf-8"))) assert CHANNEL_ALREADY_LINKED_MESSAGE in get_response_text(data)
def test_unlink_team(self): """Test that a team can be unlinked from a Slack channel""" response = self.get_success_response() self.assertTemplateUsed(response, "sentry/integrations/slack/unlink-team.html") response = self.get_success_response(data={}) self.assertTemplateUsed( response, "sentry/integrations/slack/unlinked-team.html") external_actors = self.get_linked_teams() assert len(external_actors) == 0 assert len(responses.calls) >= 1 data = json.loads(str(responses.calls[0].request.body.decode("utf-8"))) assert ( f"This channel will no longer receive issue alert notifications for the {self.team.slug} team." in get_response_text(data)) team_settings = NotificationSetting.objects.filter( scope_type=NotificationScopeType.TEAM.value, target=self.team.actor.id) assert len(team_settings) == 0
def test_unlink_multiple_teams(self): """ Test that if you have linked multiple teams to a single channel, when you type `/sentry unlink team`, we unlink all teams from that channel. This should only apply to the one organization who did this before we blocked users from doing so. """ team2 = self.create_team(organization=self.organization, name="Team Hellboy") self.link_team(team2) external_actors = self.get_linked_teams( [self.team.actor_id, team2.actor_id]) assert len(external_actors) == 2 response = self.get_success_response() self.assertTemplateUsed(response, "sentry/integrations/slack/unlink-team.html") response = self.get_success_response(data={}) self.assertTemplateUsed( response, "sentry/integrations/slack/unlinked-team.html") external_actors = self.get_linked_teams( [self.team.actor_id, team2.actor_id]) assert len(external_actors) == 0 assert len(responses.calls) >= 1 data = json.loads(str(responses.calls[0].request.body.decode("utf-8"))) assert ( f"This channel will no longer receive issue alert notifications for the {self.team.slug} team." in get_response_text(data)) team_settings = NotificationSetting.objects.filter( scope_type=NotificationScopeType.TEAM.value, target=self.team.actor.id) assert len(team_settings) == 0
def test_link_team(self): """Test that we successfully link a team to a Slack channel""" response = self.get_success_response() self.assertTemplateUsed(response, "sentry/integrations/slack/link-team.html") response = self.get_success_response(data={"team": self.team.id}) self.assertTemplateUsed( response, "sentry/integrations/slack/post-linked-team.html") external_actors = self.get_linked_teams() assert len(external_actors) == 1 assert external_actors[0].actor_id == self.team.actor_id assert len(responses.calls) >= 1 data = json.loads(str(responses.calls[0].request.body.decode("utf-8"))) assert ( f"The {self.team.slug} team will now receive issue alert notifications in the {external_actors[0].external_name} channel." in get_response_text(data)) team_settings = NotificationSetting.objects.filter( scope_type=NotificationScopeType.TEAM.value, target=self.team.actor.id) assert len(team_settings) == 1
def test_unlink_command(self): self.link_user() data = self.send_slack_message("unlink") assert "to unlink your identity" in get_response_text(data)
def test_link_command(self): data = self.send_slack_message("link") assert "Link your Slack identity" in get_response_text(data)
def assert_unknown_command_text(data: SlackBody, unknown_command: Optional[str] = None) -> None: text = get_response_text(data) assert f"Unknown command: `{unknown_command}`" in text assert "Here are the commands you can use" in text
def test_link_command_already_linked(self): self.link_user() data = self.send_slack_message("link") assert "You are already linked as" in get_response_text(data)
def test_idp_does_not_exist(self): """Test that get_identity fails if we cannot find a matching idp.""" data = self.send_slack_message("", team_id="slack:2") assert DISCONNECTED_MESSAGE in get_response_text(data)
def assert_is_help_text(data: SlackBody, expected_command: Optional[str] = None) -> None: text = get_response_text(data) assert "Here are the commands you can use" in text if expected_command: assert expected_command in text
def test_unlink_command_already_unlinked(self): data = self.send_slack_message("unlink") assert NOT_LINKED_MESSAGE in get_response_text(data)