Skip to content

Commit

Permalink
Add API key login test, refactor credential removal code
Browse files Browse the repository at this point in the history
  • Loading branch information
boydgreenfield committed May 15, 2018
1 parent debaeed commit 4341e7c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 11 additions & 5 deletions onecodex/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,26 @@ def _login(server, creds_file=None, api_key=None):
return email


def _logout(creds_file=None):
def _remove_creds(creds_file=None):
"""
Logout main function, just rm ~/.onecodex more or less
Remove ~/.onecodex file, returning True if successul or False if the file didn't exist
"""

# creds file path setup
if creds_file is None:
fp = os.path.expanduser("~/.onecodex")
else:
fp = creds_file

if os.path.exists(fp):
# we might not want to do this if there's there are cached schema in it?
os.remove(fp)
return True
return False


def _logout(creds_file=None):
"""
Logout main function, just rm ~/.onecodex more or less
"""
if _remove_creds(creds_file=creds_file):
click.echo("Successfully removed One Codex credentials.", err=True)
sys.exit(0)
else:
Expand Down
4 changes: 2 additions & 2 deletions onecodex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
warn_simplejson, telemetry)
from onecodex.api import Api
from onecodex.exceptions import ValidationWarning, ValidationError, UploadException
from onecodex.auth import _login, _logout, _silent_login
from onecodex.auth import _login, _logout, _remove_creds, _silent_login
from onecodex.scripts import filter_reads
from onecodex.version import __version__

Expand Down Expand Up @@ -280,7 +280,7 @@ def login(ctx):
# with, e.g., connection error catching (it's not part of our formally documeted API at the moment)
if ocx._client.Account.instances()['email'] != email:
click.echo('Your login credentials do not match the provided email!', err=True)
_logout()
_remove_creds()
sys.exit(1)


Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ def test_api_login(runner, mocked_creds_path):
assert successful_login_msg in result.output


@pytest.mark.parametrize('email,success,code', [
('incorrect+email@onecodex.com', False, 1),
('asmngs@onecodex.com', True, 0),
])
def test_api_key_login(runner, api_data, mocked_creds_path, email, success, code):
result = runner.invoke(Cli, ['--api-key', '0' * 32, 'login'], input=email)
assert result.exit_code == code
assert os.path.exists(os.path.expanduser('~/.onecodex')) is success


def test_creds_file_exists(runner, mocked_creds_file):
with runner.isolated_filesystem():
make_creds_file()
Expand Down

0 comments on commit 4341e7c

Please sign in to comment.