1616from code42cli .main import cli
1717from code42cli .options import CLIState
1818from code42cli .sdk_client import create_sdk
19- from code42cli .sdk_client import validate_connection
2019
2120
2221@pytest .fixture
@@ -29,6 +28,17 @@ def mock_sdk_factory(mocker):
2928 return mocker .patch ("py42.sdk.from_local_account" )
3029
3130
31+ @pytest .fixture
32+ def mock_profile_with_password ():
33+ profile = create_mock_profile ()
34+
35+ def mock_get_password ():
36+ return "Test Password"
37+
38+ profile .get_password = mock_get_password
39+ return profile
40+
41+
3242@pytest .fixture
3343def requests_exception (mocker ):
3444 mock_response = mocker .MagicMock (spec = Response )
@@ -54,99 +64,84 @@ def test_create_sdk_when_profile_has_ssl_errors_disabled_sets_py42_setting_and_p
5464
5565
5666def test_create_sdk_when_py42_exception_occurs_raises_and_logs_cli_error (
57- sdk_logger , mock_sdk_factory , requests_exception
67+ sdk_logger , mock_sdk_factory , requests_exception , mock_profile_with_password
5868):
5969
6070 mock_sdk_factory .side_effect = Py42UnauthorizedError (requests_exception )
61- profile = create_mock_profile ()
62-
63- def mock_get_password ():
64- return "Test Password"
6571
66- profile .get_password = mock_get_password
6772 with pytest .raises (Code42CLIError ) as err :
68- create_sdk (profile , False )
73+ create_sdk (mock_profile_with_password , False )
6974
7075 assert "Invalid credentials for user" in err .value .message
7176 assert sdk_logger .log_error .call_count == 1
72- assert "Failure in HTTP call" in sdk_logger .log_error .call_args [0 ][0 ]
77+ assert "Failure in HTTP call" in str ( sdk_logger .log_error .call_args [0 ][0 ])
7378
7479
7580def test_create_sdk_when_connection_exception_occurs_raises_and_logs_cli_error (
76- sdk_logger , mock_sdk_factory
81+ sdk_logger , mock_sdk_factory , mock_profile_with_password
7782):
7883 mock_sdk_factory .side_effect = ConnectionError ("connection message" )
79- profile = create_mock_profile ()
8084
81- def mock_get_password ():
82- return "Test Password"
83-
84- profile .get_password = mock_get_password
8585 with pytest .raises (LoggedCLIError ) as err :
86- create_sdk (profile , False )
86+ create_sdk (mock_profile_with_password , False )
8787
8888 assert "Problem connecting to" in err .value .message
8989 assert sdk_logger .log_error .call_count == 1
90- assert "connection message" in sdk_logger .log_error .call_args [0 ][0 ]
90+ assert "connection message" in str ( sdk_logger .log_error .call_args [0 ][0 ])
9191
9292
9393def test_create_sdk_when_unknown_exception_occurs_raises_and_logs_cli_error (
94- sdk_logger , mock_sdk_factory
94+ sdk_logger , mock_sdk_factory , mock_profile_with_password
9595):
9696 mock_sdk_factory .side_effect = Exception ("test message" )
97- profile = create_mock_profile ()
98-
99- def mock_get_password ():
100- return "Test Password"
10197
102- profile .get_password = mock_get_password
10398 with pytest .raises (LoggedCLIError ) as err :
104- create_sdk (profile , False )
99+ create_sdk (mock_profile_with_password , False )
105100
106101 assert "Unknown problem validating" in err .value .message
107102 assert sdk_logger .log_error .call_count == 1
108- assert "test message" in sdk_logger .log_error .call_args [0 ][0 ]
109-
103+ assert "test message" in str (sdk_logger .log_error .call_args [0 ][0 ])
110104
111- def test_create_sdk_when_told_to_debug_turns_on_debug (mock_sdk_factory ):
112- profile = create_mock_profile ()
113-
114- def mock_get_password ():
115- return "Test Password"
116105
117- profile .get_password = mock_get_password
118- create_sdk (profile , True )
106+ def test_create_sdk_when_told_to_debug_turns_on_debug (
107+ mock_sdk_factory , mock_profile_with_password
108+ ):
109+ create_sdk (mock_profile_with_password , True )
119110 assert py42 .settings .debug .level == debug .DEBUG
120111
121112
122- def test_validate_connection_uses_given_credentials (mock_sdk_factory ):
123- assert validate_connection ("Authority" , "Test" , "Password" , None )
124- mock_sdk_factory .assert_called_once_with ("Authority" , "Test" , "Password" , totp = None )
113+ def test_create_sdk_uses_given_credentials (
114+ mock_sdk_factory , mock_profile_with_password
115+ ):
116+ create_sdk (mock_profile_with_password , False )
117+ mock_sdk_factory .assert_called_once_with (
118+ "example.com" , "foo" , "Test Password" , totp = None
119+ )
125120
126121
127- def test_validate_connection_when_mfa_required_exception_raised_prompts_for_totp (
128- mocker , monkeypatch , mock_sdk_factory , capsys
122+ def test_create_sdk_connection_when_mfa_required_exception_raised_prompts_for_totp (
123+ mocker , monkeypatch , mock_sdk_factory , capsys , mock_profile_with_password
129124):
130125 monkeypatch .setattr ("sys.stdin" , StringIO ("101010" ))
131126 response = mocker .MagicMock (spec = Response )
132127 mock_sdk_factory .side_effect = [
133128 Py42MFARequiredError (HTTPError (response = response )),
134129 None ,
135130 ]
136- validate_connection ( "Authority" , "Test" , "Password" , None )
131+ create_sdk ( mock_profile_with_password , False )
137132 output = capsys .readouterr ()
138133 assert "Multi-factor authentication required. Enter TOTP:" in output .out
139134
140135
141- def test_validate_connection_when_mfa_token_invalid_raises_expected_cli_error (
142- mocker , mock_sdk_factory
136+ def test_create_sdk_connection_when_mfa_token_invalid_raises_expected_cli_error (
137+ mocker , mock_sdk_factory , mock_profile_with_password
143138):
144139 response = mocker .MagicMock (spec = Response )
145140 response .text = '{"data":null,"error":[{"primaryErrorKey":"INVALID_TIME_BASED_ONE_TIME_PASSWORD","otherErrors":null}],"warnings":null}'
146141 mock_sdk_factory .side_effect = Py42UnauthorizedError (HTTPError (response = response ))
147142 with pytest .raises (Code42CLIError ) as err :
148- validate_connection ( "Authority" , "Test" , "Password" , "1234" )
149- assert str (err .value ) == "Invalid TOTP token for user Test ."
143+ create_sdk ( mock_profile_with_password , False , totp = "1234" )
144+ assert str (err .value ) == "Invalid TOTP token for user foo ."
150145
151146
152147def test_totp_option_when_passed_is_passed_to_sdk_initialization (
0 commit comments