def test_step1_get_device_and_user_codes_non_json_failure(self):
status = int(http_client.BAD_REQUEST)
content = 'Nope not JSON.'
error_msg = 'Invalid response {0}.'.format(status)
self._step1_get_device_and_user_codes_fail_helper(status, content,
error_msg)
python类BAD_REQUEST的实例源码
def test_exchange_failure(self):
http = http_mock.HttpMock(
headers={'status': http_client.BAD_REQUEST},
data=b'{"error":"invalid_request"}',
)
with self.assertRaises(client.FlowExchangeError):
self.flow.step2_exchange(code='some random code', http=http)
def test_exchange_code_for_token_fail(self):
http = http_mock.HttpMock(
headers={'status': http_client.BAD_REQUEST},
data=b'{"error":"invalid_request"}',
)
with self.assertRaises(client.FlowExchangeError):
client.credentials_from_code(
self.client_id, self.client_secret, self.scope,
self.code, http=http, redirect_uri=self.redirect_uri)
def test_exchange_code_and_file_for_token_fail(self):
http = http_mock.HttpMock(
headers={'status': http_client.BAD_REQUEST},
data=b'{"error":"invalid_request"}',
)
with self.assertRaises(client.FlowExchangeError):
client.credentials_from_clientsecrets_and_code(
datafile('client_secrets.json'), self.scope,
self.code, http=http)
def test_assemble_node_failed(self, mock_request, mock_get_url,
mock_delete_node):
"""Test allocate resource conflict when compose node"""
CONF.set_override('url', 'http://localhost:8442/', group='podm')
mock_get_url.return_value = '/redfish/v1/Nodes'
# Fake response for getting nodes root
fake_node_root_resp = fakes.mock_request_get(fakes.fake_nodes_root(),
http_client.OK)
# Fake response for allocating node
fake_node_allocation_conflict = mock.MagicMock()
fake_node_allocation_conflict.status_code = http_client.CREATED
fake_node_allocation_conflict.headers['Location'] = \
os.path.normpath("/".join([CONF.podm.url, 'redfish/v1/Nodes/1']))
# Fake response for getting url of node assembling
fake_node_detail = fakes.mock_request_get(fakes.fake_node_detail(),
http_client.OK)
# Fake response for assembling node
fake_node_assemble_failed = fakes.mock_request_get(
fakes.fake_assemble_node_failed(), http_client.BAD_REQUEST)
mock_request.side_effect = [fake_node_root_resp,
fake_node_allocation_conflict,
fake_node_detail,
fake_node_assemble_failed]
with self.assertRaises(exception.RedfishException):
redfish.compose_node({"name": "test_node"})
mock_delete_node.assert_called_once()
def test_check_creation_incomplete_parameters(self):
incomplete_values = {
'name': 'name',
'url': 'url'
}
response = self.app.post('/v1/pod_managers',
content_type='application/json',
data=json.dumps(incomplete_values))
response = json.loads(response.data.decode())
self.assertEqual(http_client.BAD_REQUEST, response['status'])
self.assertEqual('Validation Error', response['title'])
def test_check_creation_invalid_authentication(self):
invalid_auth_values = {
"name": "podm_name",
"url": "https://10.0.0.2",
'authentication': {
"username": "username",
"password": "password"
}
}
response = self.app.post('/v1/pod_managers',
content_type='application/json',
data=json.dumps(invalid_auth_values))
response = json.loads(response.data.decode())
self.assertEqual(http_client.BAD_REQUEST, response['status'])
self.assertEqual('Validation Error', response['title'])
def test_compose_request_invalid_params(self, mock_conn):
req = {
"name": "test_request1",
"properties": {"invalid_key": "invalid_value"}
}
resp = self.app.post('/v1/nodes',
content_type='application/json',
data=json.dumps(req))
response = json.loads(resp.data.decode())
self.assertEqual(http_client.BAD_REQUEST, response['status'])
self.assertEqual('Validation Error', response['title'])
def test_node_action_request_invalid(self, mock_node, mock_connection):
req = {
"Boot": {
"Type": "On"
}
}
resp = self.app.post('/v1/nodes/fake-node/action',
content_type='application/json',
data=json.dumps(req))
response = json.loads(resp.data.decode())
self.assertEqual(http_client.BAD_REQUEST, response['status'])
self.assertEqual('Validation Error', response['title'])
def test_server_exception_description_only(self):
error_msg = "test error msg"
error_body = _get_error_body(detail=error_msg)
kwargs = {"valence_url": "http://localhost/"}
client = http.HTTPClient(**kwargs)
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.BAD_REQUEST)
self.assertRaises(exc.BadRequest,
client.json_request,
'GET', 'redfish/v1')
def test_from_response_know(self):
method = "GET"
url = "/fake"
status_code = http_client.BAD_REQUEST
json_data = {"error": {"message": "fake message",
"details": "fake details"}}
self.assert_exception(exceptions.BadRequest,
method, url, status_code, json_data)
def test_from_response(self, mock_apiclient):
fake_response = mock.Mock(status_code=http_client.BAD_REQUEST)
exc.from_response(fake_response, error=self.error,
method=self.method, url=self.url)
self.assertEqual(http_client.BAD_REQUEST, fake_response.status_code)
self.assertEqual(self.expected_json, fake_response.json())
mock_apiclient.assert_called_once_with(
fake_response, method=self.method, url=self.url)
def test_from_response_status(self, mock_apiclient):
fake_response = mock.Mock(status=http_client.BAD_REQUEST)
fake_response.getheader.return_value = 'fake-header'
delattr(fake_response, 'status_code')
exc.from_response(fake_response, error=self.error, method=self.method,
url=self.url)
expected_header = {'Content-Type': 'fake-header'}
self.assertEqual(expected_header, fake_response.headers)
self.assertEqual(http_client.BAD_REQUEST, fake_response.status_code)
self.assertEqual(self.expected_json, fake_response.json())
mock_apiclient.assert_called_once_with(
fake_response, method=self.method, url=self.url)
def test_bad_payload(self):
response = self.app.get('/credentials')
self.assertEqual(http_client.BAD_REQUEST, response.status_code)
response = self.app.put('/credentials',
data=json.dumps(dict(bad_payload='test',)),
content_type='application/json')
self.assertEqual(http_client.BAD_REQUEST, response.status_code)
def test_validate_href(self, head_mock):
href = 'http://1.2.3.4/abc.iso'
response = head_mock.return_value
response.status_code = http_client.OK
utils.validate_href(href)
head_mock.assert_called_once_with(href)
response.status_code = http_client.NO_CONTENT
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href,
href)
response.status_code = http_client.BAD_REQUEST
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
def test_validate_href_error_code(self, head_mock):
href = 'http://1.2.3.4/abc.iso'
head_mock.return_value.status_code = http_client.BAD_REQUEST
self.assertRaises(exception.ImageRefValidationFailed,
utils.validate_href, href)
head_mock.assert_called_once_with(href)
def set_key(self, req, dpid, key, **_kwargs):
def _set_val(dpid, key):
try:
val = req.json if req.body else {}
except ValueError:
return Response(status=http_client.BAD_REQUEST,
body='invalid syntax %s' % req.body)
self.conf_switch.set_key(dpid, key, val)
return None
def _ret(_ret):
return Response(status=http_client.CREATED)
return self._do_key(dpid, key, _set_val, _ret)
def test_redir_not_allowed(self, *_args):
"""Test redirection not allowed error when protocol not http"""
resp = self.app.get('/redir/srv/type2/cell1/foo/bar')
self.assertEqual(resp.status_code, http_client.BAD_REQUEST)
payload = json.loads(resp.data)
self.assertEqual('Redirection not allowed for p2 protocol',
payload['message'])
def test_aggregate_post_with_empty_az(self):
body = {"name": "test_empty",
"metadata": {"availability_zone": ""}}
response = self.post_json(
'/aggregates', body, headers=self.headers, expect_errors=True)
self.assertEqual(http_client.BAD_REQUEST, response.status_code)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])
def test_aggregate_post_with_empty_affinity_zone(self):
body = {"name": "test_empty",
"metadata": {"affinity_zone": ""}}
response = self.post_json(
'/aggregates', body, headers=self.headers, expect_errors=True)
self.assertEqual(http_client.BAD_REQUEST, response.status_code)
self.assertEqual('application/json', response.content_type)
self.assertTrue(response.json['error_message'])