diff --git a/common/djangoapps/third_party_auth/saml_configuration/tests/test_saml_configuration.py b/common/djangoapps/third_party_auth/saml_configuration/tests/test_saml_configuration.py index 540d73ad86..4e99104144 100644 --- a/common/djangoapps/third_party_auth/saml_configuration/tests/test_saml_configuration.py +++ b/common/djangoapps/third_party_auth/saml_configuration/tests/test_saml_configuration.py @@ -51,7 +51,7 @@ class SAMLConfigurationTests(APITestCase): """ @classmethod def setUpTestData(cls): - super(SAMLConfigurationTests, cls).setUpTestData() + super().setUpTestData() cls.user = User.objects.create_user(username='testuser', password=TEST_PASSWORD) cls.site, _ = Site.objects.get_or_create(domain='example.com') for config in SAML_CONFIGURATIONS: @@ -74,7 +74,7 @@ class SAMLConfigurationTests(APITestCase): ) def setUp(self): - super(SAMLConfigurationTests, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.client.login(username=self.user.username, password=TEST_PASSWORD) def test_get_saml_configurations_successful(self): diff --git a/common/djangoapps/third_party_auth/saml_configuration/views.py b/common/djangoapps/third_party_auth/saml_configuration/views.py index 20cbbc5fed..093717b296 100644 --- a/common/djangoapps/third_party_auth/saml_configuration/views.py +++ b/common/djangoapps/third_party_auth/saml_configuration/views.py @@ -10,7 +10,7 @@ from ..models import SAMLConfiguration from .serializers import SAMLConfigurationSerializer -class SAMLConfigurationMixin(object): +class SAMLConfigurationMixin: authentication_classes = (JwtAuthentication, SessionAuthentication,) permission_classes = (permissions.IsAuthenticated,) serializer_class = SAMLConfigurationSerializer diff --git a/common/djangoapps/third_party_auth/samlproviderconfig/tests/test_samlproviderconfig.py b/common/djangoapps/third_party_auth/samlproviderconfig/tests/test_samlproviderconfig.py index 29ccf53bb1..f41ae253ad 100644 --- a/common/djangoapps/third_party_auth/samlproviderconfig/tests/test_samlproviderconfig.py +++ b/common/djangoapps/third_party_auth/samlproviderconfig/tests/test_samlproviderconfig.py @@ -48,7 +48,7 @@ class SAMLProviderConfigTests(APITestCase): """ @classmethod def setUpTestData(cls): - super(SAMLProviderConfigTests, cls).setUpTestData() + super().setUpTestData() cls.user = User.objects.create_user(username='testuser', password='testpwd') cls.site, _ = Site.objects.get_or_create(domain='example.com') cls.enterprise_customer = EnterpriseCustomer.objects.create( diff --git a/common/djangoapps/third_party_auth/samlproviderconfig/views.py b/common/djangoapps/third_party_auth/samlproviderconfig/views.py index 050d655f21..a8cdb40d7d 100644 --- a/common/djangoapps/third_party_auth/samlproviderconfig/views.py +++ b/common/djangoapps/third_party_auth/samlproviderconfig/views.py @@ -18,7 +18,7 @@ from .serializers import SAMLProviderConfigSerializer from ..utils import convert_saml_slug_provider_id -class SAMLProviderMixin(object): +class SAMLProviderMixin: authentication_classes = [JwtAuthentication, SessionAuthentication] permission_classes = [permissions.IsAuthenticated] serializer_class = SAMLProviderConfigSerializer @@ -95,7 +95,7 @@ class SAMLProviderConfigViewSet(PermissionRequiredMixin, SAMLProviderMixin, view try: enterprise_customer = EnterpriseCustomer.objects.get(pk=customer_uuid) except EnterpriseCustomer.DoesNotExist: - raise ValidationError('Enterprise customer not found at uuid: {}'.format(customer_uuid)) # lint-amnesty, pylint: disable=raise-missing-from + raise ValidationError(f'Enterprise customer not found at uuid: {customer_uuid}') # lint-amnesty, pylint: disable=raise-missing-from # Create the samlproviderconfig model first serializer = self.get_serializer(data=request.data) diff --git a/common/djangoapps/third_party_auth/samlproviderdata/tests/test_samlproviderdata.py b/common/djangoapps/third_party_auth/samlproviderdata/tests/test_samlproviderdata.py index 8208d8ae88..e9b593ff79 100644 --- a/common/djangoapps/third_party_auth/samlproviderdata/tests/test_samlproviderdata.py +++ b/common/djangoapps/third_party_auth/samlproviderdata/tests/test_samlproviderdata.py @@ -48,7 +48,7 @@ class SAMLProviderDataTests(APITestCase): """ @classmethod def setUpTestData(cls): - super(SAMLProviderDataTests, cls).setUpTestData() + super().setUpTestData() cls.user = User.objects.create_user(username='testuser', password='testpwd') cls.site, _ = Site.objects.get_or_create(domain='example.com') cls.enterprise_customer = EnterpriseCustomer.objects.create( diff --git a/common/djangoapps/third_party_auth/samlproviderdata/views.py b/common/djangoapps/third_party_auth/samlproviderdata/views.py index e636018982..43c24db812 100644 --- a/common/djangoapps/third_party_auth/samlproviderdata/views.py +++ b/common/djangoapps/third_party_auth/samlproviderdata/views.py @@ -17,7 +17,7 @@ from ..models import SAMLProviderConfig, SAMLProviderData from .serializers import SAMLProviderDataSerializer -class SAMLProviderDataMixin(object): +class SAMLProviderDataMixin: authentication_classes = [JwtAuthentication, SessionAuthentication] permission_classes = [permissions.IsAuthenticated] serializer_class = SAMLProviderDataSerializer diff --git a/common/djangoapps/third_party_auth/tests/factories.py b/common/djangoapps/third_party_auth/tests/factories.py index df3ecd9848..bc20502cb0 100644 --- a/common/djangoapps/third_party_auth/tests/factories.py +++ b/common/djangoapps/third_party_auth/tests/factories.py @@ -14,7 +14,7 @@ class SAMLConfigurationFactory(DjangoModelFactory): """ Factory or SAMLConfiguration model in third_party_auth app. """ - class Meta(object): + class Meta: model = SAMLConfiguration site = SubFactory(SiteFactory) @@ -25,7 +25,7 @@ class SAMLProviderConfigFactory(DjangoModelFactory): """ Factory or SAMLProviderConfig model in third_party_auth app. """ - class Meta(object): + class Meta: model = SAMLProviderConfig django_get_or_create = ('slug', 'metadata_source', "entity_id") diff --git a/common/djangoapps/third_party_auth/tests/samlutils.py b/common/djangoapps/third_party_auth/tests/samlutils.py index ff1617c264..aa2428d297 100644 --- a/common/djangoapps/third_party_auth/tests/samlutils.py +++ b/common/djangoapps/third_party_auth/tests/samlutils.py @@ -12,9 +12,9 @@ def _jwt_token_from_role_context_pairs(user, role_context_pairs): """ roles = [] for role, context in role_context_pairs: - role_data = '{role}'.format(role=role) + role_data = f'{role}' if context is not None: - role_data += ':{context}'.format(context=context) + role_data += f':{context}' roles.append(role_data) payload = generate_unversioned_payload(user) diff --git a/common/djangoapps/third_party_auth/tests/specs/base.py b/common/djangoapps/third_party_auth/tests/specs/base.py index 66ad493202..aeadd6d37d 100644 --- a/common/djangoapps/third_party_auth/tests/specs/base.py +++ b/common/djangoapps/third_party_auth/tests/specs/base.py @@ -6,9 +6,9 @@ Base integration test for provider implementations. import json import unittest from contextlib import contextmanager -import pytest +from unittest import mock -import mock +import pytest from django import test from django.conf import settings from django.contrib import auth @@ -40,7 +40,7 @@ def create_account(request): return RegistrationView().post(request) -class HelperMixin(object): +class HelperMixin: """ Contains helper methods for IntegrationTestMixin and IntegrationTest classes below. """ @@ -73,11 +73,11 @@ class HelperMixin(object): # Check that the correct provider was selected. self.assertContains( response, - u'"errorMessage": null' + '"errorMessage": null' ) self.assertContains( response, - u'"currentProvider": "{}"'.format(self.provider.name), + f'"currentProvider": "{self.provider.name}"', ) # Expect that each truthy value we've prepopulated the register form # with is actually present. @@ -122,7 +122,7 @@ class HelperMixin(object): assert 302 == response.status_code assert 'canceled' in location assert self.backend_name in location - assert location.startswith((expected_uri + '?')) + assert location.startswith(expected_uri + '?') def assert_json_failure_response_is_inactive_account(self, response): """Asserts failure on /login for inactive account looks right.""" @@ -186,8 +186,9 @@ class HelperMixin(object): assert 302 == response.status_code # NOTE: Ideally we should use assertRedirects(), however it errors out due to the hostname, testserver, # not being properly set. This may be an issue with the call made by PSA, but we are not certain. - assert response.get('Location').endswith((expected_redirect_url or - django_settings.SOCIAL_AUTH_LOGIN_REDIRECT_URL)) + assert response.get('Location').endswith( + expected_redirect_url or django_settings.SOCIAL_AUTH_LOGIN_REDIRECT_URL + ) def assert_redirect_to_login_looks_correct(self, response): """Asserts a response would redirect to /login.""" @@ -363,7 +364,7 @@ class IntegrationTestMixin(testutil.TestCase, test.TestCase, HelperMixin): USER_USERNAME = "override" def setUp(self): - super(IntegrationTestMixin, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.request_factory = test.RequestFactory() self.login_page_url = reverse('signin_user') @@ -533,7 +534,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase, HelperMixin): """Abstract base class for provider integration tests.""" def setUp(self): - super(IntegrationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.request_factory = test.RequestFactory() # Actual tests, executed once per child. @@ -796,11 +797,11 @@ class IntegrationTest(testutil.TestCase, test.TestCase, HelperMixin): def test_first_party_auth_trumps_third_party_auth_and_fails_when_credentials_bad(self): self.assert_first_party_auth_trumps_third_party_auth( - email='user@example.com', password=u'password', success=False) + email='user@example.com', password='password', success=False) def test_first_party_auth_trumps_third_party_auth_and_succeeds_when_credentials_good(self): self.assert_first_party_auth_trumps_third_party_auth( - email='user@example.com', password=u'password', success=True) + email='user@example.com', password='password', success=True) def test_pipeline_redirects_to_requested_url(self): requested_redirect_url = 'foo' # something different from '/dashboard' diff --git a/common/djangoapps/third_party_auth/tests/specs/test_azuread.py b/common/djangoapps/third_party_auth/tests/specs/test_azuread.py index e980b5da04..eeffc4afe7 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_azuread.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_azuread.py @@ -8,7 +8,7 @@ class AzureADOauth2IntegrationTest(base.Oauth2IntegrationTest): # lint-amnesty, """Integration tests for Azure Active Directory / Microsoft Account provider.""" def setUp(self): - super(AzureADOauth2IntegrationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.provider = self.configure_azure_ad_provider( enabled=True, visible=True, diff --git a/common/djangoapps/third_party_auth/tests/specs/test_generic.py b/common/djangoapps/third_party_auth/tests/specs/test_generic.py index f02ccd54d2..b9a41914e9 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_generic.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_generic.py @@ -20,7 +20,7 @@ class GenericIntegrationTest(IntegrationTestMixin, testutil.TestCase): USER_USERNAME = "Galactica1" def setUp(self): - super(GenericIntegrationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.configure_dummy_provider(enabled=True, visible=True) def do_provider_login(self, provider_redirect_url): diff --git a/common/djangoapps/third_party_auth/tests/specs/test_google.py b/common/djangoapps/third_party_auth/tests/specs/test_google.py index c3cf331c92..c74b78d1c1 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_google.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_google.py @@ -5,10 +5,10 @@ import base64 import hashlib import hmac import json +from unittest.mock import patch from django.conf import settings from django.urls import reverse -from mock import patch from social_core.exceptions import AuthException from common.djangoapps.student.tests.factories import UserFactory @@ -20,7 +20,7 @@ class GoogleOauth2IntegrationTest(base.Oauth2IntegrationTest): # lint-amnesty, """Integration tests for provider.GoogleOauth2.""" def setUp(self): - super(GoogleOauth2IntegrationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.provider = self.configure_google_provider( enabled=True, visible=True, @@ -50,7 +50,7 @@ class GoogleOauth2IntegrationTest(base.Oauth2IntegrationTest): # lint-amnesty, return self.get_response_data().get('email').split('@')[0] def assert_redirect_to_provider_looks_correct(self, response): - super(GoogleOauth2IntegrationTest, self).assert_redirect_to_provider_looks_correct(response) # lint-amnesty, pylint: disable=super-with-arguments + super().assert_redirect_to_provider_looks_correct(response) assert 'google.com' in response['Location'] def test_custom_form(self): diff --git a/common/djangoapps/third_party_auth/tests/specs/test_linkedin.py b/common/djangoapps/third_party_auth/tests/specs/test_linkedin.py index 9a48ac0f59..fd9ce1da5f 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_linkedin.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_linkedin.py @@ -17,7 +17,7 @@ class LinkedInOauth2IntegrationTest(base.Oauth2IntegrationTest): # lint-amnesty """Integration tests for provider.LinkedInOauth2.""" def setUp(self): - super(LinkedInOauth2IntegrationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.provider = self.configure_linkedin_provider( enabled=True, visible=True, diff --git a/common/djangoapps/third_party_auth/tests/specs/test_lti.py b/common/djangoapps/third_party_auth/tests/specs/test_lti.py index adfc070f7f..0c505bf80a 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_lti.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_lti.py @@ -30,10 +30,10 @@ class IntegrationTestLTI(testutil.TestCase): """ def setUp(self): - super(IntegrationTestLTI, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.hostname = 'testserver' self.client.defaults['SERVER_NAME'] = self.hostname - self.url_prefix = 'http://{}'.format(self.hostname) + self.url_prefix = f'http://{self.hostname}' self.configure_lti_provider( name='Other Tool Consumer 1', enabled=True, lti_consumer_key='other1', diff --git a/common/djangoapps/third_party_auth/tests/specs/test_testshib.py b/common/djangoapps/third_party_auth/tests/specs/test_testshib.py index 30c8c7c391..1589571e5a 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_testshib.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_testshib.py @@ -8,26 +8,27 @@ import json import logging import os from unittest import skip +from unittest.mock import MagicMock, patch import ddt import httpretty from django.conf import settings from django.contrib import auth +from enterprise.models import EnterpriseCustomerIdentityProvider, EnterpriseCustomerUser from freezegun import freeze_time -from mock import MagicMock, patch from social_core import actions from social_django import views as social_views from social_django.models import UserSocialAuth from testfixtures import LogCapture -from enterprise.models import EnterpriseCustomerIdentityProvider, EnterpriseCustomerUser -from openedx.core.djangoapps.user_authn.views.login import login_user -from openedx.core.djangoapps.user_api.accounts.settings_views import account_settings_context -from openedx.features.enterprise_support.tests.factories import EnterpriseCustomerFactory from common.djangoapps.third_party_auth import pipeline -from common.djangoapps.third_party_auth.saml import SapSuccessFactorsIdentityProvider, log as saml_log +from common.djangoapps.third_party_auth.saml import SapSuccessFactorsIdentityProvider +from common.djangoapps.third_party_auth.saml import log as saml_log from common.djangoapps.third_party_auth.tasks import fetch_saml_metadata from common.djangoapps.third_party_auth.tests import testutil, utils +from openedx.core.djangoapps.user_api.accounts.settings_views import account_settings_context +from openedx.core.djangoapps.user_authn.views.login import login_user +from openedx.features.enterprise_support.tests.factories import EnterpriseCustomerFactory from .base import IntegrationTestMixin @@ -37,7 +38,7 @@ TESTSHIB_METADATA_URL_WITH_CACHE_DURATION = 'https://mock.testshib.org/metadata/ TESTSHIB_SSO_URL = 'https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO' -class SamlIntegrationTestUtilities(object): +class SamlIntegrationTestUtilities: """ Class contains methods particular to SAML integration testing so that they can be separated out from the actual test methods. @@ -52,7 +53,7 @@ class SamlIntegrationTestUtilities(object): USER_USERNAME = "myself" def setUp(self): - super(SamlIntegrationTestUtilities, self).setUp() # lint-amnesty, pylint: disable=no-member, super-with-arguments + super().setUp() # lint-amnesty, pylint: disable=no-member, super-with-arguments self.enable_saml( # lint-amnesty, pylint: disable=no-member private_key=self._get_private_key(), # lint-amnesty, pylint: disable=no-member public_key=self._get_public_key(), # lint-amnesty, pylint: disable=no-member @@ -156,7 +157,7 @@ class TestShibIntegrationTest(SamlIntegrationTestUtilities, IntegrationTestMixin 'id': 'id_value', 'firstName': 'firstName_value', 'idp_name': 'testshib', - 'attributes': {u'urn:oid:0.9.2342.19200300.100.1.1': [u'myself'], 'name_id': '1'}, + 'attributes': {'urn:oid:0.9.2342.19200300.100.1.1': ['myself'], 'name_id': '1'}, 'session_index': '1', } @@ -307,7 +308,7 @@ class TestShibIntegrationTest(SamlIntegrationTestUtilities, IntegrationTestMixin expected_next_url = "/dashboard" (msg, action_type, idp_name, request_data, next_url, xml), _kwargs = mock_log.call_args_list[0] - assert msg.startswith(u'SAML login %s') + assert msg.startswith('SAML login %s') assert action_type == 'request' assert idp_name == self.PROVIDER_IDP_SLUG self.assertDictContainsSubset( @@ -318,7 +319,7 @@ class TestShibIntegrationTest(SamlIntegrationTestUtilities, IntegrationTestMixin assert '