Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upload return codes #4857

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conans/client/rest/uploader_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions conans/test/unittests/client/rest/uploader_test.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test a regular behavior of a 401 returned when trying to upload (bintray)


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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this manages the (my point of view) weird artifactory behavior while uploading


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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test a classic wrong token while uploading


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)