def test_file_is_image(self, extension):
        file_name = 'test_image{}'.format(extension)
        test_file_contents = open('tests/test_files/' + file_name, 'rb').read()

        assert file_is_image(MockFile(test_file_contents, file_name)) is True
        assert file_is_image(MockFile(test_file_contents, 'test_zip.zip')) is False
        assert file_is_image(MockFile(open('tests/test_files/test_zip.zip', 'rb').read(), file_name)) is False
        assert file_is_image(MockFile(open('tests/test_files/test_zip.zip', 'rb').read(), 'test_zip.zip')) is False
    def test_file_is_open_document_format(self, extension):
        file_name = 'test_{}.{}'.format(extension, extension)
        test_file_contents = open('tests/test_files/' + file_name, 'rb').read()
        invalid_file_contents = open('tests/test_files/test_image.jpg', 'rb').read()

        assert file_is_open_document_format(MockFile(test_file_contents, file_name)) is True
        assert file_is_open_document_format(MockFile(test_file_contents, 'test_file.doc')) is False
        assert file_is_open_document_format(MockFile(invalid_file_contents, file_name)) is False
        assert file_is_open_document_format(MockFile(invalid_file_contents, 'test_image.jpg')) is False
コード例 #3
0
 def test_validate_multiple_documents(self):
     self.assertEqual(
         validate_documents({
             'file1': MockFile(b"*" * 5400001, 'file1.pdf'),
             'file2': MockFile(b"*", 'file1.pdf'),
             'file3': MockFile(b"*", 'file1.doc'),
         }), {
             'file1': 'file_is_less_than_5mb',
             'file3': 'file_is_open_document_format',
         })
    def test_validate_multiple_documents(self):
        big_pdf = MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read() + b"*" * 5400001, 'test_pdf.pdf')
        pdf = MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_pdf.pdf')

        assert validate_documents({
            'file1': big_pdf,
            'file2': pdf,
            'file3': MockFile(b"*", 'file1.doc'),
        }) == {
            'file1': 'file_is_less_than_5mb',
            'file3': 'file_is_open_document_format',
        }
コード例 #5
0
 def test_filter_empty_files(self):
     file1 = MockFile(b"*", 'file1')
     file2 = MockFile(b"", 'file2')
     file3 = MockFile(b"*" * 10, 'file3')
     self.assertEquals(
         filter_empty_files({
             'f1': file1,
             'f2': file2,
             'f3': file3
         }), {
             'f1': file1,
             'f3': file3
         })
    def test_only_files_in_section_are_uploaded(self):
        request_files = {'serviceDefinitionDocumentURL': MockFile(b"*" * 100, 'q1.pdf')}

        files, errors = upload_service_documents(
            self.uploader, 'documents', self.documents_url, self.service,
            request_files, self.section)

        assert len(files) == 0
        assert len(errors) == 0
    def test_upload_with_validation_errors(self):
        request_files = {'pricingDocumentURL': MockFile(b"*" * 100, 'q1.bad')}

        files, errors = upload_service_documents(
            self.uploader, 'documents', self.documents_url, self.service,
            request_files, self.section)

        assert files is None
        assert 'pricingDocumentURL' in errors
    def test_empty_files_are_filtered(self):
        request_files = {'pricingDocumentURL': MockFile(b"", 'q1.pdf')}

        files, errors = upload_service_documents(
            self.uploader, 'documents', self.documents_url, self.service,
            request_files, self.section)

        assert len(files) == 0
        assert len(errors) == 0
 def setup(self):
     self.section = mock.Mock()
     self.section.get_question_ids.return_value = ['pricingDocumentURL']
     self.service = {
         'frameworkSlug': 'g-cloud-7',
         'supplierId': '12345',
         'id': '654321',
     }
     self.uploader = mock.Mock()
     self.documents_url = 'http://localhost'
     self.test_pdf = MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_pdf.pdf')
コード例 #10
0
    def test_empty_files_are_filtered(self):
        request_files = {'modernSlaveryStatement': MockFile(b"", 'q1.pdf')}

        files, errors = upload_declaration_documents(
            self.uploader, 'documents', self.documents_url,
            request_files, self.section,
            'g-cloud-11', 12345
        )

        assert len(files) == 0
        assert len(errors) == 0
