sources/oauth: add tests for google type

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-27 15:19:54 +02:00
parent 48c0c0baca
commit 68d120b3b4
3 changed files with 45 additions and 9 deletions

View File

@ -18,7 +18,7 @@ DISCORD_USER = {
} }
class TestTypeGitHub(TestCase): class TestTypeDiscord(TestCase):
"""OAuth Source tests""" """OAuth Source tests"""
def setUp(self): def setUp(self):
@ -32,7 +32,7 @@ class TestTypeGitHub(TestCase):
) )
def test_enroll_context(self): def test_enroll_context(self):
"""Test GitHub Enrollment context""" """Test discord Enrollment context"""
ak_context = DiscordOAuth2Callback().get_user_enroll_context( ak_context = DiscordOAuth2Callback().get_user_enroll_context(
self.source, UserOAuthSourceConnection(), DISCORD_USER self.source, UserOAuthSourceConnection(), DISCORD_USER
) )

View File

@ -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"])

View File

@ -1,5 +1,4 @@
"""Dispatch OAuth views to respective views""" """Dispatch OAuth views to respective views"""
from django.http import Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.views import View from django.views import View
from structlog.stdlib import get_logger from structlog.stdlib import get_logger
@ -15,12 +14,9 @@ class DispatcherView(View):
kind = "" kind = ""
def dispatch(self, *args, **kwargs): def dispatch(self, *args, source_slug: str, **kwargs):
"""Find Source by slug and forward request""" """Find Source by slug and forward request"""
slug = kwargs.get("source_slug", None) source = get_object_or_404(OAuthSource, slug=source_slug)
if not slug:
raise Http404
source = get_object_or_404(OAuthSource, slug=slug)
view = MANAGER.find(source.provider_type, kind=RequestKind(self.kind)) view = MANAGER.find(source.provider_type, kind=RequestKind(self.kind))
LOGGER.debug("dispatching OAuth2 request to", view=view, kind=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)