From 68d120b3b4c015f72c37df396e5480b0f08f1ab9 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 27 Apr 2021 15:19:54 +0200 Subject: [PATCH] sources/oauth: add tests for google type Signed-off-by: Jens Langhammer --- .../sources/oauth/tests/test_type_discord.py | 4 +- .../sources/oauth/tests/test_type_google.py | 40 +++++++++++++++++++ authentik/sources/oauth/views/dispatcher.py | 10 ++--- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 authentik/sources/oauth/tests/test_type_google.py diff --git a/authentik/sources/oauth/tests/test_type_discord.py b/authentik/sources/oauth/tests/test_type_discord.py index 0e01056db..6b337d1bd 100644 --- a/authentik/sources/oauth/tests/test_type_discord.py +++ b/authentik/sources/oauth/tests/test_type_discord.py @@ -18,7 +18,7 @@ DISCORD_USER = { } -class TestTypeGitHub(TestCase): +class TestTypeDiscord(TestCase): """OAuth Source tests""" def setUp(self): @@ -32,7 +32,7 @@ class TestTypeGitHub(TestCase): ) def test_enroll_context(self): - """Test GitHub Enrollment context""" + """Test discord Enrollment context""" ak_context = DiscordOAuth2Callback().get_user_enroll_context( self.source, UserOAuthSourceConnection(), DISCORD_USER ) diff --git a/authentik/sources/oauth/tests/test_type_google.py b/authentik/sources/oauth/tests/test_type_google.py new file mode 100644 index 000000000..6f43812ad --- /dev/null +++ b/authentik/sources/oauth/tests/test_type_google.py @@ -0,0 +1,40 @@ +"""google Type tests""" +from django.test import TestCase + +from authentik.sources.oauth.models import OAuthSource, UserOAuthSourceConnection +from authentik.sources.oauth.types.google import GoogleOAuth2Callback + +# https://developers.google.com/identity/protocols/oauth2/openid-connect?hl=en +GOOGLE_USER = { + "id": "1324813249123401234", + "email": "foo@bar.baz", + "verified_email": True, + "name": "foo bar", + "given_name": "foo", + "family_name": "bar", + "picture": "", + "locale": "en", +} + + +class TestTypeGoogle(TestCase): + """OAuth Source tests""" + + def setUp(self): + self.source = OAuthSource.objects.create( + name="test", + slug="test", + provider_type="google", + authorization_url="", + profile_url="", + consumer_key="", + ) + + def test_enroll_context(self): + """Test Google Enrollment context""" + ak_context = GoogleOAuth2Callback().get_user_enroll_context( + self.source, UserOAuthSourceConnection(), GOOGLE_USER + ) + self.assertEqual(ak_context["username"], GOOGLE_USER["email"]) + self.assertEqual(ak_context["email"], GOOGLE_USER["email"]) + self.assertEqual(ak_context["name"], GOOGLE_USER["name"]) diff --git a/authentik/sources/oauth/views/dispatcher.py b/authentik/sources/oauth/views/dispatcher.py index 63a0769b5..4ee0cf3b2 100644 --- a/authentik/sources/oauth/views/dispatcher.py +++ b/authentik/sources/oauth/views/dispatcher.py @@ -1,5 +1,4 @@ """Dispatch OAuth views to respective views""" -from django.http import Http404 from django.shortcuts import get_object_or_404 from django.views import View from structlog.stdlib import get_logger @@ -15,12 +14,9 @@ class DispatcherView(View): kind = "" - def dispatch(self, *args, **kwargs): + def dispatch(self, *args, source_slug: str, **kwargs): """Find Source by slug and forward request""" - slug = kwargs.get("source_slug", None) - if not slug: - raise Http404 - source = get_object_or_404(OAuthSource, slug=slug) + source = get_object_or_404(OAuthSource, slug=source_slug) view = MANAGER.find(source.provider_type, kind=RequestKind(self.kind)) LOGGER.debug("dispatching OAuth2 request to", view=view, kind=self.kind) - return view.as_view()(*args, **kwargs) + return view.as_view()(*args, source_slug=source_slug, **kwargs)