authentik.sources.oauth.tests.test_type_patreon

Patreon Type tests

  1"""Patreon Type tests"""
  2
  3from copy import deepcopy
  4from urllib.parse import parse_qs, urlparse
  5
  6from django.test import RequestFactory, TestCase
  7from django.urls import reverse
  8from requests_mock import Mocker
  9
 10from authentik.sources.oauth.models import OAuthSource
 11from authentik.sources.oauth.types.patreon import PatreonOAuthCallback, PatreonType
 12
 13PATREON_USER = {
 14    "data": {
 15        "attributes": {
 16            "email": "corgi@example.com",
 17            "full_name": "Corgi The Dev",
 18            "vanity": "corgithedev",
 19        },
 20        "id": "0000000",
 21        "type": "user",
 22    },
 23}
 24
 25
 26class TestTypePatreon(TestCase):
 27    """OAuth Source tests"""
 28
 29    def setUp(self):
 30        self.source = OAuthSource.objects.create(
 31            name="test",
 32            slug="test",
 33            provider_type="patreon",
 34        )
 35        self.callback = PatreonOAuthCallback(request=RequestFactory().get("/"))
 36
 37    def test_enroll_context(self):
 38        """Test Patreon Enrollment context"""
 39        ak_context = PatreonType().get_base_user_properties(source=self.source, info=PATREON_USER)
 40        self.assertEqual(ak_context["username"], PATREON_USER["data"]["attributes"]["vanity"])
 41        self.assertEqual(ak_context["email"], PATREON_USER["data"]["attributes"]["email"])
 42        self.assertEqual(ak_context["name"], PATREON_USER["data"]["attributes"]["full_name"])
 43
 44    def test_enroll_context_without_vanity(self):
 45        """Patrons without a vanity name still have a stable username."""
 46        for vanity in [None, ""]:
 47            with self.subTest(vanity=vanity):
 48                info = deepcopy(PATREON_USER)
 49                info["data"]["attributes"]["vanity"] = vanity
 50                context = PatreonType().get_base_user_properties(info=info)
 51                self.assertEqual(context["username"], PATREON_USER["data"]["id"])
 52        info = deepcopy(PATREON_USER)
 53        del info["data"]["attributes"]["vanity"]
 54        context = PatreonType().get_base_user_properties(info=info)
 55        self.assertEqual(context["username"], PATREON_USER["data"]["id"])
 56
 57    def test_user_id(self):
 58        """Keep the Patreon user ID used by existing source connections."""
 59        self.assertEqual(self.callback.get_user_id(PATREON_USER), PATREON_USER["data"]["id"])
 60
 61    def test_redirect_scopes(self):
 62        """Request both the v2 identity and email permissions."""
 63        response = self.client.get(
 64            reverse("authentik_sources_oauth:oauth-client-login", kwargs={"source_slug": "test"})
 65        )
 66        self.assertEqual(response.status_code, 302)
 67        query = parse_qs(urlparse(response.url).query)
 68        self.assertEqual(set(query["scope"][0].split()), {"identity", "identity[email]"})
 69
 70    def test_profile(self):
 71        """Fetch explicit v2 fields and send the access token only in the header."""
 72        with Mocker() as mocker:
 73            mocker.get(
 74                "https://www.patreon.com/api/oauth2/v2/identity"
 75                "?fields%5Buser%5D=vanity,email,full_name",
 76                complete_qs=True,
 77                request_headers={"Authorization": "Bearer test-token"},
 78                json=PATREON_USER,
 79            )
 80            client = self.callback.get_client(self.source)
 81            profile = client.get_profile_info(
 82                {"access_token": "test-token", "token_type": "Bearer"}
 83            )
 84        self.assertEqual(profile, PATREON_USER)
 85        self.assertEqual(self.callback.get_user_id(profile), "0000000")
 86        context = PatreonType().get_base_user_properties(info=profile)
 87        self.assertEqual(
 88            context,
 89            {"username": "corgithedev", "email": "corgi@example.com", "name": "Corgi The Dev"},
 90        )
 91
 92    def test_profile_failure(self):
 93        """Do not treat a rejected v2 token as a user profile."""
 94        with Mocker() as mocker:
 95            mocker.get(
 96                "https://www.patreon.com/api/oauth2/v2/identity",
 97                status_code=401,
 98                json={"errors": [{"status": "401"}]},
 99            )
