Skip to content

Commit 293d028

Browse files
nirizrshiftre
authored andcommitted
Nirizr/improve tests (#202)
* Make all test requests content type json Signed-off-by: Nir Izraeli <nirizr@gmail.com> * use client_api instead of client for tests Signed-off-by: Nir Izraeli <nirizr@gmail.com> * Add simple template test Signed-off-by: Nir Izraeli <nirizr@gmail.com>
1 parent 6d7db90 commit 293d028

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

tests/server/collab/test_all.py

+34-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
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
87

98
import random
109

1110

11+
@pytest.fixture
12+
def api_client():
13+
return test.APIClient()
14+
15+
16+
@pytest.fixture
17+
def admin_api_client(admin_user):
18+
client = test.APIClient()
19+
client.force_authenticate(user=admin_user)
20+
return client
21+
22+
1223
def rand_hash(n):
1324
return ''.join(random.choice("01234567890ABCDEF") for _ in range(n))
1425

@@ -119,49 +130,57 @@ def assert_response(response, status, data=None):
119130

120131
@pytest.mark.django_db
121132
@pytest.mark.parametrize('model_name', collab_models.keys())
122-
def test_empty_lists(client, model_name):
123-
response = client.get('/collab/{}/'.format(model_name))
133+
def test_empty_lists(api_client, model_name):
134+
response = api_client.get('/collab/{}/'.format(model_name),
135+
HTTP_ACCEPT='application/json')
124136
assert_response(response, status.HTTP_200_OK, [])
125137

126138

127139
@pytest.mark.django_db
128140
@pytest.mark.parametrize('model_name', collab_models.keys())
129-
def test_model_guest_list(client, admin_user, model_name):
141+
def test_model_guest_list(api_client, admin_user, model_name):
130142
# setup objects
131143
obj = create_model(model_name, admin_user)
132144
obj.save()
133145

134-
response = client.get('/collab/{}/'.format(model_name))
146+
response = api_client.get('/collab/{}/'.format(model_name),
147+
HTTP_ACCEPT="application/json")
135148
assert_response(response, status.HTTP_200_OK, [obj])
136149

137150

138151
@pytest.mark.django_db
139152
@pytest.mark.parametrize('model_name', collab_models.keys())
140-
def test_model_guest_creation(client, admin_user, model_name):
153+
def test_model_guest_creation(api_client, admin_user, model_name):
141154
model_data = setup_model(model_name, admin_user)
142155

143-
response = client.post('/collab/{}/'.format(model_name),
144-
data=json.dumps(model_data),
145-
content_type="application/json")
156+
response = api_client.post('/collab/{}/'.format(model_name),
157+
data=model_data,
158+
HTTP_ACCEPT="application/json")
146159
assert_response(response, status.HTTP_401_UNAUTHORIZED)
147160

148161

149162
@pytest.mark.django_db
150163
@pytest.mark.parametrize('model_name', collab_models.keys())
151-
def test_model_creation(client, admin_client, admin_user, model_name):
164+
def test_model_creation(api_client, admin_api_client, admin_user, model_name):
152165
model_data = setup_model(model_name, admin_user)
153166

154-
response = admin_client.post('/collab/{}/'.format(model_name),
155-
data=json.dumps(model_data),
156-
content_type="application/json")
167+
response = admin_api_client.post('/collab/{}/'.format(model_name),
168+
data=model_data,
169+
HTTP_ACCEPT='application/json')
157170

158171
assert_response(response, status.HTTP_201_CREATED)
159172
projects_created = [response.json()]
160173

161-
response = client.get('/collab/{}/'.format(model_name))
174+
response = api_client.get('/collab/{}/'.format(model_name),
175+
HTTP_ACCEPT="application/json")
162176
assert_eq(response.json(), projects_created)
163177

164178

179+
def test_template(admin_client):
180+
response = admin_client.get('/accounts/profile/')
181+
assert_response(response, status.HTTP_200_OK)
182+
183+
165184
@pytest.mark.django_db
166185
def test_file_fileversion(admin_client, admin_user):
167186
file = create_model('files', admin_user)

0 commit comments

Comments
 (0)