Skip to content

Commit

Permalink
fix: fix middleware get_response parameter deprecation warning (opene…
Browse files Browse the repository at this point in the history
  • Loading branch information
UsamaSadiq authored Aug 22, 2023
1 parent 68e9a03 commit 7710e60
Show file tree
Hide file tree
Showing 32 changed files with 95 additions and 67 deletions.
2 changes: 1 addition & 1 deletion common/djangoapps/student/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUp(self):
@staticmethod
def _add_session(request):
"""Annotate the request object with a session"""
middleware = SessionMiddleware()
middleware = SessionMiddleware(get_response=lambda request: None)
middleware.process_request(request)
request.session.save()

Expand Down
4 changes: 2 additions & 2 deletions common/djangoapps/third_party_auth/tests/specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def assert_exception_redirect_looks_correct(self, expected_uri, auth_entry=None)
middleware.ExceptionMiddleware makes sure the user ends up in the right
place when they cancel authentication via the provider's UX.
"""
exception_middleware = middleware.ExceptionMiddleware()
exception_middleware = middleware.ExceptionMiddleware(get_response=lambda request: None)
request, _ = self.get_request_and_strategy(auth_entry=auth_entry)
response = exception_middleware.process_exception(
request, exceptions.AuthCanceled(request.backend))
Expand Down Expand Up @@ -730,7 +730,7 @@ def test_already_associated_exception_populates_dashboard_with_error(self):

# Monkey-patch storage for messaging; pylint: disable=protected-access
post_request._messages = fallback.FallbackStorage(post_request)
middleware.ExceptionMiddleware().process_exception(
middleware.ExceptionMiddleware(get_response=lambda request: None).process_exception(
post_request,
exceptions.AuthAlreadyAssociated(self.provider.backend_name, 'account is already in use.'))

Expand Down
4 changes: 2 additions & 2 deletions common/djangoapps/third_party_auth/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_http_exception_redirection(self):
exception.response = HttpResponse(status=502)

# Add error message for error in auth pipeline
MessageMiddleware().process_request(request)
response = ExceptionMiddleware().process_exception(
MessageMiddleware(get_response=lambda request: None).process_request(request)
response = ExceptionMiddleware(get_response=lambda request: None).process_exception(
request, exception
)
target_url = response.url
Expand Down
4 changes: 2 additions & 2 deletions common/djangoapps/track/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TrackMiddlewareTestCase(TestCase):

def setUp(self):
super().setUp()
self.track_middleware = TrackMiddleware()
self.track_middleware = TrackMiddleware(get_response=lambda request: None)
self.request_factory = RequestFactory()

patcher = patch('common.djangoapps.track.views.server_track')
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_request_with_user(self):

def test_request_with_session(self):
request = self.request_factory.get('/courses/')
SessionMiddleware().process_request(request)
SessionMiddleware(get_response=lambda request: None).process_request(request)
request.session.save()
session_key = request.session.session_key
expected_session_key = self.track_middleware.substitute_session_key(session_key)
Expand Down
6 changes: 3 additions & 3 deletions common/djangoapps/track/views/tests/test_segmentio.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_invalid_user_id(self):

@data('foo/bar/baz', 'course-v1:foo+bar+baz')
def test_success(self, course_id):
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)

request = self.create_request(
data=self.create_segmentio_event_json(data={'foo': 'bar'}, course_id=course_id),
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_hiding_failure(self):
@unpack
def test_video_event(self, name, event_type):
course_id = 'foo/bar/baz'
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)

input_payload = {
'current_time': 132.134456,
Expand Down Expand Up @@ -414,7 +414,7 @@ def test_previous_builds(self,
was sent instead of edx.video.position.changed
"""
course_id = 'foo/bar/baz'
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)
input_payload = {
"code": "mobile",
"new_time": 89.699177437,
Expand Down
6 changes: 3 additions & 3 deletions common/djangoapps/track/views/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_user_track_with_username_in_data(self, event_data, expected_event_data)
def test_user_track_with_middleware_and_processors(self):
self.recreate_tracker()

middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)
payload = '{"foo": "bar"}'
user_id = 1
request = self.request_factory.get('/event', {
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_server_track(self):
assert_event_matches(expected_event, actual_event)

def test_server_track_with_middleware(self):
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)
request = self.request_factory.get(self.path_with_course)
middleware.process_request(request)
# The middleware emits an event, reset the mock to ignore it since we aren't testing that feature.
Expand Down Expand Up @@ -279,7 +279,7 @@ def test_server_track_with_middleware(self):
)

