def setUp(self):
     super().setUp()
     self.link_user()
     self.link_team()
     self.external_actor = ExternalActor.objects.filter(
         actor_id=self.team.actor_id,
         organization=self.organization,
         integration=self.integration,
         provider=ExternalProviders.SLACK.value,
         external_name="general",
         external_id="CXXXXXXX9",
     )
     responses.add(
         method=responses.POST,
         url="https://slack.com/api/chat.postMessage",
         body='{"ok": true}',
         status=200,
         content_type="application/json",
     )
     self.team_unlinking_url = build_team_unlinking_url(
         self.integration,
         self.organization.id,
         "UXXXXXXX1",
         "CXXXXXXX9",
         "general",
         "http://example.slack.com/response_url",
     )
Example #2
0
    def unlink_team(self, slack_request: SlackCommandRequest) -> Response:

        if slack_request.channel_name == DIRECT_MESSAGE_CHANNEL_NAME:
            return self.send_ephemeral_notification(LINK_FROM_CHANNEL_MESSAGE)

        if not slack_request.has_identity:
            return self.send_ephemeral_notification(LINK_USER_FIRST_MESSAGE)

        integration = slack_request.integration
        organization = integration.organizations.all()[0]

        if not ExternalActor.objects.filter(
                organization=organization,
                integration=integration,
                provider=ExternalProviders.SLACK.value,
                external_name=slack_request.channel_name,
                external_id=slack_request.channel_id,
        ).exists():
            return self.send_ephemeral_notification(TEAM_NOT_LINKED_MESSAGE)

        associate_url = build_team_unlinking_url(
            integration=integration,
            organization_id=organization.id,
            slack_id=slack_request.user_id,
            channel_id=slack_request.channel_id,
            channel_name=slack_request.channel_name,
            response_url=slack_request.response_url,
        )
        return self.send_ephemeral_notification(
            UNLINK_TEAM_MESSAGE.format(associate_url=associate_url))
Example #3
0
    def unlink_team(self, slack_request: SlackCommandRequest) -> Response:
        if slack_request.channel_name == DIRECT_MESSAGE_CHANNEL_NAME:
            return self.reply(slack_request, LINK_FROM_CHANNEL_MESSAGE)

        identity = get_identity(slack_request)
        if not identity:
            return self.reply(slack_request, LINK_USER_FIRST_MESSAGE)

        integration = slack_request.integration
        organization_memberships = OrganizationMember.objects.get_for_integration(
            integration, identity.user
        )
        if not organization_memberships:
            return self.reply(slack_request, INSUFFICIENT_ROLE_MESSAGE)

        organization = organization_memberships[0].organization
        if not is_team_linked_to_channel(organization, slack_request):
            return self.reply(slack_request, TEAM_NOT_LINKED_MESSAGE)

        if not is_valid_role(organization_memberships[0]):
            return self.reply(slack_request, INSUFFICIENT_ROLE_MESSAGE)

        associate_url = build_team_unlinking_url(
            integration=integration,
            organization_id=organization.id,
            slack_id=slack_request.user_id,
            channel_id=slack_request.channel_id,
            channel_name=slack_request.channel_name,
            response_url=slack_request.response_url,
        )
        return self.reply(slack_request, UNLINK_TEAM_MESSAGE.format(associate_url=associate_url))
Example #4
0
    def test_unlink_team_multiple_organizations(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)

        # Team order should not matter.
        for team in (self.team, team2):
            external_actors = self.get_linked_teams(
                organization=team.organization, actor_ids=[team.actor_id])
            assert len(external_actors) == 1

            # Override the URL.
            self.url = build_team_unlinking_url(
                integration=self.integration,
                organization_id=team.organization.id,
                slack_id=self.external_id,
                channel_id=self.channel_id,
                channel_name=self.channel_name,
                response_url=self.response_url,
            )
            response = self.get_success_response(data={})
            self.assertTemplateUsed(
                response, "sentry/integrations/slack/unlinked-team.html")

            external_actors = self.get_linked_teams(
                organization=team.organization, actor_ids=[team.actor_id])
            assert len(external_actors) == 0
Example #5
0
    def setUp(self):
        super().setUp()

        self.link_team()
        self.url = build_team_unlinking_url(
            integration=self.integration,
            organization_id=self.organization.id,
            slack_id=self.external_id,
            channel_id=self.channel_id,
            channel_name=self.channel_name,
            response_url=self.response_url,
        )
    def test_unlink_team(self):
        """Test that a team can be unlinked from a Slack channel"""
        data = self.send_slack_message(
            "unlink team",
            channel_name="general",
            channel_id="CXXXXXXX9",
        )
        assert "Click here to unlink your team from this channel." in data[
            "text"]
        team_unlinking_url = build_team_unlinking_url(
            self.integration,
            self.organization.id,
            "UXXXXXXX1",
            "CXXXXXXX9",
            "general",
            "http://example.slack.com/response_url",
        )

        resp = self.client.get(team_unlinking_url)
        assert resp.status_code == 200
        self.assertTemplateUsed(resp,
                                "sentry/integrations/slack-unlink-team.html")

        resp = self.client.post(team_unlinking_url)
        assert resp.status_code == 200
        self.assertTemplateUsed(
            resp, "sentry/integrations/slack-unlinked-team.html")

        assert len(self.external_actor) == 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 data["text"])

        team_settings = NotificationSetting.objects.filter(
            scope_type=NotificationScopeType.TEAM.value,
            target=self.team.actor.id)
        assert len(team_settings) == 0
Example #7
0
    def unlink_team(self, slack_request: SlackCommandRequest) -> Response:

        if slack_request.channel_name == DIRECT_MESSAGE_CHANNEL_NAME:
            return self.reply(slack_request, LINK_FROM_CHANNEL_MESSAGE)

        identity = self.get_identity(slack_request)
        if not identity:
            return self.reply(slack_request, LINK_USER_FIRST_MESSAGE)

        integration = slack_request.integration
        organization = integration.organizations.all()[0]

        if not ExternalActor.objects.filter(
                organization=organization,
                integration=integration,
                provider=ExternalProviders.SLACK.value,
                external_name=slack_request.channel_name,
                external_id=slack_request.channel_id,
        ).exists():
            return self.reply(slack_request, TEAM_NOT_LINKED_MESSAGE)

        org_member = OrganizationMember.objects.get(user=identity.user,
                                                    organization=organization)

        if not is_valid_role(org_member):
            return self.reply(slack_request, INSUFFICIENT_ROLE_MESSAGE)

        associate_url = build_team_unlinking_url(
            integration=integration,
            organization_id=organization.id,
            slack_id=slack_request.user_id,
            channel_id=slack_request.channel_id,
            channel_name=slack_request.channel_name,
            response_url=slack_request.response_url,
        )
        return self.reply(
            slack_request,
            UNLINK_TEAM_MESSAGE.format(associate_url=associate_url))