1
+ import json
2
+
1
3
from django .contrib .auth .models import User
2
4
from django .test import TestCase
5
+ from django .test .client import Client
3
6
from django .urls import reverse
4
7
from rest_framework import status
5
8
from rest_framework .test import APIClient , APITestCase
@@ -18,8 +21,10 @@ class BaseTestCase(TestCase):
18
21
def setUp (self ):
19
22
self .user = User .objects .create (username = 'testuser' , is_superuser = True )
20
23
self .token = Token .objects .create (user = self .user )
24
+ # todo change to Client
21
25
self .client = APIClient ()
22
26
self .client .credentials (HTTP_AUTHORIZATION = f'Token { self .token .key } ' )
27
+ self .gql_client = Client (HTTP_AUTHORIZATION = f'Token { self .token .key } ' )
23
28
24
29
25
30
class ASNTestCase (BaseTestCase ):
@@ -92,6 +97,27 @@ def test_uniqueconstraint_asn(self):
92
97
response = self .client .post (url , data , format = 'json' )
93
98
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
94
99
100
+ def test_graphql (self ):
101
+ url = reverse ('graphql' )
102
+ query = 'query bgp_asn($id: Int!){bgp_asn(id: $id){number}}'
103
+ response = self .gql_client .post (
104
+ url ,
105
+ json .dumps ({'query' : query , 'variables' : {'id' : self .asn1 .pk }}),
106
+ content_type = 'application/json'
107
+ )
108
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
109
+ self .assertEqual (json .loads (response .content )['data' ]['bgp_asn' ]['number' ], self .asn1 .number )
110
+
111
+ def test_graphql_list (self ):
112
+ url = reverse ('graphql' )
113
+ query = '{bgp_asn_list{number}}'
114
+ response = self .gql_client .post (
115
+ url ,
116
+ json .dumps ({'query' : query }),
117
+ content_type = 'application/json'
118
+ )
119
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
120
+
95
121
96
122
class CommunityTestCase (BaseTestCase ):
97
123
def setUp (self ):
@@ -126,6 +152,27 @@ def test_update_community(self):
126
152
def test_delete_community (self ):
127
153
pass
128
154
155
+ def test_graphql (self ):
156
+ url = reverse ('graphql' )
157
+ query = 'query community($id: Int!){community(id: $id){value}}'
158
+ response = self .gql_client .post (
159
+ url ,
160
+ json .dumps ({'query' : query , 'variables' : {'id' : self .community1 .pk }}),
161
+ content_type = 'application/json'
162
+ )
163
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
164
+ self .assertEqual (json .loads (response .content )['data' ]['community' ]['value' ], self .community1 .value )
165
+
166
+ def test_graphql_list (self ):
167
+ url = reverse ('graphql' )
168
+ query = '{community_list{value}}'
169
+ response = self .gql_client .post (
170
+ url ,
171
+ json .dumps ({'query' : query }),
172
+ content_type = 'application/json'
173
+ )
174
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
175
+
129
176
130
177
class PeerGroupTestCase (BaseTestCase ):
131
178
def setUp (self ):
@@ -160,6 +207,27 @@ def test_update_peer_group(self):
160
207
def test_delete_peer_group (self ):
161
208
pass
162
209
210
+ def test_graphql (self ):
211
+ url = reverse ('graphql' )
212
+ query = 'query peer_group($id: Int!){peer_group(id: $id){name}}'
213
+ response = self .gql_client .post (
214
+ url ,
215
+ json .dumps ({'query' : query , 'variables' : {'id' : self .peer_group .pk }}),
216
+ content_type = 'application/json'
217
+ )
218
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
219
+ self .assertEqual (json .loads (response .content )['data' ]['peer_group' ]['name' ], self .peer_group .name )
220
+
221
+ def test_graphql_list (self ):
222
+ url = reverse ('graphql' )
223
+ query = '{peer_group_list{name}}'
224
+ response = self .gql_client .post (
225
+ url ,
226
+ json .dumps ({'query' : query }),
227
+ content_type = 'application/json'
228
+ )
229
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
230
+
163
231
164
232
class SessionTestCase (BaseTestCase ):
165
233
def setUp (self ):
@@ -251,3 +319,24 @@ def test_session_no_device(self):
251
319
self .assertEqual (response .status_code , status .HTTP_201_CREATED )
252
320
self .assertEqual (BGPSession .objects .get (pk = response .data ['id' ]).name , 'test_session' )
253
321
self .assertEqual (BGPSession .objects .get (pk = response .data ['id' ]).description , 'session_descr' )
322
+
323
+ def test_graphql (self ):
324
+ url = reverse ('graphql' )
325
+ query = 'query bgp_session($id: Int!){bgp_session(id: $id){name}}'
326
+ response = self .gql_client .post (
327
+ url ,
328
+ json .dumps ({'query' : query , 'variables' : {'id' : self .session .pk }}),
329
+ content_type = 'application/json'
330
+ )
331
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
332
+ self .assertEqual (json .loads (response .content )['data' ]['bgp_session' ]['name' ], self .session .name )
333
+
334
+ def test_graphql_list (self ):
335
+ url = reverse ('graphql' )
336
+ query = '{bgp_session_list{name}}'
337
+ response = self .gql_client .post (
338
+ url ,
339
+ json .dumps ({'query' : query }),
340
+ content_type = 'application/json'
341
+ )
342
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
0 commit comments