Example #1
0
    def setUp(self):
        WebauthTestCase.setUp(self)

        # mock service for testing
        service = OAuth2Service(
            "example",
            consumer_key="123",
            consumer_secret="456",
            access_token_url="http://example.com/access_token",
            authorize_url="http://example.com/authorize",
        )
        self.service = service

        def raise_for_status():
            raise Exception("Response not OK!")

        self.raise_for_status = raise_for_status

        # mock response for testing
        response = Mock()
        response.content = "access_token=321"
        response.ok = True
        response.status_code = 200
        response.raise_for_status = lambda: None
        self.response = response
Example #2
0
def get_mock_session(page_text):
    page_obj = Mock()
    page_obj.text = page_text
    page_obj.raise_for_status = Mock()
    session = requests.Session()
    session.get = Mock(return_value=page_obj)
    return page_obj, session
Example #3
0
    def setUp(self):
        WebauthTestCase.setUp(self)

        # mock service for testing
        service = OAuth1Service(
            "example",
            consumer_key="123",
            consumer_secret="456",
            request_token_url="http://example.com/request_token",
            access_token_url="http://example.com/access_token",
            authorize_url="http://example.com/authorize",
        )
        self.service = service

        def raise_for_status(*args, **kwargs):
            raise Exception("Response not OK!")

        self.raise_for_status = raise_for_status

        # mock response for testing
        response = Mock()
        response.content = "oauth_token=123&oauth_token_secret=456"
        response.ok = True
        response.raise_for_status = lambda: None
        self.response = response
Example #4
0
    def test_request_raises(self):
        failed_resp = Mock(ok=False, status_code=500)
        failed_resp.raise_for_status = Mock(side_effect=requests.exceptions.RequestException)
        self.session.request = Mock(return_value=failed_resp)

        wc = BaseWebClient(self.session)
        with self.assertRaises(Error):
            wc.request("http://example.com/")
Example #5
0
def get_mock_session(page_text):
    page_obj = Mock()
    page_obj.text = page_text
    page_obj.raise_for_status = Mock()
    session = requests.Session()
    session.send = Mock(return_value=page_obj)
    session.prepare_request = Mock(return_value=None)
    return page_obj, session
Example #6
0
    def test_valid_package_raises_HTTPError(self):
        from requests import HTTPError
        from pep438.core import valid_package

        response = Mock(status_code=500)
        response.raise_for_status = Mock(side_effect=HTTPError())
        config = {"return_value": response}
        with patch("pep438.core.requests.head", **config):
            self.assertRaises(HTTPError, valid_package, "dummy_package")
Example #7
0
 def mock_response(self, content, status_code=200, bad_json=False):
     mock = Mock(spec=requests.Response)
     mock.content = content
     mock.ok = status_code >= 200 and status_code < 300
     mock.status_code = status_code
     mock.iter_content = lambda size: mock.content
     if bad_json:
         mock.json = self._raise(ValueError)
     else:
         mock.json = lambda: json.loads(mock.content or '""')
     mock.raise_for_status = lambda: None
     return mock
Example #8
0
    def test_request_token(self, request):
        post_response_mock = Mock()
        post_response_mock.raise_for_status = Mock(return_value=False)
        post_response_mock.json = Mock(return_value={"token": "token1", "expires": "123"})
        request.post = Mock(return_value=post_response_mock)

        self.patient._return_false_for_status = Mock(return_value=(True, None))

        self.patient._request_token()

        self.assertEqual(self.patient.token, "token1")
        self.assertEqual(self.patient.token_expire_milliseconds, 123)
Example #9
0
    def test_fetch_requests_token_when_expired(self, request, sleep):
        post_response_mock = Mock()
        post_response_mock.raise_for_status = Mock(return_value=False)
        post_response_mock.json = Mock(return_value={"token": "token1", "expires": "123"})
        request.post = Mock(return_value=post_response_mock)

        request_token_mock = Mock()
        return_false_for_status_mock = Mock(return_value=(True, None))

        self.patient._request_token = request_token_mock
        self.patient._return_false_for_status = return_false_for_status_mock

        self.patient._fetch("url")

        request_token_mock.assert_called_once()
