aboutsummaryrefslogblamecommitdiffstats
path: root/pyblackbird_cc/resources/tests/test_views.py
blob: e85dd9f2287a1c2f3b098aacd91e99fc73753ee5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
               
                        
 
             
                                







                                                             
                                      



                                     
                                  
 
 
                                               




                                              





                                              
                      
                                          
                           

                                                                                   
 

                                              
                                      

                                                                   

 


                                                       





                                                                                            






































































































































                                                                                        
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