コード例 #11
0
    def test_upload_with_validation_errors(self):
        request_files = {'modernSlaveryStatement': MockFile(b"*" * 100, 'q1.bad')}

        files, errors = upload_declaration_documents(
            self.uploader, 'documents', self.documents_url,
            request_files, self.section,
            'g-cloud-11', 12345
        )

        assert files is None
        assert 'modernSlaveryStatement' in errors
コード例 #12
0
 def test_document_upload_s3_error(self):
     uploader = mock.Mock()
     # using True as the mock "error_response" for this exception as we want to know if the function under test
     # wants to access a property of the exception rather than for it to continue in happy ignorance
     uploader.save.side_effect = ClientError(mock.MagicMock(), 'Forbidden')
     with freeze_time('2015-01-02 04:05:00'):
         self.assertFalse(
             upload_document(uploader, 'documents', 'http://assets', {
                 'id': "123",
                 'supplierId': 5,
                 'frameworkSlug': 'g-cloud-6'
             }, "pricingDocumentURL", MockFile(b"*", 'file.pdf')))
コード例 #13
0
    def test_document_upload_with_invalid_upload_type(self):
        uploader = mock.Mock()
        with pytest.raises(ValueError):
            assert upload_document(
                uploader,
                'invalid',
                'http://assets',
                {'id': "123", 'supplierId': 5, 'frameworkSlug': 'g-cloud-6'},
                "pricingDocumentURL",
                MockFile(b"*", 'file.pdf')
            ) == 'http://assets/g-cloud-6/submissions/5/123-pricing-document-2015-01-02-0405.pdf'

        assert not uploader.save.called
コード例 #14
0
    def test_upload_service_documents(self):
        request_files = {'pricingDocumentURL': MockFile(b"*" * 100, 'q1.pdf')}

        with freeze_time('2015-10-04 14:36:05'):
            files, errors = upload_service_documents(
                self.uploader, 'documents', self.documents_url, self.service,
                request_files, self.section)

        self.uploader.save.assert_called_with(
            'g-cloud-7/documents/12345/654321-pricing-document-2015-10-04-1436.pdf',
            mock.ANY,
            acl='public-read')

        assert 'pricingDocumentURL' in files
        assert len(errors) == 0
コード例 #15
0
    def test_document_upload_with_other_upload_type(self):
        uploader = mock.Mock()
        with freeze_time('2015-01-02 04:05:00'):
            self.assertEquals(
                upload_document(uploader, 'submissions', 'http://assets', {
                    'id': "123",
                    'supplierId': 5,
                    'frameworkSlug': 'g-cloud-6'
                }, "pricingDocumentURL", MockFile(b"*", 'file.pdf')),
                'http://assets/g-cloud-6/submissions/5/123-pricing-document-2015-01-02-0405.pdf'
            )

        uploader.save.assert_called_once_with(
            'g-cloud-6/submissions/5/123-pricing-document-2015-01-02-0405.pdf',
            mock.ANY,
            acl='public-read')
コード例 #16
0
    def test_document_upload_with_null_service_id(self):
        uploader = mock.Mock()
        with freeze_time('2015-01-02 04:05:00'):
            assert upload_document(
                uploader,
                'documents',
                'http://assets',
                {'supplierId': 5, 'frameworkSlug': 'g-cloud-11'},
                "modernSlaveryStatement",
                MockFile(b"*", 'file.pdf')
            ) == 'http://assets/g-cloud-11/documents/5/modern-slavery-statement-2015-01-02-0405.pdf'

        uploader.save.assert_called_once_with(
            'g-cloud-11/documents/5/modern-slavery-statement-2015-01-02-0405.pdf',
            mock.ANY,
            acl='public-read'
        )
コード例 #17
0
    def test_document_private_upload(self):
        uploader = mock.Mock()
        with freeze_time('2015-01-02 04:05:00'):
            assert upload_document(
                uploader,
                'documents',
                'http://assets',
                {'id': "123", 'supplierId': 5, 'frameworkSlug': 'g-cloud-6'},
                "pricingDocumentURL",
                MockFile(b"*", 'file.pdf'),
                public=False
            ) == 'http://assets/g-cloud-6/documents/5/123-pricing-document-2015-01-02-0405.pdf'

        uploader.save.assert_called_once_with(
            'g-cloud-6/documents/5/123-pricing-document-2015-01-02-0405.pdf',
            mock.ANY,
            acl='bucket-owner-full-control'
        )
