forked from kausaltech/mocaf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
133 lines (113 loc) · 3.68 KB
/
conftest.py
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
import json
import pytest
from graphene_django.utils.testing import graphql_query
from pytest_factoryboy import register
from trips.tests import factories as trips_factories
register(trips_factories.DeviceFactory)
register(trips_factories.TripFactory)
@pytest.fixture
def graphql_client_query(client):
def func(*args, **kwargs):
response = graphql_query(*args, **kwargs, client=client, graphql_url='/v1/graphql/')
return json.loads(response.content)
return func
@pytest.fixture
def graphql_client_query_data(graphql_client_query):
"""Make a GraphQL request, make sure the `error` field is not present and return the `data` field."""
def func(*args, **kwargs):
response = graphql_client_query(*args, **kwargs)
assert 'errors' not in response
return response['data']
return func
@pytest.fixture
def uuid(device):
return str(device.uuid)
@pytest.fixture
def registered_device():
return trips_factories.DeviceFactory(register_after_creation=True)
@pytest.fixture
def contains_error():
def func(response, code=None, message=None):
if 'errors' not in response:
return False
expected_parts = {}
if code is not None:
expected_parts['extensions'] = {'code': code}
if message is not None:
expected_parts['message'] = message
return any(expected_parts.items() <= error.items() for error in response['errors'])
return func
@pytest.fixture
def disable_mocaf(graphql_client_query_data):
def func(uuid, token):
data = graphql_client_query_data(
'''
mutation($uuid: String!, $token: String!) @device(uuid: $uuid, token: $token) {
disableMocaf {
ok
}
}
''',
variables={'uuid': str(uuid), 'token': token}
)
expected = {
'disableMocaf': {
'ok': True,
}
}
assert data == expected
return func
@pytest.fixture
def enable_mocaf(graphql_client_query_data):
def func(uuid, token=None):
if token is None:
data = graphql_client_query_data(
'''
mutation($uuid: String!) {
enableMocaf(uuid: $uuid) {
ok
token
}
}
''',
variables={'uuid': str(uuid)}
)
else:
data = graphql_client_query_data(
'''
mutation($uuid: String!, $token: String!) @device(uuid: $uuid, token: $token) {
enableMocaf {
ok
token
}
}
''',
variables={'uuid': str(uuid), 'token': token}
)
data = data['enableMocaf']
assert data['ok']
# assert data['token'] # if we ran enableMocaf with the @device directive, this will be None
return data['token']
return func
@pytest.fixture
def token(device):
assert device.enabled
assert device.token
return str(device.token)
@pytest.fixture
def register_device(graphql_client_query_data):
def func(uuid, token, account_key):
data = graphql_client_query_data(
'''
mutation($uuid: String!, $token: String!, $accountKey: String!)
@device(uuid: $uuid, token: $token)
{
registerDevice(accountKey: $accountKey) {
ok
}
}
''',
variables={'uuid': str(uuid), 'token': token, 'accountKey': account_key}
)
assert data['registerDevice']['ok']
return func