-
Notifications
You must be signed in to change notification settings - Fork 63
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 #205: Add functionality to reset authentication token and logout #233
Open
nikochiko
wants to merge
12
commits into
Cloud-CV:master
Choose a base branch
from
nikochiko:ResetToken
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
061ca84
Add functionality to reset existing token
nikochiko 5d5139e
Fix flake8 errors
nikochiko eae955a
Fix click syntax
nikochiko 346faf6
Minor fixes
nikochiko 3c1f95d
Fix testcase issues
nikochiko df17a5e
Use three double quotes for multi-line strings
nikochiko 84dcf59
Revert last change, update multi-line string syntax
nikochiko b704f20
Add docstring for logout command
nikochiko 013a8b2
Remove option to clear token from set_token command
nikochiko 45cebc9
Add docstring for logout command
nikochiko 41efe01
Merge branch 'ResetToken' of https://github.com/nikochiko/evalai-cli …
nikochiko a36dbf6
Clarify unit test name
nikochiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import click | ||
|
||
from click import echo, style | ||
|
||
from evalai.utils.auth import reset_user_auth_token | ||
|
||
|
||
@click.group(invoke_without_command=True) | ||
def logout(): | ||
""" | ||
Log out the user by resetting authentication token | ||
""" | ||
""" | ||
Invoked by `evalai logout` | ||
""" | ||
reset_user_auth_token() | ||
echo(style("Logout successful", bold=True, fg="green")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import json | ||
import os | ||
import random | ||
import shutil | ||
import string | ||
import tempfile | ||
|
||
from io import StringIO | ||
from unittest import mock | ||
from unittest import TestCase | ||
|
||
from evalai.utils.auth import reset_user_auth_token | ||
|
||
|
||
class TestResetUserAuthToken(TestCase): | ||
def setUp(self): | ||
self.base_temp_dir = tempfile.mkdtemp() | ||
self.token_dir = os.path.join(self.base_temp_dir, ".evalai") | ||
self.token_path = os.path.join(self.token_dir, "token.json") | ||
|
||
self.token = "".join(random.choice(string.ascii_lowercase) for _ in range(40)) | ||
self.token_json = json.dumps({"token": self.token}) | ||
|
||
os.makedirs(self.token_dir) | ||
with open(self.token_path, "w") as fw: | ||
fw.write(self.token_json) | ||
|
||
self.patcher = mock.patch("evalai.utils.auth.AUTH_TOKEN_PATH", self.token_path) | ||
self.patcher.start() | ||
|
||
def tearDown(self): | ||
self.patcher.stop() | ||
if os.path.exists(self.base_temp_dir): | ||
shutil.rmtree(self.base_temp_dir) | ||
|
||
def test_reset_user_auth_token_success(self): | ||
self.assertTrue(os.path.exists(self.token_path)) # Make sure the path exists already | ||
reset_user_auth_token() | ||
|
||
self.assertFalse(os.path.exists(self.token_path)) | ||
|
||
def test_reset_user_auth_token_when_token_is_not_configured(self): | ||
os.remove(self.token_path) | ||
expected = str( | ||
"The authentication token has not been configured. Please use the commands " | ||
"`evalai login` or `evalai set_token TOKEN` first to set up the configuration." | ||
) | ||
|
||
with mock.patch("sys.stdout", StringIO()) as fake_out: | ||
with self.assertRaises(SystemExit) as cm: | ||
reset_user_auth_token() | ||
exit_code = cm.exception.code | ||
value = fake_out.getvalue().strip() | ||
|
||
self.assertEqual(exit_code, 1) | ||
self.assertEqual(value, expected) | ||
|
||
@mock.patch("evalai.utils.auth.os.remove") | ||
def test_reset_user_auth_token_when_writing_to_file_fails(self, mock_remove): | ||
error = "ExampleError: Example Error Description" | ||
mock_remove.side_effect = OSError(error) | ||
|
||
with mock.patch("sys.stdout", StringIO()) as fake_out: | ||
with self.assertRaises(SystemExit) as cm: | ||
reset_user_auth_token() | ||
exit_code = cm.exception.code | ||
value = fake_out.getvalue().strip() | ||
|
||
self.assertEqual(exit_code, 1) | ||
self.assertEqual(value, error) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikochiko What does this test signify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pushkalkatara this is the test for the case when file deleting fails. And oops I think I named it incorrectly 😕 . How about changing the name to
test_reset_user_auth_token_when_token_file_removal_fails
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! 👍