From 26242da904a795fbec0e1843acae736e20b3651d Mon Sep 17 00:00:00 2001 From: jain-naman-sf Date: Fri, 8 Dec 2023 10:53:32 +0530 Subject: [PATCH 1/2] Handled HTTP Error in AuthCode Grant --- metadeploy/api/salesforce.py | 8 ++++++- metadeploy/api/tests/test_salesforce.py | 30 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/metadeploy/api/salesforce.py b/metadeploy/api/salesforce.py index 89872dde9f..c0e6f8f535 100644 --- a/metadeploy/api/salesforce.py +++ b/metadeploy/api/salesforce.py @@ -257,10 +257,16 @@ 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 d6fbeeb2bd..dbaf2aef1e 100644 --- a/metadeploy/api/tests/test_salesforce.py +++ b/metadeploy/api/tests/test_salesforce.py @@ -14,6 +14,8 @@ _poll_for_scratch_org_completion, delete_scratch_org, refresh_access_token, + _get_access_token, + _handle_sf_error ) @@ -181,3 +183,31 @@ 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 From bc5649ca0db3936e9d2237dea8a69ddf9ca0960d Mon Sep 17 00:00:00 2001 From: jain-naman-sf Date: Fri, 8 Dec 2023 11:23:48 +0530 Subject: [PATCH 2/2] Lint Fixes --- metadeploy/api/salesforce.py | 3 +-- metadeploy/api/tests/test_salesforce.py | 22 +++++++++------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/metadeploy/api/salesforce.py b/metadeploy/api/salesforce.py index c0e6f8f535..c3dc2acd45 100644 --- a/metadeploy/api/salesforce.py +++ b/metadeploy/api/salesforce.py @@ -260,8 +260,7 @@ def _get_access_token(*, org_result, scratch_org_config): try: auth_result = oauth2_client.auth_code_grant(org_result["AuthCode"]).json() except HTTPError as err: - _handle_sf_error(err) - + _handle_sf_error(err) scratch_org_config.config["access_token"] = scratch_org_config._sfdx_info[ "access_token" diff --git a/metadeploy/api/tests/test_salesforce.py b/metadeploy/api/tests/test_salesforce.py index dbaf2aef1e..9066724406 100644 --- a/metadeploy/api/tests/test_salesforce.py +++ b/metadeploy/api/tests/test_salesforce.py @@ -9,13 +9,12 @@ from ..salesforce import ( ScratchOrgError, + _get_access_token, _get_devhub_api, _get_org_result, _poll_for_scratch_org_completion, delete_scratch_org, refresh_access_token, - _get_access_token, - _handle_sf_error ) @@ -184,30 +183,27 @@ 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") + patch("metadeploy.api.salesforce.get_current_job") ) get_current_job.return_value = MagicMock(id=123) OAuth2ClientConfig = stack.enter_context( - patch("metadeploy.api.salesforce.OAuth2ClientConfig") + patch("metadeploy.api.salesforce.OAuth2ClientConfig") ) OAuth2Client = stack.enter_context( - patch("metadeploy.api.salesforce.OAuth2Client") + patch("metadeploy.api.salesforce.OAuth2Client") ) - error=HTTPError( - "Error message.", response=MagicMock(status_code=400) - ) - OAuth2Client().auth_code_grant.side_effect =error - + 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(), + org_result=MagicMock(), + scratch_org_config=MagicMock(), ) - assert OAuth2ClientConfig.called assert OAuth2Client.called