100            client = self.callback.get_client(self.source)
101            self.assertIsNone(
102                client.get_profile_info({"access_token": "invalid-token", "token_type": "Bearer"})
103            )
PATREON_USER = {'data': {'attributes': {'email': 'corgi@example.com', 'full_name': 'Corgi The Dev', 'vanity': 'corgithedev'}, 'id': '0000000', 'type': 'user'}}
class TestTypePatreon(django.test.testcases.TestCase):
 27class TestTypePatreon(TestCase):
 28    """OAuth Source tests"""
 29
 30    def setUp(self):
 31        self.source = OAuthSource.objects.create(
 32            name="test",
 33            slug="test",
 34            provider_type="patreon",
 35        )
 36        self.callback = PatreonOAuthCallback(request=RequestFactory().get("/"))
 37
 38    def test_enroll_context(self):
 39        """Test Patreon Enrollment context"""
 40        ak_context = PatreonType().get_base_user_properties(source=self.source, info=PATREON_USER)
 41        self.assertEqual(ak_context["username"], PATREON_USER["data"]["attributes"]["vanity"])
 42        self.assertEqual(ak_context["email"], PATREON_USER["data"]["attributes"]["email"])
 43        self.assertEqual(ak_context["name"], PATREON_USER["data"]["attributes"]["full_name"])
 44
 45    def test_enroll_context_without_vanity(self):
 46        """Patrons without a vanity name still have a stable username."""
 47        for vanity in [None, ""]:
 48            with self.subTest(vanity=vanity):
 49                info = deepcopy(PATREON_USER)
 50                info["data"]["attributes"]["vanity"] = vanity
 51                context = PatreonType().get_base_user_properties(info=info)
 52                self.assertEqual(context["username"], PATREON_USER["data"]["id"])
 53        info = deepcopy(PATREON_USER)
 54        del info["data"]["attributes"]["vanity"]
 55        context = PatreonType().get_base_user_properties(info=info)
 56        self.assertEqual(context["username"], PATREON_USER["data"]["id"])
 57
 58    def test_user_id(self):
 59        """Keep the Patreon user ID used by existing source connections."""
 60        self.assertEqual(self.callback.get_user_id(PATREON_USER), PATREON_USER["data"]["id"])
 61
 62    def test_redirect_scopes(self):
 63        """Request both the v2 identity and email permissions."""
 64        response = self.client.get(
 65            reverse("authentik_sources_oauth:oauth-client-login", kwargs={"source_slug": "test"})
 66        )
 67        self.assertEqual(response.status_code, 302)
 68        query = parse_qs(urlparse(response.url).query)
 69        self.assertEqual(set(query["scope"][0].split()), {"identity", "identity[email]"})
 70
 71    def test_profile(self):
 72        """Fetch explicit v2 fields and send the access token only in the header."""
 73        with Mocker() as mocker:
 74            mocker.get(
 75                "https://www.patreon.com/api/oauth2/v2/identity"
 76                "?fields%5Buser%5D=vanity,email,full_name",
 77                complete_qs=True,
 78                request_headers={"Authorization": "Bearer test-token"},
 79                json=PATREON_USER,
 80            )
 81            client = self.callback.get_client(self.source)
 82            profile = client.get_profile_info(
 83                {"access_token": "test-token", "token_type": "Bearer"}
 84            )
 85        self.assertEqual(profile, PATREON_USER)
 86        self.assertEqual(self.callback.get_user_id(profile), "0000000")
 87        context = PatreonType().get_base_user_properties(info=profile)
 88        self.assertEqual(
 89            context,
 90            {"username": "corgithedev", "email": "corgi@example.com", "name": "Corgi The Dev"},
 91        )
 92
 93    def test_profile_failure(self):
 94        """Do not treat a rejected v2 token as a user profile."""
 95        with Mocker() as mocker:
 96            mocker.get(
 97                "https://www.patreon.com/api/oauth2/v2/identity",
 98                status_code=401,
 99                json={"errors": [{"status": "401"}]},
100            )
101            client = self.callback.get_client(self.source)
102            self.assertIsNone(
103                client.get_profile_info({"access_token": "invalid-token", "token_type": "Bearer"})
104            )

