def test_get_access_token__unknown_user(self, Command):
        sf = mock.Mock()
        sf.query_all.return_value = {"records": []}

        config = ScratchOrgConfig({}, "test")

        with mock.patch("cumulusci.core.config.OrgConfig.salesforce_client", sf):
            with pytest.raises(
                SfdxOrgException,
                match="Couldn't find a username for the specified user",
            ):
                config.get_access_token(alias="dadvisor")
    def test_get_access_token__no_access_token(self, Command):
        sf = mock.Mock()
        sf.query_all.return_value = {"records": [{"Username": "******"}]}

        sfdx_response = mock.Mock(returncode=1)
        sfdx_response.stdout_text.read.return_value = '{"message": "blah blah..."}'
        sfdx = mock.Mock(return_value=sfdx_response)

        config = ScratchOrgConfig({}, "test")
        with mock.patch("cumulusci.core.config.OrgConfig.salesforce_client", sf):
            with mock.patch("cumulusci.core.config.sfdx_org_config.sfdx", sfdx):
                exception = (
                    "Unable to find access token for [email protected]\nblah blah..."
                )
                with pytest.raises(SfdxOrgException, match=exception):
                    config.get_access_token(alias="dadvisor")
 def test_get_access_token__default(self, Command):
     """Verify that with no args, get_access_token returns the default token"""
     config = ScratchOrgConfig({}, "test")
     _marker = object()
     config._sfdx_info = {"access_token": _marker}
     access_token = config.get_access_token()
     assert access_token is _marker
    def test_get_access_token__multiple_users(self, Command):
        sf = mock.Mock()
        sf.query_all.return_value = {
            "records": [
                {"Username": "******"},
                {"Username": "******"},
            ]
        }

        config = ScratchOrgConfig({}, "test")

        with mock.patch("cumulusci.core.config.OrgConfig.salesforce_client", sf):
            with pytest.raises(
                SfdxOrgException,
                match="More than one user matched the search critiera.",
            ):
                config.get_access_token(alias="dadvisor")
    def test_get_access_token(self, Command):
        """Verify that get_access_token calls out to sfdx"""
        sf = mock.Mock()
        sf.query_all.return_value = {"records": [{"Username": "******"}]}

        sfdx_response = mock.Mock(returncode=0)
        sfdx_response.stdout_text.read.return_value = (
            '{"result": {"accessToken": "the-token"}}'
        )
        sfdx = mock.Mock(return_value=sfdx_response)

        config = ScratchOrgConfig({}, "test")
        with mock.patch("cumulusci.core.config.OrgConfig.salesforce_client", sf):
            with mock.patch("cumulusci.core.config.sfdx_org_config.sfdx", sfdx):
                access_token = config.get_access_token(alias="dadvisor")
                sfdx.assert_called_once_with(
                    "force:org:display [email protected] --json"
                )
                assert access_token == "the-token"