Example #1
0
    def test_success(self):
        data = self.get_dict()
        header_url, footer_url = self.get_img_urls()

        img = open(get_image_path('persona-header.jpg'), 'rb')
        r_ajax = self.client.post(header_url, {'upload_image': img})
        data.update(header_hash=json.loads(r_ajax.content)['upload_hash'])

        img = open(get_image_path('persona-footer.jpg'), 'rb')
        r_ajax = self.client.post(footer_url, {'upload_image': img})
        data.update(footer_hash=json.loads(r_ajax.content)['upload_hash'])

        r = self.client.post(self.url, data)
        addon = Addon.objects.exclude(id=5579)[0]
        persona = addon.persona
        self.assertRedirects(
            r, reverse('personas.submit.done', args=[addon.slug]), 302)

        # Test for correct Addon and Persona values.
        eq_(unicode(addon.name), data['name'])

        eq_(sorted(addon.categories.values_list('id', flat=True)),
            sorted(Category.objects.values_list('id', flat=True)))

        tags = ', '.join(sorted(addon.tags.values_list('tag_text', flat=True)))
        eq_(tags, data['tags'])

        eq_(persona.persona_id, 0)
        eq_(persona.license_id, data['license'])

        eq_(persona.accentcolor, data['accentcolor'].lstrip('#'))
        eq_(persona.textcolor, data['textcolor'].lstrip('#'))

        user = UserProfile.objects.get(pk=999)
        eq_(persona.author, user.name)
        eq_(persona.display_username, user.username)

        v = addon.versions.all()
        eq_(len(v), 1)
        eq_(v[0].version, '0')

        # Test for header, footer, and preview images.
        dst = os.path.join(settings.PERSONAS_PATH, str(addon.id))

        img = os.path.join(dst, 'header.jpg')
        eq_(persona.footer, 'footer.jpg')
        eq_(os.path.exists(img), True)
        eq_(Image.open(img).size, (3000, 200))
        eq_(amo.PERSONA_IMAGE_SIZES['header'][1], (3000, 200))

        img = os.path.join(dst, 'footer.jpg')
        eq_(persona.footer, 'footer.jpg')
        eq_(os.path.exists(img), True)
        eq_(Image.open(img).size, (3000, 100))
        eq_(amo.PERSONA_IMAGE_SIZES['footer'][1], (3000, 100))

        img = os.path.join(dst, 'preview.jpg')
        eq_(os.path.exists(img), True)
        eq_(Image.open(img).size, (680, 100))
        eq_(amo.PERSONA_IMAGE_SIZES['header'][0], (680, 100))
Example #2
0
    def setUp(self):
        self.client.login(username='******', password='******')
        waffle.models.Switch.objects.get_or_create(name='mkt-themes',
                                                   active=True)

        License.objects.create(id=amo.LICENSE_COPYRIGHT.id)
        header_img = get_image_path('persona-header.jpg')
        res = self.client.post(
            reverse('submit.theme.upload', args=['persona_header']),
            {'upload_image': open(header_img, 'rb')})
        header_hash = json.loads(res.content)['upload_hash']
        footer_img = get_image_path('persona-footer.jpg')
        res = self.client.post(
            reverse('submit.theme.upload', args=['persona_footer']),
            {'upload_image': open(footer_img, 'rb')})
        footer_hash = json.loads(res.content)['upload_hash']
        self.data = {
            'category': '1',
            'name': 'testpersona',
            'slug': 'testslug',
            'summary': 'testsummary',
            'tags': 'tag1, tag2',
            'license': amo.LICENSE_COPYRIGHT.id,
            'accentcolor': '#000000',
            'textcolor': '#ffffff',
            'header_hash': header_hash,
            'footer_hash': footer_hash,
        }
Example #3
0
    def setUp(self):
        self.client.login(username='******', password='******')
        waffle.models.Switch.objects.get_or_create(name='mkt-themes',
                                                   active=True)

        License.objects.create(id=amo.LICENSE_COPYRIGHT.id)
        header_img = get_image_path('persona-header.jpg')
        res = self.client.post(reverse('submit.theme.upload',
                                       args=['persona_header']),
                               {'upload_image': open(header_img, 'rb')})
        header_hash = json.loads(res.content)['upload_hash']
        footer_img = get_image_path('persona-footer.jpg')
        res = self.client.post(reverse('submit.theme.upload',
                                       args=['persona_footer']),
                               {'upload_image': open(footer_img, 'rb')})
        footer_hash = json.loads(res.content)['upload_hash']
        self.data = {
            'category': '1',
            'name': 'testpersona',
            'slug': 'testslug',
            'summary': 'testsummary',
            'tags': 'tag1, tag2',
            'license': amo.LICENSE_COPYRIGHT.id,
            'accentcolor': '#000000',
            'textcolor': '#ffffff',
            'header_hash': header_hash,
            'footer_hash': footer_hash,
        }
