Skip to content

Commit

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

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
6 changes: 3 additions & 3 deletions lms/djangoapps/certificates/apis/v0/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_no_certificate(self):
def test_query_counts(self):
# Test student with no certificates
student_no_cert = UserFactory.create(password=self.user_password)
with self.assertNumQueries(17, table_ignorelist=WAFFLE_TABLES):
with self.assertNumQueries(21, table_ignorelist=WAFFLE_TABLES):
resp = self.get_response(
AuthType.jwt,
requesting_user=self.global_staff,
Expand All @@ -319,7 +319,7 @@ def test_query_counts(self):
assert len(resp.data) == 0

# Test student with 1 certificate
with self.assertNumQueries(12, table_ignorelist=WAFFLE_TABLES):
with self.assertNumQueries(13, table_ignorelist=WAFFLE_TABLES):
resp = self.get_response(
AuthType.jwt,
requesting_user=self.global_staff,
Expand Down Expand Up @@ -359,7 +359,7 @@ def test_query_counts(self):
download_url='www.google.com',
grade="0.88",
)
with self.assertNumQueries(12, table_ignorelist=WAFFLE_TABLES):
with self.assertNumQueries(13, table_ignorelist=WAFFLE_TABLES):
resp = self.get_response(
AuthType.jwt,
requesting_user=self.global_staff,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ 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):
self.client.logout()
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
with transaction.atomic():
Expand Down
11 changes: 2 additions & 9 deletions openedx/core/djangoapps/safe_sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,9 @@ def process_response(self, request, response):
if request.session.get('email', None) is None:
# .. custom_attribute_name: session_with_no_email_found
# .. custom_attribute_description: Indicates that user's email was not
# stored in the user's session.
# yet stored in the user's session.
set_custom_attribute('session_with_no_email_found', 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.
if not settings.FEATURES.get('DISABLE_SET_EMAIL_IN_SESSION_FOR_TESTS', False):
request.session['email'] = request.user.email
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
10 changes: 5 additions & 5 deletions openedx/core/djangoapps/user_api/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_get_username(self):
Test that a client (logged in) can get her own username.
"""
self.client.login(username=self.user.username, password=TEST_PASSWORD)
self._verify_get_own_username(16)
self._verify_get_own_username(19)

def test_get_username_inactive(self):
"""
Expand All @@ -242,7 +242,7 @@ def test_get_username_inactive(self):
self.client.login(username=self.user.username, password=TEST_PASSWORD)
self.user.is_active = False
self.user.save()
self._verify_get_own_username(16)
self._verify_get_own_username(19)

def test_get_username_not_logged_in(self):
"""
Expand Down Expand Up @@ -358,7 +358,7 @@ class TestAccountsAPI(FilteredQueryCountMixin, CacheIsolationTestCase, UserAPITe
"""

ENABLED_CACHES = ['default']
TOTAL_QUERY_COUNT = 24
TOTAL_QUERY_COUNT = 27
FULL_RESPONSE_FIELD_COUNT = 29

def setUp(self):
Expand Down Expand Up @@ -811,7 +811,7 @@ def verify_get_own_information(queries):
assert data['time_zone'] is None

self.client.login(username=self.user.username, password=TEST_PASSWORD)
verify_get_own_information(self._get_num_queries(22))
verify_get_own_information(self._get_num_queries(25))

# Now make sure that the user can get the same information, even if not active
self.user.is_active = False
Expand All @@ -831,7 +831,7 @@ def test_get_account_empty_string(self):
legacy_profile.save()

self.client.login(username=self.user.username, password=TEST_PASSWORD)
with self.assertNumQueries(self._get_num_queries(22), table_ignorelist=WAFFLE_TABLES):
with self.assertNumQueries(self._get_num_queries(25), table_ignorelist=WAFFLE_TABLES):
response = self.send_get(self.client)
for empty_field in ("level_of_education", "gender", "country", "state", "bio",):
assert response.data[empty_field] is None
Expand Down

0 comments on commit 14e52bd

Please sign in to comment.