def setUp(self): self.resource = SearchResource() self.factory = RequestFactory() self.profile = UserProfile.objects.get(pk=2519) self.user = self.profile.user
class TestSearchResource(TestCase): fixtures = fixture('user_2519', 'webapp_337141') def setUp(self): self.resource = SearchResource() self.factory = RequestFactory() self.profile = UserProfile.objects.get(pk=2519) self.user = self.profile.user def region_for(self, region): req = self.factory.get('/', ({} if region is None else { 'region': region })) req.API = True req.LANG = '' req.user = self.user req.amo_user = self.profile RegionMiddleware().process_request(req) ACLMiddleware().process_request(req) return self.resource.get_region(req) def give_permission(self): self.grant_permission(self.profile, 'Regions:BypassFilters') def make_curator(self): collection = Collection.objects.create( collection_type=COLLECTIONS_TYPE_BASIC) collection.curators.add(self.profile) @patch('mkt.regions.middleware.RegionMiddleware.region_from_request') def test_get_region_all(self, mock_request_region): self.give_permission() geoip_fallback = regions.PE # Different than the default (worldwide). mock_request_region.return_value = geoip_fallback.slug # Test none-ish values (should return None, i.e. no region). eq_(self.region_for('None'), None) # Test string values (should return region with that slug). eq_(self.region_for('worldwide'), regions.WORLDWIDE) eq_(self.region_for('us'), regions.US) # Test fallback to request.REGION (should return GeoIP region if region # isn't specified or is specified and empty). eq_(self.region_for(None), geoip_fallback) eq_(self.region_for(''), geoip_fallback) # Test fallback to worldwide (e.g. if GeoIP fails). with patch('mkt.regions.middleware.RegionMiddleware.' 'process_request') as mock_process_request: eq_(self.region_for(None), regions.WORLDWIDE) ok_(mock_process_request.called) # Test invalid value (should raise exception). with self.assertRaises(ImmediateHttpResponse): self.region_for('cvanland') # Scary place def test_get_region_permission(self): self.give_permission() eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US) def test_collection_curator(self): self.make_curator() eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US) def test_no_permission_not_curator(self): with self.assertRaises(ImmediateHttpResponse): eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US)
from tastypie.api import Api from tastypie_services.services import (ErrorResource, SettingsResource) from mkt.api.base import handle_500 from mkt.api.resources import (AppResource, CategoryResource, PreviewResource, StatusResource, ValidationResource) from mkt.ratings.resources import RatingResource from mkt.search.api import SearchResource, WithCreaturedResource api = Api(api_name='apps') api.register(ValidationResource()) api.register(AppResource()) api.register(CategoryResource()) api.register(PreviewResource()) api.register(WithCreaturedResource()) api.register(SearchResource()) api.register(StatusResource()) api.register(RatingResource()) urls = [url(r'^', include(api.urls)),] if settings.ALLOW_TASTYPIE_SERVICES: services = Api(api_name='services') services.register(ErrorResource(set_handler=handle_500)) if getattr(settings, 'CLEANSED_SETTINGS_ACCESS', False): services.register(SettingsResource()) urls.append(url(r'^', include(services.urls))) urlpatterns = patterns('', *urls)
class TestSearchResource(TestCase): fixtures = fixture('user_2519', 'webapp_337141') def setUp(self): self.resource = SearchResource() self.factory = RequestFactory() self.profile = UserProfile.objects.get(pk=2519) self.user = self.profile.user def region_for(self, region): req = self.factory.get('/', ({} if region is None else {'region': region})) req.API = True req.LANG = '' req.user = self.user req.amo_user = self.profile RegionMiddleware().process_request(req) ACLMiddleware().process_request(req) return self.resource.get_region(req) def give_permission(self): self.grant_permission(self.profile, 'Regions:BypassFilters') def make_curator(self): collection = Collection.objects.create( collection_type=COLLECTIONS_TYPE_BASIC) collection.curators.add(self.profile) @patch('mkt.regions.middleware.RegionMiddleware.region_from_request') def test_get_region_all(self, mock_request_region): self.give_permission() geoip_fallback = regions.PE # Different than the default (worldwide). mock_request_region.return_value = geoip_fallback.slug # Test none-ish values (should return None, i.e. no region). eq_(self.region_for('None'), None) # Test string values (should return region with that slug). eq_(self.region_for('worldwide'), regions.WORLDWIDE) eq_(self.region_for('us'), regions.US) # Test fallback to request.REGION (should return GeoIP region if region # isn't specified or is specified and empty). eq_(self.region_for(None), geoip_fallback) eq_(self.region_for(''), geoip_fallback) # Test fallback to worldwide (e.g. if GeoIP fails). with patch('mkt.regions.middleware.RegionMiddleware.' 'process_request') as mock_process_request: eq_(self.region_for(None), regions.WORLDWIDE) ok_(mock_process_request.called) # Test invalid value (should raise exception). with self.assertRaises(ImmediateHttpResponse): self.region_for('cvanland') # Scary place def test_get_region_permission(self): self.give_permission() eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US) def test_collection_curator(self): self.make_curator() eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US) def test_no_permission_not_curator(self): with self.assertRaises(ImmediateHttpResponse): eq_(self.region_for('None'), None) eq_(self.region_for('us'), regions.US)