def test_get_miss(self, session, session_tracker): result = ApiKey.get(session, 'unknown') assert result is None session_tracker(1) result2 = ApiKey.get(session, 'unknown') assert result2 is None session_tracker(1)
def test_get(self): key = uuid.uuid4().hex self.session.add(ApiKey(valid_key=key, shortname='foo')) self.session.flush() result = ApiKey.get(self.session, key) self.assertTrue(isinstance(result, ApiKey)) # shortname wasn't loaded at first self.assertFalse('shortname' in result.__dict__) # but is eagerly loaded self.assertEqual(result.shortname, 'foo')
def test_get(self, session): key = uuid.uuid4().hex session.add(ApiKey(valid_key=key, shortname='foo')) session.flush() result = ApiKey.get(session, key) assert isinstance(result, ApiKey) # shortname wasn't loaded at first assert 'shortname' not in result.__dict__ # but is eagerly loaded assert result.shortname == 'foo'
def test_get(self, session, session_tracker): key = uuid.uuid4().hex session.add(ApiKey(valid_key=key, shortname='foo')) session.flush() session_tracker(1) result = ApiKey.get(session, key) assert isinstance(result, ApiKey) session_tracker(2) # shortname wasn't loaded at first assert 'shortname' not in result.__dict__ with pytest.raises(DetachedInstanceError): # and cannot be eagerly loaded assert result.shortname == 'foo' session_tracker(2) result2 = ApiKey.get(session, key) assert isinstance(result2, ApiKey) session_tracker(2)
def check(self): api_key = None api_key_text = self.parse_apikey() skip_check = False if api_key_text is None: self.log_count(None, False) if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) if api_key_text is not None: try: session = self.request.db_ro_session api_key = ApiKey.get(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): self.log_count(api_key.valid_key, True) rate_key = 'apilimit:{key}:{path}:{time}'.format( key=api_key_text, path=self.metric_path, time=util.utcnow().strftime('%Y%m%d') ) should_limit = rate_limit_exceeded( self.redis_client, rate_key, maxreq=api_key.maxreq ) if should_limit: raise self.prepare_exception(DailyLimitExceeded()) elif skip_check: pass else: if api_key_text is not None: self.log_count('invalid', False) 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 api_key = api_key or ApiKey(valid_key=None, allow_fallback=False, allow_locate=True) return self.view(api_key)
def check(self): api_key = None api_key_text = self.parse_apikey() skip_check = False if api_key_text is None: self.log_count(None, False) if self.error_on_invalidkey: raise self.prepare_exception(InvalidAPIKey()) if api_key_text is not None: try: session = self.request.db_session api_key = ApiKey.get(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): self.log_count(api_key.valid_key, True) rate_key = 'apilimit:{key}:{path}:{time}'.format( key=api_key_text, path=self.metric_path, time=util.utcnow().strftime('%Y%m%d')) should_limit = rate_limit_exceeded(self.redis_client, rate_key, maxreq=api_key.maxreq) if should_limit: raise self.prepare_exception(DailyLimitExceeded()) elif skip_check: pass else: if api_key_text is not None: self.log_count('invalid', False) 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 api_key = api_key or ApiKey(valid_key=None, allow_fallback=False, allow_locate=True, allow_transfer=False, store_sample_locate=100, store_sample_submit=100) return self.view(api_key)
def test_get_miss(self): result = ApiKey.get(self.session, 'unknown') self.assertTrue(result is None)
def test_get_miss(self, session): result = ApiKey.get(session, 'unknown') assert result is None