@@ -79,23 +79,38 @@ def _run_result(self, stdout: str) -> MagicMock:
7979 return result
8080
8181 def test_returns_token_and_instance_url (self , reader ):
82+ display_result = self ._run_result (
83+ _sf_display_output ("redacted" , "https://org.salesforce.com" )
84+ )
85+ token_result = self ._run_result (
86+ json .dumps ({"status" : 0 , "result" : {"accessToken" : "mytoken" }})
87+ )
8288 with patch (
8389 "subprocess.run" ,
84- return_value = self ._run_result (
85- _sf_display_output ("mytoken" , "https://org.salesforce.com" )
86- ),
90+ side_effect = [display_result , token_result ],
8791 ) as mock_run :
8892 token , url = reader ._get_token ()
8993
9094 assert token == "mytoken"
9195 assert url == "https://org.salesforce.com"
92- mock_run .assert_called_once_with (
96+ assert mock_run .call_count == 2
97+ mock_run .assert_any_call (
9398 ["sf" , "org" , "display" , "--target-org" , "dev1" , "--json" ],
9499 capture_output = True ,
95100 text = True ,
96101 check = True ,
97102 timeout = 30 ,
98103 )
104+ mock_run .assert_any_call (
105+ [
106+ "sf" , "org" , "auth" , "show-access-token" ,
107+ "--target-org" , "dev1" , "--json" ,
108+ ],
109+ capture_output = True ,
110+ text = True ,
111+ check = True ,
112+ timeout = 30 ,
113+ )
99114
100115 def test_file_not_found_raises_runtime_error (self , reader ):
101116 with patch ("subprocess.run" , side_effect = FileNotFoundError ):
@@ -148,21 +163,21 @@ def test_nonzero_status_without_message_uses_unknown_error(self, reader):
148163 reader ._get_token ()
149164
150165 def test_missing_access_token_raises_runtime_error (self , reader ):
151- payload = json .dumps (
166+ display_result = MagicMock ()
167+ display_result .stdout = json .dumps (
152168 {"status" : 0 , "result" : {"instanceUrl" : "https://x.salesforce.com" }}
153169 )
154- result = MagicMock ()
155- result .stdout = payload
156- with patch ("subprocess.run" , return_value = result ):
157- with pytest .raises (RuntimeError , match = "access token or instance URL " ):
170+ token_result = MagicMock ()
171+ token_result .stdout = json . dumps ({ "status" : 0 , "result" : {}})
172+ with patch ("subprocess.run" , side_effect = [ display_result , token_result ] ):
173+ with pytest .raises (RuntimeError , match = "did not return an access token " ):
158174 reader ._get_token ()
159175
160176 def test_missing_instance_url_raises_runtime_error (self , reader ):
161- payload = json .dumps ({"status" : 0 , "result" : {"accessToken" : "tok" }})
162- result = MagicMock ()
163- result .stdout = payload
164- with patch ("subprocess.run" , return_value = result ):
165- with pytest .raises (RuntimeError , match = "access token or instance URL" ):
177+ display_result = MagicMock ()
178+ display_result .stdout = json .dumps ({"status" : 0 , "result" : {}})
179+ with patch ("subprocess.run" , return_value = display_result ):
180+ with pytest .raises (RuntimeError , match = "did not return an instance URL" ):
166181 reader ._get_token ()
167182
168183
0 commit comments