-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
15 deletions.
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
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,21 @@ | ||
import os | ||
|
||
|
||
def get_token() -> str: | ||
# Get token from environment variable | ||
if 'CRAWLAB_TOKEN' in os.environ: | ||
return os.environ['CRAWLAB_TOKEN'] | ||
|
||
# Prompt user to enter token | ||
token = input('Please enter your API token for Crawlab AI: ') | ||
return token | ||
|
||
|
||
def get_auth_headers() -> dict: | ||
return { | ||
'Authorization': f'Bearer {get_token()}' | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
print(get_token()) |
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
Empty file.
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,35 @@ | ||
import os | ||
from unittest.mock import patch, mock_open | ||
from crawlab_ai.utils import auth | ||
|
||
|
||
def test_set_token_creates_directory_and_file(): | ||
with patch('os.path.exists', return_value=False), patch('os.makedirs'), patch('builtins.open', | ||
new_callable=mock_open): | ||
auth.set_token('test_token') | ||
os.makedirs.assert_called_once_with(auth.ROOT_DIRECTORY) | ||
open.assert_called_once_with(f'{auth.ROOT_DIRECTORY}/token', 'w') | ||
|
||
|
||
def test_set_token_writes_token_to_file(): | ||
with patch('os.path.exists', return_value=True), patch('builtins.open', new_callable=mock_open) as new_mock_open: | ||
auth.set_token('test_token') | ||
new_mock_open().write.assert_called_once_with('test_token') | ||
|
||
|
||
def test_get_token_returns_env_var_token(): | ||
with patch.dict(os.environ, {'CRAWLAB_TOKEN': 'env_token'}): | ||
assert auth.get_token() == 'env_token' | ||
|
||
|
||
def test_get_token_returns_file_token(): | ||
with patch('os.path.exists', return_value=True), patch('builtins.open', new_callable=mock_open, | ||
read_data='file_token'): | ||
assert auth.get_token() == 'file_token' | ||
|
||
|
||
def test_get_token_prompts_for_token(): | ||
with patch('os.path.exists', return_value=False), patch('builtins.input', return_value='input_token'), patch( | ||
'crawlab_ai.utils.auth.set_token') as mock_set_token: | ||
assert auth.get_token() == 'input_token' | ||
mock_set_token.assert_called_once_with('input_token') |