Skip to content

Commit

Permalink
5343 upgrade django in mitxonline (#2387)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cp-at-mit and pre-commit-ci[bot] authored Sep 19, 2024
1 parent 87097a8 commit 955981d
Show file tree
Hide file tree
Showing 66 changed files with 955 additions and 1,076 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"filename": "docker-compose.yml",
"hashed_secret": "965748b380ab0ab25d1846afc174a3d93a8ec06c",
"is_verified": false,
"line_number": 8
"line_number": 6
}
],
"frontend/public/src/constants.js": [
Expand Down Expand Up @@ -193,7 +193,7 @@
"filename": "main/settings.py",
"hashed_secret": "09edaaba587f94f60fbb5cee2234507bcb883cc2",
"is_verified": false,
"line_number": 958
"line_number": 955
}
],
"pants": [
Expand Down Expand Up @@ -240,5 +240,5 @@
}
]
},
"generated_at": "2024-07-02T16:40:29Z"
"generated_at": "2024-09-16T18:23:36Z"
}
5 changes: 3 additions & 2 deletions authentication/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Authentication middleware"""

from urllib.parse import quote

from django.shortcuts import redirect
from django.utils.http import urlquote
from social_core.exceptions import SocialAuthBaseException
from social_django.middleware import SocialAuthExceptionMiddleware

Expand Down Expand Up @@ -31,5 +32,5 @@ def process_exception(self, request, exception):
if url: # noqa: RET503
url += (
"?" in url and "&" or "?"
) + f"message={urlquote(message)}&backend={backend_name}"
) + f"message={quote(message)}&backend={backend_name}"
return redirect(url)
24 changes: 14 additions & 10 deletions authentication/middleware_test.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
"""Tests for auth middleware"""

from urllib.parse import quote

from django.contrib.sessions.middleware import SessionMiddleware
from django.shortcuts import reverse
from django.utils.http import urlquote
from rest_framework import status
from social_core.exceptions import AuthAlreadyAssociated
from social_django.utils import load_backend, load_strategy

from authentication.middleware import SocialAuthExceptionRedirectMiddleware


def test_process_exception_no_strategy(rf, settings):
def test_process_exception_no_strategy(mocker, rf, settings):
"""Tests that if the request has no strategy it does nothing"""
settings.DEBUG = False
get_response = mocker.MagicMock()
request = rf.get(reverse("social:complete", args=("email",)))
middleware = SocialAuthExceptionRedirectMiddleware()
middleware = SocialAuthExceptionRedirectMiddleware(get_response)
assert middleware.process_exception(request, None) is None


def test_process_exception(rf, settings):
def test_process_exception(mocker, rf, settings):
"""Tests that a process_exception handles auth exceptions correctly"""
settings.DEBUG = False
request = rf.get(reverse("social:complete", args=("email",)))
# social_django depends on request.sesssion, so use the middleware to set that
SessionMiddleware().process_request(request)
get_response = mocker.MagicMock()
SessionMiddleware(get_response).process_request(request)
strategy = load_strategy(request)
backend = load_backend(strategy, "email", None)
request.social_strategy = strategy
request.backend = backend

middleware = SocialAuthExceptionRedirectMiddleware()
middleware = SocialAuthExceptionRedirectMiddleware(get_response)
error = AuthAlreadyAssociated(backend)
result = middleware.process_exception(request, error)
assert result.status_code == status.HTTP_302_FOUND
assert result.url == "{}?message={}&backend={}".format(
reverse("login"), urlquote(error.__str__()), backend.name
reverse("login"), quote(error.__str__()), backend.name
)


def test_process_exception_non_auth_error(rf, settings):
def test_process_exception_non_auth_error(mocker, rf, settings):
"""Tests that a process_exception handles non-auth exceptions correctly"""
settings.DEBUG = False
request = rf.get(reverse("social:complete", args=("email",)))
# social_django depends on request.sesssion, so use the middleware to set that
SessionMiddleware().process_request(request)
get_response = mocker.MagicMock()
SessionMiddleware(get_response).process_request(request)
strategy = load_strategy(request)
backend = load_backend(strategy, "email", None)
request.social_strategy = strategy
request.backend = backend

middleware = SocialAuthExceptionRedirectMiddleware()
middleware = SocialAuthExceptionRedirectMiddleware(get_response)
assert (
middleware.process_exception(request, Exception("something bad happened"))
is None
Expand Down
25 changes: 15 additions & 10 deletions authentication/pipeline/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ def validate_email_auth_request_not_email_backend(mocker):
[(True, {"flow": SocialAuthState.FLOW_LOGIN}), (False, {})],
)
@pytest.mark.django_db
def test_validate_email_auth_request(rf, has_user, expected):
def test_validate_email_auth_request(mocker, rf, has_user, expected):
"""Test that validate_email_auth_request returns correctly given the input"""
request = rf.post("/complete/email")
middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()
strategy = load_strategy(request)
Expand Down Expand Up @@ -141,15 +142,16 @@ def test_user_password_not_email_backend(mocker):


@pytest.mark.parametrize("user_password", ["abc123", "def456"])
def test_user_password_login(rf, user, user_password):
def test_user_password_login(mocker, rf, user, user_password):
"""Tests that user_password works for login case"""
request_password = "abc123" # noqa: S105
user.set_password(user_password)
user.save()
request = rf.post(
"/complete/email", {"password": request_password, "email": user.email}
)
middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()
strategy = load_strategy(request)
Expand Down Expand Up @@ -177,15 +179,16 @@ def test_user_password_login(rf, user, user_password):
)


def test_user_password_not_login(rf, user):
def test_user_password_not_login(mocker, rf, user):
"""
Tests that user_password performs denies authentication
for an existing user if password not provided regardless of auth_type
"""
user.set_password("abc123")
user.save()
request = rf.post("/complete/email", {"email": user.email})
middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()
strategy = load_strategy(request)
Expand All @@ -201,12 +204,13 @@ def test_user_password_not_login(rf, user):
)


def test_user_password_not_exists(rf):
def test_user_password_not_exists(mocker, rf):
"""Tests that user_password raises auth error for nonexistent user"""
request = rf.post(
"/complete/email", {"password": "abc123", "email": "doesntexist@localhost"}
)
middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()
strategy = load_strategy(request)
Expand All @@ -222,13 +226,14 @@ def test_user_password_not_exists(rf):
)


def test_user_not_active(rf, user):
def test_user_not_active(mocker, rf, user):
"""Tests that an inactive user raises auth error, InvalidPasswordException"""
user.set_password("abc123")
user.is_active = False
user.save()
request = rf.post("/complete/email", {"password": "abc123", "email": user.email})
middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()
strategy = load_strategy(request)
Expand Down
18 changes: 9 additions & 9 deletions cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def test_course_page_context_edx_access( # noqa: PLR0913
patched_get_relevant_run_qset.assert_called_once_with(course=course_page.course)


def generate_flexible_pricing_response(request_user, flexible_pricing_form):
def generate_flexible_pricing_response(mocker, request_user, flexible_pricing_form):
"""
Generates a fully realized request for the Flexible Pricing tests.
Expand All @@ -248,8 +248,8 @@ def generate_flexible_pricing_response(request_user, flexible_pricing_form):
rf = RequestFactory()
request = rf.get("/")
request.user = request_user

