16
16
from code42cli .main import cli
17
17
from code42cli .options import CLIState
18
18
from code42cli .sdk_client import create_sdk
19
- from code42cli .sdk_client import validate_connection
20
19
21
20
22
21
@pytest .fixture
@@ -29,6 +28,17 @@ def mock_sdk_factory(mocker):
29
28
return mocker .patch ("py42.sdk.from_local_account" )
30
29
31
30
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
+
32
42
@pytest .fixture
33
43
def requests_exception (mocker ):
34
44
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
54
64
55
65
56
66
def 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
58
68
):
59
69
60
70
mock_sdk_factory .side_effect = Py42UnauthorizedError (requests_exception )
61
- profile = create_mock_profile ()
62
-
63
- def mock_get_password ():
64
- return "Test Password"
65
71
66
- profile .get_password = mock_get_password
67
72
with pytest .raises (Code42CLIError ) as err :
68
- create_sdk (profile , False )
73
+ create_sdk (mock_profile_with_password , False )
69
74
70
75
assert "Invalid credentials for user" in err .value .message
71
76
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 ])
73
78
74
79
75
80
def 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
77
82
):
78
83
mock_sdk_factory .side_effect = ConnectionError ("connection message" )
79
- profile = create_mock_profile ()
80
84
81
- def mock_get_password ():
82
- return "Test Password"
83
-
84
- profile .get_password = mock_get_password
85
85
with pytest .raises (LoggedCLIError ) as err :
86
- create_sdk (profile , False )
86
+ create_sdk (mock_profile_with_password , False )
87
87
88
88
assert "Problem connecting to" in err .value .message
89
89
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 ])
91
91
92
92
93
93
def 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
95
95
):
96
96
mock_sdk_factory .side_effect = Exception ("test message" )
97
- profile = create_mock_profile ()
98
-
99
- def mock_get_password ():
100
- return "Test Password"
101
97
102
- profile .get_password = mock_get_password
103
98
with pytest .raises (LoggedCLIError ) as err :
104
- create_sdk (profile , False )
99
+ create_sdk (mock_profile_with_password , False )
105
100
106
101
assert "Unknown problem validating" in err .value .message
107
102
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 ])
110
104
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"
116
105
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 )
119
110
assert py42 .settings .debug .level == debug .DEBUG
120
111
121
112
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
+ )
125
120
126
121
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
129
124
):
130
125
monkeypatch .setattr ("sys.stdin" , StringIO ("101010" ))
131
126
response = mocker .MagicMock (spec = Response )
132
127
mock_sdk_factory .side_effect = [
133
128
Py42MFARequiredError (HTTPError (response = response )),
134
129
None ,
135
130
]
136
- validate_connection ( "Authority" , "Test" , "Password" , None )
131
+ create_sdk ( mock_profile_with_password , False )
137
132
output = capsys .readouterr ()
138
133
assert "Multi-factor authentication required. Enter TOTP:" in output .out
139
134
140
135
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
143
138
):
144
139
response = mocker .MagicMock (spec = Response )
145
140
response .text = '{"data":null,"error":[{"primaryErrorKey":"INVALID_TIME_BASED_ONE_TIME_PASSWORD","otherErrors":null}],"warnings":null}'
146
141
mock_sdk_factory .side_effect = Py42UnauthorizedError (HTTPError (response = response ))
147
142
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 ."
150
145
151
146
152
147
def test_totp_option_when_passed_is_passed_to_sdk_initialization (
0 commit comments