Ejemplo n.º 1
0
    def test_refresh_oauth_token_error(self, SalesforceOAuth2):
        config = OrgConfig({"refresh_token": mock.sentinel.refresh_token}, "test")
        keychain = mock.Mock()
        SalesforceOAuth2.return_value = oauth = mock.Mock()
        oauth.refresh_token.return_value = mock.Mock(status_code=400, text=":(")

        with self.assertRaises(SalesforceCredentialsException):
            config.refresh_oauth_token(keychain)
Ejemplo n.º 2
0
    def test_refresh_oauth_token_error(self, SalesforceOAuth2):
        config = OrgConfig({"refresh_token": mock.sentinel.refresh_token}, "test")
        keychain = mock.Mock()
        SalesforceOAuth2.return_value = oauth = mock.Mock()
        oauth.refresh_token.return_value = mock.Mock(status_code=400, text=":(")

        with self.assertRaises(SalesforceCredentialsException):
            config.refresh_oauth_token(keychain)
Ejemplo n.º 3
0
def is_org_good(org):
    config = org.config
    org_name = org.task.org_config_name
    try:
        org_config = OrgConfig(config, org_name)
        org_config.refresh_oauth_token(None)
        return "access_token" in org_config.config
    except HTTPError:
        return False
Ejemplo n.º 4
0
    def test_refresh_oauth_token(self, SalesforceOAuth2):
        config = OrgConfig({"refresh_token": mock.sentinel.refresh_token}, "test")
        config._load_userinfo = mock.Mock()
        keychain = mock.Mock()
        SalesforceOAuth2.return_value = oauth = mock.Mock()
        oauth.refresh_token.return_value = resp = mock.Mock()
        resp.json.return_value = {}

        config.refresh_oauth_token(keychain)

        oauth.refresh_token.assert_called_once_with(mock.sentinel.refresh_token)
Ejemplo n.º 5
0
    def test_refresh_oauth_token(self, SalesforceOAuth2):
        config = OrgConfig({"refresh_token": mock.sentinel.refresh_token}, "test")
        config._load_userinfo = mock.Mock()
        config._load_orginfo = mock.Mock()
        keychain = mock.Mock()
        SalesforceOAuth2.return_value = oauth = mock.Mock()
        oauth.refresh_token.return_value = resp = mock.Mock(status_code=200)
        resp.json.return_value = {}

        config.refresh_oauth_token(keychain)

        oauth.refresh_token.assert_called_once_with(mock.sentinel.refresh_token)
Ejemplo n.º 6
0
def is_org_good(org):
    """Check whether we can still get a valid access token for the org.

    (Most likely reason for not being able to is that the org was deleted.)
    """
    config = org.config
    org_name = org.org_config_name
    try:
        org_config = OrgConfig(config, org_name)
        org_config.refresh_oauth_token(None)
        return "access_token" in org_config.config
    except Exception:
        return False
Ejemplo n.º 7
0
def refresh_access_token(*, scratch_org, config, org_name, keychain=None):
    """Refresh the JWT.

    Construct a new OrgConfig because ScratchOrgConfig tries to use sfdx
    which we don't want now -- this is a total hack which I'll try to
    smooth over with some improvements in CumulusCI
    """
    try:
        org_config = OrgConfig(config, org_name, keychain=keychain)
        org_config.refresh_oauth_token(keychain)
        return org_config
    except HTTPError as err:
        _handle_sf_error(err, scratch_org=scratch_org)
Ejemplo n.º 8
0
def create_task(task_class,
                options=None,
                project_config=None,
                org_config=None):
    "Older create_task helper which does not support orginfo cache."
    if project_config is None:
        project_config = create_project_config("TestRepo", "TestOwner")
    if org_config is None:
        org_config = OrgConfig(
            {
                "instance_url": "https://test.salesforce.com",
                "access_token": "TOKEN",
                "org_id": "ORG_ID",
                "username": "******",
            },
            "test",
            keychain=DummyKeychain(),
        )
        org_config.refresh_oauth_token = mock.Mock()
    if options is None:
        options = {}
    task_config = TaskConfig({"options": options})
    with mock.patch(
            "cumulusci.tasks.salesforce.BaseSalesforceTask._get_client_name",
            return_value="ccitests",
    ):
        return task_class(project_config, task_config, org_config)
Ejemplo n.º 9
0
    def test_run(self, get_org, get_gh_api):
        # mock github zipball
        def archive(format, zip_content, ref):
            with open(Path(__file__).parent / "testproject.zip", "rb") as f:
                zip_content.write(f.read())

        mock_api = mock.Mock()
        mock_api.archive.side_effect = archive
        get_gh_api.return_value = mock_api

        # mock org config
        org_config = OrgConfig({}, "test")
        org_config.refresh_oauth_token = mock.Mock()
        get_org.return_value = org_config

        build = BuildFactory()
        build.plan.flows = "test"

        try:
            build.run()
        finally:
            detach_logger(build)

        assert build.status == "success", build.log
        assert "Build flow test completed successfully" in build.log
        assert "running test flow" in build.flows.get().log
Ejemplo n.º 10
0
def refresh_access_token(*,
                         scratch_org,
                         config,
                         org_name,
                         keychain=None,
                         originating_user_id=None):
    """
    Construct a new OrgConfig because ScratchOrgConfig tries to use sfdx
    which we don't want now -- this is a total hack which I'll try to
    smooth over with some improvements in CumulusCI
    """
    with delete_org_on_error(scratch_org=scratch_org,
                             originating_user_id=originating_user_id):
        org_config = OrgConfig(config, org_name, keychain=keychain)
        org_config.refresh_oauth_token(keychain)
        return org_config
