Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
19 changes: 4 additions & 15 deletions evalai/login.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import click
import json

from click import echo, style
from evalai.utils.auth import get_user_auth_token_by_login
from evalai.utils.common import store_data_to_json
from evalai.utils.config import AUTH_TOKEN_PATH, AUTH_TOKEN_DIR


Expand All @@ -17,19 +17,8 @@ def login(ctx):
password = click.prompt("Enter password", type=str, hide_input=True)
token = get_user_auth_token_by_login(username, password)

if os.path.exists(AUTH_TOKEN_PATH):
with open(str(AUTH_TOKEN_PATH), "w") as TokenFile:
try:
json.dump(token, TokenFile)
except (OSError, IOError) as e:
echo(e)
else:
if not os.path.exists(AUTH_TOKEN_DIR):
os.makedirs(AUTH_TOKEN_DIR)
with open(str(AUTH_TOKEN_PATH), "w+") as TokenFile:
try:
json.dump(token, TokenFile)
except (OSError, IOError) as e:
echo(e)
if not os.path.exists(AUTH_TOKEN_DIR):
os.makedirs(AUTH_TOKEN_DIR)
store_data_to_json(AUTH_TOKEN_PATH, token, "Unable to store token data due to error: {}")

echo(style("\nLogged in successfully!", bold=True))
17 changes: 16 additions & 1 deletion evalai/utils/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import click
import json
import sys

from bs4 import BeautifulSoup
from click import echo, style
Expand Down Expand Up @@ -88,3 +89,17 @@ def clean_data(data):

def notify_user(message, color="green", bold=False):
echo(style(message, fg=color, bold=bold))


def store_data_to_json(path, content, message):
with open(path, "w") as file:
try:
json.dump(content, file)
except (OSError, IOError) as e:
echo(
style(
message.format(e),
bold=True,
fg="red",
)
)
35 changes: 35 additions & 0 deletions tests/test_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import responses
import json

from unittest.mock import patch
from click.testing import CliRunner
from evalai.utils.auth import URLS
from evalai.utils.config import API_HOST_URL
from evalai.login import login

from .base import BaseTestClass


class TestLogin(BaseTestClass):
def setup(self):
token = json.loads("""{"token": "test"}""")

url = "{}{}"
responses.add(
responses.POST,
url.format(API_HOST_URL, URLS.login.value),
json=token,
status=200,
)

@responses.activate
@patch("evalai.utils.common.json.dump")
def test_login_when_storing_json_fails(self, mock_dump):
error = "Exception"
expected = "Unable to store token data due to error: {}".format(error)
mock_dump.side_effect = OSError(error)

runner = CliRunner()
result = runner.invoke(login, input="username\npassword")
response = result.output
assert expected in response