diff --git a/engine/apps/api/tests/test_user.py b/engine/apps/api/tests/test_user.py index 7c064616..78da25d9 100644 --- a/engine/apps/api/tests/test_user.py +++ b/engine/apps/api/tests/test_user.py @@ -800,6 +800,30 @@ def test_admin_can_unlink_another_user_backend_account( assert response.status_code == status.HTTP_200_OK +@pytest.mark.django_db +def test_admin_can_unlink_another_user_slack_account( + make_organization_with_slack_team_identity, + make_user_for_organization, + make_user_with_slack_user_identity, + make_token_for_organization, + make_user_auth_headers, +): + organization, slack_team_identity = make_organization_with_slack_team_identity() + admin = make_user_for_organization(organization, role=Role.ADMIN) + editor, slack_user_identity_1 = make_user_with_slack_user_identity( + slack_team_identity, organization, slack_id="user_1", role=Role.EDITOR + ) + + _, token = make_token_for_organization(organization) + client = APIClient() + url = reverse("api-internal:user-unlink-slack", kwargs={"pk": editor.public_primary_key}) + + response = client.post(url, format="json", **make_user_auth_headers(admin, token)) + assert response.status_code == status.HTTP_200_OK + editor.refresh_from_db() + assert editor.slack_user_identity is None + + """Test user permissions""" @@ -1038,6 +1062,28 @@ def test_user_cant_get_another_user_backend_verification_code( assert response.status_code == status.HTTP_403_FORBIDDEN +@pytest.mark.django_db +def test_user_can_unlink_own_slack_account( + make_organization_with_slack_team_identity, + make_user_with_slack_user_identity, + make_token_for_organization, + make_user_auth_headers, +): + organization, slack_team_identity = make_organization_with_slack_team_identity() + user, slack_user_identity_1 = make_user_with_slack_user_identity( + slack_team_identity, organization, slack_id="user_1", role=Role.EDITOR + ) + + _, token = make_token_for_organization(organization) + client = APIClient() + url = reverse("api-internal:user-unlink-slack", kwargs={"pk": user.public_primary_key}) + + response = client.post(url, format="json", **make_user_auth_headers(user, token)) + assert response.status_code == status.HTTP_200_OK + user.refresh_from_db() + assert user.slack_user_identity is None + + @pytest.mark.django_db def test_user_can_unlink_backend_own_account( make_organization, make_user_for_organization, make_token_for_organization, make_user_auth_headers @@ -1086,6 +1132,31 @@ def test_user_unlink_backend_backend_account_not_found( assert response.status_code == status.HTTP_400_BAD_REQUEST +@pytest.mark.django_db +def test_user_cant_unlink_slack_another_user( + make_organization_with_slack_team_identity, + make_user_with_slack_user_identity, + make_token_for_organization, + make_user_auth_headers, +): + organization, slack_team_identity = make_organization_with_slack_team_identity() + first_user, slack_user_identity_1 = make_user_with_slack_user_identity( + slack_team_identity, organization, slack_id="user_1", role=Role.EDITOR + ) + second_user, slack_user_identity_2 = make_user_with_slack_user_identity( + slack_team_identity, organization, slack_id="user_2", role=Role.EDITOR + ) + + _, token = make_token_for_organization(organization) + client = APIClient() + url = reverse("api-internal:user-unlink-slack", kwargs={"pk": first_user.public_primary_key}) + + response = client.post(url, format="json", **make_user_auth_headers(second_user, token)) + assert response.status_code == status.HTTP_403_FORBIDDEN + first_user.refresh_from_db() + assert first_user.slack_user_identity is not None + + @pytest.mark.django_db def test_user_cant_unlink_backend__another_user( make_organization, make_user_for_organization, make_token_for_organization, make_user_auth_headers diff --git a/engine/apps/api/views/user.py b/engine/apps/api/views/user.py index 7911acd2..d436b2d0 100644 --- a/engine/apps/api/views/user.py +++ b/engine/apps/api/views/user.py @@ -127,6 +127,7 @@ class UserView( "get_verification_code", "get_backend_verification_code", "get_telegram_verification_code", + "unlink_slack", "unlink_telegram", "unlink_backend", "make_test_call", @@ -146,6 +147,7 @@ class UserView( "get_verification_code", "get_backend_verification_code", "get_telegram_verification_code", + "unlink_slack", "unlink_telegram", "unlink_backend", "make_test_call", @@ -350,6 +352,13 @@ class UserView( return Response({"telegram_code": str(new_code.uuid), "bot_link": bot_link}, status=status.HTTP_200_OK) + @action(detail=True, methods=["post"]) + def unlink_slack(self, request, pk): + user = self.get_object() + user.slack_user_identity = None + user.save(update_fields=["slack_user_identity"]) + return Response(status=status.HTTP_200_OK) + @action(detail=True, methods=["post"]) def unlink_telegram(self, request, pk): user = self.get_object() diff --git a/grafana-plugin/src/containers/UserSettings/parts/connectors/SlackConnector.tsx b/grafana-plugin/src/containers/UserSettings/parts/connectors/SlackConnector.tsx index ad84b330..61094479 100644 --- a/grafana-plugin/src/containers/UserSettings/parts/connectors/SlackConnector.tsx +++ b/grafana-plugin/src/containers/UserSettings/parts/connectors/SlackConnector.tsx @@ -32,6 +32,10 @@ const SlackConnector = (props: SlackConnectorProps) => { onTabChange(UserSettingsTab.SlackInfo); }, []); + const handleUnlinkSlackAccount = useCallback(() => { + userStore.unlinkSlack(userStore.currentUserPk); + }, []); + return (