コード例 #18
0
 def test_file_is_csv(self):
     """
     Our CSV checking is more lenient than other file checking it checks that a file is not of another file type
     we check for.
     """
     # Check a bytestring with valid extensions
     assert file_is_csv(MockFile(b"a,b,c,d\n1,2,3,4", 'file1.csv')) is True
     assert file_is_csv(MockFile(b"", 'file1.csv')) is True
     # Check a bytestring with invalid extensions
     assert file_is_csv(MockFile(b"a,b,c,d\n1,2,3,4", 'file1.tsv')) is False
     assert file_is_csv(MockFile(b"", 'file1.tsv')) is False
     # Check an image file with valid extension
     assert file_is_csv(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'file1.csv')) is False
     # Check an image file with invalid extension
     assert file_is_csv(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'file1.jpg')) is False
コード例 #19
0
 def test_file_is_empty(self):
     empty_file = MockFile(b"", 'file1')
     assert not file_is_not_empty(empty_file)
     assert file_is_empty(empty_file)
コード例 #20
0
 def test_file_is_not_empty(self):
     non_empty_file = MockFile(b"*", 'file1')
     assert file_is_not_empty(non_empty_file)
     assert not file_is_empty(non_empty_file)
コード例 #21
0
 def test_validate_documents_not_open_document_above_5mb(self):
     assert validate_documents({'file1': MockFile(b"*" * 5400001, 'file1.doc')}) == {
         'file1': 'file_is_open_document_format'
     }
コード例 #22
0
 def test_file_is_pdf(self):
     assert file_is_pdf(MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_pdf.pdf')) is True
     assert file_is_pdf(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'test_pdf.pdf')) is False
     assert file_is_pdf(MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_image.jpg')) is False
     assert file_is_pdf(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'test_image.jpg')) is False
コード例 #23
0
 def test_validate_documents(self):
     assert validate_documents(
         {'file1': MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_pdf.pdf')}
     ) == {}
コード例 #24
0
 def test_filter_empty_files(self):
     file1 = MockFile(b"*", 'file1')
     file2 = MockFile(b"", 'file2')
     file3 = MockFile(b"*" * 10, 'file3')
     assert filter_empty_files({'f1': file1, 'f2': file2, 'f3': file3}) == {'f1': file1, 'f3': file3}
コード例 #25
0
 def test_file_is_less_than_5mb(self):
     assert file_is_less_than_5mb(MockFile(b"*", 'file1'))
     assert not file_is_less_than_5mb(MockFile(b"*" * 5400001, 'file1'))
コード例 #26
0
 def test_file_is_csv(self):
     self.assertTrue(file_is_csv(MockFile(b"*", 'file.csv')))
     self.assertFalse(file_is_csv(MockFile(b"*", 'file.sit')))
コード例 #27
0
 def test_file_is_zip(self):
     assert file_is_zip(MockFile(open('tests/test_files/test_zip.zip', 'rb').read(), 'test_zip.zip')) is True
     assert file_is_zip(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'test_image.zip')) is False
     assert file_is_zip(MockFile(open('tests/test_files/test_zip.zip', 'rb').read(), 'test_image.jpg')) is False
     assert file_is_zip(MockFile(open('tests/test_files/test_image.jpg', 'rb').read(), 'test_image.jpg')) is False
コード例 #28
0
 def test_file_is_pdf(self):
     self.assertTrue(file_is_pdf(MockFile(b"*", 'file.pdf')))
     self.assertFalse(file_is_pdf(MockFile(b"*", 'file.doc')))
コード例 #29
0
 def setup(self):
     self.section = mock.Mock()
     self.section.get_question_ids.return_value = ['modernSlaveryStatement']
     self.uploader = mock.Mock()
     self.documents_url = 'http://localhost'
     self.test_pdf = MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read(), 'test_pdf.pdf')
コード例 #30
0
    def test_validate_documents_not_less_than_5mb(self):
        big_pdf = MockFile(open('tests/test_files/test_pdf.pdf', 'rb').read() + b"*" * 5400001, 'test_pdf.pdf')

        assert validate_documents({'file1': big_pdf}) == {'file1': 'file_is_less_than_5mb'}