Example #4
0
    def test_icon_modified(self, update_mock):
        name = 'transparent.png'
        form = forms.AddonFormMedia({'icon_upload_hash': name},
                                    request=self.request,
                                    instance=self.addon)

        img = get_image_path(name)
        dest = self.paths[1] / name
        shutil.copyfile(get_image_path(name), dest)

        assert form.is_valid()
        form.save(addon=self.addon)
        assert update_mock.called
Example #5
0
    def test_icon_modified(self, update_mock):
        name = 'transparent.png'
        form = forms.AddonFormMedia({'icon_upload_hash': name},
                                    request=self.request,
                                    instance=self.addon)

        img = get_image_path(name)
        dest = self.paths[1] / name
        shutil.copyfile(get_image_path(name), dest)

        assert form.is_valid()
        form.save(addon=self.addon)
        assert update_mock.called
Example #6
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                      delete=False)

    # resize_icon removes the original
    shutil.copyfile(img, src.name)

    src_image = Image.open(src.name)
    eq_(src_image.size, original_size)

    if isinstance(final_size, list):
        uploadto = user_media_path('addon_icons')
        os.makedirs(uploadto)
        for rsize, fsize in zip(resize_size, final_size):
            dest_name = str(path.path(uploadto) / '1234')

            tasks.resize_icon(src.name, dest_name, resize_size, locally=True)
            dest_image = Image.open(open('%s-%s.png' % (dest_name, rsize)))
            eq_(dest_image.size, fsize)

            if os.path.exists(dest_image.filename):
                os.remove(dest_image.filename)
            assert not os.path.exists(dest_image.filename)
    else:
        dest = tempfile.mktemp(suffix='.png')
        tasks.resize_icon(src.name, dest, resize_size, locally=True)
        dest_image = Image.open(dest)
        eq_(dest_image.size, final_size)

    assert not os.path.exists(src.name)
Example #7
0
 def test_preview_modified(self, update_mock):
     name = 'transparent.png'
     form = forms.PreviewForm({'upload_hash': name, 'position': 1})
     shutil.copyfile(get_image_path(name), os.path.join(self.dest, name))
     assert form.is_valid(), form.errors
     form.save(self.addon)
     assert update_mock.called
Example #8
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (82, 31)

    src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                      delete=False)

    # resize_icon removes the original
    shutil.copyfile(img, src.name)

    src_image = Image.open(src.name)
    eq_(src_image.size, original_size)

    if isinstance(final_size, list):
        for rsize, fsize in zip(resize_size, final_size):
            dest_name = str(path.path(settings.ADDON_ICONS_PATH) / '1234')

            resize_icon(src.name, dest_name, resize_size)
            dest_image = Image.open("%s-%s.png" % (dest_name, rsize))
            eq_(dest_image.size, fsize)

            if os.path.exists(dest_image.filename):
                os.remove(dest_image.filename)
            assert not os.path.exists(dest_image.filename)
    else:
        dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png")
        resize_icon(src.name, dest.name, resize_size)
        dest_image = Image.open(dest.name)
        eq_(dest_image.size, final_size)

    assert not os.path.exists(src.name)
Example #9
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    for rsize, fsize in zip(resize_size, final_size):
        dest_name = os.path.join(settings.ADDON_ICONS_PATH, '1234')
        src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix='.png',
                                          delete=False)
        # resize_icon removes the original, copy it to a tempfile and use that.
        shutil.copyfile(img, src.name)
        # Sanity check.
        with storage.open(src.name) as fp:
            src_image = Image.open(fp)
            src_image.load()
        eq_(src_image.size, original_size)

        val = tasks.resize_icon(src.name, dest_name, resize_size, locally=True)
        eq_(val, {'icon_hash': 'bb362450'})
        with storage.open('%s-%s.png' % (dest_name, rsize)) as fp:
            dest_image = Image.open(fp)
            dest_image.load()

        # Assert that the width is always identical.
        eq_(dest_image.size[0], fsize[0])
        # Assert that the height can be a wee bit fuzzy.
        assert -1 <= dest_image.size[1] - fsize[1] <= 1, (
            'Got width %d, expected %d' %
                (fsize[1], dest_image.size[1]))

        if os.path.exists(dest_image.filename):
            os.remove(dest_image.filename)
        assert not os.path.exists(dest_image.filename)

    assert not os.path.exists(src.name)
