コード例 #1
0
    def test_load_service_failed_retrying(self):
        control_client = mock.MagicMock(spec=client.Client)

        given = {
            u'wsgi.url_scheme': u'http',
            u'PATH_INFO': u'/any',
            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 = sc_messages.CheckResponse(
            operationId=u'fake_operation_id')
        control_client.check.return_value = dummy_response

        loader = mock.MagicMock()
        # fail to load twice, then load
        loader.load.side_effect = [None, None, service.Loaders.SIMPLE.load()]
        # will consume first value
        result = wsgi.ConfigFetchWrapper(
            _DummyWsgiApp(), self.PROJECT_ID, control_client,
            loader=loader, disable_threading=True)

        test_app = webtest.TestApp(result)
        # will retry, consuming second value
        resp = test_app.get('/any', expect_errors=True)
        assert resp.status_code == 503
        # will retry, consuming third value
        resp = test_app.get('/any')
        assert resp.status_code == 200
コード例 #2
0
    def test_should_not_enter_scheduler_for_cached_checks(
            self, sched, thread_class):
        thread_class.return_value.start.side_effect = lambda: 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 = sc_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)
コード例 #3
0
ファイル: test_wsgi.py プロジェクト: uetopia/metagame
 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 = sc_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'))
     expect(control_client.allocate_quota.called).to(be_false)
コード例 #4
0
 def test_should_send_quota_requests_with_no_param(self):
     wrappee = _DummyWsgiApp()
     control_client = mock.MagicMock(spec=client.Client)
     given = {
         u'wsgi.url_scheme': u'http',
         u'PATH_INFO': u'/uvw/method2/with_no_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 = sc_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
     control_client.allocate_quota.side_effect = lambda req: sc_messages.AllocateQuotaResponse(
         operationId=req.allocateQuotaRequest.allocateOperation.operationId)
     wrapped(given, _dummy_start_response)
     expect(control_client.check.called).to(be_true)
     req = control_client.check.call_args[0][0]
     expect(req.checkRequest.operation.consumerId).to(
         equal(u'project:middleware-with-params'))
     expect(control_client.report.called).to(be_true)
     expect(control_client.allocate_quota.called).to(be_true)
コード例 #5
0
    def test_should_extend_expiration_on_receipt_of_a_response(self):
        req = _make_test_request(self.SERVICE_NAME)
        fake_response = sc_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))
コード例 #6
0
    def test_signals_resend_on_1st_call_after_flush_interval_with_errors(self):
        req = _make_test_request(self.SERVICE_NAME)
        failure_code = sc_messages.CheckError.CodeValueValuesEnum.NOT_FOUND
        fake_response = sc_messages.CheckResponse(
            operationId=self.FAKE_OPERATION_ID, checkErrors=[
                sc_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
コード例 #7
0
 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 = sc_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'))
         expect(control_client.allocate_quota.called).to(be_false)
コード例 #8
0
 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',
         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 = sc_messages.CheckResponse(
         operationId = u'fake_operation_id',
         checkErrors = [
             sc_messages.CheckError(
                 code=sc_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)
     expect(control_client.allocate_quota.called).to(be_false)
コード例 #9
0
 def test_should_cache_responses(self):
     req = _make_test_request(self.SERVICE_NAME)
     fake_response = sc_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))
コード例 #10
0
ファイル: test_wsgi_errors.py プロジェクト: uetopia/metagame
def test_handle_missing_api_key(control_client, test_app):
    url = '/uvw/method_needs_api_key/more_stuff'
    check_resp = sc_messages.CheckResponse(operationId=u'fake_operation_id')
    control_client.check.return_value = check_resp
    resp = test_app.get(url, expect_errors=True)
    assert resp.status_code == 401
    assert resp.content_type == 'application/json'
    assert wsgi.Middleware._NO_API_KEY_MSG in resp.json['message']
コード例 #11
0
 def test_should_not_cache_requests_with_important_operations(self):
     req = _make_test_request(
         self.SERVICE_NAME,
         importance=sc_messages.Operation.ImportanceValueValuesEnum.HIGH)
     fake_response = sc_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)
コード例 #12
0
 def test_should_include_project_id_in_error_text_when_needed(self):
     resp = sc_messages.CheckResponse(
         checkErrors = [
             sc_messages.CheckError(
                 code=sc_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))
コード例 #13
0
 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 = sc_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)
コード例 #14
0
 def test_should_include_detail_in_error_text_when_needed(self):
     detail = u'details, details, details'
     resp = sc_messages.CheckResponse(
         checkErrors = [
             sc_messages.CheckError(
                 code=sc_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))
コード例 #15
0
 def test_does_flush_requests_that_have_been_updated(self):
     req = _make_test_request(self.SERVICE_NAME)
     fake_response = sc_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
コード例 #16
0
ファイル: test_wsgi_errors.py プロジェクト: uetopia/metagame
def test_handle_out_of_quota(control_client, test_app):
    quota_resp = sc_messages.AllocateQuotaResponse(allocateErrors=[
        sc_messages.QuotaError(
            code=quota_request._QuotaErrors.RESOURCE_EXHAUSTED,
            description=u'details')
    ])
    check_resp = sc_messages.CheckResponse(operationId=u'fake_operation_id')
    control_client.check.return_value = check_resp
    control_client.allocate_quota.return_value = quota_resp
    url = '/uvw/method2/with_no_param'
    resp = test_app.get(url, expect_errors=True)
    expected_status, expected_detail = quota_request._QUOTA_ERROR_CONVERSION[
        quota_request._QuotaErrors.RESOURCE_EXHAUSTED]
    assert resp.status_code == expected_status
    assert resp.content_type == 'application/json'
    assert expected_detail in resp.json['message']
コード例 #17
0
    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',
            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 = sc_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)
        expect(control_client.allocate_quota.called).to(be_false)
コード例 #18
0
    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',
            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 = sc_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)
        # no quota definitions in this service config
        expect(control_client.allocate_quota.called).to(be_false)
コード例 #19
0
    def test_signals_a_resend_on_1st_call_after_flush_interval(self):
        req = _make_test_request(self.SERVICE_NAME)
        fake_response = sc_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
コード例 #20
0
 def test_should_return_none_initially_as_req_is_not_cached(self):
     req = _make_test_request(self.SERVICE_NAME)
     fake_response = sc_messages.CheckResponse(
         operationId=self.FAKE_OPERATION_ID)
     agg = self.agg
     expect(agg.check(req)).to(be_none)
コード例 #21
0
 def test_should_be_ok_with_no_errors(self):
     code, message, _ = check_request.convert_response(
         sc_messages.CheckResponse(), self.PROJECT_ID)
     expect(code).to(equal(httplib.OK))
     expect(message).to(equal(u''))