def test_calls_request(self, mock_session, mock_time_sleep): request = RequestNode( { "path": "http://foo.com", "name": "foo" }, endpoint=EndpointNode({ "name": "foo", "requests": [{}], "delay": 1 }), ) result = request.run() mock_time_sleep.assert_called_once_with(0.001) mock_session().request.assert_called_once_with( request.http_method, request.full_url_path, headers=request.headers, params=request.params, json=request.body, allow_redirects=False, ) assert result == { "response": mock_session().request(), "tests_results": [], "no_failure": True, }
def test_build_result( self, test_results, expected_no_failure, mock_session, mock_run_tests, mock_console_write_result, ): mock_run_tests.return_value = test_results request = RequestNode( {"name": "request_name"}, endpoint=EndpointNode({ "name": "endpoint_name", "requests": [{}] }), ) result = request.run() assert result == { "response": mock_session().request(), "tests_results": test_results, "no_failure": expected_no_failure, "request_node_name": "request_name", }
def test_should_call_validate_keys(self, mock_validate_keys): spec = { "headers": {"foo": "bar"}, "name": "foo", "path": "foo.bar", } node = RequestNode( spec, endpoint=EndpointNode({"name": "foo", "requests": [{}]}) ) keys = spec.keys() node._validate() mock_validate_keys.assert_called_with( keys, ( "body", "headers", "method", "name", "params", "path", "tests", tree_keys.VARS_KEY, "delay", "retry", "options", ), ("name",), "request", ) assert len(keys) == 3 assert "headers" in keys assert "name" in keys assert "path" in keys
def test_calls_request(self, mock_request): request = RequestNode({}, endpoint=EndpointNode({})) request.run() mock_request.assert_called_once_with( request.http_method, request.full_url_path, headers=request.headers, params=request.params, json=request.body, allow_redirects=False, )
def test_when_endpoint_has_params(self): params = {"abc": "def"} endpoint_params = {"xxx": "www"} request = RequestNode({"params": params}, endpoint=EndpointNode( {"params": endpoint_params})) assert request.params == {"abc": "def", "xxx": "www"}
def test_when_endpoint_has_delay(self): request = RequestNode({"name": "foo"}, endpoint=EndpointNode({ "name": "bar", "delay": 2 })) assert request.delay == 2
def test_calls_evaluate(self, mocker, mock_evaluate): endpoint = EndpointNode({"path": "http://foo.com/"}) request = RequestNode({"path": "/foo/"}, endpoint=endpoint) request.full_url_path calls = [mocker.call("http://foo.com/"), mocker.call("/foo/")] mock_evaluate.assert_has_calls(calls)
def test_with_repeated_keys(self): params = {"abc": "def"} endpoint_params = {"xxx": "www", "abc": "zxc"} request = RequestNode({"params": params}, endpoint=EndpointNode( {"params": endpoint_params})) assert request.params == {"abc": "def", "xxx": "www"}
def test_when_endpoint_has_no_url(self): path = "http://foo.com" request = RequestNode( {"name": "foo", "path": path}, endpoint=EndpointNode({"name": "foo", "requests": [{}], "path": ""}), ) assert request.full_url_path == path
def test_when_endpoint_has_url(self): endpoint_path = "http://foo.com/api" endpoint = EndpointNode( {"name": "foo", "requests": [{}], "path": endpoint_path} ) request = RequestNode({"path": "/foo", "name": "foo"}, endpoint=endpoint) assert request.full_url_path == f"http://foo.com/api/foo"
def test_when_request_has_no_name(self): with raises(MissingMandatoryKeyError) as excinfo: RequestNode( {}, endpoint=EndpointNode({"name": "foo", "requests": []}) ) assert str(excinfo.value) == "Missing name, path at 'request'"
def test_build_result( self, test_results, expected_no_failure, mock_request, mock_run_tests, ): mock_run_tests.return_value = test_results request = RequestNode( {"name": "foo"}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) result = request.run() assert result == { "response": mock_request(), "tests_results": test_results, "no_failure": expected_no_failure, }
def test_missing_required_keys(self): with pytest.raises(MissingMandatoryKeyError) as excinfo: request = RequestNode( spec={}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) assert str(excinfo.value) == "Missing 'name' key(s) at 'request' scope"
def test_when_endpoint_has_no_headers(self): headers = {"abc": "def"} request = RequestNode( {"headers": headers, "path": "http://foo.com", "name": "foo"}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) assert request.headers == headers
def test_when_endpoint_has_no_params(self): params = {"abc": "def"} request = RequestNode( {"params": params, "path": "http://foo.com", "name": "foo"}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) assert request.params == params
def test_with_trailintest_with_path_not_stringg_slashes(self): endpoint = EndpointNode( {"name": "foo", "requests": [{}], "path": "http://foo.com/"} ) request = RequestNode( {"name": "foo", "path": []}, endpoint=endpoint ) assert request.full_url_path == "http://foo.com/[]"
def test_with_repeated_keys(self): headers = {"abc": "def"} endpoint_headers = {"xxx": "www", "abc": "zxc"} request = RequestNode( {"headers": headers}, endpoint=EndpointNode({"headers": endpoint_headers}), ) assert request.headers == {"abc": "def", "xxx": "www"}
def test_when_endpoint_has_headers(self): headers = {"abc": "def"} endpoint_headers = {"xxx": "www"} request = RequestNode( {"headers": headers}, endpoint=EndpointNode({"headers": endpoint_headers}), ) assert request.headers == {"abc": "def", "xxx": "www"}
def testing_node(self): endpoint_node = EndpointNode(spec={"name": "foo"}) request_node = RequestNode(spec={"name": "bar"}, endpoint=endpoint_node) spec = { "name": "status_is_200", "assert": "${{ response.status_code == 200 }}", } return TestingNode(spec=spec, request=request_node)
def test_when_request_node_path_is_defined(self): endpoint = EndpointNode({"name": "foo", "requests": [{}]}) request = RequestNode(spec={ "name": "bar", "path": "/bar/" }, endpoint=endpoint) assert repr(request) == "<RequestNode /bar/>"
def test_full_name(self): endpoint_node = EndpointNode({"name": "foo"}) request_node = RequestNode(spec={"name": "bar"}, endpoint=endpoint_node) test_node = TestingNode( spec={"name": "lol", "assert": "okay"}, request=request_node ) assert test_node.full_name == "foo::bar::lol"
def test_when_method_is_invalid(self): request = RequestNode({"method": "xxx"}, endpoint=EndpointNode({})) with pytest.raises(HTTPMethodNotAllowedError) as excinfo: request.http_method assert ( str(excinfo.value) == "HTTP method not supported: XXX. " "Supported methods: ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')." )
def test_calls_evaluate(self, mocker, mock_evaluate): request = RequestNode( {"body": {"ghi": "jkl"}, "path": "http://foo.com", "name": "foo",}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) request.body calls = [mocker.call({"ghi": "jkl"})] mock_evaluate.assert_has_calls(calls)
def test_calls_evaluate(self, mocker, mock_evaluate): request = RequestNode({"body": { "ghi": "jkl" }}, endpoint=EndpointNode({})) request.body calls = [mocker.call({"ghi": "jkl"})] mock_evaluate.assert_has_calls(calls)
def test_when_request_has_delay(self): request = RequestNode( { "name": "foo", "delay": 1 }, endpoint=EndpointNode({"name": "bar"}), ) assert request.delay == 1
def test_with_repeated_keys(self): headers = {"abc": "def"} endpoint_headers = {"xxx": "www", "abc": "zxc"} request = RequestNode( {"headers": headers, "path": "http://foo.com", "name": "foo"}, endpoint=EndpointNode( {"headers": endpoint_headers, "name": "foo", "requests": [{}],} ), ) assert request.headers == {"abc": "def", "xxx": "www"}
def test_when_request_has_body(self): request = RequestNode( { "body": {"abc": "def"}, "path": "http://foo.com", "name": "foo", }, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) assert request.body == {"abc": "def"}
def test_calls_evaluate(self, mocker, mock_evaluate): endpoint = EndpointNode({"params": {"abc": "def"}}) request = RequestNode({"params": { "ghi": "jkl" }}, endpoint=endpoint) request.params calls = [mocker.call({"abc": "def", "ghi": "jkl"})] mock_evaluate.assert_has_calls(calls)
def test_missing_required_keys(self): with pytest.raises(MissingMandatoryKeyError) as excinfo: request_node = RequestNode( spec={"name": "foo", "path": "bar"}, endpoint=EndpointNode({"name": "foo", "requests": [{}]}), ) test_node = TestingNode(spec={}, request=request_node) assert ( str(excinfo.value) == "Missing 'assert', 'name' key(s) at 'test' scope" )
def test_when_both_request_and_endpoint_have_delay(self): request = RequestNode( { "name": "foo", "delay": 3 }, endpoint=EndpointNode({ "name": "bar", "delay": 4 }), ) assert request.delay == 3