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
|
import pytest
import responses
from gql import gql
from gql import Client
from gql.transport.exceptions import TransportQueryError
from gql.transport.requests import RequestsHTTPTransport
from sentry_sdk.integrations.gql import GQLIntegration
@responses.activate
def _execute_mock_query(response_json):
url = "http://example.com/graphql"
query_string = """
query Example {
example
}
"""
# Mock the GraphQL server response
responses.add(
method=responses.POST,
url=url,
json=response_json,
status=200,
)
transport = RequestsHTTPTransport(url=url)
client = Client(transport=transport)
query = gql(query_string)
return client.execute(query)
def _make_erroneous_query(capture_events):
"""
Make an erroneous GraphQL query, and assert that the error was reraised, that
exactly one event was recorded, and that the exception recorded was a
TransportQueryError. Then, return the event to allow further verifications.
"""
events = capture_events()
response_json = {"errors": ["something bad happened"]}
with pytest.raises(TransportQueryError):
_execute_mock_query(response_json)
assert (
len(events) == 1
), "the sdk captured %d events, but 1 event was expected" % len(events)
(event,) = events
(exception,) = event["exception"]["values"]
assert (
exception["type"] == "TransportQueryError"
), "%s was captured, but we expected a TransportQueryError" % exception(type)
assert "request" in event
return event
def test_gql_init(sentry_init):
"""
Integration test to ensure we can initialize the SDK with the GQL Integration
"""
sentry_init(integrations=[GQLIntegration()])
def test_real_gql_request_no_error(sentry_init, capture_events):
"""
Integration test verifying that the GQLIntegration works as expected with successful query.
"""
sentry_init(integrations=[GQLIntegration()])
events = capture_events()
response_data = {"example": "This is the example"}
response_json = {"data": response_data}
result = _execute_mock_query(response_json)
assert (
result == response_data
), "client.execute returned a different value from what it received from the server"
assert (
len(events) == 0
), "the sdk captured an event, even though the query was successful"
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events):
"""
Integration test verifying that the GQLIntegration works as expected with query resulting
in a GraphQL error, and that PII is not sent.
"""
sentry_init(integrations=[GQLIntegration()])
event = _make_erroneous_query(capture_events)
assert "data" not in event["request"]
assert "response" not in event["contexts"]
def test_real_gql_request_with_error_with_pii(sentry_init, capture_events):
"""
Integration test verifying that the GQLIntegration works as expected with query resulting
in a GraphQL error, and that PII is not sent.
"""
sentry_init(integrations=[GQLIntegration()], send_default_pii=True)
event = _make_erroneous_query(capture_events)
assert "data" in event["request"]
assert "response" in event["contexts"]
|