Exemplo n.º 1
0
    def test_credential_refresh_failure(self):
        """Verify that a useful error message results when the Odilo bearer
        token cannot be refreshed, since this is the most likely point
        of failure on a new setup.
        """
        self.api.access_token_response = MockRequestsResponse(
            200, {"Content-Type": "text/html"},
            "Hi, this is the website, not the API.")
        credential = self.api.credential_object(lambda x: x)
        assert_raises_regexp(
            BadResponseException,
            "Bad response from .*: .* may not be the right base URL. Response document was: 'Hi, this is the website, not the API.'",
            self.api.refresh_creds, credential)

        # Also test a 400 response code.
        self.api.access_token_response = MockRequestsResponse(
            400, {"Content-Type": "application/json"},
            json.dumps(dict(errors=[dict(description="Oops")])))
        assert_raises_regexp(BadResponseException,
                             "Bad response from .*: Oops",
                             self.api.refresh_creds, credential)

        # If there's a 400 response but no error information,
        # the generic error message is used.
        self.api.access_token_response = MockRequestsResponse(
            400, {"Content-Type": "application/json"}, json.dumps(dict()))
        assert_raises_regexp(
            BadResponseException,
            "Bad response from .*: .* may not be the right base URL.",
            self.api.refresh_creds, credential)
Exemplo n.º 2
0
    def test_process_debuggable_response(self):
        """Test a method that gives more detailed information when a 
        problem happens.
        """
        m = HTTP.process_debuggable_response
        success = MockRequestsResponse(200, content="Success!")
        eq_(success, m(success))

        success = MockRequestsResponse(302, content="Success!")
        eq_(success, m(success))

        # An error is turned into a detailed ProblemDetail
        error = MockRequestsResponse(500, content="Error!")
        problem = m(error)
        assert isinstance(problem, ProblemDetail)
        eq_(INTEGRATION_ERROR.uri, problem.uri)
        eq_("500 response from integration server: 'Error!'", problem.detail)

        content, status_code, headers = INVALID_INPUT.response
        error = MockRequestsResponse(status_code, headers, content)
        problem = m(error)
        assert isinstance(problem, ProblemDetail)
        eq_(INTEGRATION_ERROR.uri, problem.uri)
        eq_(u"Remote service returned a problem detail document: %r" % content,
            problem.detail)

        # You can force a response to be treated as successful by
        # passing in its response code as allowed_response_codes.
        eq_(error, m(error, allowed_response_codes=[400]))
        eq_(error, m(error, allowed_response_codes=["400"]))
        eq_(error, m(error, allowed_response_codes=['4xx']))
Exemplo n.º 3
0
    def test_bad_status_code_helper(object):
        response = MockRequestsResponse(500, content="Internal Server Error!")
        exc = BadResponseException.bad_status_code("http://url/", response)
        doc, status_code, headers = exc.as_problem_detail_document(
            debug=True).response
        doc = json.loads(doc)

        assert doc['debug_message'].startswith(
            "Got status code 500 from external server, cannot continue.")
Exemplo n.º 4
0
    def test_401_during_refresh_raises_error(self):
        """If we fail to refresh the OAuth bearer token, an exception is
        raised.
        """
        self.api.access_token_response = MockRequestsResponse(401, {}, "")

        assert_raises_regexp(
            BadResponseException,
            ".*Got status code 401.*can only continue on: 200.",
            self.api.refresh_creds, None)
Exemplo n.º 5
0
    def test_helper_constructor(self):
        response = MockRequestsResponse(102, content="nonsense")
        exc = BadResponseException.from_response(
            "http://url/", "Terrible response, just terrible", response)

        # Turn the exception into a problem detail document, and it's full
        # of useful information.
        doc, status_code, headers = exc.as_problem_detail_document(
            debug=True).response
        doc = json.loads(doc)

        eq_('Bad response', doc['title'])
        eq_(
            'The server made a request to http://url/, and got an unexpected or invalid response.',
            doc['detail'])
        eq_(
            u'Terrible response, just terrible\n\nStatus code: 102\nContent: nonsense',
            doc['debug_message'])

        # Unless debug is turned off, in which case none of that
        # information is present.
        doc, status_code, headers = exc.as_problem_detail_document(
            debug=False).response
        assert 'debug_message' not in json.loads(doc)
Exemplo n.º 6
0
 def queue_response(self, status_code, headers={}, content=None):
     from testing import MockRequestsResponse
     self.responses.insert(
         0, MockRequestsResponse(status_code, headers, content)
     )
Exemplo n.º 7
0
 def mock_access_token_response(self, credential):
     token = dict(access_token=credential, expires_in=3600)
     return MockRequestsResponse(200, {}, json.dumps(token))
Exemplo n.º 8
0
 def fake_200_response(*args, **kwargs):
     return MockRequestsResponse(200, content="Hurray")
Exemplo n.º 9
0
 def fake_401_response(*args, **kwargs):
     return MockRequestsResponse(401, content="Weird")
Exemplo n.º 10
0
 def fake_500_response(*args, **kwargs):
     return MockRequestsResponse(500, content="Failure!")
Exemplo n.º 11
0
 def response(self, *args, **kwargs):
     self.requests.append((args, kwargs))
     return MockRequestsResponse(200, content="Success!")
Exemplo n.º 12
0
 def fake_200_response(*args, **kwargs):
     return MockRequestsResponse(200, content="Success!")