OAuth Source tests

def setUp(self):
30    def setUp(self):
31        self.source = OAuthSource.objects.create(
32            name="test",
33            slug="test",
34            provider_type="patreon",
35        )
36        self.callback = PatreonOAuthCallback(request=RequestFactory().get("/"))

Hook method for setting up the test fixture before exercising it.

def test_enroll_context(self):
38    def test_enroll_context(self):
39        """Test Patreon Enrollment context"""
40        ak_context = PatreonType().get_base_user_properties(source=self.source, info=PATREON_USER)
41        self.assertEqual(ak_context["username"], PATREON_USER["data"]["attributes"]["vanity"])
42        self.assertEqual(ak_context["email"], PATREON_USER["data"]["attributes"]["email"])
43        self.assertEqual(ak_context["name"], PATREON_USER["data"]["attributes"]["full_name"])

Test Patreon Enrollment context

def test_enroll_context_without_vanity(self):
45    def test_enroll_context_without_vanity(self):
46        """Patrons without a vanity name still have a stable username."""
47        for vanity in [None, ""]:
48            with self.subTest(vanity=vanity):
49                info = deepcopy(PATREON_USER)
50                info["data"]["attributes"]["vanity"] = vanity
51                context = PatreonType().get_base_user_properties(info=info)
52                self.assertEqual(context["username"], PATREON_USER["data"]["id"])
53        info = deepcopy(PATREON_USER)
54        del info["data"]["attributes"]["vanity"]
55        context = PatreonType().get_base_user_properties(info=info)
56        self.assertEqual(context["username"], PATREON_USER["data"]["id"])

Patrons without a vanity name still have a stable username.

def test_user_id(self):
58    def test_user_id(self):
59        """Keep the Patreon user ID used by existing source connections."""
60        self.assertEqual(self.callback.get_user_id(PATREON_USER), PATREON_USER["data"]["id"])

Keep the Patreon user ID used by existing source connections.

def test_redirect_scopes(self):
62    def test_redirect_scopes(self):
63        """Request both the v2 identity and email permissions."""
64        response = self.client.get(
65            reverse("authentik_sources_oauth:oauth-client-login", kwargs={"source_slug": "test"})
66        )
67        self.assertEqual(response.status_code, 302)
68        query = parse_qs(urlparse(response.url).query)
69        self.assertEqual(set(query["scope"][0].split()), {"identity", "identity[email]"})

Request both the v2 identity and email permissions.

def test_profile(self):
71    def test_profile(self):
72        """Fetch explicit v2 fields and send the access token only in the header."""
73        with Mocker() as mocker:
74            mocker.get(
75                "https://www.patreon.com/api/oauth2/v2/identity"
76                "?fields%5Buser%5D=vanity,email,full_name",
77                complete_qs=True,
78                request_headers={"Authorization": "Bearer test-token"},
79                json=PATREON_USER,
80            )
81            client = self.callback.get_client(self.source)
82            profile = client.get_profile_info(
83                {"access_token": "test-token", "token_type": "Bearer"}
84            )
85        self.assertEqual(profile, PATREON_USER)
86        self.assertEqual(self.callback.get_user_id(profile), "0000000")
87        context = PatreonType().get_base_user_properties(info=profile)
88        self.assertEqual(
89            context,
90            {"username": "corgithedev", "email": "corgi@example.com", "name": "Corgi The Dev"},
91        )

Fetch explicit v2 fields and send the access token only in the header.

def test_profile_failure(self):
 93    def test_profile_failure(self):
 94        """Do not treat a rejected v2 token as a user profile."""
 95        with Mocker() as mocker:
 96            mocker.get(
 97                "https://www.patreon.com/api/oauth2/v2/identity",
 98                status_code=401,
 99                json={"errors": [{"status": "401"}]},
100            )
101            client = self.callback.get_client(self.source)
102            self.assertIsNone(
103                client.get_profile_info({"access_token": "invalid-token", "token_type": "Bearer"})
104            )

Do not treat a rejected v2 token as a user profile.