def test_get_miss(self, session, session_tracker): result = get_key(session, 'unknown') assert result is None session_tracker(1) # Test get cache result2 = get_key(session, 'unknown') assert result2 is None session_tracker(1)
def test_get(self, session, session_tracker): api_key = ApiKeyFactory() session.flush() session_tracker(1) result = get_key(session, api_key.valid_key) assert isinstance(result, Key) session_tracker(2) # Test get cache result2 = get_key(session, api_key.valid_key) assert isinstance(result2, Key) session_tracker(2)
def __call__(self): """Execute the view and return a response.""" api_key = None api_key_text = self.parse_apikey() skip_check = False if api_key_text is None: self.log_count("none") if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) if api_key_text is not None: try: api_key = get_key(self.request.db_session, api_key_text) except (DatabaseError, DBAPIError): # if we cannot connect to backend DB, skip api key check skip_check = True self.raven_client.captureException() bind_threadlocal( api_key=api_key_text, api_path=self.metric_path, api_type=self.view_type, api_key_db_fail=True, ) if api_key is not None: valid_key = api_key.valid_key if api_key.allowed(self.view_type): self.log_count(valid_key) # Potentially avoid overhead of Redis connection. if self.ip_log_and_rate_limit: if self.log_ip_and_rate_limited(valid_key, api_key.maxreq): raise self.prepare_exception(DailyLimitExceeded()) else: self.log_count("invalid") # Switch "invalid" with real key, add "api_key_allowed" bind_threadlocal(api_key=valid_key, api_key_allowed=False) if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) elif skip_check: pass else: if api_key_text is not None: self.log_count("invalid") bind_threadlocal(invalid_api_key=api_key_text) if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) # If we failed to look up an ApiKey, create an empty one # rather than passing None through if api_key is None: api_key = Key() return self.view(api_key)
def __call__(self): """Execute the view and return a response.""" api_key = None api_key_text = self.parse_apikey() skip_check = False if api_key_text is None: self.log_count('none') if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) if api_key_text is not None: try: api_key = get_key(self.request.db_session, api_key_text) except Exception: # if we cannot connect to backend DB, skip api key check skip_check = True self.raven_client.captureException() if api_key is not None and api_key.allowed(self.view_type): valid_key = api_key.valid_key self.log_count(valid_key) # Potentially avoid overhead of Redis connection. if self.ip_log_and_rate_limit: if self.log_ip_and_rate_limited(valid_key, api_key.maxreq): raise self.prepare_exception(DailyLimitExceeded()) elif skip_check: pass else: if api_key_text is not None: self.log_count('invalid') if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) # If we failed to look up an ApiKey, create an empty one # rather than passing None through if api_key is None: api_key = Key() return self.view(api_key)