Skip to content

Commit

Permalink
prompt for org and user if needed (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: thoward <[email protected]>
  • Loading branch information
tahoward and thoward-godaddy authored Dec 4, 2020
1 parent 671b264 commit 84c8e80
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ atomicwrites = "*"
importlib-metadata = "*"

[packages]
aws-okta-processor = {path = "."}
aws-okta-processor = {path = ".", editable=true}
229 changes: 106 additions & 123 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/aws_okta_processor/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.5.1'
__version__ = '1.5.2'
8 changes: 8 additions & 0 deletions src/aws_okta_processor/core/okta.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,17 @@ def __init__(
)

if not self.okta_session_id:
if not user_name:
print_tty(string="UserName: ", newline=False)
user_name = input()

if not user_pass:
user_pass = getpass.getpass()

if not organization:
print_tty(string="Organization: ", newline=False)
self.organization = input()

self.okta_single_use_token = self.get_okta_single_use_token(
user_name=user_name,
user_pass=user_pass
Expand Down
67 changes: 67 additions & 0 deletions tests/commands/test_authenticate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,79 @@
from unittest.mock import patch

import tests
import os

from aws_okta_processor.commands.authenticate import Authenticate
from tests.test_base import TestBase


CREDENTIALS = {
"AccessKeyId": "access_key_id",
"SecretAccessKey": "secret_access_key",
"SessionToken": "session_token"
}


class TestAuthenticate(TestBase):
@patch("aws_okta_processor.commands.authenticate.JSONFileCache")
@patch("aws_okta_processor.commands.authenticate.SAMLFetcher")
def test_authenticate(self, mock_saml_fetcher, mock_json_file_cache):
mock_saml_fetcher().fetch_credentials.return_value = CREDENTIALS
auth = Authenticate(self.OPTIONS)
credentials = auth.authenticate()

mock_json_file_cache.assert_called_once_with()
assert credentials == CREDENTIALS

@patch("aws_okta_processor.commands.authenticate.print")
def test_run(self, mock_print):
auth = Authenticate(self.OPTIONS)
auth.authenticate = (lambda: CREDENTIALS)
auth.run()

mock_print.assert_called_once_with(
'{"AccessKeyId": "access_key_id", '
'"SecretAccessKey": "secret_access_key", '
'"SessionToken": "session_token", '
'"Version": 1}'
)

@patch("aws_okta_processor.commands.authenticate.os")
@patch("aws_okta_processor.commands.authenticate.print")
def test_run_nt(self, mock_print, mock_os):
mock_os.name = "nt"
self.OPTIONS["--environment"] = True
auth = Authenticate(self.OPTIONS)
auth.authenticate = (lambda: CREDENTIALS)
auth.run()

mock_print.assert_called_once_with(
"$env:AWS_ACCESS_KEY_ID='access_key_id'; "
"$env:AWS_SECRET_ACCESS_KEY='secret_access_key'; "
"$env:AWS_SESSION_TOKEN='session_token'"
)

@patch("aws_okta_processor.commands.authenticate.os")
@patch("aws_okta_processor.commands.authenticate.print")
def test_run_linux(self, mock_print, mock_os):
mock_os.name = "linux"
self.OPTIONS["--environment"] = True
auth = Authenticate(self.OPTIONS)
auth.authenticate = (lambda: CREDENTIALS)
auth.run()

mock_print.assert_called_once_with(
"export AWS_ACCESS_KEY_ID='access_key_id' && "
"export AWS_SECRET_ACCESS_KEY='secret_access_key' && "
"export AWS_SESSION_TOKEN='session_token'"
)

def test_get_configuration_env(self):
os.environ["AWS_OKTA_ENVIRONMENT"] = "1"
auth = Authenticate(self.OPTIONS)
del os.environ["AWS_OKTA_ENVIRONMENT"]

assert auth.configuration["AWS_OKTA_ENVIRONMENT"] == "1"

def test_output_export_command_with_fish_as_target_shell(self):
""" Tests the export command for fish shell """
Expand Down
4 changes: 4 additions & 0 deletions tests/commands/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@


class TestBase(TestBase):
def test_get_config(self):
with self.assertRaises(NotImplementedError) as context:
Base(self.OPTIONS)

@patch('os.path.expanduser', side_effect=tests.expand_user_side_effect)
def test_get_userfile_should_locate_user_file(self, mock_expanduser):
actual = Base.get_userfile()
Expand Down

0 comments on commit 84c8e80

Please sign in to comment.