From 2cdb1b16977d1173d0e7522cb445d83a2da0d9c1 Mon Sep 17 00:00:00 2001 From: Shaurya Date: Tue, 26 Jan 2021 21:12:20 +0530 Subject: [PATCH 1/7] Added tests for login --- tests/test_auth.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_auth.py b/tests/test_auth.py index 6f15cccef..217ef7b5f 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,6 +14,7 @@ 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 @@ -219,3 +221,36 @@ 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 teardown(self): + if os.path.exists(HOST_URL_FILE_PATH): + os.remove(HOST_URL_FILE_PATH) + + def test_login(self): + expected = "\nLogged in successfully!" + runner = CliRunner() + result = runner.invoke(host, ["-sh", "http://localhost:8000"]) + result = runner.invoke(login, input="host\npassword") + responses = result.output.strip() + assert expected in str(responses) + assert os.path.exists(AUTH_TOKEN_PATH), "Auth Token is not set" + + 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(host) + result = runner.invoke(login, input="host\npassword") + responses = result.output.strip() + assert expected in str(responses) + + def test_login_when_wrong_credentials(self): + expected = "\nUnable to log in with provided credentials." + runner = CliRunner() + result = runner.invoke(host, ["-sh", "http://localhost:8000"]) + result = runner.invoke(host) + result = runner.invoke(login, input="notahost\nnotapassword") + responses = result.output.strip() + assert expected in str(responses) \ No newline at end of file From 120c8e8fcaf754ebb7b141a961a826cf5f5a44ae Mon Sep 17 00:00:00 2001 From: Shaurya Date: Tue, 26 Jan 2021 22:46:48 +0530 Subject: [PATCH 2/7] Added mock requests for login tests --- tests/data/auth_response.py | 39 +++++++++++++++++++++++++++ tests/test_auth.py | 54 +++++++++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/data/auth_response.py diff --git a/tests/data/auth_response.py b/tests/data/auth_response.py new file mode 100644 index 000000000..b7d35cc33 --- /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" +} +""" \ No newline at end of file diff --git a/tests/test_auth.py b/tests/test_auth.py index 217ef7b5f..715ed2e96 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.utils.auth import get_host_url from evalai.login import login from evalai.challenges import challenge, challenges from evalai.set_host import host @@ -19,7 +20,7 @@ ) 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 @@ -224,33 +225,76 @@ def test_set_and_load_host_url(self): 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", "http://localhost:8000"]) + result = runner.invoke(host, ["-sh", "https://eval.ai"]) result = runner.invoke(login, input="host\npassword") responses = result.output.strip() assert expected in str(responses) 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(host) result = runner.invoke(login, input="host\npassword") responses = result.output.strip() assert expected in str(responses) + @responses.activate def test_login_when_wrong_credentials(self): expected = "\nUnable to log in with provided credentials." runner = CliRunner() - result = runner.invoke(host, ["-sh", "http://localhost:8000"]) - result = runner.invoke(host) + result = runner.invoke(host, ["-sh", "https://eval.ai"]) result = runner.invoke(login, input="notahost\nnotapassword") responses = result.output.strip() assert expected in str(responses) \ No newline at end of file From 3025137af63ee67844529b0e1309e716be12f039 Mon Sep 17 00:00:00 2001 From: Shaurya Date: Thu, 28 Jan 2021 17:37:31 +0530 Subject: [PATCH 3/7] Fixed linting issues --- tests/data/auth_response.py | 2 +- tests/test_auth.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/data/auth_response.py b/tests/data/auth_response.py index b7d35cc33..749060b6f 100644 --- a/tests/data/auth_response.py +++ b/tests/data/auth_response.py @@ -36,4 +36,4 @@ { "Authorization": "Token test_token" } -""" \ No newline at end of file +""" diff --git a/tests/test_auth.py b/tests/test_auth.py index 715ed2e96..01b74067c 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -6,7 +6,6 @@ from click.testing import CliRunner from termcolor import colored -from evalai.utils.auth import get_host_url from evalai.login import login from evalai.challenges import challenge, challenges from evalai.set_host import host @@ -235,7 +234,7 @@ def setup(self): responses.POST, url.format("https://eval.ai", URLS.login.value), json = valid_login_response, - match=[ + match = [ responses.urlencoded_params_matcher(valid_login_body) ], status=200 @@ -247,7 +246,7 @@ def setup(self): responses.POST, url.format("https://eval.ai", URLS.login.value), json = invalid_login_response, - match=[ + match = [ responses.urlencoded_params_matcher(invalid_login_body) ], status=400 @@ -297,4 +296,5 @@ def test_login_when_wrong_credentials(self): result = runner.invoke(host, ["-sh", "https://eval.ai"]) result = runner.invoke(login, input="notahost\nnotapassword") responses = result.output.strip() - assert expected in str(responses) \ No newline at end of file + assert expected in str(responses) + \ No newline at end of file From 071444fff1094a8132ff469b7ddc74035c8c2b56 Mon Sep 17 00:00:00 2001 From: Shaurya Date: Thu, 28 Jan 2021 17:56:55 +0530 Subject: [PATCH 4/7] Fixing Linting issues --- tests/test_auth.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 01b74067c..232aa25f8 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -233,8 +233,8 @@ def setup(self): responses.add( responses.POST, url.format("https://eval.ai", URLS.login.value), - json = valid_login_response, - match = [ + json=valid_login_response, + match=[ responses.urlencoded_params_matcher(valid_login_body) ], status=200 @@ -245,8 +245,8 @@ def setup(self): responses.add( responses.POST, url.format("https://eval.ai", URLS.login.value), - json = invalid_login_response, - match = [ + json=invalid_login_response, + match=[ responses.urlencoded_params_matcher(invalid_login_body) ], status=400 @@ -278,7 +278,7 @@ def test_login(self): 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) + assert json.loads(TokenFile.read()) == json.loads(auth_response.get_access_token_response) @responses.activate def test_login_when_httperror(self): @@ -297,4 +297,3 @@ def test_login_when_wrong_credentials(self): result = runner.invoke(login, input="notahost\nnotapassword") responses = result.output.strip() assert expected in str(responses) - \ No newline at end of file From bee687624911ef8e66a6fd5c6240ea3d28e3c534 Mon Sep 17 00:00:00 2001 From: Shaurya Date: Thu, 28 Jan 2021 18:06:16 +0530 Subject: [PATCH 5/7] Changed "reponses" variable to "response" --- tests/test_auth.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 232aa25f8..f6e17edd1 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -273,8 +273,8 @@ def test_login(self): runner = CliRunner() result = runner.invoke(host, ["-sh", "https://eval.ai"]) result = runner.invoke(login, input="host\npassword") - responses = result.output.strip() - assert expected in str(responses) + 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: @@ -286,8 +286,8 @@ def test_login_when_httperror(self): runner = CliRunner() result = runner.invoke(host, ["-sh", "https://evalaiwrongurl.ai"]) result = runner.invoke(login, input="host\npassword") - responses = result.output.strip() - assert expected in str(responses) + response = result.output.strip() + assert expected in str(response) @responses.activate def test_login_when_wrong_credentials(self): @@ -295,5 +295,5 @@ def test_login_when_wrong_credentials(self): runner = CliRunner() result = runner.invoke(host, ["-sh", "https://eval.ai"]) result = runner.invoke(login, input="notahost\nnotapassword") - responses = result.output.strip() - assert expected in str(responses) + response = result.output.strip() + assert expected in str(response) From ca2dbe8735c36eb74b33132470a3642f6fb9be7e Mon Sep 17 00:00:00 2001 From: Shaurya Date: Thu, 28 Jan 2021 18:15:48 +0530 Subject: [PATCH 6/7] Added "responses" in "requirements.txt" --- requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From bcd227f8bc3449b0c68cee6530c891b5c21bc361 Mon Sep 17 00:00:00 2001 From: Shaurya Date: Sat, 6 Feb 2021 19:35:11 +0530 Subject: [PATCH 7/7] Changed responses version in setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", ]