class Meta: queryset = CollectionMaps.objects.all() resource_name = 'coll-maps' allowed_methods = ['get', 'post', 'put'] throttle = CacheDBThrottle(600) post_throttle = CacheDBThrottle(3, timeframe=60) authorization = DjangoAuthorization() always_return_data = True
def test_long_url(self, mocked_time): # This test does nothing on SQLite - max length of a varchar is not enforced # The test is here as an example, or for a future where we run tests on # another database engine. url = 'https://testserver/api/resource?' + 'longurlparams' * 100 throttle_1 = CacheDBThrottle(throttle_at=2, timeframe=5, expiration=2) throttle_1.accessed(identifier='accessor', url=url) access = ApiAccess.objects.filter(identifier='accessor').first() self.assertEqual(access.url, url)
class Meta: queryset = ThreadedComment.objects.visible() resource_name = "comments" fields = [ 'id', 'object_pk', 'comment', 'submit_date', 'user_name', 'user_email', 'user_url', 'replied_to', ] list_allowed_methods = [ 'get', 'post', ] detail_allowed_methods = [ 'get', ] throttle = CacheDBThrottle() filtering = { 'object_pk': ('exact', ), } serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = DjangoAuthorization() limit = 0
class Meta: queryset = I4pProjectTranslation.objects.filter( master__in=I4pProject.on_site.all()) include_resource_uri = True throttle = CacheDBThrottle() fields = ['slug', 'language_code', 'title', 'baseline']
class Meta: queryset = BlogProxy.secondary.published().order_by('-publish_date') resource_name = "post" fields = [ 'id', 'title', 'featured_image', 'description', 'publish_date', 'allow_comments', 'comments_count', 'rating_average', 'rating_count', 'modified_date', ] list_allowed_methods = [ 'get', ] detail_allowed_methods = [ 'get', ] throttle = CacheDBThrottle() filtering = { "publish_date": ('gt', ), } serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization() limit = 5 paginator_class = RestKitPaginator
class Meta: queryset = WorkGroup.objects.filter(visible=True) resource_name = "workgroup" throttle = CacheDBThrottle() fields = ["name", "language", "slug"] filtering = { "language": 'exact', }
class Meta: queryset = I4pProject.objects.all() fields = ["created", "website", "status"] include_resource_uri = False authentication = ApiKeyAuthentication() authorization = Authorization() throttle = CacheDBThrottle() new_allowed_methods = ['post']
class Meta: queryset = ProjectPicture.objects.all() resource_name = 'picture' throttle = CacheDBThrottle() authentication = ApiKeyAuthentication() authorization = Authorization() allowed_methods = ['post'] fields = ["desc", "author", "source"]
class Meta: queryset = RestPeriod.objects.all() resource_name = "rest_period" fields = ['id', 'days', 'hours', 'minutes',] list_allowed_methods = ['get',] detail_allowed_methods = ['get',] throttle = CacheDBThrottle() serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization()
class Meta: queryset = BlogCategory.objects.all() resource_name = "categories" fields = ['id', 'title',] list_allowed_methods = ['get',] detail_allowed_methods = ['get',] limit = 0 throttle = CacheDBThrottle() serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization()
class Meta: queryset = Ingredient.objects.all() resource_name = "ingredient" fields = ['id', 'quantity', 'unit', 'ingredient', 'note',] list_allowed_methods = ['get',] detail_allowed_methods = ['get',] limit = 0 throttle = CacheDBThrottle() serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization()
class Meta: always_return_data = True allowed_methods = ['get', 'post', 'put', 'patch', 'options', 'head'] authentication = Authentication authorization = DjangoAuthorization() validation = Validation() collection_name = 'data' cache = SimpleCache(timeout=10) throttle = CacheDBThrottle(throttle_at=settings.THROTTLE_TIMEOUT)
class Meta: queryset = I4pProjectTranslation.objects.filter( master__in=I4pProject.on_site.all()) resource_name = 'project' throttle = CacheDBThrottle() bestof_allowed_methods = ['get'] latest_allowed_methods = ['get'] random_allowed_methods = ['get'] bycountry_allowed_methods = ['get'] fields = ['id', 'slug', 'language_code', 'title', 'baseline'] filtering = {'language_code': ['exact']}
class Meta: queryset = AssignedKeyword.objects.all() resource_name = "assigned_keywords" fields = ['id', 'object_pk', '_order',] list_allowed_methods = ['get',] detail_allowed_methods = ['get',] throttle = CacheDBThrottle() filtering = { 'object_pk': ('exact',), } serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization()
class Meta: queryset = Rating.objects.all() resource_name = "rating" fields = ['id', 'object_pk', 'value',] list_allowed_methods = ['get', 'post',] detail_allowed_methods = ['get',] throttle = CacheDBThrottle() filtering = { 'object_pk': ('exact',), } serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = DjangoAuthorization()
class Meta: queryset = Keyword.objects.all() resource_name = "keywords" fields = ['id', 'title'] list_allowed_methods = ['get',] detail_allowed_methods = ['get',] limit = 0 throttle = CacheDBThrottle() filtering = { 'title': ('exact',), } serializer = CamelCaseJSONSerializer() authentication = ApiKeyAuthentication() authorization = ReadOnlyAuthorization()
class Meta: queryset = Resource.objects.all() resource_name = 'resource' excludes = ['source_peer'] allowed_methods = ['get', 'post', 'put'] authentication = MultiAuthentication(ApiKeyAuthentication(), SessionAuthentication()) authorization = ORBResourceAuthorization() serializer = ResourceSerializer() always_return_data = True include_resource_uri = True throttle = CacheDBThrottle(throttle_at=1000, timeframe=3600) ordering = ['update_date'] filtering = { 'update_date': ['lte', 'gte'], # `exact` would imply a timestamp, not date comparison 'status': ['exact'], }
def test_throttling(self): throttle_1 = CacheDBThrottle(throttle_at=2, timeframe=5, expiration=2) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 0) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 0) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 1) self.assertEqual(cache.get('cody_accesses'), None) self.assertEqual(ApiAccess.objects.count(), 1) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 1) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(throttle_1.should_be_throttled('cody'), False) self.assertEqual(len(cache.get('daniel_accesses')), 2) self.assertEqual(len(cache.get('cody_accesses')), 1) self.assertEqual(ApiAccess.objects.count(), 3) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 2) self.assertEqual(throttle_1.accessed('cody'), None) # THROTTLE'D! self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 3) self.assertEqual(ApiAccess.objects.count(), 5) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 3) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 4) self.assertEqual(ApiAccess.objects.count(), 6) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4) # Should be no interplay. self.assertEqual(throttle_1.should_be_throttled('cody'), True) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4) # Test the timeframe. ret_time = time.time() + throttle_1.timeframe + 1 with mock.patch('tastypie.throttle.time') as mocked_time: mocked_time.time.return_value = ret_time self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4)
def test_throttling(self): throttle_1 = CacheDBThrottle(throttle_at=2, timeframe=5, expiration=2) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 0) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 0) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 1) self.assertEqual(cache.get('cody_accesses'), None) self.assertEqual(ApiAccess.objects.count(), 1) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 1) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(throttle_1.should_be_throttled('cody'), False) self.assertEqual(len(cache.get('daniel_accesses')), 2) self.assertEqual(len(cache.get('cody_accesses')), 1) self.assertEqual(ApiAccess.objects.count(), 3) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 2) self.assertEqual(throttle_1.accessed('cody'), None) # THROTTLE'D! self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 3) self.assertEqual(ApiAccess.objects.count(), 5) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 3) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 4) self.assertEqual(ApiAccess.objects.count(), 6) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4) # Should be no interplay. self.assertEqual(throttle_1.should_be_throttled('cody'), True) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4) # Test the timeframe. ret_time = time.time() + throttle_1.timeframe + 1 with mock.patch('tastypie.throttle.time') as mocked_time: mocked_time.time.return_value = ret_time self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4)
def test_throttling(self): throttle_1 = CacheDBThrottle(throttle_at=2, timeframe=5, expiration=2) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 0) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 0) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 1) self.assertEqual(cache.get('cody_accesses'), None) self.assertEqual(ApiAccess.objects.count(), 1) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 1) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(throttle_1.should_be_throttled('cody'), False) self.assertEqual(len(cache.get('daniel_accesses')), 2) self.assertEqual(len(cache.get('cody_accesses')), 1) self.assertEqual(ApiAccess.objects.count(), 3) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 2) self.assertEqual(throttle_1.accessed('cody'), None) # THROTTLE'D! self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 3) self.assertEqual(ApiAccess.objects.count(), 5) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 3) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 4) self.assertEqual(ApiAccess.objects.count(), 6) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4) # Should be no interplay. self.assertEqual(throttle_1.should_be_throttled('cody'), True) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4) # Test the timeframe. time.sleep(3) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual(ApiAccess.objects.filter(identifier='daniel').count(), 4)
def test_throttling(self): throttle_1 = CacheDBThrottle(throttle_at=2, timeframe=5, expiration=2) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 0) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 0) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 1) self.assertEqual(cache.get('cody_accesses'), None) self.assertEqual(ApiAccess.objects.count(), 1) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 1) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(throttle_1.should_be_throttled('cody'), False) self.assertEqual(len(cache.get('daniel_accesses')), 2) self.assertEqual(len(cache.get('cody_accesses')), 1) self.assertEqual(ApiAccess.objects.count(), 3) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 2) self.assertEqual(throttle_1.accessed('cody'), None) # THROTTLE'D! self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 3) self.assertEqual(ApiAccess.objects.count(), 5) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 3) self.assertEqual(throttle_1.accessed('daniel'), None) self.assertEqual(throttle_1.should_be_throttled('daniel'), True) self.assertEqual(len(cache.get('daniel_accesses')), 4) self.assertEqual(ApiAccess.objects.count(), 6) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4) # Should be no interplay. self.assertEqual(throttle_1.should_be_throttled('cody'), True) self.assertEqual(throttle_1.accessed('cody'), None) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4) # Test the timeframe. time.sleep(3) self.assertEqual(throttle_1.should_be_throttled('daniel'), False) self.assertEqual(len(cache.get('daniel_accesses')), 0) self.assertEqual(ApiAccess.objects.count(), 7) self.assertEqual( ApiAccess.objects.filter(identifier='daniel').count(), 4)