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_simple_validate_headers(self, example_response, includes): """Make sure a simple value comparison works""" r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("headers", example_response["headers"]) assert not r.errors
def test_simple_validate_redirect_query_params(self, example_response, includes): """Make sure a simple value comparison works """ r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("redirect_query_params", {"search": "breadsticks"}) assert not r.errors
def test_save_body(self, example_response, includes): """Save a key from the body into the right name """ example_response["save"] = {"body": {"test_code": "code"}} r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r._save_value("body", example_response["body"]) assert saved == {"test_code": example_response["body"]["code"]}
def test_validate_single_status_code_incorrect(self, example_response, includes): """single status code mismatch""" example_response["status_code"] = 100 r = RestResponse(Mock(), "Test 1", example_response, includes) r._check_status_code(102, {}) assert r.errors
def test_validate_list_body(self, example_response, includes): """Make sure a list response can be validated""" example_response["json"] = ["a", 1, "b"] r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("json", example_response["json"]) assert not r.errors
def test_validate_multiple_status_codes_passes(self, example_response, includes): """Check it can match mutliple status codes""" example_response["status_code"] = [100, 200, 300] r = RestResponse(Mock(), "Test 1", example_response, includes) r._check_status_code(100, {}) assert not r.errors
def test_validate_multiple_status_codes_missing(self, example_response, includes): """Status code was not in list""" example_response["status_code"] = [100, 200, 300] r = RestResponse(Mock(), "Test 1", example_response, includes) r._check_status_code(103, {}) assert r.errors
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_save_body(self, example_response, includes): """Save a key from the body into the right name""" example_response["save"] = {"json": {"test_code": "code"}} r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r.maybe_get_save_values_from_save_block( "json", example_response["json"]) assert saved == {"test_code": example_response["json"]["code"]}
def test_validate_nested_body(self, example_response, includes): """Make sure a nested value comparison works""" example_response["json"]["nested"] = {"subthing": "blah"} r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("json", example_response["json"]) assert not r.errors
def test_validate_list_body_wrong_order(self, example_response, includes): """Order of list items matters""" example_response["json"] = ["a", 1, "b"] r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("json", example_response["json"][::-1]) assert r.errors
def test_bad_save(self, save_from, example_response, includes): example_response["save"] = {save_from: {"abc": "123"}} r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r.maybe_get_save_values_from_save_block(save_from, {}) assert not saved assert r.errors
def test_save_header(self, example_response, includes): """Save a key from the headers into the right name """ example_response["save"] = {"headers": {"next_location": "location"}} r = RestResponse(Mock(), "Test 1", example_response, includes) saved = r._save_value("headers", example_response["headers"]) assert saved == {"next_location": example_response["headers"]["location"]}
def test_validate_wrong_dict_list(self, example_response, includes): """We expected a dict, but we got a list in the response""" example_response["body"] = {"a": "b"} bad_expected = ["a", "b", "c"] r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("body", bad_expected) assert r.errors
def test_validate_wrong_list_dict(self, example_response, includes): """We expected a list, but we got a dict in the response""" example_response["json"] = ["a", 1, "b", "c"] bad_expected = {"a": "b"} r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("json", bad_expected) assert r.errors
def test_validate_missing_list_key(self, example_response, includes): """If we expect 4 items and 3 were returned, catch error""" example_response["body"] = ["a", 1, "b", "c"] bad_expected = example_response["body"][:-1] r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("body", bad_expected) assert r.errors
def test_saved_value_in_validate(self, nested_response, nested_schema, includes): r = RestResponse(Mock(), "Test 1", nested_schema, 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_validate_nested_null(self, example_response, includes): """Check that nested 'null' comparisons do not work""" example_response["json"] = {"nested": {"subthing": None}} expected = {"nested": {"subthing": "blah"}} r = RestResponse(Mock(), "Test 1", example_response, includes) r._validate_block("json", expected) assert r.errors
def test_save_body_nested_list(self, example_response, includes): """Save a key from the body into the right name """ example_response["body"]["nested"] = {"subthing": ["abc", "def"]} example_response["save"] = {"body": {"test_nested_thing": "nested.subthing.0"}} 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"][0] }
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.maybe_get_save_values_from_save_block( "body", example_response["body"] ) assert saved == { "test_nested_thing": example_response["body"]["nested"]["subthing"] }
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_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_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