From 0083cd55df4b19f23e4630b3bef6b464ca3f2cd8 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 13 Dec 2020 19:53:26 +0100 Subject: [PATCH] sources/oauth: start adding tests for types --- authentik/sources/oauth/tests/__init__.py | 0 .../sources/oauth/tests/test_type_discord.py | 41 +++++++ .../sources/oauth/tests/test_type_github.py | 71 +++++++++++ .../sources/oauth/tests/test_type_twitter.py | 112 ++++++++++++++++++ .../oauth/{tests.py => tests/test_views.py} | 5 +- authentik/sources/oauth/types/discord.py | 2 +- authentik/sources/oauth/types/facebook.py | 2 +- authentik/sources/oauth/types/google.py | 2 +- authentik/sources/oauth/types/oidc.py | 2 +- authentik/sources/oauth/types/reddit.py | 2 +- authentik/sources/oauth/types/twitter.py | 2 +- 11 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 authentik/sources/oauth/tests/__init__.py create mode 100644 authentik/sources/oauth/tests/test_type_discord.py create mode 100644 authentik/sources/oauth/tests/test_type_github.py create mode 100644 authentik/sources/oauth/tests/test_type_twitter.py rename authentik/sources/oauth/{tests.py => tests/test_views.py} (89%) diff --git a/authentik/sources/oauth/tests/__init__.py b/authentik/sources/oauth/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/authentik/sources/oauth/tests/test_type_discord.py b/authentik/sources/oauth/tests/test_type_discord.py new file mode 100644 index 000000000..0e01056db --- /dev/null +++ b/authentik/sources/oauth/tests/test_type_discord.py @@ -0,0 +1,41 @@ +"""Discord Type tests""" +from django.test import TestCase + +from authentik.sources.oauth.models import OAuthSource, UserOAuthSourceConnection +from authentik.sources.oauth.types.discord import DiscordOAuth2Callback + +# https://discord.com/developers/docs/resources/user#user-object +DISCORD_USER = { + "id": "80351110224678912", + "username": "Nelly", + "discriminator": "1337", + "avatar": "8342729096ea3675442027381ff50dfe", + "verified": True, + "email": "nelly@discord.com", + "flags": 64, + "premium_type": 1, + "public_flags": 64, +} + + +class TestTypeGitHub(TestCase): + """OAuth Source tests""" + + def setUp(self): + self.source = OAuthSource.objects.create( + name="test", + slug="test", + provider_type="openid-connect", + authorization_url="", + profile_url="", + consumer_key="", + ) + + def test_enroll_context(self): + """Test GitHub Enrollment context""" + ak_context = DiscordOAuth2Callback().get_user_enroll_context( + self.source, UserOAuthSourceConnection(), DISCORD_USER + ) + self.assertEqual(ak_context["username"], DISCORD_USER["username"]) + self.assertEqual(ak_context["email"], DISCORD_USER["email"]) + self.assertEqual(ak_context["name"], DISCORD_USER["username"]) diff --git a/authentik/sources/oauth/tests/test_type_github.py b/authentik/sources/oauth/tests/test_type_github.py new file mode 100644 index 000000000..3acce60fc --- /dev/null +++ b/authentik/sources/oauth/tests/test_type_github.py @@ -0,0 +1,71 @@ +"""GitHub Type tests""" +from django.test import TestCase + +from authentik.sources.oauth.models import OAuthSource, UserOAuthSourceConnection +from authentik.sources.oauth.types.github import GitHubOAuth2Callback + +# https://developer.github.com/v3/users/#get-the-authenticated-user +GITHUB_USER = { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": False, + "name": "monalisa octocat", + "company": "GitHub", + "blog": "https://github.com/blog", + "location": "San Francisco", + "email": "octocat@github.com", + "hireable": False, + "bio": "There once was...", + "twitter_username": "monatheoctocat", + "public_repos": 2, + "public_gists": 1, + "followers": 20, + "following": 0, + "created_at": "2008-01-14T04:33:35Z", + "updated_at": "2008-01-14T04:33:35Z", + "private_gists": 81, + "total_private_repos": 100, + "owned_private_repos": 100, + "disk_usage": 10000, + "collaborators": 8, + "two_factor_authentication": True, + "plan": {"name": "Medium", "space": 400, "private_repos": 20, "collaborators": 0}, +} + + +class TestTypeGitHub(TestCase): + """OAuth Source tests""" + + def setUp(self): + self.source = OAuthSource.objects.create( + name="test", + slug="test", + provider_type="openid-connect", + authorization_url="", + profile_url="", + consumer_key="", + ) + + def test_enroll_context(self): + """Test GitHub Enrollment context""" + ak_context = GitHubOAuth2Callback().get_user_enroll_context( + self.source, UserOAuthSourceConnection(), GITHUB_USER + ) + self.assertEqual(ak_context["username"], GITHUB_USER["login"]) + self.assertEqual(ak_context["email"], GITHUB_USER["email"]) + self.assertEqual(ak_context["name"], GITHUB_USER["name"]) diff --git a/authentik/sources/oauth/tests/test_type_twitter.py b/authentik/sources/oauth/tests/test_type_twitter.py new file mode 100644 index 000000000..b0918fa62 --- /dev/null +++ b/authentik/sources/oauth/tests/test_type_twitter.py @@ -0,0 +1,112 @@ +"""Twitter Type tests""" +from django.test import Client, TestCase + +from authentik.sources.oauth.models import OAuthSource, UserOAuthSourceConnection +from authentik.sources.oauth.types.twitter import TwitterOAuthCallback + +# https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/ \ +# api-reference/get-account-verify_credentials +TWITTER_USER = { + "contributors_enabled": True, + "created_at": "Sat May 09 17:58:22 +0000 2009", + "default_profile": False, + "default_profile_image": False, + "description": "I taught your phone that thing you like.", + "favourites_count": 588, + "follow_request_sent": None, + "followers_count": 10625, + "following": None, + "friends_count": 1181, + "geo_enabled": True, + "id": 38895958, + "id_str": "38895958", + "is_translator": False, + "lang": "en", + "listed_count": 190, + "location": "San Francisco", + "name": "Sean Cook", + "notifications": None, + "profile_background_color": "1A1B1F", + "profile_background_image_url": "", + "profile_background_image_url_https": "", + "profile_background_tile": True, + "profile_image_url": "", + "profile_image_url_https": "", + "profile_link_color": "2FC2EF", + "profile_sidebar_border_color": "181A1E", + "profile_sidebar_fill_color": "252429", + "profile_text_color": "666666", + "profile_use_background_image": True, + "protected": False, + "screen_name": "theSeanCook", + "show_all_inline_media": True, + "status": { + "contributors": None, + "coordinates": {"coordinates": [-122.45037293, 37.76484123], "type": "Point"}, + "created_at": "Tue Aug 28 05:44:24 +0000 2012", + "favorited": False, + "geo": {"coordinates": [37.76484123, -122.45037293], "type": "Point"}, + "id": 240323931419062272, + "id_str": "240323931419062272", + "in_reply_to_screen_name": "messl", + "in_reply_to_status_id": 240316959173009410, + "in_reply_to_status_id_str": "240316959173009410", + "in_reply_to_user_id": 18707866, + "in_reply_to_user_id_str": "18707866", + "place": { + "attributes": {}, + "bounding_box": { + "coordinates": [ + [ + [-122.45778216, 37.75932999], + [-122.44248216, 37.75932999], + [-122.44248216, 37.76752899], + [-122.45778216, 37.76752899], + ] + ], + "type": "Polygon", + }, + "country": "United States", + "country_code": "US", + "full_name": "Ashbury Heights, San Francisco", + "id": "866269c983527d5a", + "name": "Ashbury Heights", + "place_type": "neighborhood", + "url": "http://api.twitter.com/1/geo/id/866269c983527d5a.json", + }, + "retweet_count": 0, + "retweeted": False, + "source": "Twitter for iPhone", + "text": "@messl congrats! So happy for all 3 of you.", + "truncated": False, + }, + "statuses_count": 2609, + "time_zone": "Pacific Time (US & Canada)", + "url": None, + "utc_offset": -28800, + "verified": False, +} + + +class TestTypeGitHub(TestCase): + """OAuth Source tests""" + + def setUp(self): + self.client = Client() + self.source = OAuthSource.objects.create( + name="test", + slug="test", + provider_type="openid-connect", + authorization_url="", + profile_url="", + consumer_key="", + ) + + def test_enroll_context(self): + """Test Twitter Enrollment context""" + ak_context = TwitterOAuthCallback().get_user_enroll_context( + self.source, UserOAuthSourceConnection(), TWITTER_USER + ) + self.assertEqual(ak_context["username"], TWITTER_USER["screen_name"]) + self.assertEqual(ak_context["email"], TWITTER_USER.get("email", None)) + self.assertEqual(ak_context["name"], TWITTER_USER["name"]) diff --git a/authentik/sources/oauth/tests.py b/authentik/sources/oauth/tests/test_views.py similarity index 89% rename from authentik/sources/oauth/tests.py rename to authentik/sources/oauth/tests/test_views.py index a6a22dff2..ec4f44eec 100644 --- a/authentik/sources/oauth/tests.py +++ b/authentik/sources/oauth/tests/test_views.py @@ -1,15 +1,14 @@ """OAuth Source tests""" from django.shortcuts import reverse -from django.test import Client, TestCase +from django.test import TestCase from authentik.sources.oauth.models import OAuthSource -class OAuthSourceTests(TestCase): +class TestOAuthSource(TestCase): """OAuth Source tests""" def setUp(self): - self.client = Client() self.source = OAuthSource.objects.create( name="test", slug="test", diff --git a/authentik/sources/oauth/types/discord.py b/authentik/sources/oauth/types/discord.py index af0ec3e30..52fb412c6 100644 --- a/authentik/sources/oauth/types/discord.py +++ b/authentik/sources/oauth/types/discord.py @@ -11,7 +11,7 @@ from authentik.sources.oauth.views.redirect import OAuthRedirect class DiscordOAuthRedirect(OAuthRedirect): """Discord OAuth2 Redirect""" - def get_additional_parameters(self, source): + def get_additional_parameters(self, source): # pragma: no cover return { "scope": "email identify", } diff --git a/authentik/sources/oauth/types/facebook.py b/authentik/sources/oauth/types/facebook.py index 78fcb0391..1f30c27e4 100644 --- a/authentik/sources/oauth/types/facebook.py +++ b/authentik/sources/oauth/types/facebook.py @@ -14,7 +14,7 @@ from authentik.sources.oauth.views.redirect import OAuthRedirect class FacebookOAuthRedirect(OAuthRedirect): """Facebook OAuth2 Redirect""" - def get_additional_parameters(self, source): + def get_additional_parameters(self, source): # pragma: no cover return { "scope": "email", } diff --git a/authentik/sources/oauth/types/google.py b/authentik/sources/oauth/types/google.py index 813fd5eae..2a007b135 100644 --- a/authentik/sources/oauth/types/google.py +++ b/authentik/sources/oauth/types/google.py @@ -11,7 +11,7 @@ from authentik.sources.oauth.views.redirect import OAuthRedirect class GoogleOAuthRedirect(OAuthRedirect): """Google OAuth2 Redirect""" - def get_additional_parameters(self, source): + def get_additional_parameters(self, source): # pragma: no cover return { "scope": "email profile", } diff --git a/authentik/sources/oauth/types/oidc.py b/authentik/sources/oauth/types/oidc.py index 90742b1c6..2555c1270 100644 --- a/authentik/sources/oauth/types/oidc.py +++ b/authentik/sources/oauth/types/oidc.py @@ -11,7 +11,7 @@ from authentik.sources.oauth.views.redirect import OAuthRedirect class OpenIDConnectOAuthRedirect(OAuthRedirect): """OpenIDConnect OAuth2 Redirect""" - def get_additional_parameters(self, source: OAuthSource): + def get_additional_parameters(self, source: OAuthSource): # pragma: no cover return { "scope": "openid email profile", } diff --git a/authentik/sources/oauth/types/reddit.py b/authentik/sources/oauth/types/reddit.py index 33852a777..4c60ece56 100644 --- a/authentik/sources/oauth/types/reddit.py +++ b/authentik/sources/oauth/types/reddit.py @@ -14,7 +14,7 @@ from authentik.sources.oauth.views.redirect import OAuthRedirect class RedditOAuthRedirect(OAuthRedirect): """Reddit OAuth2 Redirect""" - def get_additional_parameters(self, source): + def get_additional_parameters(self, source): # pragma: no cover return { "scope": "identity", "duration": "permanent", diff --git a/authentik/sources/oauth/types/twitter.py b/authentik/sources/oauth/types/twitter.py index bd27f22d4..3b7acf6cf 100644 --- a/authentik/sources/oauth/types/twitter.py +++ b/authentik/sources/oauth/types/twitter.py @@ -18,6 +18,6 @@ class TwitterOAuthCallback(OAuthCallback): ) -> Dict[str, Any]: return { "username": info.get("screen_name"), - "email": info.get("email"), + "email": info.get("email", None), "name": info.get("name"), }