Skip to content

Commit 9221b5c

Browse files
committed
use client_api instead of client for tests
Signed-off-by: Nir Izraeli <[email protected]>
1 parent 973267d commit 9221b5c

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

server/utils/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
22

33

4-
class ViewSetTemplateMixin(object):
4+
class ViewSetTemplateMixin:
5+
def get_model_name(self):
6+
return self.__class__.__name__.lower().replace('viewset', '')
7+
58
def get_template_names(self):
6-
name_parts = [self.__class__.__name__.lower(),
9+
name_parts = [self.get_model_name(),
710
"{}.html".format(self.action)]
811
template_name = os.path.join(*name_parts)
912
return [template_name]

tests/server/collab/test_all.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
2-
import json
32
from functools import partial
4-
from rest_framework import status
3+
from rest_framework import status, test
54

65
from django.db import models
76
from collab.models import Project, File, FileVersion, Task, Instance, Vector
@@ -10,6 +9,18 @@
109
import 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+
1324
def 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

Comments
 (0)