Пример #1
0
 def __init__(self, name, content_type, size, charset, content_type_extra):
     if settings.FILE_UPLOAD_TEMP_DIR:
         file = tempfile.NamedTemporaryFile(suffix='.upload',
             dir=settings.FILE_UPLOAD_TEMP_DIR)
     else:
         file = tempfile.NamedTemporaryFile(suffix='.upload')
     super(TemporaryUploadedFile, self).__init__(file, name, content_type, size, charset, content_type_extra)
Пример #2
0
    def test_fileupload_getlist(self):
        file1 = tempfile.NamedTemporaryFile()
        file1.write('a' * (2**23))
        file1.seek(0)

        file2 = tempfile.NamedTemporaryFile()
        file2.write('a' * (2 * 2**18))
        file2.seek(0)

        file2a = tempfile.NamedTemporaryFile()
        file2a.write('a' * (5 * 2**20))
        file2a.seek(0)

        response = self.client.post(
            '/file_uploads/getlist_count/', {
                'file1': file1,
                'field1': u'test',
                'field2': u'test3',
                'field3': u'test5',
                'field4': u'test6',
                'field5': u'test7',
                'file2': (file2, file2a)
            })
        got = simplejson.loads(response.content)

        self.assertEqual(got.get('file1'), 1)
        self.assertEqual(got.get('file2'), 2)
Пример #3
0
    def test_large_upload(self):
        tdir = tempfile.gettempdir()

        file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir)
        file1.write('a' * (2**21))
        file1.seek(0)

        file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir)
        file2.write('a' * (10 * 2**20))
        file2.seek(0)

        post_data = {
            'name': 'Ringo',
            'file_field1': file1,
            'file_field2': file2,
        }

        for key in post_data.keys():
            try:
                post_data[key + '_hash'] = sha_constructor(
                    post_data[key].read()).hexdigest()
                post_data[key].seek(0)
            except AttributeError:
                post_data[key + '_hash'] = sha_constructor(
                    post_data[key]).hexdigest()

        response = self.client.post('/file_uploads/verify/', post_data)

        self.assertEqual(response.status_code, 200)
Пример #4
0
 def __init__(self, progress_id, name, content_type, size, charset):
     suffix = get_upload_suffix(progress_id)
     if settings.FILE_UPLOAD_TEMP_DIR:
         file = tempfile.NamedTemporaryFile(
             suffix=suffix, dir=settings.FILE_UPLOAD_TEMP_DIR)
     else:
         file = tempfile.NamedTemporaryFile(suffix=suffix)
     super(ProgressUploadedFile, self).__init__(file, name, content_type,
                                                size, charset)
Пример #5
0
def test_extract_and_commit_source_from_version(settings):
    addon = addon_factory(
        file_kw={'filename': 'webextension_no_id.xpi'},
        version_kw={'version': '0.1'})

    # Generate source file
    source = temp.NamedTemporaryFile(suffix='.zip', dir=settings.TMP_PATH)
    with zipfile.ZipFile(source, 'w') as zip_file:
        zip_file.writestr('manifest.json', '{}')
    source.seek(0)
    addon.current_version.source = DjangoFile(source)
    addon.current_version.save()

    repo = AddonGitRepository.extract_and_commit_source_from_version(
        addon.current_version)

    assert repo.git_repository_path == os.path.join(
        settings.GIT_FILE_STORAGE_PATH, id_to_path(addon.id), 'source')
    assert os.listdir(repo.git_repository_path) == ['.git']

    # Verify via subprocess to make sure the repositories are properly
    # read by the regular git client
    output = _run_process('git branch', repo)
    assert 'listed' in output

    output = _run_process('git log listed', repo)
    expected = 'Create new version {} ({}) for {} from source file'.format(
        repr(addon.current_version), addon.current_version.id, repr(addon))
    assert expected in output