Example #10
0
    def preview_add(self, amount=1):
        img = get_image_path('mozilla.png')
        src_image = open(img, 'rb')

        data = dict(upload_image=src_image)
        data_formset = self.formset_media(**data)
        url = self.preview_upload

        r = self.client.post(url, data_formset)

        details = json.loads(r.content)
        upload_hash = details['upload_hash']

        # Create and post with the formset.
        fields = []
        for i in range(amount):
            fields.append(
                self.formset_new_form(caption='hi',
                                      upload_hash=upload_hash,
                                      position=i))
        data_formset = self.formset_media(*fields)

        self.media_edit_url

        r = self.client.post(self.media_edit_url, data_formset)
Example #11
0
 def _upload_image(self, url, image_file=None):
     if not image_file:
         image_file = get_image_path('non-animated.png')
     with open(image_file, 'rb') as data:
         rp = self.client.post(url, {'upload_image': data})
     eq_(rp.status_code, 200)
     return json.loads(rp.content)['upload_hash']
Example #12
0
    def test_create_fileupload(self):
        self.post()

        upload = FileUpload.objects.get(name='animated.png')
        eq_(upload.name, 'animated.png')
        data = open(get_image_path('animated.png'), 'rb').read()
        eq_(storage.open(upload.path).read(), data)
Example #13
0
 def _upload_image(self, url, image_file=None):
     if not image_file:
         image_file = get_image_path('non-animated.png')
     with open(image_file, 'rb') as data:
         rp = self.client.post(url, {'upload_image': data})
     eq_(rp.status_code, 200)
     return json.loads(rp.content)['upload_hash']
Example #14
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (82, 31)

    src = tempfile.NamedTemporaryFile(mode='r+w+b',
                                      suffix=".png",
                                      delete=False)

    # resize_icon removes the original
    shutil.copyfile(img, src.name)

    src_image = Image.open(src.name)
    eq_(src_image.size, original_size)

    if isinstance(final_size, list):
        for rsize, fsize in zip(resize_size, final_size):
            dest_name = str(path.path(settings.ADDON_ICONS_PATH) / '1234')

            tasks.resize_icon(src.name, dest_name, resize_size)
            dest_image = Image.open("%s-%s.png" % (dest_name, rsize))
            eq_(dest_image.size, fsize)

            if os.path.exists(dest_image.filename):
                os.remove(dest_image.filename)
            assert not os.path.exists(dest_image.filename)
    else:
        dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png")
        tasks.resize_icon(src.name, dest.name, resize_size)
        dest_image = Image.open(dest.name)
        eq_(dest_image.size, final_size)

    assert not os.path.exists(src.name)
Example #15
0
    def test_edit_uploadedicon(self):
        img = get_image_path('mozilla.png')
        src_image = open(img, 'rb')

        response = self.client.post(self.icon_upload,
                                    dict(upload_image=src_image))
        response_json = json.loads(response.content)
        webapp = self.get_webapp()

        # Now, save the form so it gets moved properly.
        data = dict(icon_type='image/png',
                    icon_upload_hash=response_json['upload_hash'])
        data_formset = self.formset_media(blank_kw=self.new_preview_hash(),
                                          **data)

        r = self.client.post(self.edit_url, data_formset)
        self.assertNoFormErrors(r)
        webapp = self.get_webapp()

        # Unfortunate hardcoding of URL
        url = webapp.get_icon_url(64)
        assert ('addon_icons/%s/%s' % (webapp.id / 1000, webapp.id)) in url, (
            'Unexpected path: %r' % url)

        eq_(data['icon_type'], 'image/png')

        # Check that it was actually uploaded
        dirname = os.path.join(settings.ADDON_ICONS_PATH,
                               '%s' % (webapp.id / 1000))
        dest = os.path.join(dirname, '%s-32.png' % webapp.id)

        eq_(os.path.exists(dest), True)

        eq_(Image.open(dest).size, (32, 12))
Example #16
0
 def test_preview_modified(self, update_mock):
     name = "transparent.png"
     form = forms.PreviewForm({"caption": "test", "upload_hash": name, "position": 1})
     shutil.copyfile(get_image_path(name), os.path.join(self.dest, name))
     assert form.is_valid(), form.errors
     form.save(self.addon)
     assert update_mock.called
