def test_makes_request_with_proxy(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = 'test_data' mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection url = '/my_test_url/' # Test http host = 'http://whynotestsforthisstuff.com' with patch.dict(os.environ, {'http_proxy': 'host:333'}): utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_once_with(num_pools=1, proxy_headers=Any(), proxy_url='http://host:333') mock_connection.request.assert_called_once() # Test https host = 'https://whynotestsforthisstuff.com' with patch.dict(os.environ, {'https_proxy': 'host:333'}): utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_with(num_pools=1, proxy_headers=Any(), proxy_url='https://host:333', ca_certs=Any(), cert_reqs=Any()) self.assertEqual(mock_connection.request.call_count, 2)
def test_makes_request(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = 'test_data' mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_once_with(num_pools=1) mock_connection.request.assert_called_once()
def test_makes_request_connection_error(self, mock_manager): """Tests for common 50X connection errors.""" for code in range(500, 506): mock_connection = MagicMock() mock_connection.request.return_value = MagicMock(status=code, data=None) mock_manager.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' args = ('GET', host, url, 'a_user', 'a_pass',) with self.assertRaises(exceptions.TXConnectionError) as err: utils.make_request(*args) self.assertEqual(err.exception.response_code, code)
def do_url_request(self, api_call, multipart=False, data=None, files=[], method="GET", **kwargs): """Issues a url request.""" # Read the credentials from the config file (.transifexrc) host = self.url_info['host'] try: username = self.txrc.get(host, 'username') passwd = self.txrc.get(host, 'password') token = self.txrc.get(host, 'token') hostname = self.txrc.get(host, 'hostname') except configparser.NoSectionError: raise Exception( "No user credentials found for host %s. Edit" " ~/.transifexrc and add the appropriate" " info in there." % host ) # Create the Url kwargs['hostname'] = hostname kwargs.update(self.url_info) url = API_URLS[api_call] % kwargs if multipart: for info, filename in files: # FIXME: It works because we only pass to files argument # only one item name = os.path.basename(filename) data = { "resource": info.split(';')[0], "language": info.split(';')[1], "uploaded_file": (name, open(filename, 'rb').read()) } return utils.make_request(method, hostname, url, username, passwd, data)
def test_url_format(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = None mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection host = 'http://test.com/' url = '/path/to/we/' expected_url = 'http://test.com/path/to/we/' utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_connection.request.assert_called_once_with('GET', expected_url, fields=Any(), headers=Any())
def test_makes_request_connection_error(self, mock_manager): """Tests for common 50X connection errors.""" for code in range(500, 506): mock_connection = MagicMock() mock_connection.request.return_value = MagicMock(status=code, data=None) mock_manager.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' args = ( 'GET', host, url, 'a_user', 'a_pass', ) with self.assertRaises(exceptions.TXConnectionError) as err: utils.make_request(*args) self.assertEqual(err.exception.response_code, code)
def test_makes_request_None(self, mock_connection_from_url): response_mock = MagicMock() response_mock.status = 200 response_mock.data = None mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_connection_from_url.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' utils.make_request( 'GET', host, url, 'a_user', 'a_pass' ) mock_connection_from_url.assert_called_once_with(host) mock_connection.request.assert_called_once()
def test_makes_request(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = 'test_data' mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' utils.make_request( 'GET', host, url, 'a_user', 'a_pass' ) mock_manager.assert_called_once_with(num_pools=1) mock_connection.request.assert_called_once()
def test_makes_request(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = 'test_data' mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_once_with(num_pools=1) mock_connection.request.assert_called_once() # In case the http proxy variable exists but is empty, # it shouldn't break things with patch.dict(os.environ, {'http_proxy': ''}): utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_with(num_pools=1) self.assertEqual(mock_connection.request.call_count, 2) # In case the https proxy variable exists but is empty, # it shouldn't break things with patch.dict(os.environ, {'https_proxy': ''}): utils.make_request('GET', host, url, 'a_user', 'a_pass') mock_manager.assert_called_with(num_pools=1) self.assertEqual(mock_connection.request.call_count, 3)
def _create_resource(self, resource, pslug, fileinfo, filename, **kwargs): """Create a resource. Args: resource: The full resource name. pslug: The slug of the project. fileinfo: The information of the resource. filename: The name of the file. Raises: URLError, in case of a problem. """ multipart = True method = "POST" api_call = 'create_resource' host = self.url_info['host'] try: username = self.txrc.get(host, 'username') passwd = self.txrc.get(host, 'password') token = self.txrc.get(host, 'token') hostname = self.txrc.get(host, 'hostname') except configparser.NoSectionError: raise Exception("No user credentials found for host %s. Edit " "~/.transifexrc and add the appropriate " "info in there." % host) # Create the Url kwargs['hostname'] = hostname kwargs.update(self.url_info) kwargs['project'] = pslug url = (API_URLS[api_call] % kwargs) i18n_type = self._get_option(resource, 'type') if i18n_type is None: raise Exception( "Please define the resource type in " ".tx/config (eg. type = PO). " "More info: http://bit.ly/txcconfig" ) name = os.path.basename(filename) data = { "slug": fileinfo.split(';')[0], "name": fileinfo.split(';')[0], "uploaded_file": (name, open(filename, 'rb').read()), "i18n_type": i18n_type } r, charset = utils.make_request( method, hostname, url, username, passwd, data ) return r
def test_makes_request_skip_decode(self, mock_conn, mock_determine): response_mock = MagicMock() response_mock.status = 200 response_mock.data = 'test_data' mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_conn.return_value = mock_connection host = 'http://whynotestsforthisstuff.com' url = '/my_test_url/' utils.make_request( 'GET', host, url, 'a_user', 'a_pass', skip_decode=True ) mock_conn.assert_called_once_with(host) mock_connection.request.assert_called_once() mock_determine.assert_not_called()
def test_url_format(self, mock_manager): response_mock = MagicMock() response_mock.status = 200 response_mock.data = None mock_connection = MagicMock() mock_connection.request.return_value = response_mock mock_manager.return_value = mock_connection host = 'http://test.com/' url = '/path/to/we/' expected_url = 'http://test.com/path/to/we/' utils.make_request( 'GET', host, url, 'a_user', 'a_pass' ) mock_connection.request.assert_called_once_with('GET', expected_url, fields=Any(), headers=Any())
def _create_resource(self, resource, pslug, fileinfo, filename, **kwargs): """Create a resource. Args: resource: The full resource name. pslug: The slug of the project. fileinfo: The information of the resource. filename: The name of the file. Raises: URLError, in case of a problem. """ method = "POST" api_call = 'create_resource' host = self.url_info['host'] try: username = self.txrc.get(host, 'username') passwd = self.txrc.get(host, 'password') hostname = self.txrc.get(host, 'hostname') except configparser.NoSectionError: raise Exception("No user credentials found for host %s. Edit " "~/.transifexrc and add the appropriate " "info in there." % host) # Create the Url kwargs['hostname'] = hostname kwargs.update(self.url_info) kwargs['project'] = pslug url = (API_URLS[api_call] % kwargs) i18n_type = self._get_option(resource, 'type') if i18n_type is None: raise Exception("Please define the resource type in " ".tx/config (eg. type = PO). " "More info: http://bit.ly/txcconfig") name = os.path.basename(filename) data = { "slug": fileinfo.split(';')[0], "name": fileinfo.split(';')[0], "uploaded_file": (name, open(filename, 'rb').read()), "i18n_type": i18n_type } r, charset = utils.make_request(method, hostname, url, username, passwd, data) return r
def do_url_request(self, api_call, multipart=False, data=None, files=[], method="GET", skip_decode=False, **kwargs): """Issues a url request.""" # Read the credentials from the config file (.transifexrc) host = self.url_info['host'] try: username = self.txrc.get(host, 'username') passwd = self.txrc.get(host, 'password') hostname = self.txrc.get(host, 'hostname') except configparser.NoSectionError: raise Exception("No user credentials found for host %s. Edit" " ~/.transifexrc and add the appropriate" " info in there." % host) # Create the Url kwargs['hostname'] = hostname kwargs.update(self.url_info) url = API_URLS[api_call] % kwargs if multipart: for info, filename in files: # FIXME: It works because we only pass to files argument # only one item name = os.path.basename(filename) data = { "resource": info.split(';')[0], "language": info.split(';')[1], "uploaded_file": (name, open(filename, 'rb').read()) } return utils.make_request(method, hostname, url, username, passwd, data, skip_decode=skip_decode)