Skip to content

Commit

Permalink
Fix duplicate upload of file data tomgross#17
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Feb 20, 2020
1 parent e15b3d7 commit ccea6a7
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 38 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dist: xenial
python:
- 3.6
- 3.7
- 3.8
install:
- pip install -r test_requirements.txt
script:
Expand Down
10 changes: 8 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Changelog
1.0a7 (unreleased)
------------------

- Add new API method `createfolderifnotexists` #19
[Arkoniak, tomgross]

- Fix duplication of data transfer on file upload #17
[blasterspike, tomgross]

- Consistently use MIT licences
[tomgross]

Expand All @@ -22,13 +28,13 @@ Changelog
[blasterspike]

- Test and claim Python 3.7 compatibility
[tomgross]
[tomgross]

1.0a4 (2017-10-29)
------------------

- Fix error with duplicate files parameter #3
[tomgross]
[tomgross]

- Fix upload of data
[tomgross]
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ignore =
.coveragerc
.editorconfig
.gitattributes
bootstrap-buildout.py

[isort]
# for details see
Expand Down
4 changes: 2 additions & 2 deletions src/pcloud/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def uploadfile(self, **kwargs):
files = {filename: open(f, "rb")}
# kwargs['filename'] = filename
else: # 'data' in kwargs:
files = {"f": (kwargs.pop("filename"), kwargs.pop("data"))}
files = {"f": (kwargs.pop("filename", "data-upload.bin"), kwargs.pop("data"))}
return self._upload("uploadfile", files, **kwargs)

@RequiredParameterCheck(("progresshash",))
Expand Down Expand Up @@ -218,7 +218,7 @@ def file_truncate(self, **kwargs):

@RequiredParameterCheck(("fd", "data"))
def file_write(self, **kwargs):
files = {"filename": BytesIO(kwargs["data"])}
files = {"file": ('upload-file.io', BytesIO(kwargs.pop("data")))}
return self._upload("file_write", files, **kwargs)

@RequiredParameterCheck(("fd",))
Expand Down
7 changes: 5 additions & 2 deletions src/pcloud/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def start_mock_server():
"""
httpd = MockServer(("", PORT), MockHandler)
httpd_thread = Thread(target=httpd.serve_forever)
httpd_thread.setDaemon(False)
httpd_thread.setDaemon(True)
httpd_thread.start()
print("start")
yield start_mock_server
print("teardown")
httpd_thread.join(1)
httpd_thread._is_stopped = True
httpd_thread._tstate_lock = None
httpd_thread._stop()

9 changes: 5 additions & 4 deletions src/pcloud/tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def do_POST(self):
"CONTENT_TYPE": self.headers["Content-Type"],
},
)
print(form)
if "data" in form:
size = len(form.getvalue("data"))
if "upload.txt" in form:
file_ = form.getvalue("upload.txt")
else:
size = len(form.getvalue("upload.txt"))
file_ = form.getvalue("file")
size = len(file_)
self.send_response(200)
self.send_header("Content-type", "applicaton/json")
self.end_headers()
print(f"File: {file_}, Size: {size}", end='')
# Send the json message
self.wfile.write(
bytes('{ "result": 0, "metadata": {"size": %s} }' % size, "utf-8")
Expand Down
20 changes: 20 additions & 0 deletions src/pcloud/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
from pcloud import api
from pcloud.pcloudfs import PCloudFS

import os.path
import pytest
Expand All @@ -10,8 +11,14 @@ class DummyPyCloud(api.PyCloud):
endpoint = "http://localhost:{0}/".format(5000)


class DummyPCloudFS(PCloudFS):

factory = DummyPyCloud


@pytest.mark.usefixtures("start_mock_server")
class TestPcloudApi(object):

def test_getdigest(self):
api = DummyPyCloud("foo", "bar")
assert api.getdigest() == b"YGtAxbUpI85Zvs7lC7Z62rBwv907TBXhV2L867Hkh"
Expand All @@ -27,3 +34,16 @@ def test_upload_files(self):
"result": 0,
"metadata": {"size": 14},
}


@pytest.mark.usefixtures("start_mock_server")
class TestPcloudFs(object):

def test_write(self, capsys):
with DummyPCloudFS(username="foo", password="bar") as fs:
data = b"hello pcloud fs unittest"
fs_f = fs.openbin("hello.bin")
fs_f.write(data)
captured = capsys.readouterr()
assert captured.out == "File: b'hello pcloud fs unittest', Size: 24"

26 changes: 0 additions & 26 deletions src/pcloud/tests/test_fs.py

This file was deleted.

0 comments on commit ccea6a7

Please sign in to comment.