Example #17
0
def test_resize_image_asset(ia):
    _instance = mock.Mock()
    ia.objects.get.return_value = _instance

    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    with storage.open(img) as fp:
        src_image = Image.open(fp)
        eq_(src_image.size, original_size)

    dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix='.png')
    # Make it resize to some arbitrary size that's larger on both sides than
    # the source image. This is where the behavior differs from resize_image.
    tasks.resize_imageasset(img,
                            dest.name, (500, 500),
                            instance='foo',
                            locally=True)
    with storage.open(dest.name) as fp:
        dest_image = Image.open(fp)
        eq_(dest_image.size, (500, 500))

    # Assert that the asset's instance gets updated with the new hue.
    ia.objects.get.assert_called_with(pk='foo')
    _instance.update.assert_called_with(hue=123)
Example #18
0
    def test_edit_uploadedicon(self):
        img = get_image_path('mozilla.png')
        src_image = open(img, 'rb')

        response = self.client.post(self.icon_upload,
                                    dict(upload_image=src_image))
        response_json = json.loads(response.content)
        webapp = self.get_webapp()

        # Now, save the form so it gets moved properly.
        data = dict(icon_type='image/png',
                    icon_upload_hash=response_json['upload_hash'])
        data_formset = self.formset_media(blank_kw=self.new_preview_hash(),
                                          **data)

        r = self.client.post(self.edit_url, data_formset)
        self.assertNoFormErrors(r)
        webapp = self.get_webapp()

        # Unfortunate hardcoding of URL
        url = webapp.get_icon_url(64)
        assert ('addon_icons/%s/%s' %
                (webapp.id / 1000, webapp.id)) in url, ('Unexpected path: %r' %
                                                        url)

        eq_(data['icon_type'], 'image/png')

        # Check that it was actually uploaded
        dirname = os.path.join(settings.ADDON_ICONS_PATH,
                               '%s' % (webapp.id / 1000))
        dest = os.path.join(dirname, '%s-32.png' % webapp.id)

        eq_(os.path.exists(dest), True)

        eq_(Image.open(dest).size, (32, 12))
Example #19
0
    def test_icon_size_req(self):
        filehandle = open(get_image_path('sunbird-small.png'), 'rb')

        res = self.client.post(self.icon_upload, {'upload_image': filehandle})
        response_json = json.loads(res.content)
        assert any(e == 'Icons must be at least 128px by 128px.' for e in
                   response_json['errors'])
Example #20
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    for rsize, fsize in zip(resize_size, final_size):
        dest_name = os.path.join(settings.ADDON_ICONS_PATH, '1234')
        src = tempfile.NamedTemporaryFile(mode='r+w+b',
                                          suffix='.png',
                                          delete=False)
        # resize_icon removes the original, copy it to a tempfile and use that.
        shutil.copyfile(img, src.name)
        # Sanity check.
        with storage.open(src.name) as fp:
            src_image = Image.open(fp)
            src_image.load()
        eq_(src_image.size, original_size)

        val = tasks.resize_icon(src.name, dest_name, resize_size, locally=True)
        eq_(val, {'icon_hash': 'bb362450'})
        with storage.open('%s-%s.png' % (dest_name, rsize)) as fp:
            dest_image = Image.open(fp)
            dest_image.load()

        # Assert that the width is always identical.
        eq_(dest_image.size[0], fsize[0])
        # Assert that the height can be a wee bit fuzzy.
        assert -1 <= dest_image.size[1] - fsize[1] <= 1, (
            'Got width %d, expected %d' % (fsize[1], dest_image.size[1]))

        if os.path.exists(dest_image.filename):
            os.remove(dest_image.filename)
        assert not os.path.exists(dest_image.filename)

    assert not os.path.exists(src.name)
Example #21
0
    def test_edit_media_uploadedicon(self):
        img = get_image_path('mozilla.png')
        src_image = open(img, 'rb')

        data = dict(upload_image=src_image)

        response = self.client.post(self.icon_upload, data)
        response_json = json.loads(response.content)
        addon = self.get_addon()

        # Now, save the form so it gets moved properly.
        data = dict(icon_type='image/png',
                    icon_upload_hash=response_json['upload_hash'])
        data_formset = self.formset_media(**data)

        r = self.client.post(self.media_edit_url, data_formset)
        eq_(r.context['form'].errors, {})
        addon = self.get_addon()

        # Unfortunate hardcoding of URL
        url = addon.get_icon_url(64)
        assert ('addon_icons/3/%s' % addon.id) in url, (
            'Unexpected path: %r' % url)

        eq_(data['icon_type'], 'image/png')

        # Check that it was actually uploaded
        dirname = os.path.join(user_media_path('addon_icons'),
                               '%s' % (addon.id / 1000))
        dest = os.path.join(dirname, '%s-32.png' % addon.id)

        assert storage.exists(dest)

        eq_(Image.open(storage.open(dest)).size, (32, 12))
