From bc45f82f5c07a3a1f7244eeacaf34b9644832918 Mon Sep 17 00:00:00 2001 From: Clinton Blackburn Date: Tue, 4 Apr 2017 17:24:23 -0400 Subject: [PATCH] Updated program filter for learner dashboard Learners will now see both active and retired programs on their dashboards. ECOM-7625 --- .../djangoapps/catalog/tests/test_utils.py | 47 +++++++++++++------ openedx/core/djangoapps/catalog/utils.py | 10 +++- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/openedx/core/djangoapps/catalog/tests/test_utils.py b/openedx/core/djangoapps/catalog/tests/test_utils.py index eb1c9790ef..7e674bf9ac 100644 --- a/openedx/core/djangoapps/catalog/tests/test_utils.py +++ b/openedx/core/djangoapps/catalog/tests/test_utils.py @@ -1,11 +1,12 @@ """Tests covering utilities for integrating with the catalog service.""" # pylint: disable=missing-docstring -import uuid import copy +import uuid +import mock from django.contrib.auth import get_user_model from django.test import TestCase -import mock +from waffle.models import Switch from openedx.core.djangoapps.catalog.models import CatalogIntegration from openedx.core.djangoapps.catalog.tests.factories import CourseRunFactory, ProgramFactory, ProgramTypeFactory @@ -19,7 +20,6 @@ from openedx.core.djangoapps.catalog.utils import ( from openedx.core.djangolib.testing.utils import skip_unless_lms from student.tests.factories import UserFactory - UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils' User = get_user_model() # pylint: disable=invalid-name @@ -37,7 +37,7 @@ class TestGetPrograms(CatalogIntegrationMixin, TestCase): UserFactory(username=self.catalog_integration.service_username) - def assert_contract(self, call_args, program_uuid=None, types=None): # pylint: disable=redefined-builtin + def assert_contract(self, call_args, program_uuid=None, types=None, expected_querystring=None): """Verify that API data retrieval utility is used correctly.""" args, kwargs = call_args @@ -58,20 +58,24 @@ class TestGetPrograms(CatalogIntegrationMixin, TestCase): self.assertEqual(kwargs['api']._store['base_url'], self.catalog_integration.internal_api_url) # pylint: disable=protected-access - querystring = { - 'marketable': 1, - 'exclude_utm': 1, - } - if program_uuid: - querystring['use_full_course_serializer'] = 1 - if types: - querystring['types'] = types_param + if expected_querystring: + querystring = expected_querystring + else: + querystring = { + 'marketable': 1, + 'exclude_utm': 1, + } + if program_uuid: + querystring['use_full_course_serializer'] = 1 + if types: + querystring['types'] = types_param + self.assertEqual(kwargs['querystring'], querystring) return args, kwargs def test_get_programs(self, mock_get_edx_api_data): - programs = [ProgramFactory() for __ in range(3)] + programs = ProgramFactory.create_batch(3) mock_get_edx_api_data.return_value = programs data = get_programs() @@ -79,6 +83,21 @@ class TestGetPrograms(CatalogIntegrationMixin, TestCase): self.assert_contract(mock_get_edx_api_data.call_args) self.assertEqual(data, programs) + def test_get_programs_with_status_filtering(self, mock_get_edx_api_data): + """ The function should request active and retired programs when the Waffle switch is enabled. """ + programs = ProgramFactory.create_batch(3) + mock_get_edx_api_data.return_value = programs + + Switch.objects.get_or_create(name='display_retired_programs_on_learner_dashboard', defaults={'active': True}) + data = get_programs() + + expected_querystring = { + 'exclude_utm': 1, + 'status': ('active', 'retired',) + } + self.assert_contract(mock_get_edx_api_data.call_args, expected_querystring=expected_querystring) + self.assertEqual(data, programs) + def test_get_one_program(self, mock_get_edx_api_data): program = ProgramFactory() mock_get_edx_api_data.return_value = program @@ -240,7 +259,7 @@ class TestGetCourseRuns(CatalogIntegrationMixin, TestCase): """ Test retrieval of course runs. """ - catalog_course_runs = [CourseRunFactory() for __ in xrange(10)] + catalog_course_runs = CourseRunFactory.create_batch(10) mock_get_edx_api_data.return_value = catalog_course_runs data = get_course_runs() diff --git a/openedx/core/djangoapps/catalog/utils.py b/openedx/core/djangoapps/catalog/utils.py index e134f8b401..d915c25188 100644 --- a/openedx/core/djangoapps/catalog/utils.py +++ b/openedx/core/djangoapps/catalog/utils.py @@ -2,6 +2,7 @@ import copy import logging +import waffle from django.conf import settings from django.contrib.auth import get_user_model from edx_rest_api_client.client import EdxRestApiClient @@ -54,11 +55,18 @@ def get_programs(uuid=None, types=None): # pylint: disable=redefined-builtin ) querystring = { - 'marketable': 1, 'exclude_utm': 1, } + + # TODO ECOM-7650: Remove this after https://github.com/edx/course-discovery/pull/805 is merged and released. + if waffle.switch_is_active('display_retired_programs_on_learner_dashboard'): + querystring['status'] = ('active', 'retired',) + else: + querystring['marketable'] = 1 + if uuid: querystring['use_full_course_serializer'] = 1 + if types_param: querystring['types'] = types_param