]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fixhancement: better handle removed social apps in profile (#9876)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sun, 11 May 2025 19:55:33 +0000 (12:55 -0700)
committerGitHub <noreply@github.com>
Sun, 11 May 2025 19:55:33 +0000 (19:55 +0000)
src/documents/tests/test_api_profile.py
src/paperless/serialisers.py

index 1075a0af88b34162866e16c2630ae481f465c1f2..8475459a2e656c822e59752792b63f5eaf86c6a8 100644 (file)
@@ -136,6 +136,36 @@ class TestApiProfile(DirectoriesMixin, APITestCase):
             ],
         )
 
+    def test_profile_w_social_removed_app(self):
+        """
+        GIVEN:
+            - Configured user and setup social account
+            - Social app has been removed
+        WHEN:
+            - API call is made to get profile
+        THEN:
+            - Profile is returned with "Unknown App" as name
+        """
+        self.setupSocialAccount()
+
+        # Remove the social app
+        SocialApp.objects.get(provider_id="keycloak-test").delete()
+
+        response = self.client.get(self.ENDPOINT)
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        self.assertEqual(
+            response.data["social_accounts"],
+            [
+                {
+                    "id": 1,
+                    "provider": "keycloak-test",
+                    "name": "Unknown App",
+                },
+            ],
+        )
+
     def test_update_profile(self):
         """
         GIVEN:
index dd315f4db166b1e326ef50d9c6bc84d998c12ae2..9943a76ee2bee1541ab095d2bd8b5c5989e59763 100644 (file)
@@ -4,6 +4,7 @@ from allauth.mfa.adapter import get_adapter as get_mfa_adapter
 from allauth.mfa.models import Authenticator
 from allauth.mfa.totp.internal.auth import TOTP
 from allauth.socialaccount.models import SocialAccount
+from allauth.socialaccount.models import SocialApp
 from django.contrib.auth.models import Group
 from django.contrib.auth.models import Permission
 from django.contrib.auth.models import User
@@ -146,8 +147,11 @@ class SocialAccountSerializer(serializers.ModelSerializer):
             "name",
         )
 
-    def get_name(self, obj) -> str:
-        return obj.get_provider_account().to_str()
+    def get_name(self, obj: SocialAccount) -> str:
+        try:
+            return obj.get_provider_account().to_str()
+        except SocialApp.DoesNotExist:
+            return "Unknown App"
 
 
 class ProfileSerializer(serializers.ModelSerializer):