-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathtest_standard_upload.py
More file actions
218 lines (165 loc) · 7.79 KB
/
test_standard_upload.py
File metadata and controls
218 lines (165 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import shutil
from django.conf import settings
from django.test import TestCase, Client, override_settings
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from rest_framework.test import APIClient
from styleguide_example.files.models import File
from styleguide_example.users.models import BaseUser
from styleguide_example.users.services import user_create
class StandardUploadApiTests(TestCase):
"""
We want to test the following general cases:
1. Upload a file, below the size limit, assert models gets created accordingly.
2. Upload a file, above the size limit (patch settings), assert API error, nothing gets created.
3. Upload a file, equal to the size limit, assert models gets created accordingly.
"""
def setUp(self):
self.client = APIClient()
self.jwt_login_url = reverse("api:authentication:jwt:login")
self.standard_upload_url = reverse("api:files:upload:standard")
@override_settings(FILE_MAX_SIZE=10)
def test_standard_upload(self):
self.assertEqual(0, File.objects.count())
self.assertEqual(0, BaseUser.objects.count())
# Create a user
credentials = {
"email": "test@hacksoft.io",
"password": "123456"
}
user_create(**credentials)
self.assertEqual(1, BaseUser.objects.count())
# Log in and get the authorization data needed
response = self.client.post(self.jwt_login_url, credentials)
self.assertEqual(200, response.status_code)
token = response.data["token"]
auth_headers = {
"HTTP_AUTHORIZATION": f"{settings.JWT_AUTH['JWT_AUTH_HEADER_PREFIX']} {token}"
}
# Create a small sized file
file_1 = SimpleUploadedFile(
name="file_small.txt",
content=(settings.FILE_MAX_SIZE - 5) * "a".encode(),
content_type="text/plain"
)
with self.subTest("1. Upload a file, below the size limit, assert models gets created accordingly"):
response = self.client.post(self.standard_upload_url, {"file": file_1}, **auth_headers)
self.assertEqual(201, response.status_code)
self.assertEqual(1, File.objects.count())
# Create a file above the size limit
file_2 = SimpleUploadedFile(
name="file_big.txt",
content=(settings.FILE_MAX_SIZE + 1) * "a".encode(),
content_type="text/plain"
)
with self.subTest("2. Upload a file, above the size limit, assert API error, nothing gets created"):
response = self.client.post(self.standard_upload_url, {"file": file_2}, **auth_headers)
self.assertEqual(400, response.status_code)
self.assertEqual(1, File.objects.count())
# Create a file equal to the size limit
file_3 = SimpleUploadedFile(
name="file_equal.txt",
content=settings.FILE_MAX_SIZE * "a".encode(),
content_type="text/plain"
)
with self.subTest("3. Upload a file, equal to the size limit, assert models gets created accordingly"):
response = self.client.post(self.standard_upload_url, {"file": file_3}, **auth_headers)
self.assertEqual(201, response.status_code)
self.assertEqual(2, File.objects.count())
def tearDown(self):
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
class StandardUploadAdminTests(TestCase):
"""
We want to test the following general cases:
File within size limit:
1. Create a new file via the Django admin, assert everything gets created (we are using services there).
2. Update an existing file via the Django admin, assert everything gets updated (we are using services there).
File not within size limit:
1. Create a new file via the Django admin, assert error, nothing gets created.
2. Update an existing fila via the Django admin, assert error, nothing gets created.
"""
def setUp(self):
self.client = Client()
self.admin_upload_file_url = reverse("admin:files_file_add")
self.admin_files_list_url = reverse("admin:files_file_changelist")
self.admin_update_file_url = lambda file: reverse(
"admin:files_file_change",
kwargs={"object_id": str(file.id)}
)
@override_settings(FILE_MAX_SIZE=10)
def test_standard_admin_upload_and_update(self):
self.assertEqual(0, File.objects.count())
# Create a superuser
credentials = {
"email": "test@hacksoft.io",
"password": "123456",
"is_admin": True,
"is_superuser": True
}
user = BaseUser.objects.create(**credentials)
self.assertEqual(1, BaseUser.objects.count())
file_1 = SimpleUploadedFile(
name="first_file.txt",
content=(settings.FILE_MAX_SIZE - 5) * "a".encode(),
content_type="text/plain"
)
data_file_1 = {
"file": file_1,
"uploaded_by": user.id
}
# Log in with the superuser account
self.client.force_login(user)
with self.subTest("1. Create a new file via the Django admin, assert everything gets created"):
response = self.client.post(self.admin_upload_file_url, data_file_1)
successfully_uploaded_file = File.objects.last()
self.assertEqual(302, response.status_code)
self.assertEqual(1, File.objects.count())
self.assertEqual(file_1.name, successfully_uploaded_file.original_file_name)
file_2 = SimpleUploadedFile(
name="second_file.txt",
content=(settings.FILE_MAX_SIZE - 1) * "a".encode(),
content_type="text/plain"
)
data_file_2 = {
"file": file_2,
"uploaded_by": user.id
}
with self.subTest("2. Update an existing file via the Django admin, assert everything gets updated"):
response = self.client.post(self.admin_update_file_url(successfully_uploaded_file), data_file_2)
self.assertEqual(302, response.status_code)
self.assertEqual(1, File.objects.count())
self.assertEqual(file_2.name, File.objects.last().original_file_name)
file_3 = SimpleUploadedFile(
name="oversized_file.txt",
content=(settings.FILE_MAX_SIZE + 1) * "a".encode(),
content_type="text/plain"
)
data_oversized_file = {
"file": file_3,
"uploaded_by": user.id
}
with self.subTest("3. Create a new oversized file via the Django admin, assert error, nothing gets created"):
response = self.client.post(self.admin_upload_file_url, data_oversized_file, follow=True)
self.assertContains(response, "File is too large")
self.assertEqual(1, File.objects.count())
self.assertEqual(file_2.name, File.objects.last().original_file_name)
file_4 = SimpleUploadedFile(
name="new_oversized_file.txt",
content=(settings.FILE_MAX_SIZE + 1) * "a".encode(),
content_type="text/plain"
)
data_new_oversized_file = {
"file": file_4,
"uploaded_by": user.id
}
with self.subTest(
"4. Update an existing file with an oversized one via the Django admin, assert error, nothing gets created"
):
response = self.client.post(
self.admin_update_file_url(File.objects.last()), data_new_oversized_file, follow=True
)
self.assertContains(response, "File is too large")
self.assertEqual(1, File.objects.count())
self.assertEqual(file_2.name, File.objects.last().original_file_name)
def tearDown(self):
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)