Ejemplo n.º 11
0
 def test_refresh_oauth_token_jwt_sandbox(self):
     responses.add(
         "POST",
         "https://cs00.salesforce.com/services/oauth2/token",
         json={
             "access_token": "TOKEN",
             "instance_url": "https://cs00.salesforce.com",
         },
     )
     with mock.patch.dict(
         os.environ,
         {"SFDX_CLIENT_ID": "some client id", "SFDX_HUB_KEY": "some private key"},
     ):
         config = OrgConfig({"instance_url": "https://cs00.salesforce.com"}, "test")
         config._load_userinfo = mock.Mock()
         config._load_orginfo = mock.Mock()
         config.refresh_oauth_token(None)
         assert config.access_token == "TOKEN"
Ejemplo n.º 12
0
def devhub_config():
    org_config = OrgConfig(
        {
            "instance_url": "https://devhub.my.salesforce.com",
            "access_token": "token"
        },
        "devhub",
    )
    org_config.refresh_oauth_token = mock.Mock()
    return org_config
Ejemplo n.º 13
0
def org_config():
    org_config = OrgConfig(
        {
            "instance_url": "https://scratch.my.salesforce.com",
            "access_token": "token",
            "config_file": "orgs/scratch_def.json",
        },
        "dev",
    )
    org_config.refresh_oauth_token = mock.Mock()
    return org_config
Ejemplo n.º 14
0
def is_org_good(org):
    config = org.config
    org_name = org.task.org_config_name
    try:
        org_config = OrgConfig(config, org_name)
        org_config.refresh_oauth_token = Mock()
        info = jwt_session(SF_CLIENT_ID, SF_CLIENT_KEY, org_config.username,
                           org_config.instance_url)
        return "access_token" in info
    except HTTPError:
        return False
Ejemplo n.º 15
0
def refresh_access_token(*, config, org_name, scratch_org,
                         originating_user_id):
    """
    Construct a new OrgConfig because ScratchOrgConfig tries to use sfdx
    which we don't want now -- this is a total hack which I'll try to
    smooth over with some improvements in CumulusCI
    """
    try:
        org_config = OrgConfig(config, org_name)
        org_config.refresh_oauth_token = Mock()
        info = jwt_session(SF_CLIENT_ID, SF_CLIENT_KEY, org_config.username,
                           org_config.instance_url)
        org_config.config["access_token"] = info["access_token"]
        return org_config
    except HTTPError as err:
        if get_current_job():
            job_id = get_current_job().id
            # This error is user-facing, and so for makemessages to
            # pick it up correctly, we need it to be a single,
            # unbroken, string literal (even though adjacent string
            # literals should be parsed by the AST into a single
            # string literal and picked up by makemessages, but
            # that's a gripe for another day). We have relatively
            # few errors that propagate directly from the backend
            # like this, but when we do, this is the pattern we
            # should use.
            #
            # This is also why we repeat the first sentence.
            error_msg = _(
                f"Are you certain that the org still exists? If you need support, your job ID is {job_id}."  # noqa: B950
            )
        else:
            error_msg = _(
                f"Are you certain that the org still exists? {err.args[0]}")

        err = err.__class__(
            error_msg,
            *err.args[1:],
        )
        scratch_org.remove_scratch_org(err,
                                       originating_user_id=originating_user_id)
        raise err
Ejemplo n.º 16
0
def create_task(task_class, options=None, project_config=None, org_config=None):
    if project_config is None:
        project_config = create_project_config("TestRepo", "TestOwner")
    if org_config is None:
        org_config = OrgConfig(
            {
                "instance_url": "https://test.salesforce.com",
                "access_token": "TOKEN",
                "org_id": "ORG_ID",
                "username": "******",
            },
            "test",
        )
        org_config.refresh_oauth_token = mock.Mock()
    if options is None:
        options = {}
    task_config = TaskConfig({"options": options})
    with mock.patch(
        "cumulusci.tasks.salesforce.BaseSalesforceTask._get_client_name",
        return_value="ccitests",
    ):
        return task_class(project_config, task_config, org_config)
Ejemplo n.º 17
0
def create_task(task_class,
                options=None,
                project_config=None,
                org_config=None):
    if project_config is None:
        project_config = create_project_config("TestRepo", "TestOwner")
    if org_config is None:
        org_config = OrgConfig(
            {
                "instance_url": "https://test.salesforce.com",
                "access_token": "TOKEN",
                "org_id": "ORG_ID",
            },
            "test",
        )
        org_config.refresh_oauth_token = mock.Mock()
    if options is None:
        options = {}
    task_config = TaskConfig({"options": options})
    with mock.patch(
            "cumulusci.tasks.salesforce.BaseSalesforceTask._get_client_name",
            return_value="ccitests",
    ):
        return task_class(project_config, task_config, org_config)
Ejemplo n.º 18
0
 def test_refresh_oauth_token_no_connected_app(self):
     config = OrgConfig({}, "test")
     with self.assertRaises(AttributeError):
         config.refresh_oauth_token(None)
Ejemplo n.º 19
0
 def test_refresh_oauth_token_no_connected_app(self):
     config = OrgConfig({}, "test")
     with self.assertRaises(AttributeError):
         config.refresh_oauth_token(None)