Example #22
0
    def test_create_fileupload(self):
        self.post()

        upload = FileUpload.objects.get(name='animated.png')
        eq_(upload.name, 'animated.png')
        data = open(get_image_path('animated.png'), 'rb').read()
        eq_(storage.open(upload.path).read(), data)
Example #23
0
    def test_edit_uploadedicon(self):
        img = get_image_path("mozilla-sq.png")
        src_image = open(img, "rb")

        response = self.client.post(self.icon_upload, dict(upload_image=src_image))
        response_json = json.loads(response.content)
        webapp = self.get_webapp()

        # Now, save the form so it gets moved properly.
        data = dict(icon_type="image/png", icon_upload_hash=response_json["upload_hash"])
        data_formset = self.formset_media(prev_blank=self.new_preview_hash(), **data)

        r = self.client.post(self.edit_url, data_formset)
        self.assertNoFormErrors(r)
        webapp = self.get_webapp()

        # Unfortunate hardcoding of URL.
        url = webapp.get_icon_url(64)
        assert ("addon_icons/%s/%s" % (webapp.id / 1000, webapp.id)) in url, "Unexpected path: %r" % url

        eq_(data["icon_type"], "image/png")

        # Check that it was actually uploaded.
        dirname = os.path.join(settings.ADDON_ICONS_PATH, "%s" % (webapp.id / 1000))
        dest = os.path.join(dirname, "%s-32.png" % webapp.id)

        eq_(storage.exists(dest), True)

        eq_(Image.open(storage.open(dest)).size, (32, 32))
Example #24
0
 def test_preview_modified(self, update_mock):
     name = 'transparent.png'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     shutil.copyfile(get_image_path(name), os.path.join(self.dest, name))
     assert form.is_valid()
     form.save(self.addon)
     assert update_mock.called
Example #25
0
 def test_img_size(self):
     img = get_image_path('mozilla.png')
     for url, img_type in zip(self.get_img_urls(), ('header', 'footer')):
         r_ajax = self.client.post(url, {'upload_image': open(img, 'rb')})
         r_json = json.loads(r_ajax.content)
         w, h = amo.PERSONA_IMAGE_SIZES.get(img_type)[1]
         eq_(r_json['errors'], ['Image must be exactly %s pixels wide '
                                'and %s pixels tall.' % (w, h)])
Example #26
0
 def test_preview_size(self):
     name = "non-animated.gif"
     form = forms.PreviewForm({"caption": "test", "upload_hash": name, "position": 1})
     with storage.open(os.path.join(self.dest, name), "wb") as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid(), form.errors
     form.save(self.addon)
     eq_(self.addon.previews.all()[0].sizes, {u"image": [250, 297], u"thumbnail": [180, 214]})
 def test_upload_unlisted_addon(self, validate_mock):
     """Unlisted addons are validated as "self hosted" addons."""
     validate_mock.return_value = '{}'
     self.url = reverse('devhub.upload_unlisted')
     data = open(get_image_path('animated.png'), 'rb')
     self.client.post(self.url, {'upload': data})
     # Make sure it was called with listed=False.
     assert not validate_mock.call_args[1]['listed']
Example #28
0
    def test_icon_animated(self):
        filehandle = open(get_image_path('animated.png'), 'rb')
        data = {'upload_image': filehandle}

        res = self.client.post(self.preview_upload, data)
        response_json = json.loads(res.content)

        eq_(response_json['errors'][0], u'Icons cannot be animated.')
Example #29
0
 def test_img_size(self):
     img = get_image_path('mozilla.png')
     for url, img_type in zip(self.get_img_urls(), ('header', 'footer')):
         r_ajax = self.client.post(url, {'upload_image': open(img, 'rb')})
         r_json = json.loads(r_ajax.content)
         w, h = amo.PERSONA_IMAGE_SIZES.get(img_type)[1]
         eq_(r_json['errors'], ['Image must be exactly %s pixels wide '
                                'and %s pixels tall.' % (w, h)])
Example #30
0
 def test_preview_size(self):
     name = 'non-animated.gif'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     shutil.copyfile(get_image_path(name), os.path.join(self.dest, name))
     assert form.is_valid()
     form.save(self.addon)
     eq_(self.addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [126, 150]})
