def org_connect(config, org_name, sandbox, login_url, default, global_org): config.check_org_overwrite(org_name) connected_app = config.keychain.get_service("connected_app") if sandbox: login_url = "https://test.salesforce.com" oauth_capture = CaptureSalesforceOAuth( client_id=connected_app.client_id, client_secret=connected_app.client_secret, callback_url=connected_app.callback_url, auth_site=login_url, scope="web full refresh_token", ) oauth_dict = oauth_capture() org_config = OrgConfig(oauth_dict, org_name) org_config.load_userinfo() org_config._load_orginfo() if org_config.organization_sobject["TrialExpirationDate"] is None: org_config.config["expires"] = "Persistent" else: org_config.config["expires"] = parse_api_datetime( org_config.organization_sobject["TrialExpirationDate"]).date() config.keychain.set_org(org_config, global_org) if default: config.keychain.set_default_org(org_name) click.echo(f"{org_name} is now the default org")
def get_refreshed_org_config(self): org_config = OrgConfig(self.config, "dev") info = jwt_session( settings.SF_CLIENT_ID, settings.SF_CLIENT_KEY, org_config.username, org_config.instance_url, ) org_config.config.update(info) org_config._load_userinfo() org_config._load_orginfo() return org_config
def test_load_orginfo(self): config = OrgConfig( { "instance_url": "https://example.com", "access_token": "TOKEN", "id": "OODxxxxxxxxxxxx/user", }, "test", ) responses.add( "GET", "https://example.com/services/data/v45.0/sobjects/Organization/OODxxxxxxxxxxxx", json={"OrganizationType": "Enterprise Edition", "IsSandbox": False}, ) config._load_orginfo() self.assertEqual("Enterprise Edition", config.org_type) self.assertEqual(False, config.is_sandbox)
def test_load_orginfo(self): config = OrgConfig( { "instance_url": "https://example.com", "access_token": "TOKEN", "id": "OODxxxxxxxxxxxx/user", }, "test", ) responses.add( "GET", "https://example.com/services/data/v45.0/sobjects/Organization/OODxxxxxxxxxxxx", json={"OrganizationType": "Enterprise Edition", "IsSandbox": False}, ) config._load_orginfo() self.assertEqual("Enterprise Edition", config.org_type) self.assertEqual(False, config.is_sandbox)
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)
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)
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"