From 9dfa04000113cb1010b98ea4b00f2e9103ff628a Mon Sep 17 00:00:00 2001 From: Luis Martinez de Bartolome Date: Thu, 28 Mar 2019 12:14:49 +0100 Subject: [PATCH 1/2] Fix upload return codes --- conans/client/rest/uploader_downloader.py | 6 ++ .../unittests/client/rest/uploader_test.py | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 conans/test/unittests/client/rest/uploader_test.py diff --git a/conans/client/rest/uploader_downloader.py b/conans/client/rest/uploader_downloader.py index 08a2e3140ac..94c4729d934 100644 --- a/conans/client/rest/uploader_downloader.py +++ b/conans/client/rest/uploader_downloader.py @@ -28,6 +28,9 @@ def upload(self, url, abs_path, auth=None, dedup=False, retry=1, retry_wait=0, h dedup_headers.update(headers) response = self.requester.put(url, data="", verify=self.verify, headers=dedup_headers, auth=auth) + if response.status_code == 401: + raise AuthenticationException(response.content) + if response.status_code == 403: if auth.token is None: raise AuthenticationException(response.content) @@ -53,6 +56,9 @@ def _upload_file(self, url, data, headers, auth): try: response = self.requester.put(url, data=data, verify=self.verify, headers=headers, auth=auth) + if response.status_code == 401: + raise AuthenticationException(response.content) + if response.status_code == 403: if auth.token is None: raise AuthenticationException(response.content) diff --git a/conans/test/unittests/client/rest/uploader_test.py b/conans/test/unittests/client/rest/uploader_test.py new file mode 100644 index 00000000000..70677ad75ec --- /dev/null +++ b/conans/test/unittests/client/rest/uploader_test.py @@ -0,0 +1,57 @@ +import tempfile +import unittest +from collections import namedtuple + +import six + +from conans.client.rest.uploader_downloader import Uploader +from conans.errors import AuthenticationException, ForbiddenException +from conans.test.utils.tools import TestBufferConanOutput +from conans.util.files import save + + +class UploaderUnitTest(unittest.TestCase): + + def test_401_raises_unauthoirzed_exception(self): + + class MockRequester(object): + + def put(self, *args, **kwargs): + return namedtuple("response", "status_code content")(401, "tururu") + + out = TestBufferConanOutput() + uploader = Uploader(MockRequester(), out, verify=False) + f = tempfile.mktemp() + save(f, "some contents") + with six.assertRaisesRegex(self, AuthenticationException, "tururu"): + uploader.upload("fake_url", f) + + def test_403_raises_unauthoirzed_exception_if_no_token(self): + + class MockRequester(object): + + def put(self, *args, **kwargs): + return namedtuple("response", "status_code content")(403, "tururu") + + out = TestBufferConanOutput() + auth = namedtuple("auth", "token")(None) + uploader = Uploader(MockRequester(), out, verify=False) + f = tempfile.mktemp() + save(f, "some contents") + with six.assertRaisesRegex(self, AuthenticationException, "tururu"): + uploader.upload("fake_url", f, auth=auth) + + def test_403_raises_forbidden_exception_if_token(self): + + class MockRequester(object): + + def put(self, *args, **kwargs): + return namedtuple("response", "status_code content")(403, "tururu") + + out = TestBufferConanOutput() + auth = namedtuple("auth", "token")("SOMETOKEN") + uploader = Uploader(MockRequester(), out, verify=False) + f = tempfile.mktemp() + save(f, "some contents") + with six.assertRaisesRegex(self, ForbiddenException, "tururu"): + uploader.upload("fake_url", f, auth=auth) \ No newline at end of file From 87e0061695e47493563b4196960655a39928dc0e Mon Sep 17 00:00:00 2001 From: Luis Martinez de Bartolome Date: Thu, 28 Mar 2019 12:16:31 +0100 Subject: [PATCH 2/2] New line --- conans/test/unittests/client/rest/uploader_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/unittests/client/rest/uploader_test.py b/conans/test/unittests/client/rest/uploader_test.py index 70677ad75ec..736231b9303 100644 --- a/conans/test/unittests/client/rest/uploader_test.py +++ b/conans/test/unittests/client/rest/uploader_test.py @@ -54,4 +54,4 @@ def put(self, *args, **kwargs): f = tempfile.mktemp() save(f, "some contents") with six.assertRaisesRegex(self, ForbiddenException, "tururu"): - uploader.upload("fake_url", f, auth=auth) \ No newline at end of file + uploader.upload("fake_url", f, auth=auth)