1
1
import pytest
2
- import json
3
2
from functools import partial
4
- from rest_framework import status
3
+ from rest_framework import status , test
5
4
6
5
from django .db import models
7
6
from collab .models import Project , File , FileVersion , Task , Instance , Vector
10
9
import string
11
10
12
11
12
+ @pytest .fixture
13
+ def api_client (db ):
14
+ return test .APIClient ()
15
+
16
+
17
+ @pytest .fixture
18
+ def admin_api_client (db , admin_user ):
19
+ client = test .APIClient ()
20
+ client .force_authenticate (user = admin_user )
21
+ return client
22
+
23
+
13
24
def rand_hash (n ):
14
25
return '' .join (random .choice (string .ascii_uppercase ) for _ in range (n ))
15
26
@@ -116,23 +127,23 @@ def assert_response(response, status):
116
127
117
128
@pytest .mark .django_db
118
129
@pytest .mark .parametrize ('model_name' , collab_models .keys ())
119
- def test_empty_lists (client , model_name ):
120
- response = client .get ('/collab/{}/' .format (model_name ),
121
- content_type = " application/json" )
130
+ def test_empty_lists (api_client , model_name ):
131
+ response = api_client .get ('/collab/{}/' .format (model_name ),
132
+ HTTP_ACCEPT = ' application/json' )
122
133
assert_response (response , status .HTTP_200_OK )
123
134
json_response = response .json ()
124
135
assert json_response == []
125
136
126
137
127
138
@pytest .mark .django_db
128
139
@pytest .mark .parametrize ('model_name' , collab_models .keys ())
129
- def test_model_guest_list (client , admin_user , model_name ):
140
+ def test_model_guest_list (api_client , admin_user , model_name ):
130
141
# setup objects
131
142
obj = create_model (model_name , admin_user )
132
143
obj .save ()
133
144
134
- response = client .get ('/collab/{}/' .format (model_name ),
135
- content_type = "application/json" )
145
+ response = api_client .get ('/collab/{}/' .format (model_name ),
146
+ HTTP_ACCEPT = "application/json" )
136
147
assert_response (response , status .HTTP_200_OK )
137
148
dct_list = response .json ()
138
149
dct = dct_list [- 1 ]
@@ -141,27 +152,27 @@ def test_model_guest_list(client, admin_user, model_name):
141
152
142
153
@pytest .mark .django_db
143
154
@pytest .mark .parametrize ('model_name' , collab_models .keys ())
144
- def test_model_guest_creation (client , admin_user , model_name ):
155
+ def test_model_guest_creation (api_client , admin_user , model_name ):
145
156
model_data = setup_model (model_name , admin_user )
146
157
147
- response = client .post ('/collab/{}/' .format (model_name ),
148
- data = json . dumps ( model_data ) ,
149
- content_type = "application/json" )
158
+ response = api_client .post ('/collab/{}/' .format (model_name ),
159
+ data = model_data ,
160
+ HTTP_ACCEPT = "application/json" )
150
161
assert_response (response , status .HTTP_401_UNAUTHORIZED )
151
162
152
163
153
164
@pytest .mark .django_db
154
165
@pytest .mark .parametrize ('model_name' , collab_models .keys ())
155
- def test_model_creation (client , admin_client , admin_user , model_name ):
166
+ def test_model_creation (api_client , admin_api_client , admin_user , model_name ):
156
167
model_data = setup_model (model_name , admin_user )
157
168
158
- response = admin_client .post ('/collab/{}/' .format (model_name ),
159
- data = json . dumps ( model_data ) ,
160
- content_type = " application/json" )
169
+ response = admin_api_client .post ('/collab/{}/' .format (model_name ),
170
+ data = model_data ,
171
+ HTTP_ACCEPT = ' application/json' )
161
172
162
173
assert_response (response , status .HTTP_201_CREATED )
163
174
projects_created = [response .json ()]
164
175
165
- response = client .get ('/collab/{}/' .format (model_name ),
166
- content_type = "application/json" )
176
+ response = api_client .get ('/collab/{}/' .format (model_name ),
177
+ HTTP_ACCEPT = "application/json" )
167
178
assert_eq (response .json (), projects_created )
0 commit comments