File: test_depth_limit_validator.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (254 lines) | stat: -rw-r--r-- 4,885 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import re

from pytest import raises
from graphql import parse, get_introspection_query, validate

from ...types import Schema, ObjectType, Interface
from ...types import String, Int, List, Field
from ..depth_limit import depth_limit_validator


class PetType(Interface):
    name = String(required=True)

    class meta:
        name = "Pet"


class CatType(ObjectType):
    class meta:
        name = "Cat"
        interfaces = (PetType,)


class DogType(ObjectType):
    class meta:
        name = "Dog"
        interfaces = (PetType,)


class AddressType(ObjectType):
    street = String(required=True)
    number = Int(required=True)
    city = String(required=True)
    country = String(required=True)

    class Meta:
        name = "Address"


class HumanType(ObjectType):
    name = String(required=True)
    email = String(required=True)
    address = Field(AddressType, required=True)
    pets = List(PetType, required=True)

    class Meta:
        name = "Human"


class Query(ObjectType):
    user = Field(HumanType, required=True, name=String())
    version = String(required=True)
    user1 = Field(HumanType, required=True)
    user2 = Field(HumanType, required=True)
    user3 = Field(HumanType, required=True)

    @staticmethod
    def resolve_user(root, info, name=None):
        pass


schema = Schema(query=Query)


def run_query(query: str, max_depth: int, ignore=None):
    document = parse(query)

    result = None

    def callback(query_depths):
        nonlocal result
        result = query_depths

    errors = validate(
        schema=schema.graphql_schema,
        document_ast=document,
        rules=(
            depth_limit_validator(
                max_depth=max_depth, ignore=ignore, callback=callback
            ),
        ),
    )

    return errors, result


def test_should_count_depth_without_fragment():
    query = """
    query read0 {
      version
    }
    query read1 {
      version
      user {
        name
      }
    }
    query read2 {
      matt: user(name: "matt") {
        email
      }
      andy: user(name: "andy") {
        email
        address {
          city
        }
      }
    }
    query read3 {
      matt: user(name: "matt") {
        email
      }
      andy: user(name: "andy") {
        email
        address {
          city
        }
        pets {
          name
          owner {
            name
          }
        }
      }
    }
    """

    expected = {"read0": 0, "read1": 1, "read2": 2, "read3": 3}

    errors, result = run_query(query, 10)
    assert not errors
    assert result == expected


def test_should_count_with_fragments():
    query = """
    query read0 {
      ... on Query {
        version
      }
    }
    query read1 {
      version
      user {
        ... on Human {
          name
        }
      }
    }
    fragment humanInfo on Human {
      email
    }
    fragment petInfo on Pet {
      name
      owner {
        name
      }
    }
    query read2 {
      matt: user(name: "matt") {
        ...humanInfo
      }
      andy: user(name: "andy") {
        ...humanInfo
        address {
          city
        }
      }
    }
    query read3 {
      matt: user(name: "matt") {
        ...humanInfo
      }
      andy: user(name: "andy") {
        ... on Human {
          email
        }
        address {
          city
        }
        pets {
          ...petInfo
        }
      }
    }
  """

    expected = {"read0": 0, "read1": 1, "read2": 2, "read3": 3}

    errors, result = run_query(query, 10)
    assert not errors
    assert result == expected


def test_should_ignore_the_introspection_query():
    errors, result = run_query(get_introspection_query(), 10)
    assert not errors
    assert result == {"IntrospectionQuery": 0}


def test_should_catch_very_deep_query():
    query = """{
    user {
      pets {
        owner {
          pets {
            owner {
              pets {
                name
              }
            }
          }
        }
      }
    }
    }
    """
    errors, result = run_query(query, 4)

    assert len(errors) == 1
    assert errors[0].message == "'anonymous' exceeds maximum operation depth of 4."


def test_should_ignore_field():
    query = """
    query read1 {
      user { address { city } }
    }
    query read2 {
      user1 { address { city } }
      user2 { address { city } }
      user3 { address { city } }
    }
    """

    errors, result = run_query(
        query,
        10,
        ignore=["user1", re.compile("user2"), lambda field_name: field_name == "user3"],
    )

    expected = {"read1": 2, "read2": 0}
    assert not errors
    assert result == expected


def test_should_raise_invalid_ignore():
    query = """
    query read1 {
      user { address { city } }
    }
    """
    with raises(ValueError, match="Invalid ignore option:"):
        run_query(query, 10, ignore=[True])