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

Added Test for Utils/auth.py #245

Open
wants to merge 6 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
3 changes: 3 additions & 0 deletions tests/data/challenge_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
invalid_token = '{"detail": "Invalid token"}'


valid_token = '{"token": "7q2jdgx52048gkoakectz5349rki"}'


token_expired = '{"detail": "Token has expired"}'


Expand Down
110 changes: 110 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,6 +48,114 @@ def test_get_user_auth_token_when_file_does_not_exist(self):
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()
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)

@responses.activate
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


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)

url = "{}{}"
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"
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


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(login, input="test\npassword",)
response = result.output.rstrip()
assert response == expected


class TestUserRequestWithInvalidToken(BaseTestClass):
def setup(self):

Expand Down