def test_should_send_requests_with_default_query_param_api_key(self): for default_key in (u'key', u'api_key'): wrappee = _DummyWsgiApp() control_client = mock.MagicMock(spec=client.Client) given = { u'wsgi.url_scheme': u'http', u'QUERY_STRING': u'%s=my-default-api-key-value' % (default_key, ), u'PATH_INFO': u'/uvw/method_needs_api_key/with_query_param', u'REMOTE_ADDR': u'192.168.0.3', u'HTTP_HOST': u'localhost', u'HTTP_REFERER': u'example.myreferer.com', u'REQUEST_METHOD': u'GET' } dummy_response = messages.CheckResponse( operationId=u'fake_operation_id') wrapped = wsgi.add_all(wrappee, self.PROJECT_ID, control_client, loader=service.Loaders.ENVIRONMENT) control_client.check.return_value = dummy_response wrapped(given, _dummy_start_response) expect(control_client.check.called).to(be_true) check_request = control_client.check.call_args_list[0].checkRequest check_req = control_client.check.call_args[0][0] expect(check_req.checkRequest.operation.consumerId).to( equal(u'api_key:my-default-api-key-value')) expect(control_client.report.called).to(be_true) report_req = control_client.report.call_args[0][0] expect(report_req.reportRequest.operations[0].consumerId).to( equal(u'api_key:my-default-api-key-value'))
def test_should_not_enter_scheduler_for_cached_checks( self, sched, thread_class): thread_class.return_value.start.side_effect = lambda: old_div(1, 0) self._subject.start() # confirm scheduler is created and initialized expect(sched.scheduler.called).to(be_true) scheduler = sched.scheduler.return_value expect(scheduler.enter.called).to(be_true) scheduler.reset_mock() # call check once, to a cache response dummy_request = _make_dummy_check_request(self.PROJECT_ID, self.SERVICE_NAME) dummy_response = messages.CheckResponse( operationId=dummy_request.checkRequest.operation.operationId) t = self._mock_transport t.services.check.return_value = dummy_response expect(self._subject.check(dummy_request)).to(equal(dummy_response)) t.reset_mock() # call check again - response is cached... expect(self._subject.check(dummy_request)).to(equal(dummy_response)) expect(self._mock_transport.services.check.called).to(be_false) # ... the scheduler is not run expect(scheduler.run.called).to(be_false)
def test_should_extend_expiration_on_receipt_of_a_response(self): req = _make_test_request(self.SERVICE_NAME) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(equal(fake_response)) # Now flush interval is reached, but not the response expiry self.timer.tick() # now past the flush_interval expect(agg.check(req)).to(be_none) # first response is null # until expiry, the response will continue to be returned expect(agg.check(req)).to(equal(fake_response)) expect(agg.check(req)).to(equal(fake_response)) # add a response as the request expires self.timer.tick() agg.add_response(req, fake_response) # it would have expired, but because the response was added it does not expect(agg.check(req)).to(equal(fake_response)) expect(agg.check(req)).to(equal(fake_response)) self.timer.tick() # now past the flush interval again expect(agg.check(req)).to(be_none) expect(agg.check(req)).to(equal(fake_response))
def test_should_send_report_request_if_check_fails(self): wrappee = _DummyWsgiApp() control_client = mock.MagicMock(spec=client.Client) given = { u'wsgi.url_scheme': u'http', u'PATH_INFO': u'/any/method', u'REMOTE_ADDR': u'192.168.0.3', u'HTTP_HOST': u'localhost', u'HTTP_REFERER': u'example.myreferer.com', u'REQUEST_METHOD': u'GET' } dummy_response = messages.CheckResponse( operationId=u'fake_operation_id', checkErrors=[ messages.CheckError(code=messages.CheckError. CodeValueValuesEnum.PROJECT_DELETED) ]) wrapped = wsgi.add_all(wrappee, self.PROJECT_ID, control_client, loader=service.Loaders.SIMPLE) control_client.check.return_value = dummy_response wrapped(given, _dummy_start_response) expect(control_client.check.called).to(be_true) expect(control_client.report.called).to(be_true)
def test_should_cache_responses(self): req = _make_test_request(self.SERVICE_NAME) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(equal(fake_response))
def test_should_include_project_id_in_error_text_when_needed(self): resp = messages.CheckResponse(checkErrors=[ messages.CheckError( code=messages.CheckError.CodeValueValuesEnum.PROJECT_DELETED) ]) code, got, _ = check_request.convert_response(resp, self.PROJECT_ID) want = u'Project %s has been deleted' % (self.PROJECT_ID, ) expect(code).to(equal(httplib.FORBIDDEN)) expect(got).to(equal(want))
def test_should_include_detail_in_error_text_when_needed(self): detail = u'details, details, details' resp = messages.CheckResponse(checkErrors=[ messages.CheckError(code=messages.CheckError.CodeValueValuesEnum. IP_ADDRESS_BLOCKED, detail=detail) ]) code, got, _ = check_request.convert_response(resp, self.PROJECT_ID) expect(code).to(equal(httplib.FORBIDDEN)) expect(got).to(equal(detail))
def test_should_not_cache_requests_with_important_operations(self): req = _make_test_request( self.SERVICE_NAME, importance=messages.Operation.ImportanceValueValuesEnum.HIGH) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(be_none)
def test_should_not_send_the_request_if_cached(self, dummy_thread_class): t = self._mock_transport self._subject.start() dummy_request = _make_dummy_check_request(self.PROJECT_ID, self.SERVICE_NAME) dummy_response = messages.CheckResponse( operationId=dummy_request.checkRequest.operation.operationId) t.services.check.return_value = dummy_response expect(self._subject.check(dummy_request)).to(equal(dummy_response)) t.reset_mock() expect(self._subject.check(dummy_request)).to(equal(dummy_response)) expect(t.services.check.called).to(be_false)
def test_does_flush_requests_that_have_been_updated(self): req = _make_test_request(self.SERVICE_NAME) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(equal(fake_response)) self.timer.tick() # now past the flush_interval expect(len(agg.flush())).to(equal(0)) # nothing expired self.timer.tick() # now past expiry self.timer.tick() # now past expiry expect(len(agg.flush())).to(equal(1)) # got the cached check request
def test_should_not_send_requests_if_there_is_no_service(self): wrappee = _DummyWsgiApp() control_client = mock.MagicMock(spec=client.Client) given = { u'wsgi.url_scheme': u'http', u'PATH_INFO': u'/any/method', u'REMOTE_ADDR': u'192.168.0.3', u'HTTP_HOST': u'localhost', u'HTTP_REFERER': u'example.myreferer.com', u'REQUEST_METHOD': u'GET' } dummy_response = messages.CheckResponse( operationId=u'fake_operation_id') wrapped = wsgi.Middleware(wrappee, self.PROJECT_ID, control_client) wrapped(given, _dummy_start_response) expect(control_client.check.called).to(be_false) expect(control_client.report.called).to(be_false)
def test_should_send_requests_using_the_client(self): wrappee = _DummyWsgiApp() control_client = mock.MagicMock(spec=client.Client) given = { u'wsgi.url_scheme': u'http', u'PATH_INFO': u'/any/method', u'REMOTE_ADDR': u'192.168.0.3', u'HTTP_HOST': u'localhost', u'HTTP_REFERER': u'example.myreferer.com', u'REQUEST_METHOD': u'GET' } dummy_response = messages.CheckResponse( operationId=u'fake_operation_id') with_control = wsgi.Middleware(wrappee, self.PROJECT_ID, control_client) wrapped = wsgi.EnvironmentMiddleware(with_control, service.Loaders.SIMPLE.load()) control_client.check.return_value = dummy_response wrapped(given, _dummy_start_response) expect(control_client.check.called).to(be_true) expect(control_client.report.called).to(be_true)
def test_signals_a_resend_on_1st_call_after_flush_interval(self): req = _make_test_request(self.SERVICE_NAME) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(equal(fake_response)) # Now flush interval is reached, but not the response expiry self.timer.tick() # now past the flush_interval expect(agg.check(req)).to(be_none) # none signals the resend # Until expiry, the response will continue to be returned expect(agg.check(req)).to(equal(fake_response)) expect(agg.check(req)).to(equal(fake_response)) # Once expired the cached response is no longer returned # expire self.timer.tick() self.timer.tick() # now expired expect(agg.check(req)).to(be_none) expect(agg.check(req)).to(be_none) # 2nd check is None as well
def test_should_not_perform_check_if_needed_api_key_is_missing(self): wrappee = _DummyWsgiApp() control_client = mock.MagicMock(spec=client.Client) given = { u'wsgi.url_scheme': u'http', u'PATH_INFO': u'/uvw/method_needs_api_key/more_stuff', u'REMOTE_ADDR': u'192.168.0.3', u'HTTP_HOST': u'localhost', u'HTTP_REFERER': u'example.myreferer.com', u'REQUEST_METHOD': u'GET' } dummy_response = messages.CheckResponse( operationId=u'fake_operation_id') wrapped = wsgi.add_all(wrappee, self.PROJECT_ID, control_client, loader=service.Loaders.ENVIRONMENT) control_client.check.return_value = dummy_response wrapped(given, _dummy_start_response) expect(control_client.check.called).to(be_false) expect(control_client.report.called).to(be_true) report_req = control_client.report.call_args[0][0] expect(report_req.reportRequest.operations[0].consumerId).to( equal(u'project:middleware-with-params'))
def test_signals_resend_on_1st_call_after_flush_interval_with_errors(self): req = _make_test_request(self.SERVICE_NAME) failure_code = messages.CheckError.CodeValueValuesEnum.NOT_FOUND fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID, checkErrors=[messages.CheckError(code=failure_code)]) agg = self.agg expect(agg.check(req)).to(be_none) agg.add_response(req, fake_response) expect(agg.check(req)).to(equal(fake_response)) # Now flush interval is reached, but not the response expiry self.timer.tick() # now past the flush_interval expect(agg.check(req)).to(be_none) # first response is null # until expiry, the response will continue to be returned expect(agg.check(req)).to(equal(fake_response)) expect(agg.check(req)).to(equal(fake_response)) # expire self.timer.tick() self.timer.tick() # now expired expect(agg.check(req)).to(be_none) expect(agg.check(req)).to(be_none) # 2nd check is None as well
def test_should_be_ok_with_no_errors(self): code, message, _ = check_request.convert_response( messages.CheckResponse(), self.PROJECT_ID) expect(code).to(equal(httplib.OK)) expect(message).to(equal(u''))
def test_should_return_none_initially_as_req_is_not_cached(self): req = _make_test_request(self.SERVICE_NAME) fake_response = messages.CheckResponse( operationId=self.FAKE_OPERATION_ID) agg = self.agg expect(agg.check(req)).to(be_none)