diff options
Diffstat (limited to 'pyblackbird_cc/resources/tests/test_views.py')
-rw-r--r-- | pyblackbird_cc/resources/tests/test_views.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/pyblackbird_cc/resources/tests/test_views.py b/pyblackbird_cc/resources/tests/test_views.py new file mode 100644 index 0000000..e71b3a2 --- /dev/null +++ b/pyblackbird_cc/resources/tests/test_views.py @@ -0,0 +1,155 @@ +import unittest + +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 ..models import ResourceCategory +from ..models import ResourceType +from ..views import create_resource + + +class PDFFileUploadTestCase(TestCase): + def setUp(self): + self.url = reverse("resources:create_resource") + self.two_page_pdf = "pyblackbird_cc/resources/tests/testdata/two_page.pdf" + self.seven_page_pdf = "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 |