11import pytest
2- import json
32from functools import partial
4- from rest_framework import status
3+ from rest_framework import status , test
54
65from django .db import models
76from collab .models import Project , File , FileVersion , Task , Instance , Vector
109import string
1110
1211
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+
1324def rand_hash (n ):
1425 return '' .join (random .choice (string .ascii_uppercase ) for _ in range (n ))
1526
@@ -116,23 +127,23 @@ def assert_response(response, status):
116127
117128@pytest .mark .django_db
118129@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' )
122133 assert_response (response , status .HTTP_200_OK )
123134 json_response = response .json ()
124135 assert json_response == []
125136
126137
127138@pytest .mark .django_db
128139@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 ):
130141 # setup objects
131142 obj = create_model (model_name , admin_user )
132143 obj .save ()
133144
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" )
136147 assert_response (response , status .HTTP_200_OK )
137148 dct_list = response .json ()
138149 dct = dct_list [- 1 ]
@@ -141,27 +152,27 @@ def test_model_guest_list(client, admin_user, model_name):
141152
142153@pytest .mark .django_db
143154@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 ):
145156 model_data = setup_model (model_name , admin_user )
146157
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" )
150161 assert_response (response , status .HTTP_401_UNAUTHORIZED )
151162
152163
153164@pytest .mark .django_db
154165@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 ):
156167 model_data = setup_model (model_name , admin_user )
157168
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' )
161172
162173 assert_response (response , status .HTTP_201_CREATED )
163174 projects_created = [response .json ()]
164175
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" )
167178 assert_eq (response .json (), projects_created )
0 commit comments