<reponame>beijbom/coralnet<filename>project/async_media/tests.py
import os
from unittest import skipIf

from bs4 import BeautifulSoup
from django.conf import settings
from django.urls import reverse
from easy_thumbnails.files import get_thumbnailer

from lib.tests.utils import BasePermissionTest, ClientTest


class PermissionTest(BasePermissionTest):

    def test_media_ajax(self):
        url = reverse('async_media:media_ajax')

        self.assertPermissionLevel(
            url, self.SIGNED_OUT, is_json=True, post_data={})

    def test_media_poll_ajax(self):
        url = reverse('async_media:media_poll_ajax')

        self.assertPermissionLevel(
            url, self.SIGNED_OUT, is_json=True, post_data={})


@skipIf(
    os.name == 'nt'
    and settings.DEFAULT_FILE_STORAGE == 'lib.storage_backends.MediaStorageS3',
    "Fetching existing thumbnails doesn't work with Windows + S3, because"
    " easy-thumbnails uses os.path for storage path separators")
class BrowseImagesThumbnailsTest(ClientTest):
    """
    Test the thumbnail functionality in Browse Images.
    """
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        cls.user = cls.create_user()
        cls.source = cls.create_source(cls.user)
        cls.browse_url = reverse('browse_images', args=[cls.source.pk])

    def test_load_existing_thumbnail(self):
        img = self.upload_image(self.user, self.source)

        # Generate thumbnail before loading browse page
        thumbnail = get_thumbnailer(img.original_file).get_thumbnail(
            dict(size=(150, 150)), generate=True)

        # Load browse page
        response = self.client.get(self.browse_url)
        response_soup = BeautifulSoup(response.content, 'html.parser')
        thumb_image = response_soup.find('img', class_='thumb')

        self.assertEqual(
            thumbnail.url, thumb_image.attrs.get('src'),
            msg="Existing thumbnail should be loaded on the browse page")

    def test_generate_and_retrieve_thumbnail(self):
        img = self.upload_image(self.user, self.source)

        # Load browse page
        response = self.client.get(self.browse_url)
        response_soup = BeautifulSoup(response.content, 'html.parser')
        thumb_image = response_soup.find('img', class_='thumb')
        request_hash = thumb_image.attrs.get('data-async-request-hash')

        # Generate thumbnail from browse page's hash (this is usually
        # initiated via Javascript).
        data = {'hashes[]': [request_hash]}
        self.client.post(reverse('async_media:media_ajax'), data=data)

        # Retrieve generated thumbnail
        data = dict(first_hash=request_hash)
        response = self.client.post(
            reverse('async_media:media_poll_ajax'), data=data)

        # Directly get thumbnail URL for assertion purposes
        thumbnail = get_thumbnailer(img.original_file).get_thumbnail(
            dict(size=(150, 150)), generate=False)

        self.assertDictEqual(
            response.json(),
            dict(
                media=[dict(index=0, url=thumbnail.url)],
                mediaRemaining=False),
            msg="Thumbnail should have been retrieved via async-media request")

import os
from unittest import skipIf

from bs4 import BeautifulSoup
from django.conf import settings
from django.urls import reverse
from easy_thumbnails.files import get_thumbnailer

from lib.tests.utils import BasePermissionTest, ClientTest


class PermissionTest(BasePermissionTest):

    def test_media_ajax(self):
        url = reverse('async_media:media_ajax')

        self.assertPermissionLevel(
            url, self.SIGNED_OUT, is_json=True, post_data={})

    def test_media_poll_ajax(self):
        url = reverse('async_media:media_poll_ajax')

        self.assertPermissionLevel(
            url, self.SIGNED_OUT, is_json=True, post_data={})


@skipIf(
    os.name == 'nt'
    and settings.DEFAULT_FILE_STORAGE == 'lib.storage_backends.MediaStorageS3',
    "Fetching existing thumbnails doesn't work with Windows + S3, because"
    " easy-thumbnails uses os.path for storage path separators")
class BrowseImagesThumbnailsTest(ClientTest):
    """
    Test the thumbnail functionality in Browse Images.
    """
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        cls.user = cls.create_user()
        cls.source = cls.create_source(cls.user)
        cls.browse_url = reverse('browse_images', args=[cls.source.pk])

    def test_load_existing_thumbnail(self):
        img = self.upload_image(self.user, self.source)

        # Generate thumbnail before loading browse page
        thumbnail = get_thumbnailer(img.original_file).get_thumbnail(
            dict(size=(150, 150)), generate=True)

        # Load browse page
        response = self.client.get(self.browse_url)
        response_soup = BeautifulSoup(response.content, 'html.parser')
        thumb_image = response_soup.find('img', class_='thumb')

        self.assertEqual(
            thumbnail.url, thumb_image.attrs.get('src'),
            msg="Existing thumbnail should be loaded on the browse page")

    def test_generate_and_retrieve_thumbnail(self):
        img = self.upload_image(self.user, self.source)

        # Load browse page
        response = self.client.get(self.browse_url)
        response_soup = BeautifulSoup(response.content, 'html.parser')
        thumb_image = response_soup.find('img', class_='thumb')
        request_hash = thumb_image.attrs.get('data-async-request-hash')

        # Generate thumbnail from browse page's hash (this is usually
        # initiated via Javascript).
        data = {'hashes[]': [request_hash]}
        self.client.post(reverse('async_media:media_ajax'), data=data)

        # Retrieve generated thumbnail
        data = dict(first_hash=request_hash)
        response = self.client.post(
            reverse('async_media:media_poll_ajax'), data=data)

        # Directly get thumbnail URL for assertion purposes
        thumbnail = get_thumbnailer(img.original_file).get_thumbnail(
            dict(size=(150, 150)), generate=False)

        self.assertDictEqual(
            response.json(),
            dict(
                media=[dict(index=0, url=thumbnail.url)],
                mediaRemaining=False),
            msg="Thumbnail should have been retrieved via async-media request")
Test the thumbnail functionality in Browse Images. # Generate thumbnail before loading browse page # Load browse page # Load browse page # Generate thumbnail from browse page's hash (this is usually # initiated via Javascript). # Retrieve generated thumbnail # Directly get thumbnail URL for assertion purposes