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
|
import unittest
from pathlib import Path
import pytest
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import RequestFactory
from django.test import TestCase
from django.urls import reverse
from .. import services
from ..forms import ResourceCreateForm
from ..models import ResourceCategory
from ..models import ResourceType
from ..views import create_resource
pytestmark = pytest.mark.django_db
def test_create_featured_resource_view(client):
url = reverse("resources:create_featured")
response = client.get(url)
assert response.status_code == 302
def test_create_resource_view(client):
url = reverse("resources:create_resource")
response = client.get(url)
assert response.status_code == 302
@pytest.mark.django_db
def test_create_resource_has_form(client):
User = get_user_model()
user = User.objects.create_user(email="testuser@example.com", password="12345")
client.login(email="testuser@example.com", password="12345")
url = reverse("resources:create_resource")
response = client.get(url)
assert response.status_code == 200
assert "form" in response.context
assert isinstance(response.context["form"], ResourceCreateForm)
class PDFFileUploadTestCase(TestCase):
def setUp(self):
self.url = reverse("resources:create_resource")
self.two_page_pdf = Path(
settings.BASE_DIR / "pyblackbird_cc" / "resources/tests/testdata/two_page.pdf"
)
self.seven_page_pdf = Path(
settings.BASE_DIR / "pyblackbird_cc" / "resources/tests/testdata/seven_page.pdf"
)
# Create a test user
self.email = "testuser@example.com"
self.password = "testpassword"
self.user = get_user_model().objects.create_user(
email=self.email,
password=self.password,
)
# Log in the test user
self.client.login(email=self.email, password=self.password)
# Open the test files and create SimpleUploadedFile objects
with open(self.two_page_pdf, "rb") as file:
self.uploaded_two_page_pdf = SimpleUploadedFile(
"two_page.pdf",
file.read(),
content_type="application/pdf",
)
with open(self.seven_page_pdf, "rb") as file:
self.uploaded_seven_page_pdf = SimpleUploadedFile(
"seven_page.pdf",
file.read(),
content_type="application/pdf",
)
def tearDown(self):
# Close the SimpleUploadedFile objects
self.uploaded_two_page_pdf.close()
self.uploaded_seven_page_pdf.close()
def test_file_upload_is_pdf(self):
"""
Test that a file can be uploaded successfully using our create_resource view.
"""
response = self.client.post(
self.url,
{"pdf_files": [self.uploaded_two_page_pdf, self.uploaded_seven_page_pdf]},
)
two_page_metadata = services.get_pdf_metadata_from_path(self.two_page_pdf)
seven_page_metadata = services.get_pdf_metadata_from_path(self.seven_page_pdf)
image_files_two_pager = list(services.export_pages_as_images(self.two_page_pdf))
image_files_seven_pager = list(
services.export_pages_as_images(self.seven_page_pdf),
)
self.assertEqual(two_page_metadata.n_pages, 2)
self.assertEqual(seven_page_metadata.n_pages, 7)
self.assertGreater(two_page_metadata.file_size, 0)
self.assertGreater(seven_page_metadata.file_size, 0)
self.assertEqual(len(image_files_two_pager), 2)
self.assertEqual(len(image_files_seven_pager), 7)
self.assertEqual(response.status_code, 200)
@unittest.skip("Currently not able to mock S3 API at this point")
class TestCreateResourceUsingResourceFactory(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.user = User.objects.create_user(
username="testuser",
password="testpassword",
)
self.two_page_pdf = "resources/tests/testdata/two_page.pdf"
self.seven_page_pdf = "resources/tests/testdata/seven_page.pdf"
# Create resource type
self.resource_type = ResourceType.objects.create(name="Test Resource Type")
# Create resource categories
self.main_resource_category = ResourceCategory.objects.create(
name="Test Main Category",
)
self.additional_resource_category = ResourceCategory.objects.create(
name="Test Additional Category",
)
def test_post_pdf(self):
# Open the test files and create SimpleUploadedFile objects
with open(self.two_page_pdf, "rb") as file:
uploaded_two_page_pdf = SimpleUploadedFile(
"two_page.pdf",
file.read(),
content_type="application/pdf",
)
with open(self.seven_page_pdf, "rb") as file:
uploaded_seven_page_pdf = SimpleUploadedFile(
"seven_page.pdf",
file.read(),
content_type="application/pdf",
)
# Create a thumbnail file (you can use a dummy file for testing)
thumbnail_file = SimpleUploadedFile(
"thumbnail.jpg",
b"thumbnail_content",
content_type="image/jpeg",
)
# Prepare the form data
form_data = {
"name": "Test Resource",
"description": "Test Description",
"resource_type": self.resource_type.id,
"age_range": "5-7",
"curriculum": "English",
"main_resource_category": self.main_resource_category.id,
"additional_resource_category": self.additional_resource_category.id,
"pdf_files": [uploaded_two_page_pdf, uploaded_seven_page_pdf],
"thumbnail_files": [thumbnail_file],
}
# Prepare the form files
form_files = {
"pdf_files": [uploaded_two_page_pdf, uploaded_seven_page_pdf],
"thumbnail_files": [thumbnail_file],
}
# Create the request object with form data and files
request = self.factory.post("/create/", data=form_data)
request.user = self.user
# TODO mock the call to the Spaces S3 service inside the view
# Call the create_resource view function
response = create_resource(request)
# Assert the response status code
self.assertEqual(
response.status_code,
302,
) # Assuming a successful form submission redirects (status code 302)
# Add more assertions as needed
|