Skip to content

Commit

Permalink
fix: add setting for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syedsajjadkazmii committed Jan 26, 2024
1 parent 093e8ed commit eaeaa12
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cms/envs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@

FEATURES['DISABLE_SET_JWT_COOKIES_FOR_TESTS'] = True

# Skip setting user emails in session for specific unit tests. This is necessary
# because certain views raise Http404() exceptions, and since Django wraps all
# tests in transactions, modifying the session and attempting to save it after
# a transaction failure due to Http404() can result in test failures. This is
# because errors within a transaction affect subsequent DB operations.
FEATURES['DISABLE_SET_EMAIL_IN_SESSION_FOR_TESTS'] = False

FEATURES['ENABLE_SERVICE_STATUS'] = True

# Toggles embargo on for testing
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/course_api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def test_too_many_courses(self):
self.setup_user(self.audit_user)

# These query counts were found empirically
query_counts = [50, 46, 46, 46, 46, 46, 46, 46, 46, 46, 16]
query_counts = [53, 46, 46, 46, 46, 46, 46, 46, 46, 46, 16]
ordered_course_ids = sorted([str(cid) for cid in (course_ids + [c.id for c in self.courses])])

self.clear_caches()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_get_masqueraded_user(self):
self.update_masquerade(username=self.user.username)
assert self.client.get(self.url).data['username'] == self.user.username

@patch.dict("django.conf.settings.FEATURES", {"DISABLE_SET_EMAIL_IN_SESSION_FOR_TESTS": True})
def test_get_unknown_course(self):
url = reverse('course-home:course-metadata', args=['course-v1:unknown+course+2T2020'])
# Django TestCase wraps every test in a transaction, so we must specifically wrap this when we expect an error
Expand Down
9 changes: 8 additions & 1 deletion openedx/core/djangoapps/safe_sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,14 @@ def process_response(self, request, response):
# .. custom_attribute_description: Indicates that user's email was not
# stored in the user's session.
set_custom_attribute('session_with_no_email_found', True)
request.session['email'] = request.user.email

# Skip setting user emails in session for specific unit tests. This is necessary
# because certain views raise Http404() exceptions, and since Django wraps all
# tests in transactions, modifying the session and attempting to save it after
# a transaction failure due to Http404() can result in test failures. This is
# because errors within a transaction affect subsequent DB operations.
if not settings.FEATURES.get('DISABLE_SET_EMAIL_IN_SESSION_FOR_TESTS', False):
request.session['email'] = request.user.email

if request_cache.get_cached_response('email_change_requested').is_found:
# Update the JWT cookies with new user email
Expand Down

0 comments on commit eaeaa12

Please sign in to comment.