def create_team(self, user, enterprise, region_list=None, team_alias=None): team_name = self.random_tenant_name(enterprise=user.enterprise_id, length=8) is_public = settings.MODULES.get('SSO_LOGIN') if not is_public: pay_type = 'payed' pay_level = 'company' else: pay_type = 'free' pay_level = 'company' expired_day = 7 if hasattr(settings, "TENANT_VALID_TIME"): expired_day = int(settings.TENANT_VALID_TIME) expire_time = datetime.datetime.now() + datetime.timedelta(days=expired_day) if not region_list: region_list = [r.region_name for r in region_repo.get_usable_regions()] if not region_list: return 404, "无可用数据中心", None default_region = region_list[0] if not team_alias: team_alias = "{0}的团队".format(user.nick_name) params = { "tenant_name": team_name, "pay_type": pay_type, "pay_level": pay_level, "creater": user.user_id, "region": default_region, "expired_time": expire_time, "tenant_alias": team_alias, "enterprise_id": enterprise.enterprise_id, "limit_memory": 4096 } team = team_repo.create_tenant(**params) return 200, "success", team
def create_team(self, user, enterprise, region_list=None, team_alias=None): team_name = self.random_tenant_name(enterprise=user.enterprise_id, length=8) is_public = settings.MODULES.get('SSO_LOGIN') if not is_public: pay_type = 'payed' pay_level = 'company' else: pay_type = 'free' pay_level = 'company' expired_day = 7 if hasattr(settings, "TENANT_VALID_TIME"): expired_day = int(settings.TENANT_VALID_TIME) expire_time = datetime.datetime.now() + datetime.timedelta( days=expired_day) default_region = "" if region_list and len(region_list) > 0: default_region = region_list[0] if not team_alias: team_alias = "{0}的团队".format(user.nick_name) params = { "tenant_name": team_name, "pay_type": pay_type, "pay_level": pay_level, "creater": user.user_id, "region": default_region, "expired_time": expire_time, "tenant_alias": team_alias, "enterprise_id": enterprise.enterprise_id, "limit_memory": 0, } team = team_repo.create_tenant(**params) create_perm_param = { "user_id": user.user_id, "tenant_id": team.ID, "identity": "owner", "enterprise_id": enterprise.ID, } team_repo.create_team_perms(**create_perm_param) # init default roles role_kind_services.init_default_roles(kind="team", kind_id=team.tenant_id) admin_role = role_kind_services.get_role_by_name( kind="team", kind_id=team.tenant_id, name=u"管理员") user_kind_role_service.update_user_roles(kind="team", kind_id=team.tenant_id, user=user, role_ids=[admin_role.ID]) return team
def test_list_users_by_tenant_id(): from console.services.user_services import user_services from console.repositories.team_repo import team_repo from console.repositories.perm_repo import perms_repo from console.repositories.user_repo import user_repo eid = "bb2f17abc58b328374351e9c92abd400" tenant_id = "374351e9c92abd400bb2f17abc58b328" params = { "tenant_id": tenant_id, "tenant_name": "xxxxxxxx", "creater": 1, "region": "rainbond", "tenant_alias": "foobar team", "enterprise_id": eid, } team = team_repo.create_tenant(**params) userinfo = [{ "nick_name": "foo", "email": "*****@*****.**", "password": "******", "eid": eid }, { "nick_name": "bar", "email": "*****@*****.**", "password": "******", "eid": eid }, { "nick_name": "dummy", "email": "*****@*****.**", "password": "******", "eid": eid }] for item in userinfo: user = user_services.create(item) perminfo = { "user_id": user.user_id, "tenant_id": team.ID, "identity": "owner", "enterprise_id": 1 } perms_repo.add_user_tenant_perm(perminfo) testcases = [ { "tenant_id": tenant_id, "query": "", "page": None, "size": None, "count": 3, "user_id": 1 }, { "tenant_id": tenant_id, "query": "bar", "page": None, "size": None, "count": 1, "user_id": 2 }, { "tenant_id": tenant_id, "query": "*****@*****.**", "page": None, "size": None, "count": 1, "user_id": 1 }, { "tenant_id": tenant_id, "query": "", "page": 2, "size": 2, "count": 1, "user_id": 3 }, { "tenant_id": tenant_id, "query": "nothing", "page": None, "size": None, "count": 0, "user_id": 0 }, { "tenant_id": tenant_id, "query": "", "page": -1, "size": None, "count": 3, "user_id": 1 }, ] for tc in testcases: result = user_repo.list_users_by_tenant_id(tc["tenant_id"], tc["query"], tc["page"], tc["size"]) print result assert len(result) == tc["count"] if len(result) > 0: assert result[0].get("user_id") == tc["user_id"]