Skip to content

Commit

Permalink
refactor: CookieMonitoringMiddleware moved to edx-django-utils
Browse files Browse the repository at this point in the history
The CookieMonitoringMiddleware and its related script
moved to edx-django-utils.

ARCHBOM-2054
  • Loading branch information
robrap committed Mar 17, 2022
1 parent 7a2eee6 commit 9fa7980
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 516 deletions.
11 changes: 5 additions & 6 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,16 +752,15 @@

MIDDLEWARE = [
'openedx.core.lib.x_forwarded_for.middleware.XForwardedForMiddleware',

'crum.CurrentRequestUserMiddleware',

'edx_django_utils.monitoring.DeploymentMonitoringMiddleware',
# A newer and safer request cache.
# Resets the request cache.
'edx_django_utils.cache.middleware.RequestCacheMiddleware',
'edx_django_utils.monitoring.MonitoringMemoryMiddleware',

# Cookie monitoring
'openedx.core.lib.request_utils.CookieMonitoringMiddleware',
# Various monitoring middleware
'edx_django_utils.monitoring.CookieMonitoringMiddleware',
'edx_django_utils.monitoring.DeploymentMonitoringMiddleware',
'edx_django_utils.monitoring.MonitoringMemoryMiddleware',

# Before anything that looks at cookies, especially the session middleware
'openedx.core.djangoapps.cookie_metadata.middleware.CookieNameChange',
Expand Down
212 changes: 0 additions & 212 deletions lms/djangoapps/monitoring/scripts/process_cookie_monitoring_logs.py

This file was deleted.

15 changes: 6 additions & 9 deletions lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2057,25 +2057,22 @@ def _make_locale_paths(settings): # pylint: disable=missing-function-docstring

MIDDLEWARE = [
'openedx.core.lib.x_forwarded_for.middleware.XForwardedForMiddleware',

'crum.CurrentRequestUserMiddleware',

'edx_django_utils.monitoring.DeploymentMonitoringMiddleware',
# A newer and safer request cache.
# Resets the request cache.
'edx_django_utils.cache.middleware.RequestCacheMiddleware',

# Generate code ownership attributes. Keep this immediately after RequestCacheMiddleware.
# Various monitoring middleware
'edx_django_utils.monitoring.CachedCustomMonitoringMiddleware',
'edx_django_utils.monitoring.CodeOwnerMonitoringMiddleware',
'edx_django_utils.monitoring.CookieMonitoringMiddleware',
'edx_django_utils.monitoring.DeploymentMonitoringMiddleware',

# Before anything that looks at cookies, especially the session middleware
'openedx.core.djangoapps.cookie_metadata.middleware.CookieNameChange',

# Monitoring and logging middleware
# Monitoring and logging for expected and ignored errors
'openedx.core.lib.request_utils.ExpectedErrorMiddleware',
'edx_django_utils.monitoring.CachedCustomMonitoringMiddleware',

# Cookie monitoring
'openedx.core.lib.request_utils.CookieMonitoringMiddleware',

'lms.djangoapps.mobile_api.middleware.AppVersionUpgrade',
'openedx.core.djangoapps.header_control.middleware.HeaderControlMiddleware',
Expand Down
12 changes: 6 additions & 6 deletions openedx/core/djangoapps/user_api/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from openedx.core.djangoapps.user_api.models import UserPreference
from openedx.core.djangoapps.user_api.preferences.api import set_user_preference
from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, FilteredQueryCountMixin, skip_unless_lms
from openedx.features.name_affirmation_api.utils import get_name_affirmation_service

from .. import ALL_USERS_VISIBILITY, CUSTOM_VISIBILITY, PRIVATE_VISIBILITY
Expand Down Expand Up @@ -195,7 +195,7 @@ def _verify_profile_image_data(self, data, has_profile_image):

@ddt.ddt
@skip_unless_lms
class TestOwnUsernameAPI(CacheIsolationTestCase, UserAPITestCase):
class TestOwnUsernameAPI(FilteredQueryCountMixin, CacheIsolationTestCase, UserAPITestCase):
"""
Unit tests for the Accounts API.
"""
Expand Down Expand Up @@ -253,13 +253,13 @@ def test_get_username_not_logged_in(self):
{'full': 50, 'small': 10},
clear=True
)
class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
class TestAccountsAPI(FilteredQueryCountMixin, CacheIsolationTestCase, UserAPITestCase):
"""
Unit tests for the Accounts API.
"""

ENABLED_CACHES = ['default']
TOTAL_QUERY_COUNT = 25
TOTAL_QUERY_COUNT = 24
FULL_RESPONSE_FIELD_COUNT = 30

def setUp(self):
Expand Down Expand Up @@ -700,7 +700,7 @@ def verify_get_own_information(queries):
assert data['accomplishments_shared'] is False

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

# Now make sure that the user can get the same information, even if not active
self.user.is_active = False
Expand All @@ -720,7 +720,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(23), table_ignorelist=WAFFLE_TABLES):
with self.assertNumQueries(self._get_num_queries(22), 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
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/waffle_utils/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Test utilities for waffle utilities.
"""

# Can be used with FilteredQueryCountMixin.assertNumQueries() to blacklist
# Can be used with FilteredQueryCountMixin.assertNumQueries() to ignore
# waffle tables. For example:
# QUERY_COUNT_TABLE_IGNORELIST = WAFFLE_TABLES
# with self.assertNumQueries(6, table_ignorelist=QUERY_COUNT_TABLE_IGNORELIST):
Expand Down
6 changes: 4 additions & 2 deletions openedx/core/djangolib/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __init__(self, test_case, num, connection, table_ignorelist=None):
def __exit__(self, exc_type, exc_value, traceback):
def is_unfiltered_query(query):
"""
Returns True if the query does not contain a blacklisted table, and
Returns True if the query does not contain a ignorelisted table, and
False otherwise.
Note: This is a simple naive implementation that makes no attempt
Expand Down Expand Up @@ -201,7 +201,7 @@ def is_unfiltered_query(query):
class FilteredQueryCountMixin:
"""
Mixin to add to any subclass of Django's TestCase that replaces
assertNumQueries with one that accepts a blacklist of tables to filter out
assertNumQueries with one that accepts a ignorelist of tables to filter out
of the count.
"""
def assertNumQueries(self, num, func=None, table_ignorelist=None, *args, **kwargs): # lint-amnesty, pylint: disable=keyword-arg-before-vararg
Expand All @@ -210,6 +210,8 @@ def assertNumQueries(self, num, func=None, table_ignorelist=None, *args, **kwarg
the addition of the following argument:
table_ignorelist (List): A list of table names to filter out of the
set of queries that get counted.
Important: TestCase must include FilteredQueryCountMixin for this to work.
"""
using = kwargs.pop("using", DEFAULT_DB_ALIAS)
conn = connections[using]
Expand Down
Loading

0 comments on commit 9fa7980

Please sign in to comment.