|
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
|
8 | 7 |
|
9 | 8 | import random
|
10 | 9 |
|
11 | 10 |
|
| 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 | + |
12 | 23 | def rand_hash(n):
|
13 | 24 | return ''.join(random.choice("01234567890ABCDEF") for _ in range(n))
|
14 | 25 |
|
@@ -119,49 +130,57 @@ def assert_response(response, status, data=None):
|
119 | 130 |
|
120 | 131 | @pytest.mark.django_db
|
121 | 132 | @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') |
124 | 136 | assert_response(response, status.HTTP_200_OK, [])
|
125 | 137 |
|
126 | 138 |
|
127 | 139 | @pytest.mark.django_db
|
128 | 140 | @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): |
130 | 142 | # setup objects
|
131 | 143 | obj = create_model(model_name, admin_user)
|
132 | 144 | obj.save()
|
133 | 145 |
|
134 |
| - response = client.get('/collab/{}/'.format(model_name)) |
| 146 | + response = api_client.get('/collab/{}/'.format(model_name), |
| 147 | + HTTP_ACCEPT="application/json") |
135 | 148 | assert_response(response, status.HTTP_200_OK, [obj])
|
136 | 149 |
|
137 | 150 |
|
138 | 151 | @pytest.mark.django_db
|
139 | 152 | @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): |
141 | 154 | model_data = setup_model(model_name, admin_user)
|
142 | 155 |
|
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") |
146 | 159 | assert_response(response, status.HTTP_401_UNAUTHORIZED)
|
147 | 160 |
|
148 | 161 |
|
149 | 162 | @pytest.mark.django_db
|
150 | 163 | @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): |
152 | 165 | model_data = setup_model(model_name, admin_user)
|
153 | 166 |
|
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') |
157 | 170 |
|
158 | 171 | assert_response(response, status.HTTP_201_CREATED)
|
159 | 172 | projects_created = [response.json()]
|
160 | 173 |
|
161 |
| - response = client.get('/collab/{}/'.format(model_name)) |
| 174 | + response = api_client.get('/collab/{}/'.format(model_name), |
| 175 | + HTTP_ACCEPT="application/json") |
162 | 176 | assert_eq(response.json(), projects_created)
|
163 | 177 |
|
164 | 178 |
|
| 179 | +def test_template(admin_client): |
| 180 | + response = admin_client.get('/accounts/profile/') |
| 181 | + assert_response(response, status.HTTP_200_OK) |
| 182 | + |
| 183 | + |
165 | 184 | @pytest.mark.django_db
|
166 | 185 | def test_file_fileversion(admin_client, admin_user):
|
167 | 186 | file = create_model('files', admin_user)
|
|
0 commit comments