-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
89 lines (69 loc) · 2.98 KB
/
Copy pathtest_exceptions.py
File metadata and controls
89 lines (69 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
from typing import Any
from infrahub_sdk.exceptions import GraphQLError, GraphQLErrorDetail
def test_graphql_error_parses_catalogue_extensions() -> None:
error = GraphQLError(
errors=[
{
"message": "Query is not valid: Cannot query field 'DemoMissingNode' on type 'Query'.",
"path": ["CoreGraphQLQueryCreate"],
"extensions": {"code": "GRAPHQL_QUERY_INVALID", "http_status": 422, "data": {}},
}
],
query="mutation { CoreGraphQLQueryCreate { ok } }",
variables={"secret": "should-not-leak"},
)
assert error.details == [
GraphQLErrorDetail(
message="Query is not valid: Cannot query field 'DemoMissingNode' on type 'Query'.",
code="GRAPHQL_QUERY_INVALID",
http_status=422,
data={},
path=["CoreGraphQLQueryCreate"],
)
]
assert error.codes == ["GRAPHQL_QUERY_INVALID"]
def test_graphql_error_message_excludes_query_and_variables() -> None:
error = GraphQLError(
errors=[
{"message": "first error"},
{"message": "second error"},
],
query="query Sensitive { secretField }",
variables={"password": "should-not-leak"},
)
assert str(error) == "An error occurred while executing the GraphQL Query: first error; second error"
assert "secretField" not in str(error)
assert "should-not-leak" not in str(error)
assert error.query == "query Sensitive { secretField }"
assert error.variables == {"password": "should-not-leak"}
def test_graphql_error_without_extensions() -> None:
error = GraphQLError(errors=[{"message": "plain error"}])
assert error.details == [GraphQLErrorDetail(message="plain error")]
assert error.codes == []
assert str(error) == "An error occurred while executing the GraphQL Query: plain error"
def test_graphql_error_with_non_dict_entries() -> None:
errors: list[Any] = ["a bare string error"]
error = GraphQLError(errors=errors)
assert error.details == [GraphQLErrorDetail(message="a bare string error")]
assert str(error) == "An error occurred while executing the GraphQL Query: a bare string error"
def test_graphql_error_with_empty_errors() -> None:
error = GraphQLError(errors=[])
assert error.details == []
assert error.codes == []
assert str(error) == "An error occurred while executing the GraphQL Query"
def test_graphql_error_ignores_malformed_extensions() -> None:
error = GraphQLError(
errors=[
{
"message": "typed wrong",
"extensions": {"code": 42, "http_status": "not-an-int", "data": ["not", "a", "dict"]},
},
{"message": "extensions not a dict", "extensions": "bogus"},
]
)
assert error.details == [
GraphQLErrorDetail(message="typed wrong"),
GraphQLErrorDetail(message="extensions not a dict"),
]
assert error.codes == []