Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions openedx/core/djangoapps/user_api/accounts/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ddt
from django.test import TestCase
from django.test.utils import override_settings
from mock import patch
from mock import patch, Mock

from completion import models
from completion.test_utils import CompletionWaffleTestMixin
Expand All @@ -16,6 +16,8 @@
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory

from openedx.core.djangolib.testing.utils import FilteredQueryCountMixin

from ..utils import format_social_link, validate_social_link, generate_password


Expand Down Expand Up @@ -65,7 +67,7 @@ def test_social_link_input(self, platform_name, link_input, formatted_link_expec


@ddt.ddt
class CompletionUtilsTestCase(SharedModuleStoreTestCase, CompletionWaffleTestMixin, TestCase):
class CompletionUtilsTestCase(SharedModuleStoreTestCase, FilteredQueryCountMixin, CompletionWaffleTestMixin, TestCase):
"""
Test completion utility functions
"""
Expand Down Expand Up @@ -113,21 +115,35 @@ def submit_faux_completions(self):
)

@override_settings(LMS_ROOT_URL='test_url:9999')
@ddt.data(True, False)
def test_retrieve_last_sitewide_block_completed(self, use_username):
@ddt.unpack
@ddt.data({
'use_username': True,
'engaged_queries': 3,
'cruft_queries': 2,
}, {
'use_username': False,
'engaged_queries': 2,
'cruft_queries': 1,
})
def test_retrieve_last_sitewide_block_completed(self, use_username, engaged_queries, cruft_queries):
"""
Test that the method returns a URL for the "last completed" block
when sending a user object
"""
block_url = retrieve_last_sitewide_block_completed(
self.engaged_user.username if use_username else self.engaged_user
)
empty_block_url = retrieve_last_sitewide_block_completed(
self.cruft_user.username if use_username else self.cruft_user
)
with self.assertNumQueries(engaged_queries):
block_url = retrieve_last_sitewide_block_completed(
self.engaged_user.username if use_username else self.engaged_user
)

with self.assertNumQueries(cruft_queries):
empty_block_url = retrieve_last_sitewide_block_completed(
self.cruft_user.username if use_username else self.cruft_user
)

self.assertEqual(
block_url,
u'test_url:9999/courses/{org}/{course}/{run}/jump_to/i4x://{org}/{course}/vertical/{vertical_id}'.format(
# Appsembler: We're omitting the domain name because our users are always on a single site.
u'/courses/{org}/{course}/{run}/jump_to/i4x://{org}/{course}/vertical/{vertical_id}'.format(
org=self.course.location.course_key.org,
course=self.course.location.course_key.course,
run=self.course.location.course_key.run,
Expand All @@ -136,6 +152,33 @@ def test_retrieve_last_sitewide_block_completed(self, use_username):
)
self.assertEqual(empty_block_url, None)

@override_settings(LMS_ROOT_URL='test_url:9999')
def test_retrieve_last_sitewide_block_performance_with_site(self):
"""
Ensures that the `SiteConfiguration.objects.all()` is not called when a specific site was found.
"""
expected_queries_with_site = 1
with self.assertNumQueries(expected_queries_with_site):
function_path = 'openedx.core.djangoapps.user_api.accounts.utils.get_config_value_from_site_or_settings'
with patch(function_path, Mock(return_value=self.course.location.course_key.org)):
assert retrieve_last_sitewide_block_completed(self.engaged_user).startswith('/')

with self.assertNumQueries(expected_queries_with_site):
assert retrieve_last_sitewide_block_completed(self.cruft_user) is None

@override_settings(LMS_ROOT_URL='test_url:9999')
def test_retrieve_last_sitewide_block_performance_multi_course(self):
"""
Ensures that the `SiteConfiguration.objects.all()` is called only once when no site was found.
"""
self.course = self.create_test_course() # create another course.
self.submit_faux_completions() # Test submission for another course
expected_queries_mutli_course_site_wide = 2
with self.assertNumQueries(expected_queries_mutli_course_site_wide):
function_path = 'openedx.core.djangoapps.user_api.accounts.utils.get_config_value_from_site_or_settings'
with patch(function_path, Mock(return_value=None)): # Pretend that no sites are matching the courses
assert retrieve_last_sitewide_block_completed(self.engaged_user).startswith('/')


class GeneratePasswordTest(TestCase):
"""Tests formation of randomly generated passwords."""
Expand Down
19 changes: 9 additions & 10 deletions openedx/core/djangoapps/user_api/accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,11 @@ def retrieve_last_sitewide_block_completed(username):
userobj = username
latest_completions_by_course = BlockCompletion.latest_blocks_completed_all_courses(userobj)

known_site_configs = [
other_site_config.get_value('course_org_filter') for other_site_config in SiteConfiguration.objects.all()
if other_site_config.get_value('course_org_filter')
]

current_site_configuration = get_config_value_from_site_or_settings(
name='course_org_filter',
site=get_current_site()
)
known_site_configs = None

# courses.edx.org has no 'course_org_filter'
# however the courses within DO, but those entries are not found in
Expand All @@ -143,6 +139,12 @@ def retrieve_last_sitewide_block_completed(username):
for course, [modified_date, block_key] in latest_completions_by_course.items():
if not current_site_configuration:
# This is a edx.org
if known_site_configs is None: # Avoid running the query twice
known_site_configs = [
other_site_config.get_value('course_org_filter')
for other_site_config in SiteConfiguration.objects.all()
if other_site_config.get_value('course_org_filter')
]
if course.org in known_site_configs:
continue
if not latest_date or modified_date > latest_date:
Expand All @@ -163,18 +165,15 @@ def retrieve_last_sitewide_block_completed(username):
if not candidate_course:
return

lms_root = SiteConfiguration.get_value_for_org(candidate_course.org, "LMS_ROOT_URL", settings.LMS_ROOT_URL)

try:
item = modulestore().get_item(candidate_block_key, depth=1)
except ItemNotFoundError:
item = None

if not (lms_root and item):
if not item:
return

return u"{lms_root}/courses/{course_key}/jump_to/{location}".format(
lms_root=lms_root,
return u"/courses/{course_key}/jump_to/{location}".format(
course_key=text_type(item.location.course_key),
location=text_type(item.location),
)
Expand Down