From 76131e40ecd29f5022ee72b9acec3373b4338573 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 20 Apr 2021 18:48:01 +0200 Subject: [PATCH] tests/e2e: monkey patch OAuth1 test instead of setting URLs manually Signed-off-by: Jens Langhammer --- authentik/sources/oauth/views/base.py | 12 +++++++++--- tests/e2e/test_source_oauth.py | 28 +++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/authentik/sources/oauth/views/base.py b/authentik/sources/oauth/views/base.py index bfdd73faa..0ab379301 100644 --- a/authentik/sources/oauth/views/base.py +++ b/authentik/sources/oauth/views/base.py @@ -2,12 +2,15 @@ from typing import Optional, Type from django.http.request import HttpRequest +from structlog.stdlib import get_logger from authentik.sources.oauth.clients.base import BaseOAuthClient from authentik.sources.oauth.clients.oauth1 import OAuthClient from authentik.sources.oauth.clients.oauth2 import OAuth2Client from authentik.sources.oauth.models import OAuthSource +LOGGER = get_logger() + # pylint: disable=too-few-public-methods class OAuthClientMixin: @@ -22,6 +25,9 @@ class OAuthClientMixin: if self.client_class is not None: # pylint: disable=not-callable return self.client_class(source, self.request, **kwargs) - if source.request_token_url: - return OAuthClient(source, self.request, **kwargs) - return OAuth2Client(source, self.request, **kwargs) + if source.type.request_token_url or source.request_token_url: + client = OAuthClient(source, self.request, **kwargs) + else: + client = OAuth2Client(source, self.request, **kwargs) + LOGGER.debug("Using client for oauth request", client=client) + return client diff --git a/tests/e2e/test_source_oauth.py b/tests/e2e/test_source_oauth.py index c3646a7f7..faeeea221 100644 --- a/tests/e2e/test_source_oauth.py +++ b/tests/e2e/test_source_oauth.py @@ -4,6 +4,7 @@ from sys import platform from time import sleep from typing import Any, Optional from unittest.case import skipUnless +from unittest.mock import Mock, patch from django.test import override_settings from docker.models.containers import Container @@ -22,12 +23,31 @@ from authentik.providers.oauth2.generators import ( generate_client_secret, ) from authentik.sources.oauth.models import OAuthSource +from authentik.sources.oauth.types.manager import SourceType +from authentik.sources.oauth.types.twitter import TwitterOAuthCallback from tests.e2e.utils import SeleniumTestCase, apply_migration, object_manager, retry CONFIG_PATH = "/tmp/dex.yml" # nosec LOGGER = get_logger() +class OAUth1Type(SourceType): + """Twitter Type definition""" + + callback_view = TwitterOAuthCallback + name = "Twitter" + slug = "twitter" + + request_token_url = "http://localhost:5000/oauth/request_token" # nosec + access_token_url = "http://localhost:5000/oauth/access_token" # nosec + authorization_url = "http://localhost:5000/oauth/authorize" + profile_url = "http://localhost:5000/api/me" + urls_customizable = False + + +SOURCE_TYPE_MOCK = Mock(return_value=OAUth1Type()) + + @skipUnless(platform.startswith("linux"), "requires local docker") class TestSourceOAuth2(SeleniumTestCase): """test OAuth Source flow""" @@ -291,10 +311,6 @@ class TestSourceOAuth1(SeleniumTestCase): authentication_flow=authentication_flow, enrollment_flow=enrollment_flow, provider_type="twitter", - request_token_url="http://localhost:5000/oauth/request_token", - access_token_url="http://localhost:5000/oauth/access_token", - authorization_url="http://localhost:5000/oauth/authorize", - profile_url="http://localhost:5000/api/me", consumer_key=self.client_id, consumer_secret=self.client_secret, ) @@ -304,6 +320,10 @@ class TestSourceOAuth1(SeleniumTestCase): @apply_migration("authentik_flows", "0008_default_flows") @apply_migration("authentik_flows", "0009_source_flows") @apply_migration("authentik_crypto", "0002_create_self_signed_kp") + @patch( + "authentik.sources.oauth.types.manager.SourceTypeManager.find_type", + SOURCE_TYPE_MOCK, + ) @object_manager def test_oauth_enroll(self): """test OAuth Source With With OIDC"""