Skip to content

Commit 1233f3e

Browse files
committed
Add a test for the standard file upload
1 parent 5cb309c commit 1233f3e

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

styleguide_example/files/tests/flows/test_standard_upload.py

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,94 @@
1-
from django.test import TestCase
1+
from django.conf import settings
2+
from django.test import TestCase, override_settings
3+
from django.urls import reverse
4+
from django.core.files.uploadedfile import SimpleUploadedFile
5+
6+
from rest_framework.test import APIClient
7+
8+
from styleguide_example.files.models import File
9+
10+
from styleguide_example.users.models import BaseUser
11+
from styleguide_example.users.services import user_create
212

313

414
class StandardUploadApiTests(TestCase):
515
"""
616
We want to test the following general cases:
717
818
1. Upload a file, below the size limit, assert models gets created accordingly.
9-
1. Upload a file, above the size limit (patch settings), assert API error, nothing gets created.
19+
2. Upload a file, above the size limit (patch settings), assert API error, nothing gets created.
20+
3. Upload a file, equal to the size limit, assert models gets created accordingly.
1021
"""
22+
def setUp(self):
23+
self.client = APIClient()
24+
25+
self.jwt_login_url = reverse("api:authentication:jwt:login")
26+
self.standard_upload_url = reverse("api:files:upload:standard")
27+
28+
@override_settings(FILE_MAX_SIZE=10)
29+
def test_standard_upload(self):
30+
file_max_size = settings.FILE_MAX_SIZE
31+
32+
self.assertEqual(0, File.objects.count())
33+
self.assertEqual(0, BaseUser.objects.count())
34+
35+
# Create a user
36+
credentials = {
37+
"email": "[email protected]",
38+
"password": "123456"
39+
}
40+
user_create(**credentials)
41+
42+
self.assertEqual(1, BaseUser.objects.count())
43+
44+
# Log in and get the authorization data needed
45+
response = self.client.post(self.jwt_login_url, credentials)
46+
47+
self.assertEqual(200, response.status_code)
48+
49+
token = response.data["token"]
50+
auth_headers = {
51+
"HTTP_AUTHORIZATION": f"{settings.JWT_AUTH['JWT_AUTH_HEADER_PREFIX']} {token}"
52+
}
53+
54+
# Create a small sized file
55+
file_1 = SimpleUploadedFile(
56+
name="file_small.txt", content=b"Test", content_type="text/plain"
57+
)
58+
59+
with self.subTest("1. Upload a file, below the size limit, assert models gets created accordingly"):
60+
response = self.client.post(
61+
self.standard_upload_url, {"file": file_1}, enctype="multipart/form-data", **auth_headers
62+
)
63+
64+
self.assertEqual(201, response.status_code)
65+
self.assertEqual(1, File.objects.count())
66+
67+
# Create a file above the size limit
68+
file_2 = SimpleUploadedFile(
69+
name="file_big.txt", content=(file_max_size + 1) * "a".encode(), content_type="text/plain"
70+
)
71+
72+
with self.subTest("2. Upload a file, above the size limit, assert API error, nothing gets created"):
73+
response = self.client.post(
74+
self.standard_upload_url, {"file": file_2}, enctype="multipart/form-data", **auth_headers
75+
)
76+
77+
self.assertEqual(400, response.status_code)
78+
self.assertEqual(1, File.objects.count())
79+
80+
# Create a file equal to the size limit
81+
file_3 = SimpleUploadedFile(
82+
name="file_equal.txt", content=file_max_size * "b".encode(), content_type="text/plain"
83+
)
84+
85+
with self.subTest("3. Upload a file, equal to the size limit, assert models gets created accordingly"):
86+
response = self.client.post(
87+
self.standard_upload_url, {"file": file_3}, enctype="multipart/form-data", **auth_headers
88+
)
89+
90+
self.assertEqual(201, response.status_code)
91+
self.assertEqual(2, File.objects.count())
1192

1293

1394
class StandardUploadAdminTests(TestCase):

0 commit comments

Comments
 (0)