def test_server_track_with_middleware_and_google_analytics_cookie(self):
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)
request = self.request_factory.get(self.path_with_course)
request.COOKIES['_ga'] = 'GA1.2.1033501218.1368477899'
middleware.process_request(request)
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/courseware/block_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ def handle_xblock_callback(request, course_id, usage_id, handler, suffix=None):
"""
# In this case, we are using Session based authentication, so we need to check CSRF token.
if request.user.is_authenticated:
error = CsrfViewMiddleware().process_view(request, None, (), {})
error = CsrfViewMiddleware(get_response=lambda request: None).process_view(request, None, (), {})
if error:
return error

Expand Down
4 changes: 2 additions & 2 deletions lms/djangoapps/courseware/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_headers(cache_response):
def test_process_404(self):
"""A 404 should not trigger anything"""
request = RequestFactory().get("dummy_url")
response = RedirectMiddleware().process_exception(
response = RedirectMiddleware(get_response=lambda request: None).process_exception(
request, Http404()
)
assert response is None
Expand All @@ -48,7 +48,7 @@ def test_redirect_exceptions(self):
request = RequestFactory().get("dummy_url")
test_url = '/test_url'
exception = Redirect(test_url)
response = RedirectMiddleware().process_exception(
response = RedirectMiddleware(get_response=lambda request: None).process_exception(
request, exception
)
assert response.status_code == 302
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,7 @@ def test_requires_matched_user_has_forum_content(self, mock_request):
class SegmentIOForumThreadViewedEventTestCase(SegmentIOTrackingTestCaseBase):

def _raise_navigation_event(self, label, include_name):
middleware = TrackMiddleware()
middleware = TrackMiddleware(get_response=lambda request: None)
kwargs = {'label': label}
if include_name:
kwargs['name'] = 'edx.bi.app.navigation.screen'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AjaxExceptionTestCase(TestCase): # lint-amnesty, pylint: disable=missing-

def setUp(self):
super().setUp()
self.a = middleware.AjaxExceptionMiddleware()
self.a = middleware.AjaxExceptionMiddleware(get_response=lambda request: None)
self.request1 = django.http.HttpRequest()
self.request0 = django.http.HttpRequest()
self.exception1 = comment_client.CommentClientRequestError('{}', 401)
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/mobile_api/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TestAppVersionUpgradeMiddleware(CacheIsolationTestCase):

def setUp(self):
super().setUp()
self.middleware = AppVersionUpgrade()
self.middleware = AppVersionUpgrade(get_response=lambda request: None)
self.set_app_version_config()

def set_app_version_config(self):
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/auth_exchange/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
super().setUp()
self.request = RequestFactory().post("dummy_url")
redirect_uri = 'dummy_redirect_url'
SessionMiddleware().process_request(self.request)
SessionMiddleware(get_response=lambda request: None).process_request(self.request)
self.request.social_strategy = social_utils.load_strategy(self.request)
# pylint: disable=no-member
self.request.backend = social_utils.load_backend(self.request.social_strategy, self.BACKEND, redirect_uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_user_logout_on_session_hash_change(self):
self.request.COOKIES[settings.SESSION_COOKIE_NAME] = str(safe_cookie_data)
self.client.response.cookies[settings.SESSION_COOKIE_NAME] = session_id
self.client.response.cookies['edx-jwt-cookie-header-payload'] = 'test-jwt-payload'
SafeSessionMiddleware().process_request(self.request)
SafeSessionMiddleware(get_response=lambda request: None).process_request(self.request)

# asserts that user, session, and JWT cookies exist
assert self.request.session.get(SESSION_KEY) is not None
Expand All @@ -76,8 +76,10 @@ def test_user_logout_on_session_hash_change(self):
assert self.client.response.cookies.get('edx-jwt-cookie-header-payload').value == 'test-jwt-payload'

with patch.object(User, 'get_session_auth_hash', return_value='abc123'):
CacheBackedAuthenticationMiddleware().process_request(self.request)
SafeSessionMiddleware().process_response(self.request, self.client.response)
CacheBackedAuthenticationMiddleware(get_response=lambda request: None).process_request(self.request)
SafeSessionMiddleware(get_response=lambda request: None).process_response(
self.request, self.client.response
)

# asserts that user, session, and JWT cookies do not exist
assert self.request.session.get(SESSION_KEY) is None
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/cors_csrf/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SessionAuthenticationCrossDomainCsrf(authentication.SessionAuthentication)
it can be mixed in with other `SessionAuthentication` subclasses.
"""
def _process_enforce_csrf(self, request):
CsrfViewMiddleware().process_request(request)
CsrfViewMiddleware(get_response=lambda request: None).process_request(request)
return super().enforce_csrf(request)

