def test_with_uri(self, fake):
        data = fake.uri()

        f = File(self.client, data)

        assert f.headers == {'Content-Location': data}
        assert f.mime_type is None
        assert f.body is None
        assert f.file_tuple() == (None, '', None, {'Content-Location': data})
    def test_with_binary(self, fake):
        data = fake.binary(512 * 1024)

        mime_type = fake.mime_type()

        f = File(self.client, data, mime_type=mime_type)

        assert 'Content-Location' not in f.headers
        assert f.mime_type == mime_type
        assert f.body == data
        assert f.file_tuple() == (None, data, mime_type, None)
    def test_with_path(self, fake):
        data = './tests/test.pdf'

        f = File(self.client, data)

        with open(data, 'rb') as fp:
            content = fp.read()

        assert 'Content-Location' not in f.headers
        assert f.mime_type == 'application/pdf'
        assert f.body == content
        assert f.file_tuple() == (None, content, 'application/pdf', None)
Esempio n. 4
0
    def test__generate_files(self, fake):
        count = fake.random_number(2)

        files = []

        while count > 0:
            files.append(File(self.client, fake.pystr(),
                              mime_type=fake.mime_type()))
            count -= 1

        result = self.outbound._generate_files(files)

        for i, f in enumerate(result):
            assert f[1] == files[i].file_tuple()

        count = fake.random_number(2)

        files = []

        while count > 0:
            files.append(fake.uri())
            count -= 1

        result = self.outbound._generate_files(files)

        for i, f in enumerate(result):
            assert f[1][3]['Content-Location'] == files[i]
Esempio n. 5
0
    def test_with_large_file(self, fake):
        data = fake.binary()
        mime_type = fake.mime_type()
        chunk_size = fake.random_int(len(data) // 20, len(data) // 2)

        document = Document(
            self.client, {
                'uri':
                'https://rest.interfax.net/outbound/documents/{0}'.format(
                    fake.random_number())
            })

        self.client.documents.create.return_value = document

        with patch('interfax.files.uuid4') as m:
            m.return_value = UUID('8fbaaaaf-87bb-4bd0-9d82-823c3eb38e49')
            f = File(self.client,
                     data,
                     mime_type=mime_type,
                     chunk_size=chunk_size)

        assert f.headers == {'Content-Location': document.uri}
        assert f.mime_type is None
        assert f.body is None
        assert f.file_tuple() == (None, '', None, {
            'Content-Location': document.uri
        })

        filename = 'upload-8fbaaaaf-87bb-4bd0-9d82-823c3eb38e49{0}'.format(
            guess_extension(mime_type))

        calls = [call.create(filename, len(data))]

        cursor = 0

        while cursor < len(data):
            chunk = data[cursor:cursor + chunk_size]

            calls.append(
                call.upload(document.id, cursor, cursor + len(chunk) - 1,
                            chunk))
            cursor += len(chunk)

        self.client.documents.assert_has_calls(calls)