diff --git a/requirements.txt b/requirements.txt index 48e9fbc17..327c13c23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index a0b63e692..254755aa5 100644 --- a/setup.py +++ b/setup.py @@ -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", ] diff --git a/tests/data/auth_response.py b/tests/data/auth_response.py new file mode 100644 index 000000000..749060b6f --- /dev/null +++ b/tests/data/auth_response.py @@ -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" +} +""" diff --git a/tests/test_auth.py b/tests/test_auth.py index 6f15cccef..f6e17edd1 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -6,6 +6,7 @@ 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 @@ -13,11 +14,12 @@ 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 @@ -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)