middleware = SessionMiddleware()
get_response = mocker.MagicMock()
middleware = SessionMiddleware(get_response)
middleware.process_request(request)
request.session.save()

Expand Down Expand Up @@ -288,7 +288,7 @@ def test_flex_pricing_form_display(mocker, is_authed, has_submission):
courseware_object=flex_form.selected_course,
)

response = generate_flexible_pricing_response(request_user, flex_form)
response = generate_flexible_pricing_response(mocker, request_user, flex_form)

# simple string checking for the rendered content
# should match what's in the factory
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_flex_pricing_form_state_display(mocker, submission_status):
courseware_object=course_page.course,
)

response = generate_flexible_pricing_response(request_user, flex_form)
response = generate_flexible_pricing_response(mocker, request_user, flex_form)

if submission_status == FlexiblePriceStatus.CREATED:
assert "Application Processing" in response.rendered_content
Expand Down Expand Up @@ -481,13 +481,13 @@ def test_flex_pricing_single_submission(

# test to make sure we get back a status message from the first form

response = generate_flexible_pricing_response(request_user, first_sub_form)
response = generate_flexible_pricing_response(mocker, request_user, first_sub_form)

assert "Application Processing" in response.rendered_content

# then test to make sure we get a status message back from the second form too

response = generate_flexible_pricing_response(request_user, second_sub_form)
response = generate_flexible_pricing_response(mocker, request_user, second_sub_form)

# should not get a form here - should get Application Processing

Expand Down Expand Up @@ -534,15 +534,15 @@ def test_flex_pricing_form_state_display_no_discount_tier(
tier=tier,
)

response = generate_flexible_pricing_response(request_user, flex_form)
response = generate_flexible_pricing_response(mocker, request_user, flex_form)

assert "No Discount Text" in response.rendered_content

flexprice.tier = other_tier
flexprice.save()
flexprice.refresh_from_db()

response = generate_flexible_pricing_response(request_user, flex_form)
response = generate_flexible_pricing_response(mocker, request_user, flex_form)

assert "Approved" in response.rendered_content

Expand Down
6 changes: 3 additions & 3 deletions cms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
The pattern(s) defined here serve the same Wagtail view that the library-defined pattern serves.
""" # noqa: RUF002

from django.conf.urls import url
from django.urls import re_path
from wagtail import views
from wagtail.coreutils import WAGTAIL_APPEND_SLASH

Expand All @@ -36,6 +36,6 @@


urlpatterns = [
url(custom_serve_pattern, views.serve, name="wagtail_serve_custom"),
url(program_custom_serve_pattern, views.serve, name="wagtail_serve_custom"),
re_path(custom_serve_pattern, views.serve, name="wagtail_serve_custom"),
re_path(program_custom_serve_pattern, views.serve, name="wagtail_serve_custom"),
]
1 change: 0 additions & 1 deletion courses/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# pylint: disable=missing-docstring,invalid-name
default_app_config = "courses.apps.CoursesConfig"
Loading

0 comments on commit 955981d

Please sign in to comment.