Example #31
0
 def test_preview_modified(self, update_mock):
     addon = Addon.objects.get(pk=3615)
     name = "transparent.png"
     form = forms.PreviewForm({"caption": "test", "upload_hash": name, "position": 1})
     with storage.open(os.path.join(self.dest, name), "w") as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid()
     form.save(addon)
     assert update_mock.called
 def test_upload_sideload_addon(self, validate_mock):
     """Sideload addons are validated as "self-hosted" addons."""
     validate_mock.return_value = json.dumps(amo.VALIDATOR_SKELETON_RESULTS)
     self.url = reverse('devhub.upload_sideload')
     data = open(get_image_path('animated.png'), 'rb')
     self.client.post(self.url, {'upload': data})
     # Make sure it was called with listed=False.
     assert not validate_mock.call_args[1]['listed']
     # No automated signing for sideload add-ons.
     assert FileUpload.objects.get().automated_signing is False
Example #33
0
 def test_preview_size(self):
     name = 'non-animated.gif'
     form = forms.PreviewForm({'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'wb') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid(), form.errors
     form.save(self.addon)
     eq_(self.addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [100, 119]})
 def test_upload_unlisted_addon(self, validate_mock):
     """Unlisted addons are validated as "self-hosted" addons."""
     validate_mock.return_value = '{"errors": 0}'
     self.url = reverse('devhub.upload_unlisted')
     data = open(get_image_path('animated.png'), 'rb')
     self.client.post(self.url, {'upload': data})
     # Make sure it was called with listed=False.
     assert not validate_mock.call_args[1]['listed']
     # Automated signing enabled for unlisted, non-sideload add-ons.
     assert FileUpload.objects.get().automated_signing is True
Example #35
0
 def test_upload_unlisted_addon(self, validate_mock):
     """Unlisted addons are validated as "self-hosted" addons."""
     validate_mock.return_value = '{"errors": 0}'
     self.url = reverse('devhub.upload_unlisted')
     data = open(get_image_path('animated.png'), 'rb')
     self.client.post(self.url, {'upload': data})
     # Make sure it was called with listed=False.
     assert not validate_mock.call_args[1]['listed']
     # Automated signing enabled for unlisted, non-sideload add-ons.
     assert FileUpload.objects.get().automated_signing is True
Example #36
0
 def test_preview_size(self):
     name = 'non-animated.gif'
     form = forms.PreviewForm({'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'wb') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid(), form.errors
     form.save(self.addon)
     eq_(self.addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [180, 214]})
Example #37
0
 def test_preview_modified(self, update_mock):
     addon = Addon.objects.get(pk=3615)
     name = 'transparent.png'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'w') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid()
     form.save(addon)
     assert update_mock.called
Example #38
0
    def test_icon_modified(self, update_mock):
        name = "transparent.png"
        form = forms.AddonFormMedia({"icon_upload_hash": name}, request=self.request, instance=self.addon)

        dest = os.path.join(self.icon_path, name)
        shutil.copyfile(get_image_path(name), dest)

        assert form.is_valid()
        form.save(addon=self.addon)
        assert update_mock.called
 def test_upload_sideload_addon(self, validate_mock):
     """Sideload addons are validated as "self-hosted" addons."""
     validate_mock.return_value = json.dumps(amo.VALIDATOR_SKELETON_RESULTS)
     self.url = reverse('devhub.upload_sideload')
     data = open(get_image_path('animated.png'), 'rb')
     self.client.post(self.url, {'upload': data})
     # Make sure it was called with listed=False.
     assert not validate_mock.call_args[1]['listed']
     # No automated signing for sideload add-ons.
     assert FileUpload.objects.get().automated_signing is False
Example #40
0
 def test_preview_modified(self, update_mock):
     addon = Addon.objects.get(pk=3615)
     name = 'transparent.png'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'w') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid()
     form.save(addon)
     assert update_mock.called
Example #41
0
 def test_preview_size(self):
     addon = Addon.objects.get(pk=3615)
     name = 'non-animated.gif'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'w') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid()
     form.save(addon)
     eq_(addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [126, 150]})
Example #42
0
    def get_image(self, filename):
        """Copy image to tmp and return tmp path.

        We do this because the task `resize_preview` removes the src file when
        finished.

        """
        src = get_image_path(filename)
        dst = os.path.join(settings.TMP_PATH, 'preview', filename)
        shutil.copy(src, dst)
        return dst
Example #43
0
 def test_preview_size(self):
     addon = Addon.objects.get(pk=3615)
     name = 'non-animated.gif'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     with storage.open(os.path.join(self.dest, name), 'w') as f:
         copyfileobj(open(get_image_path(name)), f)
     assert form.is_valid()
     form.save(addon)
     eq_(addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [126, 150]})
