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 #307 : Added tests for login command #314

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ docker==3.6.0
lxml==4.6.2
python-dateutil==2.7.3
requests==2.25.1
responses==0.12.1
validators==0.12.6
termcolor==1.1.0
tqdm>=4.49.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def run_tests(self):
"pytest==3.5.1",
"pytest-cov==2.5.1",
"pytest-env==0.6.2",
"responses==0.9.0",
"responses==0.12.1",
"pre-commit==1.14.4",
]

Expand Down
39 changes: 39 additions & 0 deletions tests/data/auth_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
valid_login_body = """
{
"username": "host",
"password": "password"
}
"""

valid_login_response = """
{
"token": "test_token"
}
"""

invalid_login_body = """
{
"username": "notahost",
"password": "notapassword"
}
"""

invalid_login_response = """
{
"non_field_errors": [
"Unable to log in with provided credentials."
]
}
"""

get_access_token_response = """
{
"token": "test_access_token"
}
"""

get_access_token_headers = """
{
"Authorization": "Token test_token"
}
"""
80 changes: 79 additions & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
from click.testing import CliRunner
from termcolor import colored

from evalai.login import login
from evalai.challenges import challenge, challenges
from evalai.set_host import host
from evalai.utils.urls import URLS
from evalai.utils.config import (
API_HOST_URL,
AUTH_TOKEN_DIR,
AUTH_TOKEN_FILE_NAME,
AUTH_TOKEN_PATH,
HOST_URL_FILE_PATH,
)
from evalai.utils.common import convert_UTC_date_to_local

from tests.data import challenge_response
from tests.data import challenge_response, auth_response
from tests.base import BaseTestClass


Expand Down Expand Up @@ -219,3 +221,79 @@ def test_set_and_load_host_url(self):
result = runner.invoke(challenges)
response = result.output.strip()
assert str(response) == self.output


class TestLogin(BaseTestClass):
def setup(self):
url = "{}{}"

valid_login_response = json.loads(auth_response.valid_login_response)
valid_login_body = json.loads(auth_response.valid_login_body)

responses.add(
responses.POST,
url.format("https://eval.ai", URLS.login.value),
json=valid_login_response,
match=[
responses.urlencoded_params_matcher(valid_login_body)
],
status=200
)

invalid_login_response = json.loads(auth_response.invalid_login_response)
invalid_login_body = json.loads(auth_response.invalid_login_body)
responses.add(
responses.POST,
url.format("https://eval.ai", URLS.login.value),
json=invalid_login_response,
match=[
responses.urlencoded_params_matcher(invalid_login_body)
],
status=400
)

get_access_token_response = json.loads(auth_response.get_access_token_response)
get_access_token_headers = json.loads(auth_response.get_access_token_headers)
responses.add(
responses.GET,
url.format("https://eval.ai", URLS.get_access_token.value),
json=get_access_token_response,
headers=get_access_token_headers,
status=200
)

def teardown(self):
if os.path.exists(HOST_URL_FILE_PATH):
os.remove(HOST_URL_FILE_PATH)

@responses.activate
def test_login(self):
json.loads(auth_response.get_access_token_response)
expected = "\nLogged in successfully!"
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(login, input="host\npassword")
response = result.output.strip()
assert expected in str(response)
assert os.path.exists(AUTH_TOKEN_PATH), "Auth Token is not set"
# Checking if the token is equal to what was set during login
with open(str(AUTH_TOKEN_PATH), "r") as TokenFile:
assert json.loads(TokenFile.read()) == json.loads(auth_response.get_access_token_response)

@responses.activate
def test_login_when_httperror(self):
expected = "\nCould not establish a connection to EvalAI. Please check the Host URL."
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://evalaiwrongurl.ai"])
result = runner.invoke(login, input="host\npassword")
response = result.output.strip()
assert expected in str(response)

@responses.activate
def test_login_when_wrong_credentials(self):
expected = "\nUnable to log in with provided credentials."
runner = CliRunner()
result = runner.invoke(host, ["-sh", "https://eval.ai"])
result = runner.invoke(login, input="notahost\nnotapassword")
response = result.output.strip()
assert expected in str(response)