Пример #6
0
def test_extract_and_commit_source_from_version_no_dotgit_clash(settings):
    addon = addon_factory(
        file_kw={'filename': 'webextension_no_id.xpi'},
        version_kw={'version': '0.1'})

    # Generate source file
    source = temp.NamedTemporaryFile(suffix='.zip', dir=settings.TMP_PATH)
    with zipfile.ZipFile(source, 'w') as zip_file:
        zip_file.writestr('manifest.json', '{}')
        zip_file.writestr('.git/config', '')
    source.seek(0)
    addon.current_version.source = DjangoFile(source)
    addon.current_version.save()

    with mock.patch('olympia.lib.git.uuid.uuid4') as uuid4_mock:
        uuid4_mock.return_value = mock.Mock(
            hex='b236f5994773477bbcd2d1b75ab1458f')
        repo = AddonGitRepository.extract_and_commit_source_from_version(
            addon.current_version)

    assert repo.git_repository_path == os.path.join(
        settings.GIT_FILE_STORAGE_PATH, id_to_path(addon.id), 'source')
    assert os.listdir(repo.git_repository_path) == ['.git']

    # Verify via subprocess to make sure the repositories are properly
    # read by the regular git client
    output = _run_process('git ls-tree -r --name-only listed', repo)
    assert set(output.split()) == {
        'extracted/manifest.json', 'extracted/.git.b236f599/config'}
