def test_save_redirect_query_param(self, example_response, includes): """Save a key from the query parameters of the redirect location """ example_response["save"] = { "redirect_query_params": { "test_search": "search" } } r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r._save_value("redirect_query_params", {"search": "breadsticks"}) assert saved == {"test_search": "breadsticks"}
def test_validate_single_value_response(self, example_response, includes, value): """Check validating single value response (string, int, etc).""" del example_response["body"] r = RestResponse(Mock(), "Test 1", example_response, includes) class FakeResponse: headers = example_response["headers"] content = "test".encode("utf8") def json(self): return value status_code = example_response["status_code"] r.verify(FakeResponse())
def test_validate_nested_anything(self, example_response, includes): """Check that nested 'anything' comparisons work This is a bit hacky because we're directly checking the ANYTHING comparison - need to add an integration test too """ example_response["body"] = {"nested": {"subthing": ANYTHING}} expected = {"nested": {"subthing": "blah"}} r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("body", expected) assert not r.errors
def test_validate_nested_null(self, example_response, includes): """Check that nested 'null' comparisons work This will be removed in a future version """ example_response["body"] = {"nested": {"subthing": None}} expected = {"nested": {"subthing": "blah"}} r = RestResponse(Mock(), "Test 1", example_response, includes) with pytest.warns(FutureWarning): r._validate_block("body", expected) assert not r.errors
def test_incorrect_status_code(self, example_response, includes): """Test full verification + return saved values """ r = RestResponse(Mock(), "Test 1", example_response, includes) class FakeResponse: headers = example_response["headers"] content = "test".encode("utf8") def json(self): return example_response["body"] status_code = 400 with pytest.raises(exceptions.TestFailError): r.verify(FakeResponse()) assert r.errors
def test_validate_and_save(self, example_response, includes): """Test full verification + return saved values """ example_response["save"] = {"body": {"test_code": "code"}} r = RestResponse(Mock(), "Test 1", example_response, includes) class FakeResponse: headers = example_response["headers"] content = "test".encode("utf8") def json(self): return example_response["body"] status_code = example_response["status_code"] saved = r.verify(FakeResponse()) assert saved == {"test_code": example_response["body"]["code"]}
def test_save_body_nested(self, example_response, includes): """Save a key from the body into the right name """ example_response["body"]["nested"] = { "subthing": "blah" } example_response["save"] = { "body": { "test_nested_thing": "nested.subthing" } } r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r._save_value("body", example_response["body"]) assert saved == {"test_nested_thing": example_response["body"]["nested"]["subthing"]}
def test_status_code_warns(example_response, includes): """Should continue if the status code is nonexistent""" example_response["status_code"] = 231234 with patch("tavern._plugins.rest.response.logger.warning") as wmock: RestResponse(Mock(), "Test 1", example_response, includes) assert wmock.called
def test_saved_value_in_validate(self, nested_response, nested_schema, includes): r = RestResponse( Mock(), "Test 1", format_keys(nested_schema, includes["variables"]), includes, ) class FakeResponse: headers = nested_response["headers"] content = "test".encode("utf8") def json(self): return nested_response["body"] status_code = nested_response["status_code"] r.verify(FakeResponse())
def test_save_body_nested_list(self, example_response, includes): """Save a key from the body into the right name""" example_response["json"]["nested"] = {"subthing": ["abc", "def"]} example_response["save"] = { "json": { "test_nested_thing": "nested.subthing[0]" } } r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r.maybe_get_save_values_from_save_block( "json", example_response["json"]) assert saved == { "test_nested_thing": example_response["json"]["nested"]["subthing"][0] }