def test_cache_control(self): """successful queries against models with caching should set a Cache-Control header.""" def mocked_get(**options): assert options['product'] == settings.DEFAULT_PRODUCT return { 'hits': { 'release': 0.1, 'nightly': 1.0, 'beta': 1.0, 'aurora': 1.0, 'esr': 1.0, } } ProductBuildTypes.implementation().get.side_effect = mocked_get url = reverse('api:model_wrapper', args=('ProductBuildTypes',)) response = self.client.get(url, {'product': settings.DEFAULT_PRODUCT}) eq_(response.status_code, 200) assert response['Cache-Control'] ok_('private' in response['Cache-Control']) cache_seconds = ProductBuildTypes.cache_seconds ok_('max-age={}'.format(cache_seconds) in response['Cache-Control'])
def test_cache_control(self): """successful queries against models with caching should set a Cache-Control header.""" def mocked_get(**options): assert options['product'] == settings.DEFAULT_PRODUCT return { 'hits': { 'release': 0.1, 'nightly': 1.0, 'beta': 1.0, 'aurora': 1.0, 'esr': 1.0, } } ProductBuildTypes.implementation().get.side_effect = mocked_get url = reverse('api:model_wrapper', args=('ProductBuildTypes',)) response = self.client.get(url, {'product': settings.DEFAULT_PRODUCT}) assert response.status_code == 200 assert response['Cache-Control'] assert 'private' in response['Cache-Control'] cache_seconds = ProductBuildTypes.cache_seconds assert 'max-age={}'.format(cache_seconds) in response['Cache-Control']
def test_api_pageview_decorator(self, logger, rpost, aw): """Test when the API is actually used. No fake request object""" # Use this mutable to keep track of executions of the mocked queue queues = [] def mocked_queue(function, data, headers, success_cb, failure_cb): queues.append(data) # Don't need to execute the function because we're only # interested in if this queue function got called. aw().queue.side_effect = mocked_queue # Use this mutable to keep track of executions mocked get. # This helps us be certain the get method really is called. gets = [] def mocked_get(**options): gets.append(options) if options.get('product') == '400': raise BadArgumentError('product') return { 'hits': { 'release': 0.1, 'beta': 1.0, } } ProductBuildTypes.implementation().get = mocked_get url = reverse('api:model_wrapper', args=('ProductBuildTypes',)) with self.settings(GOOGLE_ANALYTICS_ID='XYZ-123'): response = self.client.get(url, {'product': 'WaterWolf'}) eq_(response.status_code, 200) eq_(len(queues), 1) # the mutable assert len(gets) == 1 eq_(queues[0]['dp'], '/api/ProductBuildTypes/') eq_( queues[0]['dl'], 'http://testserver/api/ProductBuildTypes/?product=WaterWolf' ) response = self.client.get(url, {'product': '400'}) assert len(gets) == 2, len(gets) eq_(response.status_code, 400) eq_(len(queues), 2) eq_(queues[1]['dp'], '/api/ProductBuildTypes/') eq_( queues[1]['dl'], 'http://testserver/api/ProductBuildTypes/?product=400' ) response = self.client.get( url, {'product': 'WaterWolf2'}, # different product => no cache HTTP_REFERER='example.com' ) assert len(gets) == 3, len(gets) eq_(response.status_code, 200) eq_(len(queues), 3) response = self.client.get( url, {'product': 'WaterWolf2'}, # different product => no cache HTTP_REFERER='http://example.com/page.html', HTTP_HOST='example.com', ) assert len(gets) == 3, len(gets) eq_(response.status_code, 200) eq_(len(queues), 3) # no increase!
def test_api_pageview_decorator(self, logger, rpost, aw): """Test when the API is actually used. No fake request object""" # Use this mutable to keep track of executions of the mocked queue queues = [] def mocked_queue(function, data, headers, success_cb, failure_cb): queues.append(data) # Don't need to execute the function because we're only # interested in if this queue function got called. aw().queue.side_effect = mocked_queue # Use this mutable to keep track of executions mocked get. # This helps us be certain the get method really is called. gets = [] def mocked_get(**options): gets.append(options) if options.get('product') == '400': raise BadArgumentError('product') return { 'hits': { 'release': 0.1, 'beta': 1.0, } } ProductBuildTypes.implementation().get = mocked_get url = reverse('api:model_wrapper', args=('ProductBuildTypes', )) with self.settings(GOOGLE_ANALYTICS_ID='XYZ-123'): response = self.client.get(url, {'product': 'WaterWolf'}) eq_(response.status_code, 200) eq_(len(queues), 1) # the mutable assert len(gets) == 1 eq_(queues[0]['dp'], '/api/ProductBuildTypes/') eq_(queues[0]['dl'], 'http://testserver/api/ProductBuildTypes/?product=WaterWolf') response = self.client.get(url, {'product': '400'}) assert len(gets) == 2, len(gets) eq_(response.status_code, 400) eq_(len(queues), 2) eq_(queues[1]['dp'], '/api/ProductBuildTypes/') eq_(queues[1]['dl'], 'http://testserver/api/ProductBuildTypes/?product=400') response = self.client.get( url, {'product': 'WaterWolf2'}, # different product => no cache HTTP_REFERER='example.com') assert len(gets) == 3, len(gets) eq_(response.status_code, 200) eq_(len(queues), 3) response = self.client.get( url, {'product': 'WaterWolf2'}, # different product => no cache HTTP_REFERER='http://example.com/page.html', HTTP_HOST='example.com', ) assert len(gets) == 3, len(gets) eq_(response.status_code, 200) eq_(len(queues), 3) # no increase!