Ejemplo n.º 1
0
    def test_can_edit_with_discovery_edit_permission(self):
        uploaded_photo = get_uploaded_file('transparent.png')
        item = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
        self.detail_url = reverse(
            'admin:discovery_primaryheroimageupload_change', args=(item.pk, ))
        user = user_factory(email='*****@*****.**')
        self.grant_permission(user, 'Discovery:Edit')
        self.client.login(email=user.email)
        response = self.client.get(self.detail_url, follow=True)
        assert response.status_code == 200
        content = response.content.decode('utf-8')
        assert 'transparent.jpg' in content

        updated_photo = get_uploaded_file('preview_4x3.jpg')
        response = self.client.post(self.detail_url,
                                    dict(custom_image=updated_photo),
                                    follow=True)
        assert response.status_code == 200
        item.reload()
        assert PrimaryHeroImage.objects.count() == 1
        assert item.custom_image == 'hero-featured-image/preview_4x3.jpg'
        assert os.path.exists(
            os.path.join(settings.MEDIA_ROOT, 'hero-featured-image',
                         'preview_4x3.jpg'))
        assert os.path.exists(
            os.path.join(settings.MEDIA_ROOT, 'hero-featured-image', 'thumbs',
                         'preview_4x3.jpg'))
        width, height = get_image_dimensions(
            os.path.join(settings.MEDIA_ROOT, 'hero-featured-image',
                         'preview_4x3.jpg'))
        t_width, t_height = get_image_dimensions(
            os.path.join(settings.MEDIA_ROOT, 'hero-featured-image', 'thumbs',
                         'preview_4x3.jpg'))
        assert width <= 960 and height <= 640
        assert t_width <= 150 and t_height <= 120
Ejemplo n.º 2
0
    def setUp(self):
        self.url = reverse_ns('hero-primary-list', api_version='v5')
        uploaded_photo_1 = get_uploaded_file('animated.png')
        uploaded_photo_2 = get_uploaded_file('non-animated.png')
        uploaded_photo_3 = get_uploaded_file('preview_4x3.jpg')
        uploaded_photo_4 = get_uploaded_file('transparent.png')

        self.phi_a = PrimaryHeroImage.objects.create(custom_image=uploaded_photo_1)
        self.phi_b = PrimaryHeroImage.objects.create(custom_image=uploaded_photo_2)
        self.phi_c = PrimaryHeroImage.objects.create(custom_image=uploaded_photo_3)
        self.phi_d = PrimaryHeroImage.objects.create(custom_image=uploaded_photo_4)
Ejemplo n.º 3
0
    def test_photo_too_big(self, mock_is_image_check, mock_is_animated_check):
        # Mock the ImageCheck object to pass checks on the uploaded image
        mock_is_image_check.return_value = True
        mock_is_image_check.return_value = False
        mock_is_animated_check.returned_value = False

        request = Mock()
        request.user = self.user
        data = {
            'username': self.user_profile.username,
            'email': self.user_profile.email
        }
        files = {'photo': get_uploaded_file('transparent.png')}
        form = UserEditForm(data,
                            files=files,
                            instance=self.user_profile,
                            request=request)

        # Creating the mock object instead of the uploaded file,
        # with a specific size over the limit
        upload_mock = MagicMock(spec=SimpleUploadedFile)
        upload_mock._name = 'transparent.png'
        upload_mock.size = 4 * 1024 * 1024 + 1
        upload_mock.content_type = 'image/png'

        # Injecting the mock object
        form.files['photo'] = upload_mock

        assert not form.is_valid()
        mock_is_image_check.assert_called()
        mock_is_animated_check.assert_called()
        assert form.errors == {
            'photo': [u'Please use images smaller than 4MB.']
        }
Ejemplo n.º 4
0
 def test_can_add_with_discovery_edit_permission(self):
     add_url = reverse('admin:discovery_primaryheroimageupload_add')
     user = user_factory()
     self.grant_permission(user, 'Admin:Tools')
     self.grant_permission(user, 'Discovery:Edit')
     self.client.login(email=user.email)
     response = self.client.get(add_url, follow=True)
     assert response.status_code == 200
     assert PrimaryHeroImage.objects.count() == 0
     photo = get_uploaded_file('preview_4x3.jpg')
     response = self.client.post(
         add_url,
         dict(custom_image=photo),
         follow=True)
     assert response.status_code == 200
     assert PrimaryHeroImage.objects.count() == 1
     item = PrimaryHeroImage.objects.get()
     assert item.custom_image == 'hero-featured-image/preview_4x3.png'
     assert os.path.exists(os.path.join(
         settings.MEDIA_ROOT, 'hero-featured-image', 'preview_4x3.png'))
     assert os.path.exists(os.path.join(
         settings.MEDIA_ROOT, 'hero-featured-image',
         'thumbs', 'preview_4x3.png'))
     width, height = get_image_dimensions(os.path.join(
         settings.MEDIA_ROOT, 'hero-featured-image', 'preview_4x3.png'))
     t_width, t_height = get_image_dimensions(os.path.join(
         settings.MEDIA_ROOT, 'hero-featured-image',
         'thumbs', 'preview_4x3.png'))
     assert width <= 960 and height <= 640
     assert t_width <= 150 and t_height <= 120