Example #44
0
    def test_icon_modified(self, update_mock):
        name = 'transparent.png'
        form = forms.AddonFormMedia({'icon_upload_hash': name},
                                    request=self.request,
                                    instance=self.addon)

        dest = os.path.join(self.icon_path, name)
        with storage.open(dest, 'w') as f:
            copyfileobj(open(get_image_path(name)), f)
        assert form.is_valid()
        form.save(addon=self.addon)
        assert update_mock.called
Example #45
0
 def test_preview_modified(self, update_mock):
     addon = Addon.objects.get(pk=3615)
     name = 'transparent.png'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     dest = path.path(settings.TMP_PATH) / 'preview'
     if not os.path.exists(dest):
         os.makedirs(dest)
     shutil.copyfile(get_image_path(name), dest / name)
     assert form.is_valid()
     form.save(addon)
     assert update_mock.called
Example #46
0
    def test_icon_modified(self, update_mock):
        name = 'transparent.png'
        form = forms.AddonFormMedia({'icon_upload_hash': name},
                                    request=self.request,
                                    instance=self.addon)

        dest = os.path.join(self.icon_path, name)
        with storage.open(dest, 'w') as f:
            copyfileobj(open(get_image_path(name)), f)
        assert form.is_valid()
        form.save(addon=self.addon)
        assert update_mock.called
Example #47
0
 def test_preview_rotated(self):
     addon = Webapp.objects.get(pk=337141)
     preview = Preview.objects.create(addon=addon, caption='Test')
     src = get_image_path('preview_landscape.jpg')
     tasks.resize_preview(src, preview)
     preview = preview.reload()
     eq_(preview.image_size, [533, 400])
     eq_(preview.thumbnail_size, [240, 180])
     eq_(preview.is_landscape, True)
     with storage.open(preview.thumbnail_path) as fp:
         im = Image.open(fp)
         eq_(list(im.size), [240, 180])
Example #48
0
 def test_preview(self):
     addon = Webapp.objects.get(pk=337141)
     preview = Preview.objects.create(addon=addon)
     src = get_image_path('preview.jpg')
     tasks.resize_preview(src, preview)
     preview = preview.reload()
     eq_(preview.image_size, [400, 533])
     eq_(preview.thumbnail_size, [180, 240])
     eq_(preview.is_landscape, False)
     with storage.open(preview.thumbnail_path) as fp:
         im = Image.open(fp)
         eq_(list(im.size), [180, 240])
Example #49
0
 def test_preview(self):
     addon = Webapp.objects.get(pk=337141)
     preview = Preview.objects.create(addon=addon, caption='Test')
     src = get_image_path('preview.jpg')
     tasks.resize_preview(src, preview)
     preview = preview.reload()
     eq_(preview.image_size, [400, 533])
     eq_(preview.thumbnail_size, [180, 240])
     eq_(preview.is_landscape, False)
     with storage.open(preview.thumbnail_path) as fp:
         im = Image.open(fp)
         eq_(list(im.size), [180, 240])
Example #50
0
 def test_preview_rotated(self):
     addon = Webapp.objects.get(pk=337141)
     preview = Preview.objects.create(addon=addon)
     src = get_image_path('preview_landscape.jpg')
     tasks.resize_preview(src, preview)
     preview = preview.reload()
     eq_(preview.image_size, [533, 400])
     eq_(preview.thumbnail_size, [240, 180])
     eq_(preview.is_landscape, True)
     with storage.open(preview.thumbnail_path) as fp:
         im = Image.open(fp)
         eq_(list(im.size), [240, 180])
Example #51
0
 def test_preview_size(self):
     addon = Addon.objects.get(pk=3615)
     name = 'non-animated.gif'
     form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
                               'position': 1})
     dest = path.path(settings.TMP_PATH) / 'preview'
     if not os.path.exists(dest):
         os.makedirs(dest)
     shutil.copyfile(get_image_path(name), dest / name)
     assert form.is_valid()
     form.save(addon)
     eq_(addon.previews.all()[0].sizes,
         {u'image': [250, 297], u'thumbnail': [126, 150]})
Example #52
0
 def test_preview_dont_generate_image(self):
     addon = Webapp.objects.get(pk=337141)
     preview = Preview.objects.create(addon=addon)
     src = get_image_path('preview.jpg')
     tasks.resize_preview(src, preview, generate_image=False)
     preview = preview.reload()
     eq_(preview.image_size, [])
     eq_(preview.thumbnail_size, [100, 133])
     eq_(preview.sizes, {u'thumbnail': [100, 133]})
     with storage.open(preview.thumbnail_path) as fp:
         im = Image.open(fp)
         eq_(list(im.size), [100, 133])
     assert not os.path.exists(preview.image_path), preview.image_path
