def test_no_response_doesnt_get_cached(self):
        """
        Response doesn't get cached when empty.
        """
        uuid = str(self.enterprise_customer.uuid)
        api_resource_name = 'enterprise-customer'
        cache_key = get_cache_key(
            resource=api_resource_name,
            querystring={},
            traverse_pagination=False,
            resource_id=uuid,
        )

        cached_enterprise_api_response = cache.get(cache_key)
        assert cached_enterprise_api_response is None

        self.mock_empty_response('enterprise-customer-courses', uuid)
        client = enterprise_api.EnterpriseApiClient(self.user)
        response = client._load_data(  # pylint: disable=protected-access
            resource=api_resource_name,
            detail_resource='courses',
            resource_id=uuid,
        )
        assert not response

        # The empty response is not cached.
        cached_api_response = cache.get(cache_key)
        assert not cached_api_response
    def test_get_content_metadata_with_enterprise_catalogs(self):
        """
        Verify that the client method `get_content_metadata` works as expected.
        """
        EnterpriseCustomerCatalogFactory(
            enterprise_customer=self.enterprise_customer,
        )
        uuid = str(self.enterprise_customer.uuid)
        course_run_ids = ['course-v1:edX+DemoX+Demo_Course_1', 'course-v1:edX+DemoX+Demo_Course_2']

        self.mock_ent_courses_api_with_pagination(
            enterprise_uuid=uuid,
            course_run_ids=course_run_ids
        )

        enterprise_catalog_uuid = str(self.enterprise_customer.enterprise_customer_catalogs.first().uuid)
        self.mock_enterprise_customer_catalogs(enterprise_catalog_uuid)

        api_resource_name = 'enterprise-customer'
        cache_key = get_cache_key(
            resource=api_resource_name,
            querystring={},
            resource_id=uuid,
            traverse_pagination=False,
        )
        cached_enterprise_api_response = cache.get(cache_key)
        self.assertIsNone(cached_enterprise_api_response)

        # Verify that by default enterprise client fetches all the course runs associated with the catalog.
        client = enterprise_api.EnterpriseApiClient(self.user)
        course_runs = client.get_content_metadata(self.enterprise_customer)
        assert len(course_runs) == 5
    def test_get_content_metadata(self):
        """
        Verify that the client method `get_content_metadata` works as expected.
        """
        uuid = str(self.enterprise_customer.uuid)
        course_run_ids = ['course-v1:edX+DemoX+Demo_Course_1', 'course-v1:edX+DemoX+Demo_Course_2']
        self.mock_ent_courses_api_with_pagination(
            enterprise_uuid=uuid,
            course_run_ids=course_run_ids
        )

        api_resource_name = 'enterprise-customer'
        cache_key = get_cache_key(
            resource=api_resource_name,
            querystring={},
            resource_id=uuid,
            traverse_pagination=False,
        )
        cached_enterprise_api_response = cache.get(cache_key)
        self.assertIsNone(cached_enterprise_api_response)

        # Verify that by default enterprise client fetches all the course runs associated with the catalog.
        client = enterprise_api.EnterpriseApiClient(self.user)
        api_response = client.get_content_metadata(self.enterprise_customer)
        self._assert_enterprise_courses_api_response(
            ['course-v1:edX+DemoX+Demo_Course_1', 'course-v1:edX+DemoX+Demo_Course_2'],
            api_response,
            expected_count=2
        )
        # Verify the enterprise API was hit twice
        self._assert_num_requests(2)
Exemple #4
0
    def test_no_response_doesnt_get_cached(self):
        """
        Response doesn't get cached when empty.
        """
        EnterpriseCustomerCatalogFactory(
            enterprise_customer=self.enterprise_customer, )
        enterprise_catalog_uuid = str(
            self.enterprise_customer.enterprise_customer_catalogs.first().uuid)

        api_resource_name = 'enterprise_catalogs'
        cache_key = get_cache_key(
            resource=api_resource_name,
            querystring={},
            traverse_pagination=False,
            resource_id=enterprise_catalog_uuid,
        )

        cached_enterprise_api_response = cache.get(cache_key)
        assert cached_enterprise_api_response is None

        self.mock_empty_response('enterprise-catalogs-detail',
                                 enterprise_catalog_uuid)
        client = enterprise_api.EnterpriseApiClient(self.user)
        response = client._load_data(  # pylint: disable=protected-access
            resource=api_resource_name,
            resource_id=enterprise_catalog_uuid,
        )
        assert not response

        # The empty response is not cached.
        cached_api_response = cache.get(cache_key)
        assert not cached_api_response
Exemple #5
0
    def test_get_content_metadata_with_enterprise_catalog_set_to_none(self):
        """
        Verify that the client method `get_content_metadata` returns courses from
        associated EnterpriseCustomerCatalog objects only if EnterpriseCustomer.catalog is set to None.
        """
        EnterpriseCustomerCatalogFactory(
            enterprise_customer=self.enterprise_customer, )

        enterprise_catalog_uuid = str(
            self.enterprise_customer.enterprise_customer_catalogs.first().uuid)
        self.mock_enterprise_customer_catalogs(enterprise_catalog_uuid)

        api_resource_name = 'enterprise-customer'
        cache_key = get_cache_key(
            resource=api_resource_name,
            querystring={},
            resource_id=str(self.enterprise_customer.uuid),
            traverse_pagination=False,
        )
        cached_enterprise_api_response = cache.get(cache_key)
        self.assertIsNone(cached_enterprise_api_response)

        # Verify that by default enterprise client fetches all the course runs associated with the enterprise catalog.
        client = enterprise_api.EnterpriseApiClient(self.user)
        course_runs = client.get_content_metadata(self.enterprise_customer)
        assert len(course_runs) == 3
 def test_skip_request_if_response_cached(self):
     """
     We skip the request portion of the API's logic if the response is already cached.
     """
     cache_key = get_cache_key(
         resource='resource',
         querystring={},
         traverse_pagination=False,
         resource_id=None,
     )
     cache_value = {'fake': 'response'}
     cache.set(cache_key, cache_value, settings.ENTERPRISE_API_CACHE_TIMEOUT)
     client = enterprise_api.EnterpriseApiClient(self.user)
     response = client._load_data('resource')  # pylint: disable=protected-access
     assert response == cache_value
Exemple #7
0
    def test_get_content_metadata_with_catalogs_to_transmit(
            self, mock_load_data):
        """
        Verify that the client method `get_content_metadata` returns courses
        from catalogs passed in 'catalogs_to_transmit' parameter.
        """
        enterprise_catalog = EnterpriseCustomerCatalogFactory(
            enterprise_customer=self.enterprise_customer)
        client = enterprise_api.EnterpriseApiClient(self.user)
        # get_content_metadata will transmit the 'enterprise_customer' catalogs
        client.get_content_metadata(self.enterprise_customer)
        assert mock_load_data.called
        assert mock_load_data.call_args[1]['resource_id'] == str(
            enterprise_catalog.uuid)

        other_catalog = EnterpriseCustomerCatalogFactory()
        # get_content_metadata will transmit the catalogs which are being passed in 'catalogs_to_transmit'
        client.get_content_metadata(
            self.enterprise_customer,
            enterprise_catalogs=EnterpriseCustomerCatalog.objects.filter(
                uuid=other_catalog.uuid))
        assert mock_load_data.called
        assert mock_load_data.call_args[1]['resource_id'] == str(
            other_catalog.uuid)