def test_send_message_with_basic_auth(mock_request, mock_set_basic_auth_if_needed): """Should give response of request if OK.""" svc = OnapService() mocked_response = Response() mocked_response.status_code = 200 basic_auth = {'username': '******', "password": "******"} mock_request.return_value = mocked_response expect_headers = { "Content-Type": "application/json", "Accept": "application/json", "Once": "Upon a time" } response = svc.send_message("GET", 'test get', 'http://my.url/', headers=expect_headers, basic_auth=basic_auth) mock_set_basic_auth_if_needed.assert_called_once_with(basic_auth, ANY) mock_request.assert_called_once_with('GET', 'http://my.url/', headers=expect_headers, verify=False, proxies=None) assert response == mocked_response
def test_send_message_json_bad_json_no_exception(mock_send): svc = OnapService() mocked_response = Response() mocked_response._content = b'yolo' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response == {}
def test_send_message_json_OK(mock_send): svc = OnapService() mocked_response = Response() mocked_response._content = b'{"yolo": "yala"}' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response['yolo'] == 'yala'
def test_send_message_json_request_error(mock_send): """RequestError exception from send_message is handled.""" svc = OnapService() mock_send.side_effect = RequestError with pytest.raises(RequestError) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is RequestError mock_send.assert_called_once()
def test_send_message_json_resource_not_found(mock_send): """ResourceNotFound exception from send_message is handled.""" svc = OnapService() mock_send.side_effect = ResourceNotFound with pytest.raises(ResourceNotFound) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is ResourceNotFound mock_send.assert_called_once()
def test_send_message_json_api_error(mock_send): """APIError (error codes) from send_message is handled.""" svc = OnapService() mock_send.side_effect = APIError with pytest.raises(APIError) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is APIError mock_send.assert_called_once()
def test_send_message_json_connection_failed(mock_send): """ConnectionFailed from send_message is handled.""" svc = OnapService() mock_send.side_effect = ConnectionFailed with pytest.raises(ConnectionFailed) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is ConnectionFailed mock_send.assert_called_once()
def test_send_message_request_error(mock_request): """Should raise RequestError for an amiguous request exception.""" svc = OnapService() mock_request.side_effect = RequestException with pytest.raises(RequestError) as exc: svc.send_message("GET", 'test get', 'http://my.url/') assert exc.type is RequestError mock_request.assert_called_once()
def test_send_message_connection_failed(mock_request): """Should raise ResourceNotFound if status code 404.""" svc = OnapService() mock_request.side_effect = ConnectionError with pytest.raises(ConnectionFailed) as exc: svc.send_message("GET", 'test get', 'http://my.url/') assert exc.type is ConnectionFailed mock_request.assert_called_once()
def test_send_message_api_error(mock_request, code): """Raise APIError if status code is between 400 and 599, and not 404.""" svc = OnapService() mocked_response = Response() mocked_response.status_code = code mock_request.return_value = mocked_response with pytest.raises(APIError) as exc: svc.send_message("GET", 'test get', 'http://my.url/') assert exc.type is APIError mock_request.assert_called_once()
def test_send_message_json_bad_send_exception(mock_send): with pytest.raises(Timeout): svc = OnapService() mock_send.side_effect = Timeout response = svc.send_message_json("GET", 'test get', 'http://my.url/', exception=Timeout) mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/', exception=Timeout)
def test_send_message_resource_not_found(mock_request): """Should raise ResourceNotFound if status code 404.""" svc = OnapService() mocked_response = Response() mocked_response.status_code = 404 mock_request.return_value = mocked_response with pytest.raises(ResourceNotFound) as exc: svc.send_message("GET", 'test get', 'http://my.url/') assert exc.type is ResourceNotFound mock_request.assert_called_once()
def test_send_message_OK(mock_request): """Returns response if OK.""" svc = OnapService() mocked_response = Response() mocked_response.status_code = 200 mock_request.return_value = mocked_response expect_headers = { "Content-Type": "application/json", "Accept": "application/json", } response = svc.send_message("GET", 'test get', 'http://my.url/') mock_request.assert_called_once_with('GET', 'http://my.url/', headers=expect_headers, verify=False, proxies=None) assert response == mocked_response
def test_send_message_error_timeout_no_exception(mock_request): """Should return None given if issues on request.""" svc = OnapService() mock_request.side_effect = Timeout() response = svc.send_message("GET", 'test get', 'http://my.url/') expect_headers = { "Content-Type": "application/json", "Accept": "application/json", } mock_request.assert_called_once_with('GET', 'http://my.url/', headers=expect_headers, verify=False, proxies=None) assert response == None
def test_send_message_json_invalid_response(mock_send): """Raises InvalidResponse if response is not JSON.""" svc = OnapService() mocked_response = Response() mocked_response._content = b'{yolo}' mocked_response.encoding = "UTF-8" mocked_response.status_code = 200 mock_send.return_value = mocked_response with pytest.raises(InvalidResponse) as exc: svc.send_message_json("GET", 'test get', 'http://my.url/') assert exc.type is InvalidResponse mock_send.assert_called_once()
def test_send_message_error_400_no_exception(mock_request): """Should give back None if issues on request.""" svc = OnapService() mocked_response = Response() mocked_response.status_code = 400 mock_request.return_value = mocked_response response = svc.send_message("GET", 'test get', 'http://my.url/') expect_headers = { "Content-Type": "application/json", "Accept": "application/json", } mock_request.assert_called_once_with('GET', 'http://my.url/', headers=expect_headers, verify=False, proxies=None) assert response == None
def test_set_header(mock_session): OnapService.send_message("GET", 'test get', 'http://my.url/') _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-key" not in headers mock_session.reset_mock() OnapService.set_header({"test-header-key": "test-header-value"}) OnapService.send_message("GET", 'test get', 'http://my.url/') _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-key" in headers assert headers["test-header-key"] == "test-header-value" mock_session.reset_mock() OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-key" in headers assert headers["test-header-key"] == "test-header-value" mock_session.reset_mock() OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-key": "test-header-another-value"}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-key" in headers assert headers["test-header-key"] == "test-header-value" mock_session.reset_mock() OnapService.set_header(None) OnapService.send_message("GET", 'test get', 'http://my.url/') _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-key" not in headers def test_header_method(): return {"test-header-callable-key": "test-header-callable-value"} mock_session.reset_mock() OnapService.set_header(test_header_method) OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-callable-key" in headers assert headers["test-header-callable-key"] == "test-header-callable-value" mock_session.reset_mock() OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-key": "test-header-value"}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-callable-key" in headers assert headers["test-header-callable-key"] == "test-header-callable-value" assert "test-header-key" in headers assert headers["test-header-key"] == "test-header-value" mock_session.reset_mock() OnapService.set_header({"test-header-dict-key": "test-header-dict-value"}) OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-callable-key" in headers assert headers["test-header-callable-key"] == "test-header-callable-value" assert "test-header-dict-key" in headers assert headers["test-header-dict-key"] == "test-header-dict-value" mock_session.reset_mock() OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-common-key": "test-header-common-value"}) _, _, kwargs = mock_session.return_value.request.mock_calls[0] headers = kwargs["headers"] assert "test-header-callable-key" in headers assert headers["test-header-callable-key"] == "test-header-callable-value" assert "test-header-dict-key" in headers assert headers["test-header-dict-key"] == "test-header-dict-value" assert "test-header-common-key" in headers assert headers["test-header-common-key"] == "test-header-common-value"
def test_init(): """Test initialization.""" svc = OnapService()
def test_send_message_json_bad_send_no_exception(mock_send): svc = OnapService() mock_send.return_value = None response = svc.send_message_json("GET", 'test get', 'http://my.url/') mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/') assert response == {}