Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_operation_query(mock_websocket):
'Test if query with type sgqlc.operation.Operation() or raw bytes works'
schema = Schema()
# MyType and Query may be declared if doctests were processed by nose
if 'MyType' in schema:
schema -= schema.MyType
if 'Query' in schema:
schema -= schema.Query
class MyType(Type):
__schema__ = schema
i = int
class Query(Type):
__schema__ = schema
my_type = MyType
op = Operation(Query)
op.my_type.i()
mock_connection = Mock()
mock_websocket.create_connection.return_value = mock_connection
return_values = [
"""
{
"type": "connection_ack",
def test_authentication(self):
"""Test whether the client authenticates on a SortingHat server"""
MockSortingHatServer(SORTINGHAT_SERVER_URL)
client = SortingHatClient('localhost', user='admin', password='admin', ssl=False)
client.connect()
self.assertIsInstance(client.gqlc, sgqlc.endpoint.http.HTTPEndpoint)
latest_requests = httpretty.latest_requests()
self.assertEqual(len(latest_requests), 2)
request = latest_requests[0]
self.assertEqual(request.method, 'GET')
self.assertEqual(dict(request.headers)['Host'], 'localhost:9314')
request = latest_requests[1]
self.assertEqual(request.method, 'POST')
self.assertEqual(dict(request.headers)['Host'], 'localhost:9314')
# Connection was established and authorization was completed
expected = {
'Authorization': 'JWT 12345678',
'X-CSRFToken': 'ABCDEFGHIJK',
def test_connect(self):
"""Test whether the client establishes a connection with a SortingHat server"""
MockSortingHatServer(SORTINGHAT_SERVER_URL)
client = SortingHatClient('localhost', ssl=False)
client.connect()
self.assertIsInstance(client.gqlc, sgqlc.endpoint.http.HTTPEndpoint)
latest_requests = httpretty.latest_requests()
self.assertEqual(len(latest_requests), 1)
request = latest_requests[0]
self.assertEqual(request.method, 'GET')
headers = dict(request.headers)
self.assertEqual(headers['Host'], 'localhost:9314')
self.assertEqual(headers['Accept'], 'text/html')
# Connection was established and tokens set
expected = {
'X-CSRFToken': 'ABCDEFGHIJK',
'Cookie': 'csrftoken=ABCDEFGHIJK'
}
def test_server_http_non_conforming_json(mock_urlopen):
'Test if HTTP error that is NOT conforming to GraphQL payload is handled'
err = urllib.error.HTTPError(
test_url,
500,
'Some Error',
{'Content-Type': 'application/json'},
io.BytesIO(b'{"message": "xpto"}'),
)
configure_mock_urlopen(mock_urlopen, err)
endpoint = HTTPEndpoint(test_url)
data = endpoint(graphql_query)
eq_(data, {
'errors': [{
'message': str(err),
'exception': err,
'status': 500,
'headers': {'Content-Type': 'application/json'},
'body': '{"message": "xpto"}',
}],
'data': None,
})
check_mock_urlopen(mock_urlopen)
def test_server_http_error_string_list(mock_urlopen):
'Test if HTTP error that a JSON error string list is handled'
err = urllib.error.HTTPError(
test_url,
500,
'Some Error',
{'Content-Type': 'application/json'},
io.BytesIO(b'{"errors": ["a", "b"]}'),
)
configure_mock_urlopen(mock_urlopen, err)
endpoint = HTTPEndpoint(test_url)
data = endpoint(graphql_query)
expected_data = {'errors': [{'message': 'a'}, {'message': 'b'}]}
expected_data.update({
'exception': err,
'status': 500,
'headers': {'Content-Type': 'application/json'},
})
eq_(data, expected_data)
check_mock_urlopen(mock_urlopen)
def test_server_http_error_string_list(mock_requests_send):
'[Requests] - Test if HTTP error that a JSON error string list is handled'
res = requests.Response()
res.status_code = 500
res.url = test_url
res.reason = 'Some Error'
res.headers = {'Content-Type': 'application/json'}
res.raw = io.BytesIO(b'{"errors": ["a", "b"]}')
err = requests.exceptions.HTTPError(response=res)
configure_mock_requests_send(mock_requests_send, err)
endpoint = RequestsEndpoint(test_url)
data = endpoint(graphql_query)
expected_data = {'errors': [{'message': 'a'}, {'message': 'b'}]}
expected_data.update({
'exception': err,
'status': 500,
'headers': {'Content-Type': 'application/json'},
})
eq_(data, expected_data)
check_mock_requests_send(mock_requests_send)
def test_operation_name(mock_requests_send):
'[Requests] - Test if operation name is passed to server'
configure_mock_requests_send(mock_requests_send, graphql_response_ok)
operation_name = 'xpto'
endpoint = RequestsEndpoint(test_url)
data = endpoint(graphql_query, operation_name=operation_name)
eq_(data, json.loads(graphql_response_ok))
check_mock_requests_send(mock_requests_send, operation_name=operation_name)
def test_server_http_error_list_message(mock_requests_send):
'[Requests] - Test HTTP JSON error with messages being a list'
res = requests.Response()
res.status_code = 500
res.url = test_url
res.reason = 'Some Error'
res.headers = {'Content-Type': 'application/json'}
res.raw = io.BytesIO(b'{"errors": [{"message": [1, 2]}]}')
err = requests.exceptions.HTTPError(response=res)
configure_mock_requests_send(mock_requests_send, err)
endpoint = RequestsEndpoint(test_url)
data = endpoint(graphql_query)
expected_data = {'errors': [{'message': '[1, 2]'}]}
expected_data.update({
'exception': err,
'status': 500,
'headers': {'Content-Type': 'application/json'},
})
eq_(data, expected_data)
check_mock_requests_send(mock_requests_send)
def test_server_http_non_conforming_json(mock_requests_send):
'[Requests] - Test HTTP error that is NOT conforming to GraphQL payload'
res = requests.Response()
res.status_code = 500
res.url = test_url
res.reason = 'Some Error'
res.headers = {'Content-Type': 'application/json'}
res.raw = io.BytesIO(b'{"message": "xpto"}')
err = requests.exceptions.HTTPError(response=res)
configure_mock_requests_send(mock_requests_send, err)
endpoint = RequestsEndpoint(test_url)
data = endpoint(graphql_query)
eq_(data, {
'errors': [{
'message': str(err),
'exception': err,
'status': 500,
'headers': {'Content-Type': 'application/json'},
'body': '{"message": "xpto"}',
}],
'data': None,
})
check_mock_requests_send(mock_requests_send)