def test_expired(self, Command): config = ScratchOrgConfig({"days": 1}, "test") now = datetime.now() config.date_created = now self.assertFalse(config.expired) config.date_created = now - timedelta(days=2) self.assertTrue(config.expired)
def test_expired(self, Command): config = ScratchOrgConfig({"days": 1}, "test") now = datetime.now() config.date_created = now self.assertFalse(config.expired) config.date_created = now - timedelta(days=2) self.assertTrue(config.expired)
def test_choose_devhub(self, Command): mock_keychain = mock.Mock() mock_keychain.get_service.return_value = ServiceConfig( {"username": "******"}) config = ScratchOrgConfig({}, "test", mock_keychain) assert config._choose_devhub() == "*****@*****.**"
def org_import(config, username_or_alias, org_name): org_config = {"username": username_or_alias} scratch_org_config = ScratchOrgConfig(org_config, org_name) scratch_org_config.config["created"] = True config.keychain.set_org(scratch_org_config) click.echo("Imported scratch org: {org_id}, username: {username}".format( **scratch_org_config.scratch_info))
def test_create_org_uses_org_def_email(self, Command): out = b"Successfully created scratch org: ORG_ID, username: USERNAME" Command.return_value = p = mock.Mock( stdout=io.BytesIO(out), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig( { "config_file": "tmp.json", "set_password": True, "email_address": "*****@*****.**", }, "test", ) config.generate_password = mock.Mock() with temporary_dir(): with open("tmp.json", "w") as f: f.write('{"adminEmail": "*****@*****.**"}') config.create_org() p.run.assert_called_once() self.assertEqual(config.config["org_id"], "ORG_ID") self.assertEqual(config.config["username"], "USERNAME") self.assertIn("date_created", config.config) config.generate_password.assert_called_once() self.assertTrue(config.config["created"]) self.assertEqual(config.scratch_org_type, "workspace") self.assertNotIn("*****@*****.**", Command.call_args[0][0])
def test_create_org(self, Command): out = b"Successfully created scratch org: ORG_ID, username: USERNAME" Command.return_value = p = mock.Mock(stdout=io.BytesIO(out), stderr=io.BytesIO(b""), returncode=0) config = ScratchOrgConfig( { "config_file": "tmp.json", "set_password": True, "email_address": "*****@*****.**", }, "test", ) config.generate_password = mock.Mock() with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") config.create_org() p.run.assert_called_once() self.assertEqual(config.config["org_id"], "ORG_ID") self.assertEqual(config.config["username"], "USERNAME") self.assertIn("date_created", config.config) config.generate_password.assert_called_once() self.assertTrue(config.config["created"]) self.assertEqual(config.scratch_org_type, "workspace") # Validate the SFDX command generated uses the email address provided. self.assertIn("*****@*****.**", Command.call_args[0][0])
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_force_refresh_oauth_token_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"error"), stderr=io.BytesIO(b""), returncode=1 ) config = ScratchOrgConfig({"username": "******"}, "test") with self.assertRaises(ScratchOrgException): config.force_refresh_oauth_token()
def test_create_org_command_error(self, Command): Command.return_value = mock.Mock(stdout=io.BytesIO(b""), stderr=io.BytesIO(b"error"), returncode=1) config = ScratchOrgConfig({"config_file": "tmp"}, "test") with self.assertRaises(ScratchOrgException): config.create_org()
def test_force_refresh_oauth_token_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"error"), stderr=io.BytesIO(b""), returncode=1 ) config = ScratchOrgConfig({"username": "******"}, "test") with self.assertRaises(ScratchOrgException): config.force_refresh_oauth_token()
def test_force_refresh_oauth_token(self, Command): Command.return_value = p = mock.Mock( stdout=io.BytesIO(b""), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig({"username": "******"}, "test") config.force_refresh_oauth_token() p.run.assert_called_once()
def test_force_refresh_oauth_token(self, Command): Command.return_value = p = mock.Mock( stdout=io.BytesIO(b""), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig({"username": "******"}, "test") config.force_refresh_oauth_token() p.run.assert_called_once()
def test_generate_password(self, Command): p = mock.Mock( stderr=io.BytesIO(b"error"), stdout=io.BytesIO(b"out"), returncode=0 ) Command.return_value = p config = ScratchOrgConfig({"username": "******"}, "test") config.generate_password() p.run.assert_called_once()
def test_delete_org(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"info"), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig({"username": "******", "created": True}, "test") config.delete_org() self.assertFalse(config.config["created"]) self.assertIs(config.config["username"], None)
def test_delete_org_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"An error occurred deleting this org"), stderr=io.BytesIO(b""), returncode=1, ) config = ScratchOrgConfig({"username": "******", "created": True}, "test") with self.assertRaises(ScratchOrgException): config.delete_org()
def test_delete_org_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b'An error occurred deleting this org'), stderr=io.BytesIO(b''), returncode=1, ) config = ScratchOrgConfig({'username': '******', 'created': True}, 'test') with self.assertRaises(ScratchOrgException): config.delete_org()
def test_generate_password(self, Command): p = mock.Mock( stderr=io.BytesIO(b"error"), stdout=io.BytesIO(b"out"), returncode=0 ) Command.return_value = p config = ScratchOrgConfig({"username": "******"}, "test") config.generate_password() p.run.assert_called_once()
def test_delete_org_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"An error occurred deleting this org"), stderr=io.BytesIO(b""), returncode=1, ) config = ScratchOrgConfig({"username": "******", "created": True}, "test") with self.assertRaises(ScratchOrgException): config.delete_org()
def test_force_refresh_oauth_token_error(self, Command): Command.return_value = p = mock.Mock( stdout=io.BytesIO(b'error'), stderr=io.BytesIO(b''), returncode=1, ) config = ScratchOrgConfig({'username': '******'}, 'test') with self.assertRaises(ScratchOrgException): config.force_refresh_oauth_token()
def test_create_org_command_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b''), stderr=io.BytesIO(b'error'), returncode=1, ) config = ScratchOrgConfig({'config_file': 'tmp'}, 'test') with self.assertRaises(ScratchOrgException): config.create_org()
def test_delete_org(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b"info"), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig({"username": "******", "created": True}, "test") config.delete_org() self.assertFalse(config.config["created"]) self.assertIs(config.config["username"], None)
def test_force_refresh_oauth_token(self, Command): Command.return_value = p = mock.Mock( stdout=io.BytesIO(b''), stderr=io.BytesIO(b''), returncode=0, ) config = ScratchOrgConfig({'username': '******'}, 'test') config.force_refresh_oauth_token() p.run.assert_called_once()
def test_user_id_from_org(self, Command): sf = mock.Mock() sf.query_all.return_value = {"records": [{"Id": "test"}]} config = ScratchOrgConfig({"username": "******"}, "test") config._sfdx_info = { "instance_url": "test_instance", "access_token": "token", } with mock.patch("cumulusci.core.config.OrgConfig.salesforce_client", sf): self.assertEqual(config.user_id, "test")
def test_generate_password_failed(self, Command): p = mock.Mock() p.stderr = io.BytesIO(b"error") p.stdout = io.BytesIO(b"out") p.returncode = 1 Command.return_value = p config = ScratchOrgConfig({"username": "******"}, "test") config.logger = mock.Mock() config.generate_password() config.logger.warning.assert_called_once()
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_generate_password_failed(self, Command): p = mock.Mock() p.stderr = io.BytesIO(b'error') p.stdout = io.BytesIO(b'out') p.returncode = 1 Command.return_value = p config = ScratchOrgConfig({'username': '******'}, 'test') config.logger = mock.Mock() config.generate_password() config.logger.warn.assert_called_once()
def test_generate_password_failed(self, Command): p = mock.Mock() p.stderr = io.BytesIO(b"error") p.stdout = io.BytesIO(b"out") p.returncode = 1 Command.return_value = p config = ScratchOrgConfig({"username": "******"}, "test") config.logger = mock.Mock() config.generate_password() config.logger.warning.assert_called_once()
def test_generate_password(self, Command): p = mock.Mock( stderr=io.BytesIO(b'error'), stdout=io.BytesIO(b'out'), returncode=0, ) Command.return_value = p config = ScratchOrgConfig({'username': '******'}, 'test') config.generate_password() p.run.assert_called_once()
def test_delete_org(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b'info'), stderr=io.BytesIO(b''), returncode=0, ) config = ScratchOrgConfig({'username': '******', 'created': True}, 'test') config.delete_org() self.assertFalse(config.config['created']) self.assertIs(config.config['username'], None)
def org_import(config, username_or_alias, org_name): org_config = {"username": username_or_alias} scratch_org_config = ScratchOrgConfig(org_config, org_name) scratch_org_config.config["created"] = True info = scratch_org_config.scratch_info scratch_org_config.config["days"] = calculate_org_days(info) scratch_org_config.config["date_created"] = parse_api_datetime( info["created_date"]) config.keychain.set_org(scratch_org_config) click.echo("Imported scratch org: {org_id}, username: {username}".format( **scratch_org_config.scratch_info))
def test_refresh_oauth_token(self, Command): result = b"""{ "result": { "instanceUrl": "url", "accessToken": "access!token", "username": "******", "password": "******", "createdDate": "1970-01-01T:00:00:00Z", "expirationDate": "1970-01-08" } }""" Command.return_value = mock.Mock(stdout=io.BytesIO(result), stderr=io.BytesIO(b""), returncode=0) config = ScratchOrgConfig({ "username": "******", "created": True }, "test") config._sfdx_info = {} config._sfdx_info_date = datetime.now() - timedelta(days=1) config.force_refresh_oauth_token = mock.Mock() config._load_orginfo = mock.Mock() config.refresh_oauth_token(keychain=None) config.force_refresh_oauth_token.assert_called_once() self.assertTrue(config._sfdx_info)
def test_create_org_command_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b""), stderr=io.BytesIO(b"scratcherror"), returncode=1 ) config = ScratchOrgConfig( {"config_file": "tmp.json", "email_address": "*****@*****.**"}, "test" ) with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") with self.assertRaises(ScratchOrgException) as ctx: config.create_org() self.assertIn("scratcherror", str(ctx.error))
def test_create_org_command_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b""), stderr=io.BytesIO(b"scratcherror"), returncode=1 ) config = ScratchOrgConfig( {"config_file": "tmp.json", "email_address": "*****@*****.**"}, "test" ) with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") with self.assertRaises(ScratchOrgException) as ctx: config.create_org() self.assertIn("scratcherror", str(ctx.error))
def test_active(self, Command): config = ScratchOrgConfig({}, "test") config.date_created = None self.assertFalse(config.active) config = ScratchOrgConfig({}, "test") config.date_created = datetime.now() self.assertTrue(config.active) config = ScratchOrgConfig({}, "test") config.date_created = datetime.now() - timedelta(days=10) self.assertFalse(config.active)
def setUp(self): self.universal_config = UniversalConfig() self.project_config = BaseProjectConfig( self.universal_config, config={"noyaml": True} ) self.project_config.config["services"] = { "connected_app": {"attributes": {"test": {"required": True}}}, "github": {"attributes": {"git": {"required": True}, "password": {}}}, "not_configured": {"attributes": {"foo": {"required": True}}}, } self.project_config.project__name = "TestProject" self.project_name = "TestProject" self.org_config = OrgConfig({"foo": "bar"}, "test") self.scratch_org_config = ScratchOrgConfig( {"foo": "bar", "scratch": True}, "test_scratch" ) self.services = { "connected_app": ServiceConfig({"test": "value"}), "github": ServiceConfig({"git": "hub"}), } self.key = "0123456789123456" self._mk_temp_home() self._home_patch = mock.patch( "pathlib.Path.home", return_value=Path(self.tempdir_home) ) self._home_patch.__enter__() self._mk_temp_project() os.chdir(self.tempdir_project)
def test_scratch_info(self, Command): result = b'''{ "result": { "instanceUrl": "url", "accessToken": "access!token", "username": "******", "password": "******" } }''' Command.return_value = mock.Mock( stderr=io.BytesIO(b''), stdout=io.BytesIO(result), returncode=0, ) config = ScratchOrgConfig({'username': '******'}, 'test') info = config.scratch_info self.assertEqual( info, { 'access_token': 'access!token', 'instance_url': 'url', 'org_id': 'access', 'password': '******', 'username': '******', }) self.assertIs(info, config._scratch_info) self.assertDictContainsSubset(info, config.config) self.assertTrue(config._scratch_info_date)
def test_scratch_info(self, Command): result = b"""{ "result": { "instanceUrl": "url", "accessToken": "access!token", "username": "******", "password": "******", "createdDate": "1970-01-01T00:00:00Z", "expirationDate": "1970-01-08" } }""" Command.return_value = mock.Mock(stderr=io.BytesIO(b""), stdout=io.BytesIO(result), returncode=0) config = ScratchOrgConfig({"username": "******"}, "test") info = config.scratch_info self.assertEqual( info, { "access_token": "access!token", "instance_url": "url", "org_id": "access", "password": "******", "username": "******", "created_date": "1970-01-01T00:00:00Z", "expiration_date": "1970-01-08", }, ) self.assertIs(info, config._scratch_info) for key in ("access_token", "instance_url", "org_id", "password", "username"): assert key in config.config self.assertTrue(config._scratch_info_date)
def test_scratch_info(self, Command): result = b"""{ "result": { "instanceUrl": "url", "accessToken": "access!token", "username": "******", "password": "******" } }""" Command.return_value = mock.Mock( stderr=io.BytesIO(b""), stdout=io.BytesIO(result), returncode=0 ) config = ScratchOrgConfig({"username": "******"}, "test") info = config.scratch_info self.assertEqual( info, { "access_token": "access!token", "instance_url": "url", "org_id": "access", "password": "******", "username": "******", }, ) self.assertIs(info, config._scratch_info) self.assertTrue(set(info.items()).issubset(set(config.config.items()))) self.assertTrue(config._scratch_info_date)
def test_scratch_org_username(self): """ Scratch Org credentials are passed by -u flag """ self.task_config.config["options"] = {"command": "force:org --help"} org_config = ScratchOrgConfig({"username": "******"}, "test") task = SFDXOrgTask(self.project_config, self.task_config, org_config) self.assertIn("-u [email protected]", task._get_command())
def test_user_id_from_org(self, Command): sf = mock.Mock() sf.query_all.return_value = {"records": [{"Id": "test"}]} config = ScratchOrgConfig({"username": "******"}, "test") config._scratch_info = { "instance_url": "test_instance", "access_token": "token", } # This is ugly...since ScratchOrgConfig is in a module # with the same name that is imported in cumulusci.core.config's # __init__.py, we have no way to externally grab the # module without going through the function's globals. with mock.patch.dict( ScratchOrgConfig.user_id.fget.__globals__, Salesforce=mock.Mock(return_value=sf), ): self.assertEqual(config.user_id, "test")
def test_refresh_oauth_token(self, Command): result = b"""{ "result": { "instanceUrl": "url", "accessToken": "access!token", "username": "******", "password": "******" } }""" Command.return_value = mock.Mock( stdout=io.BytesIO(result), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig({"username": "******"}, "test") config._scratch_info = {} config._scratch_info_date = datetime.now() - timedelta(days=1) config.force_refresh_oauth_token = mock.Mock() config._load_orginfo = mock.Mock() config.refresh_oauth_token(keychain=None) config.force_refresh_oauth_token.assert_called_once() self.assertTrue(config._scratch_info)
def test_can_delete(self, Command): config = ScratchOrgConfig({"date_created": datetime.now()}, "test") self.assertTrue(config.can_delete())
def test_access_token(self, Command): config = ScratchOrgConfig({}, "test") _marker = object() config._scratch_info = {"access_token": _marker} self.assertIs(config.access_token, _marker)
def test_instance_url(self, Command): config = ScratchOrgConfig({}, "test") _marker = object() config._scratch_info = {"instance_url": _marker} self.assertIs(config.instance_url, _marker)
def test_username_from_scratch_info(self, Command): config = ScratchOrgConfig({}, "test") _marker = object() config._scratch_info = {"username": _marker} self.assertIs(config.username, _marker)
def test_password_from_scratch_info(self, Command): config = ScratchOrgConfig({}, "test") _marker = object() config._scratch_info = {"password": _marker} self.assertIs(config.password, _marker)
def test_expires(self, Command): config = ScratchOrgConfig({"days": 1}, "test") now = datetime.now() config.date_created = now self.assertEqual(config.expires, now + timedelta(days=1))
def test_days_alive(self, Command): config = ScratchOrgConfig({}, "test") config.date_created = datetime.now() self.assertEqual(config.days_alive, 1)
def test_create_org_no_config_file(self, Command): config = ScratchOrgConfig({}, "test") self.assertEqual(config.create_org(), None) Command.assert_not_called()
def test_generate_password_skips_if_failed(self, Command): config = ScratchOrgConfig({"username": "******"}, "test") config.password_failed = True config.generate_password() Command.assert_not_called()
def test_delete_org_not_created(self, Command): config = ScratchOrgConfig({"created": False}, "test") config.delete_org() Command.assert_not_called()