def testCheckStart_fail(self): request, _ = testing_helpers.GetRequestObjects(project=self.project) request.headers['X-AppEngine-Country'] = 'US' request.remote_addr = '192.168.1.0' now = 0.0 cachekeysets, _, _, _ = ratelimiter._CacheKeys(request, now) values = [{key: ratelimiter.DEFAULT_LIMIT for key in cachekeys} for cachekeys in cachekeysets] for value in values: memcache.add_multi(value) with self.assertRaises(ratelimiter.RateLimitExceeded): self.ratelimiter.CheckStart(request, now)
def testCheckStart_expiredEntries(self): request, _ = testing_helpers.GetRequestObjects(project=self.project) request.headers['X-AppEngine-Country'] = 'US' request.remote_addr = '192.168.1.0' now = 0.0 cachekeysets, _, _, _ = ratelimiter._CacheKeys(request, now) values = [{key: ratelimiter.DEFAULT_LIMIT for key in cachekeys} for cachekeys in cachekeysets] for value in values: memcache.add_multi(value) now = now + 2 * ratelimiter.EXPIRE_AFTER_SECS self.ratelimiter.CheckStart(request, now)
def testCheckStart_sameIPDifferentUserIDs(self): # Behind a NAT, e.g. now = 0.0 # Exceed DEFAULT_LIMIT calls, but vary user_id so different # users behind the same IP aren't ratelimited together. for m in range(ratelimiter.DEFAULT_LIMIT * 2): request, _ = testing_helpers.GetRequestObjects( project=self.project) request.remote_addr = '192.168.1.0' os.environ['USER_EMAIL'] = '*****@*****.**' % m request.headers['X-AppEngine-Country'] = 'US' ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001 # Exceed the limit, but only for one userID+IP address. The # others should be fine. with self.assertRaises(ratelimiter.RateLimitExceeded): for m in range(ratelimiter.DEFAULT_LIMIT + 2): # pragma: no branch request, _ = testing_helpers.GetRequestObjects( project=self.project) request.headers['X-AppEngine-Country'] = 'US' request.remote_addr = '192.168.1.0' os.environ['USER_EMAIL'] = '*****@*****.**' ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001 # Now proceed to make requests for other user IDs # besides 42. for m in range(ratelimiter.DEFAULT_LIMIT * 2): request, _ = testing_helpers.GetRequestObjects( project=self.project) request.headers['X-AppEngine-Country'] = 'US' # Skip .0 since it's already exceeded the limit. request.remote_addr = '192.168.1.0' os.environ['USER_EMAIL'] = '*****@*****.**' % (43 + m) ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001
def testCheckStart_differentIPs(self): now = 0.0 ratelimiter.COUNTRY_LIMITS = {} # Exceed DEFAULT_LIMIT calls, but vary remote_addr so different # remote addresses aren't ratelimited together. for m in range(ratelimiter.DEFAULT_LIMIT * 2): request, _ = testing_helpers.GetRequestObjects( project=self.project) request.headers['X-AppEngine-Country'] = 'US' request.remote_addr = '192.168.1.%d' % (m % 16) ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001 # Exceed the limit, but only for one IP address. The # others should be fine. with self.assertRaises(ratelimiter.RateLimitExceeded): for m in range(ratelimiter.DEFAULT_LIMIT): # pragma: no branch request, _ = testing_helpers.GetRequestObjects( project=self.project) request.headers['X-AppEngine-Country'] = 'US' request.remote_addr = '192.168.1.0' ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001 # Now proceed to make requests for all of the other IP # addresses besides .0. for m in range(ratelimiter.DEFAULT_LIMIT * 2): request, _ = testing_helpers.GetRequestObjects( project=self.project) request.headers['X-AppEngine-Country'] = 'US' # Skip .0 since it's already exceeded the limit. request.remote_addr = '192.168.1.%d' % (m + 1) ratelimiter._CacheKeys(request, now) self.ratelimiter.CheckStart(request, now) now = now + 0.001