Skip to content

Commit

Permalink
test: add UserLanguagePreferenceMiddlewere tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed May 21, 2024
1 parent d2607eb commit cdff4be
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
16 changes: 16 additions & 0 deletions eox_core/edxapp_wrapper/backends/lang_pref_middleware_p_v1_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
LanguagePreferenceMiddleware Backend.
"""

def get_language_preference_middleware():
"""Backend to get the LanguagePreferenceMiddleware from openedx."""
class LanguagePreferenceMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
# Simulate the behavior of LanguagePreferenceMiddleware
# For example, set a language preference in the request
request.LANGUAGE_CODE = 'en'
return self.get_response(request)
return LanguagePreferenceMiddleware
2 changes: 1 addition & 1 deletion eox_core/edxapp_wrapper/language_preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def get_language_preference_middleware(*args, **kwargs):
""" Get LanguagePreferenceMiddleware. """
backend_function = settings.EOX_CORE_LANG_PREF_BACKEND
backend = import_module(backend_function)
return backend.get_language_preference_middleware(*args, **kwargs)
return backend.get_language_preference_middleware(*args, **kwargs)
1 change: 1 addition & 0 deletions eox_core/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def plugin_settings(settings): # pylint: disable=function-redefined
settings.EOX_CORE_USER_UPDATE_SAFE_FIELDS = ["is_active", "password", "fullname"]
settings.EOX_CORE_BEARER_AUTHENTICATION = 'eox_core.edxapp_wrapper.backends.bearer_authentication_j_v1_test'
settings.EOX_CORE_THIRD_PARTY_AUTH_BACKEND = 'eox_core.edxapp_wrapper.backends.third_party_auth_l_v1'
settings.EOX_CORE_LANG_PREF_BACKEND = 'eox_core.edxapp_wrapper.backends.lang_pref_middleware_p_v1_test'


SETTINGS = SettingsClass()
Expand Down
39 changes: 38 additions & 1 deletion eox_core/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.test import RequestFactory, TestCase
from django.conf import settings

from eox_core.middleware import PathRedirectionMiddleware, RedirectionsMiddleware
from eox_core.middleware import PathRedirectionMiddleware, RedirectionsMiddleware, UserLanguagePreferenceMiddleware
from eox_core.models import Redirection


Expand Down Expand Up @@ -165,3 +166,39 @@ def test_redirection(self, redirection_get_mock):
result = self.middleware_instance.process_request(request)

self.assertIsNotNone(result)

class UserLanguagePreferenceMiddlewareTestCase(TestCase):
"""
Test the UserLanguagePreferenceMiddleware.
"""
def setUp(self):
self.factory = RequestFactory()
self.middleware = UserLanguagePreferenceMiddleware(get_response=lambda req: None)

def test_process_request_with_language_cookie(self):
"""
Test if the language cookie is set correctly in the request.
"""
language_code = 'fr'
request = self.factory.get('/')
request.META['HTTP_COOKIE'] = f'{settings.LANGUAGE_COOKIE_NAME}={language_code}'

# Process the request through the middleware
response = self.middleware(request)

# Check if the language cookie was set correctly in the request
self.assertIn(settings.LANGUAGE_COOKIE_NAME, request.COOKIES)
self.assertEqual(request.COOKIES[settings.LANGUAGE_COOKIE_NAME], language_code)

def test_process_request_without_language_cookie(self):
"""
Test if the language cookie is not set in the request.
"""

request = self.factory.get('/')

# Process the request through the middleware
response = self.middleware(request)

# Check that the language cookie is not set in the request
self.assertNotIn(settings.LANGUAGE_COOKIE_NAME, request.COOKIES)

0 comments on commit cdff4be

Please sign in to comment.