Example #53
0
def test_resize_icon_poorly():
    """If we attempt to set the src/dst, we do nothing."""
    somepic = get_image_path('mozilla.png')
    src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                      delete=False, dir=settings.TMP_PATH)
    shutil.copyfile(somepic, src.name)
    src_image = Image.open(src.name)
    eq_(src_image.size, (339, 128))

    resize_icon(src.name, src.name, locally=True)

    # assert nothing happenned
    src_image = Image.open(src.name)
    eq_(src_image.size, (339, 128))
Example #54
0
 def test_preview_size(self):
     name = 'non-animated.gif'
     form = forms.PreviewForm({
         'caption': 'test',
         'upload_hash': name,
         'position': 1
     })
     shutil.copyfile(get_image_path(name), os.path.join(self.dest, name))
     assert form.is_valid()
     form.save(self.addon)
     eq_(self.addon.previews.all()[0].sizes, {
         u'image': [250, 297],
         u'thumbnail': [126, 150]
     })
Example #55
0
def test_resize_image_asset():
    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    with storage.open(img) as fp:
        src_image = Image.open(fp)
        eq_(src_image.size, original_size)

    dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix='.png')
    # Make it resize to some arbitrary size that's larger on both sides than
    # the source image. This is where the behavior differs from resize_image.
    tasks.resize_imageasset(img, dest.name, (500, 500), locally=True)
    with storage.open(dest.name) as fp:
        dest_image = Image.open(fp)
        eq_(dest_image.size, (500, 500))
Example #56
0
def test_resize_photo_poorly():
    """If we attempt to set the src/dst, we do nothing."""
    somepic = get_image_path('mozilla.png')
    src = tempfile.NamedTemporaryFile(mode='r+w+b',
                                      suffix=".png",
                                      delete=False)
    shutil.copyfile(somepic, src.name)
    src_image = Image.open(src.name)
    eq_(src_image.size, (82, 31))

    resize_photo(src.name, src.name)

    # assert nothing happenned
    src_image = Image.open(src.name)
    eq_(src_image.size, (82, 31))
Example #57
0
def _uploader(resize_size, final_size):
    img = get_image_path('mozilla.png')
    original_size = (339, 128)

    src = tempfile.NamedTemporaryFile(mode='r+w+b',
                                      suffix=".png",
                                      delete=False)

    # resize_icon removes the original
    shutil.copyfile(img, src.name)

    with storage.open(src.name) as fp:
        src_image = Image.open(fp)
        src_image.load()
    eq_(src_image.size, original_size)

    if isinstance(final_size, list):
        for rsize, fsize in zip(resize_size, final_size):
            dest_name = os.path.join(settings.ADDON_ICONS_PATH, '1234')

            tasks.resize_icon(src.name, dest_name, resize_size, locally=True)
            with storage.open("%s-%s.png" % (dest_name, rsize)) as fp:
                dest_image = Image.open(fp)
                dest_image.load()

            # Assert that the width is always identical.
            eq_(dest_image.size[0], fsize[0])
            # Assert that the height can be a wee bit fuzzy.
            assert -1 <= dest_image.size[1] - fsize[1] <= 1, (
                "Got width %d, expected %d" % (fsize[1], dest_image.size[1]))

            if os.path.exists(dest_image.filename):
                os.remove(dest_image.filename)
            assert not os.path.exists(dest_image.filename)
    else:
        dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png")
        tasks.resize_icon(src.name, dest.name, resize_size, locally=True)
        with storage.open(dest.name) as fp:
            dest_image = Image.open(fp)
            dest_image.load()

        # Assert that the width is always identical.
        eq_(dest_image.size[0], final_size[0])
        # Assert that the height can be a wee bit fuzzy.
        assert -1 <= dest_image.size[1] - final_size[1] <= 1, (
            "Got width %d, expected %d" % (final_size[1], dest_image.size[1]))

    assert not os.path.exists(src.name)
Example #58
0
def test_resize_icon():
    somepic = get_image_path('mozilla.png')

    src = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                      delete=False, dir=settings.TMP_PATH)
    dest = tempfile.NamedTemporaryFile(mode='r+w+b', suffix=".png",
                                       dir=settings.TMP_PATH)

    # resize_icon removes the original
    shutil.copyfile(somepic, src.name)

    src_image = Image.open(src.name)
    eq_(src_image.size, (339, 128))
    resize_icon(src.name, dest.name, locally=True)

    dest_image = Image.open(dest.name)
    eq_(dest_image.size, (32, 12))

    assert not os.path.exists(src.name)