Ejemplo n.º 5
0
    def test_can_not_delete_without_discovery_edit_permission(self):
        uploaded_photo = get_uploaded_file('transparent.png')
        item = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
        src = os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image', 'transparent.png')
        dest = os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image', 'thumbs',
            'transparent.png')
        copy_stored_file(src, dest)
        delete_url = reverse(
            'admin:discovery_primaryheroimageupload_delete', args=(item.pk,)
        )
        user = user_factory()
        self.grant_permission(user, 'Admin:Tools')
        self.client.login(email=user.email)
        # Can not access delete confirmation page.
        response = self.client.get(delete_url, follow=True)
        assert response.status_code == 403
        assert PrimaryHeroImage.objects.filter(pk=item.pk).exists()
        assert os.path.exists(os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image', 'transparent.png'))
        assert os.path.exists(os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image', 'thumbs',
            'transparent.png'))

        # Can not actually delete either.
        response = self.client.post(
            delete_url, data={'post': 'yes'}, follow=True)
        assert response.status_code == 403
        assert PrimaryHeroImage.objects.filter(pk=item.pk).exists()
        assert os.path.exists(os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image', 'transparent.png'))
        assert os.path.exists(os.path.join(
            settings.MEDIA_ROOT, 'hero-featured-image',
            'thumbs', 'transparent.png'))
 def test_external_addon(self):
     addon = addon_factory(
         summary='Summary', homepage='https://foo.baa', version_kw={
             'channel': amo.RELEASE_CHANNEL_UNLISTED})
     uploaded_photo = get_uploaded_file('transparent.png')
     phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     hero = PrimaryHero.objects.create(
         disco_addon=DiscoveryItem.objects.create(addon=addon),
         select_image=phi,
         gradient_color='#008787',
         is_external=True)
     assert PrimaryHeroShelfSerializer(instance=hero).data == {
         'featured_image': self.image,
         'description': 'Summary',
         'gradient': {
             'start': GRADIENT_START_COLOR[1],
             'end': 'color-green-70'
         },
         'external': ExternalAddonSerializer(instance=addon).data,
     }
     assert ExternalAddonSerializer(instance=addon).data == {
         'id': addon.id,
         'guid': addon.guid,
         'homepage': {'en-US': str(addon.homepage)},
         'name': {'en-US': str(addon.name)},
         'type': 'extension',
     }
Ejemplo n.º 7
0
    def test_photo_too_big(self, mock_is_image_check, mock_is_animated_check):
        # Mock the ImageCheck object to pass checks on the uploaded image
        mock_is_image_check.return_value = True
        mock_is_image_check.return_value = False
        mock_is_animated_check.returned_value = False

        request = Mock()
        request.user = self.user
        data = {'username': self.user_profile.username,
                'email': self.user_profile.email}
        files = {'photo': get_uploaded_file('transparent.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=request)

        # Creating the mock object instead of the uploaded file,
        # with a specific size over the limit
        upload_mock = MagicMock(spec=SimpleUploadedFile)
        upload_mock._name = 'transparent.png'
        upload_mock.size = 4 * 1024 * 1024 + 1
        upload_mock.content_type = 'image/png'

        # Injecting the mock object
        form.files['photo'] = upload_mock

        assert not form.is_valid()
        mock_is_image_check.assert_called()
        mock_is_animated_check.assert_called()
        assert form.errors == {
            'photo': [u'Please use images smaller than 4MB.']
        }
Ejemplo n.º 8
0
 def setUp(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     self.phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     self.phi.update(modified=datetime(2021, 4, 8, 15, 16, 23, 42))
     self.expected_image_url = (
         'http://testserver/user-media/hero-featured-image/transparent.jpg'
         '?modified=1617894983')
Ejemplo n.º 9
0
 def test_can_list_with_discovery_edit_permission(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     user = user_factory(email='*****@*****.**')
     self.grant_permission(user, 'Discovery:Edit')
     self.client.login(email=user.email)
     response = self.client.get(self.list_url, follow=True)
     assert response.status_code == 200
     assert 'transparent.jpg' in response.content.decode('utf-8')
Ejemplo n.º 10
0
    def test_can_not_edit_without_discovery_edit_permission(self):
        uploaded_photo = get_uploaded_file('transparent.png')
        item = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
        detail_url = reverse('admin:discovery_primaryheroimageupload_change',
                             args=(item.pk, ))
        user = user_factory(email='*****@*****.**')
        self.client.login(email=user.email)
        response = self.client.get(detail_url, follow=True)
        assert response.status_code == 403
        updated_photo = get_uploaded_file('non-animated.png')

        response = self.client.post(detail_url,
                                    dict(custom_image=updated_photo),
                                    follow=True)
        assert response.status_code == 403
        item.reload()
        assert PrimaryHeroImage.objects.count() == 1
        assert item.custom_image == 'hero-featured-image/transparent.jpg'
Ejemplo n.º 11
0
 def test_can_not_add_without_discovery_edit_permission(self):
     add_url = reverse('admin:discovery_primaryheroimageupload_add')
     user = user_factory(email='*****@*****.**')
     self.client.login(email=user.email)
     response = self.client.get(add_url, follow=True)
     assert response.status_code == 403
     photo = get_uploaded_file('transparent.png')
     response = self.client.post(add_url, dict(custom_image=photo), follow=True)
     assert response.status_code == 403
     assert PrimaryHeroImage.objects.count() == 0
Ejemplo n.º 12
0
 def test_image_url(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     ph = PrimaryHero.objects.create(
         disco_addon=DiscoveryItem.objects.create(addon=addon_factory()),
         select_image=phi)
     assert ph.image_url == (
         'http://testserver/user-media/hero-featured-image/transparent.jpg')
     ph.update(select_image=None)
     assert ph.image_url is None
Ejemplo n.º 13
0
 def test_photo_modified(self, update_mock):
     request = Mock()
     request.user = self.user
     data = {'username': self.user_profile.username,
             'email': self.user_profile.email}
     files = {'photo': get_uploaded_file('transparent.png')}
     form = UserEditForm(data, files=files, instance=self.user_profile,
                         request=request)
     assert form.is_valid()
     form.save()
     assert update_mock.called
Ejemplo n.º 14
0
    def test_photo_invalid_though_content_type_is_correct(self):
        request = Mock()
        request.user = self.user
        data = {'username': self.user_profile.username,
                'email': self.user_profile.email}
        files = {'photo': get_uploaded_file('non-image.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=request)

        assert not form.is_valid()
        assert form.errors == {'photo': [u'Images must be either PNG or JPG.']}
Ejemplo n.º 15
0
    def test_photo_invalid_animated(self):
        request = Mock()
        request.user = self.user
        data = {'username': self.user_profile.username,
                'email': self.user_profile.email}
        files = {'photo': get_uploaded_file('animated.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=request)

        assert not form.is_valid()
        assert form.errors == {'photo': [u'Images cannot be animated.']}
Ejemplo n.º 16
0
    def test_photo_modified(self, update_mock):
        dummy = Mock()
        dummy.user = self.user

        data = {'username': self.user_profile.username,
                'email': self.user_profile.email,
                'lang': 'en-US'}
        files = {'photo': get_uploaded_file('transparent.png')}
        form = UserEditForm(data, files=files, instance=self.user_profile,
                            request=dummy)
        assert form.is_valid()
        form.save()
        assert update_mock.called
Ejemplo n.º 17
0
 def test_icon(self, update_mock):
     collection = Collection.objects.get(pk=57181)
     # TODO(andym): altering this form is too complicated, can we simplify?
     form = forms.CollectionForm(
         {'listed': collection.listed,
          'slug': collection.slug,
          'name': collection.name},
         instance=collection,
         files={'icon': get_uploaded_file('transparent.png')},
         initial={'author': collection.author,
                  'application': collection.application})
     assert form.is_valid()
     form.save()
     assert update_mock.called
Ejemplo n.º 18
0
 def test_icon(self, update_mock):
     collection = Collection.objects.get(pk=57181)
     # TODO(andym): altering this form is too complicated, can we simplify?
     form = forms.CollectionForm(
         {'listed': collection.listed,
          'slug': collection.slug,
          'name': collection.name},
         instance=collection,
         files={'icon': get_uploaded_file('transparent.png')},
         initial={'author': collection.author,
                  'application': collection.application})
     assert form.is_valid()
     form.save()
     assert update_mock.called
Ejemplo n.º 19
0
    def test_photo_invalid_though_content_type_is_correct(self):
        request = Mock()
        request.user = self.user
        data = {
            'username': self.user_profile.username,
            'email': self.user_profile.email
        }
        files = {'photo': get_uploaded_file('non-image.png')}
        form = UserEditForm(data,
                            files=files,
                            instance=self.user_profile,
                            request=request)

        assert not form.is_valid()
        assert form.errors == {'photo': [u'Images must be either PNG or JPG.']}
Ejemplo n.º 20
0
    def test_photo_invalid_animated(self):
        request = Mock()
        request.user = self.user
        data = {
            'username': self.user_profile.username,
            'email': self.user_profile.email
        }
        files = {'photo': get_uploaded_file('animated.png')}
        form = UserEditForm(data,
                            files=files,
                            instance=self.user_profile,
                            request=request)

        assert not form.is_valid()
        assert form.errors == {'photo': [u'Images cannot be animated.']}
Ejemplo n.º 21
0
    def test_can_add_primary_hero_with_discovery_edit_permission(self):
        addon = addon_factory(name=u'BarFöo')
        item = DiscoveryItem.objects.create(addon=addon)
        uploaded_photo = get_uploaded_file('transparent.png')
        image = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
        self.detail_url = reverse(
            'admin:discovery_discoveryitem_change', args=(item.pk,)
        )
        user = user_factory()
        self.grant_permission(user, 'Admin:Tools')
        self.grant_permission(user, 'Discovery:Edit')
        self.client.login(email=user.email)
        response = self.client.get(self.detail_url, follow=True)
        assert response.status_code == 200
        content = response.content.decode('utf-8')
        assert 'BarFöo' in content
        assert 'No image selected' in content
        assert PrimaryHero.objects.count() == 0

        response = self.client.post(
            self.detail_url,
            dict(self._get_heroform(str(item.pk)), **{
                'addon': str(addon.pk),
                'custom_addon_name': 'Xäxâxàxaxaxa !',
                'recommendable': True,
                'primaryhero-0-gradient_color': '#054096',
                'primaryhero-0-image': '*****@*****.**',
                'primaryhero-0-select_image': image.pk,
            }),
            follow=True)
        assert response.status_code == 200
        item.reload()
        assert DiscoveryItem.objects.count() == 1
        assert PrimaryHero.objects.count() == 1
        assert item.addon == addon
        assert item.custom_addon_name == 'Xäxâxàxaxaxa !'
        assert item.recommendable is True
        hero = PrimaryHero.objects.last()
        hero.select_image == PrimaryHeroImage.objects.last()
        assert hero.image == '*****@*****.**'
        assert hero.select_image.pk == image.pk
        assert hero.gradient_color == '#054096'
        assert hero.disco_addon == item
 def test_basic(self):
     addon = addon_factory()
     uploaded_photo = get_uploaded_file('transparent.png')
     phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     hero = PrimaryHero.objects.create(
         disco_addon=DiscoveryItem.objects.create(
             addon=addon),
         description='Déscription',
         select_image=phi,
         gradient_color='#008787')
     data = PrimaryHeroShelfSerializer(instance=hero).data
     assert data == {
         'featured_image': self.image,
         'description': 'Déscription',
         'gradient': {
             'start': GRADIENT_START_COLOR[1],
             'end': 'color-green-70'
         },
         'addon': DiscoveryAddonSerializer(instance=addon).data,
     }
Ejemplo n.º 23
0
    def test_can_add_primary_hero(self):
        addon = addon_factory(name=u'BarFöo')
        item = PromotedAddon.objects.create(addon=addon)
        uploaded_photo = get_uploaded_file('transparent.png')
        image = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
        self.detail_url = reverse(self.detail_url_name, args=(item.pk, ))
        user = user_factory()
        self.grant_permission(user, 'Admin:Tools')
        self.grant_permission(user, 'Discovery:Edit')
        self.client.login(email=user.email)
        response = self.client.get(self.detail_url, follow=True)
        assert response.status_code == 200
        content = response.content.decode('utf-8')
        assert 'BarFöo' in content
        assert 'No image selected' in content
        assert PrimaryHero.objects.count() == 0

        response = self.client.post(self.detail_url,
                                    dict(
                                        self._get_heroform(item.pk),
                                        **self._get_approval_form(item, []),
                                        **{
                                            'primaryhero-0-gradient_color':
                                            '#054096',
                                            'primaryhero-0-select_image':
                                            image.pk,
                                            'primaryhero-0-description':
                                            'primary descriptíon',
                                        }),
                                    follow=True)
        assert response.status_code == 200
        item.reload()
        assert PromotedAddon.objects.count() == 1
        assert PrimaryHero.objects.count() == 1
        assert item.addon == addon
        hero = PrimaryHero.objects.last()
        hero.select_image == image
        assert hero.select_image.pk == image.pk
        assert hero.gradient_color == '#054096'
        assert hero.promoted_addon == item
        assert hero.description == 'primary descriptíon'
Ejemplo n.º 24
0
 def setUp(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     self.phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
Ejemplo n.º 25
0
 def setUp(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     self.phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     self.phi.update(modified=datetime(2021, 4, 8, 15, 16, 23, 42))
Ejemplo n.º 26
0
 def setUp(self):
     uploaded_photo = get_uploaded_file('transparent.png')
     self.phi = PrimaryHeroImage.objects.create(custom_image=uploaded_photo)
     self.image = (
         'http://testserver/user-media/hero-featured-image/transparent.jpg')