From 7c194cfd2bd035273d6cf9e5888753cdcfb10f83 Mon Sep 17 00:00:00 2001 From: jayy <35180217+nsjcorps@users.noreply.github.com> Date: Fri, 3 Jan 2020 03:15:49 +0100 Subject: [PATCH 1/5] added test for login --- tests/data/challenge_response.py | 3 +++ tests/test_auth.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tests/data/challenge_response.py b/tests/data/challenge_response.py index 369f1ae51..d731441f5 100644 --- a/tests/data/challenge_response.py +++ b/tests/data/challenge_response.py @@ -266,6 +266,9 @@ invalid_token = '{"detail": "Invalid token"}' +valid_token = '{"token": "7q2jdgx52048gkoakectz5349rki"}' + + token_expired = '{"detail": "Token has expired"}' diff --git a/tests/test_auth.py b/tests/test_auth.py index ab5cde3a0..acab21976 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -8,6 +8,8 @@ from evalai.challenges import challenge, challenges from evalai.set_host import host +from evalai.utils.auth import get_host_url +from evalai.login import login from evalai.utils.urls import URLS from evalai.utils.config import ( API_HOST_URL, @@ -30,10 +32,35 @@ def setup(self): self.token = fo.read() os.remove(self.token_file) + valid_token_data = json.loads(challenge_response.valid_token) + + url = "{}{}" + responses.add( + responses.POST, + url.format(get_host_url(), URLS.login.value), + json=valid_token_data, + status=200, + ) + def teardown(self): with open(self.token_file, "w") as f: f.write(self.token) + def test_get_user_auth_token_by_login_success(self): + expected = "username: test" + expected = "{}\n{}".format( + expected, + "Enter password: " + ) + expected = "{}\n{}".format( + expected, + "\nLogged in successfully!" + ) + runner = CliRunner() + result = runner.invoke(login, input="test\npassword",) + response = result.output.rstrip() + assert response == expected + def test_get_user_auth_token_when_file_does_not_exist(self): expected = ( "\nThe authentication token json file doesn't exists at the required path. " From 0bc48f8020fd3863fa589bbbce68099a5a6972cc Mon Sep 17 00:00:00 2001 From: jayy <35180217+nsjcorps@users.noreply.github.com> Date: Fri, 3 Jan 2020 03:29:44 +0100 Subject: [PATCH 2/5] added test for login --- tests/test_auth.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_auth.py b/tests/test_auth.py index acab21976..9fddcf009 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -41,11 +41,17 @@ def setup(self): json=valid_token_data, status=200, ) + responses.add( + responses.POST, + url.format(get_host_url(), URLS.login.value), + status=406, + ) def teardown(self): with open(self.token_file, "w") as f: f.write(self.token) + @responses.activate() def test_get_user_auth_token_by_login_success(self): expected = "username: test" expected = "{}\n{}".format( @@ -61,6 +67,22 @@ def test_get_user_auth_token_by_login_success(self): response = result.output.rstrip() assert response == expected + @responses.activate() + def test_get_user_auth_token_by_login_when_http_error(self): + expected = "username: test" + expected = "{}\n{}".format( + expected, + "Enter password: " + ) + expected = "{}\n{}".format( + expected, + "\nUnable to log in with provided credentials." + ) + runner = CliRunner() + result = runner.invoke(login, input="test\npassword",) + response = result.output.rstrip() + assert response == expected + def test_get_user_auth_token_when_file_does_not_exist(self): expected = ( "\nThe authentication token json file doesn't exists at the required path. " From 37fd9ea114bda9dc9a26f48ce92e9a994c715ece Mon Sep 17 00:00:00 2001 From: jayy <35180217+nsjcorps@users.noreply.github.com> Date: Fri, 3 Jan 2020 03:34:38 +0100 Subject: [PATCH 3/5] added test for login --- tests/test_auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 9fddcf009..86e48ccb3 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -51,7 +51,7 @@ def teardown(self): with open(self.token_file, "w") as f: f.write(self.token) - @responses.activate() + @responses.activate def test_get_user_auth_token_by_login_success(self): expected = "username: test" expected = "{}\n{}".format( @@ -67,7 +67,7 @@ def test_get_user_auth_token_by_login_success(self): response = result.output.rstrip() assert response == expected - @responses.activate() + @responses.activate def test_get_user_auth_token_by_login_when_http_error(self): expected = "username: test" expected = "{}\n{}".format( From 028f595a2de8f8bac0026de08d0b3ed5ab420edf Mon Sep 17 00:00:00 2001 From: jayy <35180217+nsjcorps@users.noreply.github.com> Date: Fri, 3 Jan 2020 03:43:48 +0100 Subject: [PATCH 4/5] Fixed Errors --- tests/test_auth.py | 89 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 14 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 86e48ccb3..243fa5090 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -27,6 +27,31 @@ class TestGetUserAuthToken(BaseTestClass): token_file = os.path.join(AUTH_TOKEN_DIR, AUTH_TOKEN_FILE_NAME) + def setup(self): + with open(self.token_file) as fo: + self.token = fo.read() + os.remove(self.token_file) + + def teardown(self): + with open(self.token_file, "w") as f: + f.write(self.token) + + def test_get_user_auth_token_when_file_does_not_exist(self): + expected = ( + "\nThe authentication token json file doesn't exists at the required path. " + "Please download the file from the Profile section of the EvalAI webapp and " + "place it at ~/.evalai/token.json\n\n" + ) + runner = CliRunner() + result = runner.invoke(challenges) + response = result.output + assert response == expected + + +class TestGetUserAuthTokenByLogin(BaseTestClass): + + token_file = os.path.join(AUTH_TOKEN_DIR, AUTH_TOKEN_FILE_NAME) + def setup(self): with open(self.token_file) as fo: self.token = fo.read() @@ -41,11 +66,6 @@ def setup(self): json=valid_token_data, status=200, ) - responses.add( - responses.POST, - url.format(get_host_url(), URLS.login.value), - status=406, - ) def teardown(self): with open(self.token_file, "w") as f: @@ -61,12 +81,32 @@ def test_get_user_auth_token_by_login_success(self): expected = "{}\n{}".format( expected, "\nLogged in successfully!" - ) + ) runner = CliRunner() result = runner.invoke(login, input="test\npassword",) response = result.output.rstrip() assert response == expected + +class TestGetUserAuthTokenByLoginWithHTTPError(BaseTestClass): + + token_file = os.path.join(AUTH_TOKEN_DIR, AUTH_TOKEN_FILE_NAME) + + def setup(self): + with open(self.token_file) as fo: + self.token = fo.read() + os.remove(self.token_file) + + responses.add( + responses.POST, + url.format(get_host_url(), URLS.login.value), + status=406, + ) + + def teardown(self): + with open(self.token_file, "w") as f: + f.write(self.token) + @responses.activate def test_get_user_auth_token_by_login_when_http_error(self): expected = "username: test" @@ -77,21 +117,42 @@ def test_get_user_auth_token_by_login_when_http_error(self): expected = "{}\n{}".format( expected, "\nUnable to log in with provided credentials." - ) + ) runner = CliRunner() result = runner.invoke(login, input="test\npassword",) response = result.output.rstrip() assert response == expected - def test_get_user_auth_token_when_file_does_not_exist(self): - expected = ( - "\nThe authentication token json file doesn't exists at the required path. " - "Please download the file from the Profile section of the EvalAI webapp and " - "place it at ~/.evalai/token.json\n\n" + +class TestGetUserAuthTokenByLoginWithRequestError(BaseTestClass): + + token_file = os.path.join(AUTH_TOKEN_DIR, AUTH_TOKEN_FILE_NAME) + + def setup(self): + with open(self.token_file) as fo: + self.token = fo.read() + os.remove(self.token_file) + + + def teardown(self): + with open(self.token_file, "w") as f: + f.write(self.token) + + @responses.activate + def test_get_user_auth_token_by_login_when_request_error(self): + expected = "username: test" + expected = "{}\n{}".format( + expected, + "Enter password: " + ) + expected = "{}\n{}".format( + expected, + "\nCould not establish a connection to EvalAI." + " Please check the Host URL.", ) runner = CliRunner() - result = runner.invoke(challenges) - response = result.output + result = runner.invoke(login, input="test\npassword",) + response = result.output.rstrip() assert response == expected From a06df8b8d90260b1bd9548bd1a67632b5788f774 Mon Sep 17 00:00:00 2001 From: jayy <35180217+nsjcorps@users.noreply.github.com> Date: Fri, 3 Jan 2020 03:46:34 +0100 Subject: [PATCH 5/5] added test for login --- tests/test_auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 243fa5090..a65906550 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -96,7 +96,8 @@ def setup(self): with open(self.token_file) as fo: self.token = fo.read() os.remove(self.token_file) - + + url = "{}{}" responses.add( responses.POST, url.format(get_host_url(), URLS.login.value), @@ -133,7 +134,6 @@ def setup(self): self.token = fo.read() os.remove(self.token_file) - def teardown(self): with open(self.token_file, "w") as f: f.write(self.token)