diff --git a/metadeploy/api/salesforce.py b/metadeploy/api/salesforce.py index 89872dde9..c3dc2acd4 100644 --- a/metadeploy/api/salesforce.py +++ b/metadeploy/api/salesforce.py @@ -257,10 +257,15 @@ def _get_access_token(*, org_result, scratch_org_config): scope="web full refresh_token", ) oauth2_client = OAuth2Client(oauth2_config) - auth_result = oauth2_client.auth_code_grant(org_result["AuthCode"]).json() + try: + auth_result = oauth2_client.auth_code_grant(org_result["AuthCode"]).json() + except HTTPError as err: + _handle_sf_error(err) + scratch_org_config.config["access_token"] = scratch_org_config._sfdx_info[ "access_token" ] = auth_result["access_token"] + scratch_org_config.config["refresh_token"] = auth_result["refresh_token"] diff --git a/metadeploy/api/tests/test_salesforce.py b/metadeploy/api/tests/test_salesforce.py index d6fbeeb2b..906672440 100644 --- a/metadeploy/api/tests/test_salesforce.py +++ b/metadeploy/api/tests/test_salesforce.py @@ -9,6 +9,7 @@ from ..salesforce import ( ScratchOrgError, + _get_access_token, _get_devhub_api, _get_org_result, _poll_for_scratch_org_completion, @@ -181,3 +182,28 @@ def test_poll_for_scratch_org_completion__failure(sleep): with pytest.raises(ScratchOrgError, match="Scratch org creation failed"): _poll_for_scratch_org_completion(devhub_api, initial_result) + + +def test_get_access_token_bad(): + with ExitStack() as stack: + get_current_job = stack.enter_context( + patch("metadeploy.api.salesforce.get_current_job") + ) + get_current_job.return_value = MagicMock(id=123) + OAuth2ClientConfig = stack.enter_context( + patch("metadeploy.api.salesforce.OAuth2ClientConfig") + ) + OAuth2Client = stack.enter_context( + patch("metadeploy.api.salesforce.OAuth2Client") + ) + error = HTTPError("Error message.", response=MagicMock(status_code=400)) + OAuth2Client().auth_code_grant.side_effect = error + + with pytest.raises(ScratchOrgError): + _get_access_token( + org_result=MagicMock(), + scratch_org_config=MagicMock(), + ) + + assert OAuth2ClientConfig.called + assert OAuth2Client.called