Example #10
0
    def test_fetch_requests_uses_exiting_token(self, request, sleep):
        post_response_mock = Mock()
        post_response_mock.raise_for_status = Mock(return_value=False)
        post_response_mock.json = Mock(return_value={"token": "token1", "expires": "123"})
        request.post = Mock(return_value=post_response_mock)

        request_token_mock = Mock()
        request.post = post_response_mock

        return_false_for_status_mock = Mock(return_value=(True, None))
        self.patient._request_token = request_token_mock
        self.patient.token_expire_milliseconds = (time() * 1000) + 10000
        self.patient._return_false_for_status = return_false_for_status_mock

        self.patient._fetch("url")

        request_token_mock.assert_not_called()
Example #11
0
    def setUp(self):
        # mock request object
        request = Request()
        request.method = "GET"
        request.url = "http://example.com/"
        request.headers = {}
        request.params = {}
        request.data = {}
        request.params_and_data = {}
        self.request = request

        # mock response object
        response = Mock()
        response.content = "access_token=321"
        response.headers = {"content-type": "text/html; charset=UTF-8"}
        response.ok = True
        response.status_code = 200
        response.raise_for_status = lambda: None
        self.response = response

        # mock raise_for_status with an error
        def raise_for_status():
            raise Exception("Response not OK!")

        self.raise_for_status = raise_for_status

        # mock consumer object
        consumer = Mock()
        consumer.key = "123"
        consumer.secret = "456"
        self.consumer = consumer

        # mock token object
        token = Mock()
        token.key = "321"
        token.secret = "456"
        self.token = token
    def test_get_file(self, mock_open, mock_requests_get, mock_session_post, mock_isfile):

        mock_file = MagicMock(spec=file)
        mock_open.return_value = mock_file

        c = DataClient(certfile=test_cert)
        self.assertTrue(c.is_authorized)

        # logger = c.get_logger(debug=True)

        # a couple of mocked responses to requests
        mock_response = Mock()
        mock_response.text = test_get_transfer_resp_xml
        mock_response.status_code = 200
        mock_response.history = [1]

        mock_response_get = Mock()
        mock_response_get.text = test_get_transfer_resp_xml
        mock_response_get.status_code = 200
        mock_response_get.history = [1]
        mock_response_get.iter_content = Mock(return_value=[1])

        mock_response_get_fail = Mock()
        mock_response_get_fail.text = test_get_transfer_resp_xml
        mock_response_get_fail.status_code = 503
        mock_response_get_fail.history = [1]
        mock_response_get_fail.raise_for_status = Mock(side_effect=Exception("Failed."))

        # We mock the client session POST response to return desired
        # XML with a list of endpoints. We then check that the post
        # was called with the correct values.
        # The "mock_requests_get" is called to actually get the data,
        # and we ensure it is called with the correct endpoint. It also
        # requires an "iter_content" method that returns an iterable
        # to simulate streaming.
        mock_session_post.return_value = mock_response
        mock_requests_get.return_value = mock_response_get
        c.transfer_file(test_localfile, uri=test_uri)

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_get_transfer_xml, json=None, verify=False, headers=test_headers
        )

        mock_requests_get.assert_called_once_with(test_endpoint1, stream=True)

        mock_open.reset_mock()
        mock_requests_get.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()

        # First GET (endpoint1) returns a failed response. The second
        # GET (endpoint2) will work
        mock_requests_get.side_effect = [mock_response_get_fail, mock_response_get]

        c.transfer_file(test_localfile, uri=test_uri)

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_get_transfer_xml, json=None, verify=False, headers=test_headers
        )

        mock_requests_get.assert_has_calls([call(test_endpoint1, stream=True), call(test_endpoint2, stream=True)])

        mock_open.reset_mock()
        mock_requests_get.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()

        # Both endpoints, and therefore the transfer, fail
        mock_requests_get.side_effect = [mock_response_get_fail, mock_response_get_fail]

        with self.assertRaises(TransferException):
            c.transfer_file(test_localfile, uri=test_uri)

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_get_transfer_xml, json=None, verify=False, headers=test_headers
        )

        mock_requests_get.assert_has_calls([call(test_endpoint1, stream=True), call(test_endpoint2, stream=True)])

        mock_open.reset_mock()
        mock_requests_get.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()
    def test_put_file(self, mock_open, mock_requests_put, mock_session_post, mock_isfile):

        mock_file = MagicMock(spec=file)
        mock_open.return_value = mock_file

        c = DataClient(certfile=test_cert)
        self.assertTrue(c.is_authorized)

        # logger = c.get_logger(debug=True)

        # a couple of mocked responses to requests
        mock_response = Mock()
        mock_response.text = test_put_transfer_resp_xml
        mock_response.status_code = 200
        mock_response.history = [1]

        mock_response_fail = Mock()
        mock_response_fail.text = test_put_transfer_resp_xml
        mock_response_fail.status_code = 503
        mock_response_fail.history = [1]
        mock_response_fail.raise_for_status = Mock(side_effect=Exception("Failed."))

        # make sure that transfer_file raises en error on bad inputs
        with self.assertRaises(ValueError):
            c.transfer_file(test_localfile)

        # We mock the client session POST response to return desired
        # XML with a list of endpoints. We then check that the post
        # was called with the correct values.
        # The "mock_requests_put" is called to actually put the data,
        # and we ensure it is called with the correct endpoint.
        #
        # We do the whole thing three times: first time specifying a uri,
        # second time specifying archive (filename for URI derived from
        # local filename), and finally specifying both archive and filename.
        for i in range(3):
            mock_session_post.return_value = mock_response
            mock_requests_put.return_value = mock_response

            if i is 0:
                c.transfer_file(test_localfile, uri=test_uri, is_put=True)
            elif i is 1:
                c.transfer_file(test_localfile, archive=test_archive, is_put=True)
            else:
                c.transfer_file(test_localfile, archive=test_archive, filename=test_localfile, is_put=True)

            mock_session_post.assert_called_once_with(
                c.base_url, data=test_put_transfer_xml, json=None, verify=False, headers=test_headers
            )

            mock_requests_put.assert_called_once_with(test_endpoint1, data=mock_file)

            mock_open.reset_mock()
            mock_requests_put.reset_mock()
            mock_session_post.reset_mock()
            mock_isfile.reset_mock()

        # Test specifying the stream
        mock_session_post.return_value = mock_response
        mock_requests_put.return_value = mock_response

        c.transfer_file(test_localfile, archive=test_archive, filename=test_localfile, is_put=True, stream=test_stream)

        expected_headers = copy.deepcopy(test_headers)
        expected_headers["X-CADC-Stream"] = test_stream

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_put_transfer_xml, json=None, verify=False, headers=expected_headers
        )

        mock_requests_put.assert_called_once_with(test_endpoint1, data=mock_file)

        mock_open.reset_mock()
        mock_requests_put.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()

        # First PUT (endpoint1) returns a failed response. The second
        # PUT (endpoint2) will work
        mock_requests_put.side_effect = [mock_response_fail, mock_response]

        c.transfer_file(test_localfile, uri=test_uri, is_put=True)

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_put_transfer_xml, json=None, verify=False, headers=test_headers
        )

        mock_requests_put.assert_has_calls([call(test_endpoint1, data=mock_file), call(test_endpoint2, data=mock_file)])

        mock_open.reset_mock()
        mock_requests_put.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()

        # Both endpoints, and therefore the transfer, fail
        mock_requests_put.side_effect = [mock_response_fail, mock_response_fail]

        with self.assertRaises(TransferException):
            c.transfer_file(test_localfile, uri=test_uri, is_put=True)

        mock_session_post.assert_called_once_with(
            c.base_url, data=test_put_transfer_xml, json=None, verify=False, headers=test_headers
        )

        mock_requests_put.assert_has_calls([call(test_endpoint1, data=mock_file), call(test_endpoint2, data=mock_file)])

        mock_open.reset_mock()
        mock_requests_put.reset_mock()
        mock_session_post.reset_mock()
        mock_isfile.reset_mock()

        # An anonymous put should raise an UnauthorizedException
        c.is_authorized = False

        with self.assertRaises(UnauthorizedException):
            c.transfer_file(test_localfile, uri=test_uri, is_put=True)
    def test_server_response(self, mock_g, mock_n, mock_k, mock_session_post):

        # Mock OpenStack clients and response to POST
        mock_image = {"name": test_image_name, "id": test_image_id}
        mock_gclient = Mock()
        mock_gclient.images = Mock()
        mock_im_list = Mock(return_value=[mock_image])
        mock_gclient.images.list = mock_im_list
        mock_gclient.image_members = Mock()
        mock_gclient.image_members.create = Mock()
        mock_g.return_value = mock_gclient

        mock_flavor = Mock()
        mock_flavor.name = test_flavor_name
        mock_flavor.id = test_flavor_id
        mock_fl_list = Mock(return_value=[mock_flavor])
        mock_nclient = Mock()
        mock_nclient.flavors = Mock()
        mock_nclient.flavors.list = mock_fl_list
        mock_n.return_value = mock_nclient

        mock_response = Mock()
        mock_response.text = test_job_id
        mock_response.status_code = 200
        mock_session_post.return_value = mock_response

        c = ProcClient(test_auth, host=test_host)

        # Failure on server side when trying to validate job
        mock_response_validate = Mock()
        mock_response_validate.text = "Failed to validate job."
        mock_response_validate.status_code = 400
        mock_response_validate.raise_for_status = Mock(side_effect=Exception("Failed."))
        mock_session_post.return_value = mock_response_validate

        with self.assertRaises(Exception):
            c.submit_job(test_job_works, test_image_name, test_flavor_name)
        mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)

        mock_session_post.return_value = mock_response
        mock_session_post.reset_mock()

        # glance client needs to be refreshed. First time it tries to obtain
        # an image list it fails. Second time it works.
        mock_gclient.images.list.side_effect = [Exception("Failed"), mock_im_list()]
        mock_gclient.images.list.reset_mock()
        mock_g.reset_mock()
        c.submit_job(test_job_works, test_image_name, test_flavor_name)
        self.assertEqual(mock_gclient.images.list.call_count, 2)
        # only one additional call to glance client constructor since it
        # was originally created with the ProcClient instance
        self.assertEqual(mock_g.call_count, 1)
        mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)

        mock_gclient.images.list.side_effect = None
        mock_session_post.reset_mock()

        # nova client needs to be refreshed. First time it tries to obtain
        # a flavor list it fails. Second time it works.
        mock_nclient.flavors.list.side_effect = [Exception("Failed"), mock_fl_list()]
        mock_nclient.flavors.list.reset_mock()
        mock_n.reset_mock()
        c.submit_job(test_job_works, test_image_name, test_flavor_name)
        self.assertEqual(mock_nclient.flavors.list.call_count, 2)
        self.assertEqual(mock_n.call_count, 1)
        mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)

        mock_nclient.flavors.list.side_effect = None
        mock_session_post.reset_mock()

        # glance client needs to be refreshed. First time it tries to
        # share an image it fails. Second time it works.
        mock_gclient.image_members.create.side_effect = [Exception("Failed"), Mock()]
        mock_gclient.image_members.create.reset_mock()
        mock_g.reset_mock()
        c.submit_job(test_job_works, test_image_name, test_flavor_name)
        self.assertEqual(mock_gclient.image_members.create.call_count, 2)
        self.assertEqual(mock_g.call_count, 1)
        mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)
Example #15
0
 def get_mock_response(self, payload):
     response = Mock()
     response.raise_for_status = Mock()
     response.json = payload
     return response