def enforce_csrf(self, request):
Expand Down
10 changes: 5 additions & 5 deletions openedx/core/djangoapps/cors_csrf/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_request(self, is_secure, http_referer):
@override_settings(FEATURES={'ENABLE_CORS_HEADERS': True})
def setUp(self):
super().setUp()
self.middleware = CorsCSRFMiddleware()
self.middleware = CorsCSRFMiddleware(get_response=lambda request: None)

def check_not_enabled(self, request):
"""
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_enabled(self, http_referer):
)
def test_disabled_no_cors_headers(self):
with pytest.raises(MiddlewareNotUsed):
CorsCSRFMiddleware()
CorsCSRFMiddleware(get_response=lambda request: None)

@override_settings(CORS_ORIGIN_WHITELIST=['https://bar.com'])
def test_disabled_wrong_cors_domain(self):
Expand Down Expand Up @@ -122,12 +122,12 @@ class TestCsrfCrossDomainCookieMiddleware(TestCase):
)
def setUp(self):
super().setUp()
self.middleware = CsrfCrossDomainCookieMiddleware()
self.middleware = CsrfCrossDomainCookieMiddleware(get_response=lambda request: None)

@override_settings(FEATURES={'ENABLE_CROSS_DOMAIN_CSRF_COOKIE': False})
def test_disabled_by_feature_flag(self):
with pytest.raises(MiddlewareNotUsed):
CsrfCrossDomainCookieMiddleware()
CsrfCrossDomainCookieMiddleware(get_response=lambda request: None)

@ddt.data('CROSS_DOMAIN_CSRF_COOKIE_NAME', 'CROSS_DOMAIN_CSRF_COOKIE_DOMAIN')
def test_improperly_configured(self, missing_setting):
Expand All @@ -140,7 +140,7 @@ def test_improperly_configured(self, missing_setting):

with override_settings(**settings):
with pytest.raises(ImproperlyConfigured):
CsrfCrossDomainCookieMiddleware()
CsrfCrossDomainCookieMiddleware(get_response=lambda request: None)

@override_settings(
CROSS_DOMAIN_CSRF_COOKIE_NAME=COOKIE_NAME,
Expand Down
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/dark_lang/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def process_middleware_request(self, cookie_language=UNSET, accept=UNSET):
)

# Process it through the Middleware to ensure the language is available as expected.
assert DarkLangMiddleware().process_request(request) is None
assert DarkLangMiddleware(get_response=lambda request: None).process_request(request) is None
return request

def assertAcceptEquals(self, value, request):
Expand Down
4 changes: 2 additions & 2 deletions openedx/core/djangoapps/geoinfo/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CountryMiddlewareTests(TestCase):
"""
def setUp(self):
super().setUp()
self.country_middleware = CountryMiddleware()
self.session_middleware = SessionMiddleware()
self.country_middleware = CountryMiddleware(get_response=lambda request: None)
self.session_middleware = SessionMiddleware(get_response=lambda request: None)
self.authenticated_user = UserFactory.create()
self.anonymous_user = AnonymousUserFactory.create()
self.request_factory = RequestFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestHeaderControlMiddlewareProcessResponse(TestCase):
"""Test the `header_control` middleware. """
def setUp(self):
super().setUp()
self.middleware = HeaderControlMiddleware()
self.middleware = HeaderControlMiddleware(get_response=lambda request: None)

def test_doesnt_barf_if_not_modifying_anything(self):
fake_request = HttpRequest()
Expand Down
4 changes: 2 additions & 2 deletions openedx/core/djangoapps/lang_pref/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class TestUserPreferenceMiddleware(CacheIsolationTestCase):

def setUp(self):
super().setUp()
self.middleware = LanguagePreferenceMiddleware()
self.session_middleware = SessionMiddleware()
self.middleware = LanguagePreferenceMiddleware(get_response=lambda request: None)
self.session_middleware = SessionMiddleware(get_response=lambda request: None)
self.user = UserFactory.create()
self.anonymous_user = AnonymousUserFactory()
self.request = RequestFactory().get('/somewhere')
Expand Down
Loading

0 comments on commit 7710e60

Please sign in to comment.