Skip to content

Commit

Permalink
Raising NoCredentialsException on utils#find_api_key
Browse files Browse the repository at this point in the history
NoCredentialsException is a ClickException, which is handled nicely by
click, displaying the error msg and returning an exit code != 0.
  • Loading branch information
josericardo committed Jun 22, 2015
1 parent 7e45965 commit fd6113c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 11 additions & 1 deletion shub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def missing_modules(*modules):


def find_api_key():
"""Finds and returns the Scrapy Cloud APIKEY"""
"""Finds and returns the Scrapy Cloud APIKEY
Raises:
ClickException: if no API key is found
"""
key = os.getenv("SHUB_APIKEY")
if not key:
key = get_key_netrc()

if not key:
err = ("Your credentials haven't been defined.\n"
"Use 'shub login' or set the SHUB_APIKEY environment variable.")
raise ClickException(err)

return key


Expand Down
1 change: 1 addition & 0 deletions tests/test_deploy_egg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setUp(self):

self.fake_requester = FakeRequester()
deploy_egg.utils.make_deploy_request = self.fake_requester.fake_request
deploy_egg.utils.find_api_key = lambda: ''

self.tmp_dir = tempfile.mktemp(prefix="shub-test-deploy-eggs")

Expand Down
9 changes: 6 additions & 3 deletions tests/test_fetch_eggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
class FetchEggsTest(unittest.TestCase):

def setUp(self):
self.runner = CliRunner()
# defining SHUB_APIKEY so it passes the login validation
self.runner = CliRunner(env={'SHUB_APIKEY': 'xxx'})

def test_raises_auth_exception(self, requests_mock):
fake_response = FakeResponse(403)
requests_mock.get.return_value = fake_response
output = self.runner.invoke(tool.cli, ['fetch-eggs', 'xxx']).output
self.assertTrue('Authentication failure' in output)
err = 'Unexpected output: %s' % output
self.assertTrue('Authentication failure' in output, err)

def test_raises_exception_if_request_error(self, requests_mock):
fake_response = FakeResponse(400)
requests_mock.get.return_value = fake_response
output = self.runner.invoke(tool.cli, ['fetch-eggs', 'xxx']).output
self.assertTrue('Eggs could not be fetched' in output)
err = 'Unexpected output: %s' % output
self.assertTrue('Eggs could not be fetched' in output, err)

0 comments on commit fd6113c

Please sign in to comment.