Пример #7
0
    def test_broken_custom_upload_handler(self):
        f = tempfile.NamedTemporaryFile()
        f.write('a' * (2**21))

        # AttributeError: You cannot alter upload handlers after the upload has been processed.
        self.assertRaises(AttributeError, self.client.post,
                          '/file_uploads/quota/broken/', {'f': open(f.name)})
 def test_logging(self, log_mock):
     with temp.NamedTemporaryFile(
         suffix='.zip', dir=temp.gettempdir()
     ) as source_file:
         with zipfile.ZipFile(source_file, 'w') as zip_file:
             zip_file.writestr('foo', 'a' * (2**21))
         source_file.seek(0)
         data = self.formset(source=source_file)
         response = self.client.post(self.url, data)
     assert response.status_code == 302
     assert log_mock.info.call_count == 4
     assert log_mock.info.call_args_list[0][0] == (
         'version_edit, form populated, addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
     assert log_mock.info.call_args_list[1][0] == (
         'version_edit, form validated, addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
     assert log_mock.info.call_args_list[2][0] == (
         'version_edit, form saved, addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
     assert log_mock.info.call_args_list[3][0] == (
         'version_edit, redirecting to next view, addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
 def test_logging_failed_validation(self, log_mock):
     with temp.NamedTemporaryFile(
         suffix='.exe', dir=temp.gettempdir()
     ) as source_file:
         with zipfile.ZipFile(source_file, 'w') as zip_file:
             zip_file.writestr('foo', 'a' * (2**21))
         source_file.seek(0)
         data = self.formset(source=source_file)
         response = self.client.post(self.url, data)
     assert response.status_code == 200
     assert response.context['version_form'].errors == {
         'source': [
             'Unsupported file type, please upload an archive file '
             + '(.zip, .tar.gz, .tgz, .tar.bz2).'
         ]
     }
     assert log_mock.info.call_count == 2
     assert log_mock.info.call_args_list[0][0] == (
         'version_edit, form populated, addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
     assert log_mock.info.call_args_list[1][0] == (
         'version_edit, validation failed, re-displaying the template, '
         + 'addon.slug: %s, version.id: %s',
         self.addon.slug,
         self.version.id,
     )
Пример #10
0
 def test_stop_upload_temporary_file_handler(self):
     with tempfile.NamedTemporaryFile() as temp_file:
         temp_file.write(b'a')
         temp_file.seek(0)
         response = self.client.post('/temp_file/stop_upload/', {'file': temp_file})
         temp_path = response.json()['temp_path']
         self.assertIs(os.path.exists(temp_path), False)
Пример #11
0
    def test_post_with_attachment(self):
        client = APIClient()
        thumb_sizes_number = len(models.Attachment.thumbnail_sizes)

        client.force_authenticate(self.user1)

        post = models.Post.objects.create(
            user=self.user1,
            status='drafted',
            content='lorem ipsum',
        )
        self.assertIsNotNone(post)

        url = f'{self.base_url}{post.uid}/attachments/'
        with tempfile.NamedTemporaryFile(suffix='.jpg') as fp:
            with Image.new('RGB', (800, 800)) as image_fp:
                image_fp.save(fp, format='JPEG')
                fp.seek(0)
                data = {
                    'content': fp,
                    'order': 1,
                }
                res = client.post(url, data, format='multipart')
                self.assertEqual(res.status_code, 201)
        post.refresh_from_db()
        self.assertEqual(post.attachment_set.count(), 1 + thumb_sizes_number - 1)
    def test_assign(cls):
        """Tests whether the :see:LocalizedFileValueDescriptor works
        properly."""

        temp_file = tempfile.NamedTemporaryFile(dir=MEDIA_ROOT)
        instance = cls.FileFieldModel()
        instance.file = {"en": temp_file.name}
        assert isinstance(instance.file.en, LocalizedFieldFile)
        assert instance.file.en.name == temp_file.name

        field_dump = pickle.dumps(instance.file)
        instance = cls.FileFieldModel()
        instance.file = pickle.loads(field_dump)
        assert instance.file.en.field == instance._meta.get_field("file")
        assert instance.file.en.instance == instance
        assert isinstance(instance.file.en, LocalizedFieldFile)

        instance = cls.FileFieldModel()
        instance.file = {"en": ContentFile("test", "testfilename")}
        assert isinstance(instance.file.en, LocalizedFieldFile)
        assert instance.file.en.name == "testfilename"

        another_instance = cls.FileFieldModel()
        another_instance.file = {"ro": instance.file.en}
        assert another_instance == another_instance.file.ro.instance
        assert another_instance.file.ro.lang == "ro"
Пример #13
0
 def test_stop_upload_temporary_file_handler(self):
     with tempfile.NamedTemporaryFile() as temp_file:
         temp_file.write(b"a")
         temp_file.seek(0)
         response = self.client.post("/temp_file/stop_upload/", {"file": temp_file})
         temp_path = response.json()["temp_path"]
         self.assertIs(os.path.exists(temp_path), False)
Пример #14
0
 def test1(self):
     session_key = temp.NamedTemporaryFile('rw', delete=True).name
     api.start(True)
     self.assertRaises(MaintenanceModeError,
                       CommandTask,
                       session_key,
                       timeout=2,
                       check_maintenance=True)
Пример #15
0
    def test_broken_custom_upload_handler(self):
        with tempfile.NamedTemporaryFile() as file:
            file.write(b'a' * (2**21))
            file.seek(0)

            msg = 'You cannot alter upload handlers after the upload has been processed.'
            with self.assertRaisesMessage(AttributeError, msg):
                self.client.post('/quota/broken/', {'f': file})
Пример #16
0
    def test_extra_content_type(self):
        f = tempfile.NamedTemporaryFile()
        f.write('a' * (2 ** 21))
        f.seek(0)
        f.content_type = 'text/plain; blob-key=upload blob key; other=test'

        response = self.client.post("/file_uploads/content_type_extra/", {'f': f})
        got = simplejson.loads(response.content)
        self.assertEqual(got['f'], 'upload blob key')
Пример #17
0
    def test_custom_upload_handler(self):
        # A small file (under the 5M quota)
        smallfile = tempfile.NamedTemporaryFile()
        smallfile.write('a' * (2 ** 21))

        # A big file (over the quota)
        bigfile = tempfile.NamedTemporaryFile()
        bigfile.write('a' * (10 * 2 ** 20))

        # Small file posting should work.
        response = self.client.post('/file_uploads/quota/', {'f': open(smallfile.name)})
        got = simplejson.loads(response.content)
        self.assert_('f' in got)

        # Large files don't go through.
        response = self.client.post("/file_uploads/quota/", {'f': open(bigfile.name)})
        got = simplejson.loads(response.content)
        self.assert_('f' not in got)
Пример #18
0
def make_test_file(content=None):
    if content == None:
        content = 'I am a test file for upload.'
    # Shamelessly stolen from Django's own file-upload tests.
    tdir = tempfile.gettempdir()
    file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt", dir=tdir)
    file_for_upload.write(content)
    file_for_upload.seek(0)
    return file_for_upload
Пример #19
0
    def test_large_upload(self):
        tdir = tempfile.gettempdir()

        file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir)
        file1.write('a' * (2**21))
        file1.seek(0)

        file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir)
        file2.write('a' * (10 * 2**20))
        file2.seek(0)

        # This file contains chinese symbols for a name.
        file3 = open(
            os.path.join(
                tdir,
                u'test_中文_Orl\u00e9ans.jpg'.encode('utf-8')),
            'w+b')
        file3.write('b' * (2**10))
        file3.seek(0)

        post_data = {
            'name': 'Ringo',
            'file_field1': open(file1.name),
            'file_field2': open(file2.name),
            'file_unicode': file3,
        }

        for key in post_data.keys():
            try:
                post_data[key + '_hash'] = sha_constructor(
                    post_data[key].read()).hexdigest()
                post_data[key].seek(0)
            except AttributeError:
                post_data[key + '_hash'] = sha_constructor(
                    post_data[key]).hexdigest()

        response = self.client.post('/file_uploads/verify/', post_data)

        try:
            os.unlink(file3.name)
        except:
            pass

        self.assertEqual(response.status_code, 200)
Пример #20
0
 def __init__(self, name, content_type, size, charset):
     # AM: since we are using the file across the session
     # and I cannot store the object in the session I need
     # to keep the file and delete it manually.
     path = settings.FILE_UPLOAD_TEMP_DIR
     file = tempfile.NamedTemporaryFile(suffix='.upload',
                                        dir=path,
                                        delete=False)
     super(StickyTemporaryUploadedFile, self).__init__(file, name, \
                                               content_type, size, charset)
Пример #21
0
    def compress_css(self, css):
        output_file = tempfile.NamedTemporaryFile(suffix='.pipeline')

        command = (settings.CSSTIDY_BINARY, "-", settings.CSSTIDY_ARGUMENTS,
                   output_file.name)
        self.execute_command(command, css)

        filtered_css = output_file.read()
        output_file.close()
        return filtered_css
Пример #22
0
 def test_should_not_accept_exe_source_file(self):
     with temp.NamedTemporaryFile(suffix='.exe',
                                  dir=temp.gettempdir()) as source_file:
         with zipfile.ZipFile(source_file, 'w') as zip_file:
             zip_file.writestr('foo', 'a' * (2**21))
         source_file.seek(0)
         data = self.formset(source=source_file)
         response = self.client.post(self.url, data)
         assert response.status_code == 200
         assert not Version.objects.get(pk=self.version.pk).source
Пример #23
0
 def __init__(self,
              name,
              content_type,
              size,
              charset,
              content_type_extra=None):
     file = tempfile.NamedTemporaryFile(suffix='.upload',
                                        dir=settings.FILE_UPLOAD_TEMP_DIR)
     super().__init__(file, name, content_type, size, charset,
                      content_type_extra)
Пример #24
0
    def test_edit_attachment(self):
        file_for_upload = make_test_file(
            content='I am a test file for editing.')

        post_data = {
            'title': 'Test editing file',
            'description': 'A test file for editing.',
            'comment': 'Initial upload',
            'file': file_for_upload,
        }

        resp = self.client.post(reverse('attachments.new_attachment'),
                                data=post_data)

        tdir = tempfile.gettempdir()
        edited_file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt",
                                                             dir=tdir)
        edited_file_for_upload.write(
            'I am a new version of the test file for editing.')
        edited_file_for_upload.seek(0)

        post_data = {
            'title': 'Test editing file',
            'description': 'A test file for editing.',
            'comment': 'Second revision.',
            'file': edited_file_for_upload,
        }

        attachment = Attachment.objects.get(title='Test editing file')

        resp = self.client.post(reverse('attachments.edit_attachment',
                                        kwargs={
                                            'attachment_id': attachment.id,
                                        }),
                                data=post_data)

        eq_(302, resp.status_code)

        # Re-fetch because it's been updated.
        attachment = Attachment.objects.get(title='Test editing file')
        eq_(resp['Location'],
            'http://testserver{0!s}'.format(attachment.get_absolute_url()))

        eq_(2, attachment.revisions.count())

        rev = attachment.current_revision
        eq_('admin', rev.creator.username)
        eq_('Second revision.', rev.comment)
        ok_(rev.is_approved)

        url = attachment.get_file_url()
        resp = self.client.get(url, HTTP_HOST=settings.ATTACHMENT_HOST)
        eq_('text/plain', rev.mime_type)
        ok_('I am a new version of the test file for editing.'
            in resp.streaming_content)
Пример #25
0
 def __init__(self,
              name,
              content_type,
              size,
              charset,
              content_type_extra=None):
     _, ext = os.path.splitext(name)
     file = tempfile.NamedTemporaryFile(suffix=".upload" + ext,
                                        dir=settings.FILE_UPLOAD_TEMP_DIR)
     super().__init__(file, name, content_type, size, charset,
                      content_type_extra)
    def test_value_to_string(cls):
        """Tests whether the :see:LocalizedFileField class's
        :see:value_to_string function works properly."""

        temp_file = File(tempfile.NamedTemporaryFile())
        instance = cls.FileFieldModel()
        field = cls.FileFieldModel._meta.get_field("file")
        field.upload_to = ""
        instance.file.en.save("testfilename", temp_file)
        expected_value_to_string = json.dumps({"en": "testfilename"})
        assert field.value_to_string(instance) == expected_value_to_string
Пример #27
0
    def test_custom_upload_handler(self):
        # A small file (under the 5M quota)
        smallfile = tempfile.NamedTemporaryFile()
        smallfile.write(b'a' * (2 ** 21))
        smallfile.seek(0)

        # A big file (over the quota)
        bigfile = tempfile.NamedTemporaryFile()
        bigfile.write(b'a' * (10 * 2 ** 20))
        bigfile.seek(0)

        # Small file posting should work.
        response = self.client.post('/file_uploads/quota/', {'f': smallfile})
        got = json.loads(response.content.decode('utf-8'))
        self.assertTrue('f' in got)

        # Large files don't go through.
        response = self.client.post("/file_uploads/quota/", {'f': bigfile})
        got = json.loads(response.content.decode('utf-8'))
        self.assertTrue('f' not in got)
Пример #28
0
    def test_content_type_extra(self):
        """Uploaded files may have content type parameters available."""
        tdir = tempfile.gettempdir()

        no_content_type = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir)
        no_content_type.write(b'something')
        no_content_type.seek(0)

        simple_file = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir)
        simple_file.write(b'something')
        simple_file.seek(0)
        simple_file.content_type = 'text/plain; test-key=test_value'

        response = self.client.post('/echo_content_type_extra/', {
            'no_content_type': no_content_type,
            'simple_file': simple_file,
        })
        received = json.loads(response.content.decode('utf-8'))
        self.assertEqual(received['no_content_type'], {})
        self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
Пример #29
0
    def _attach_file_to_post(post):
        from contents import models as c_models

        with tempfile.NamedTemporaryFile(suffix='.jpg') as fp:
            with Image.new('RGB', (800, 800)) as image_fp:
                image_fp.save(fp, format='JPEG')
                fp.seek(0)

            c_models.Attachment.objects.create(
                post=post,
                content=fp.name,
            )
Пример #30
0
def make_test_file(content=None, suffix='.txt'):
    """
    Create a fake file for testing purposes.
    """
    if content is None:
        content = 'I am a test file for upload.'
    # Shamelessly stolen from Django's own file-upload tests.
    tdir = tempfile.gettempdir()
    file_for_upload = tempfile.NamedTemporaryFile(suffix=suffix, dir=tdir)
    file_for_upload.write(content.encode('utf-8'))
    file_for_upload.seek(0)
    return file_for_upload