Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion metadeploy/api/salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
26 changes: 26 additions & 0 deletions metadeploy/api/tests/test_salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ..salesforce import (
ScratchOrgError,
_get_access_token,
_get_devhub_api,
_get_org_result,
_poll_for_scratch_org_completion,
Expand Down Expand Up @@ -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