def get_group(id=None, name=None): try: if id: return Group.get(Group.id==id) elif name: return Group.get(Group.name==name) else: raise ValueError('nither `id` or `name` was given') except Group.DoesNotExist: raise NotFoundException("no group could be found with these attributes")
def test_assign_user_to_multiple_group(self): rebels = Group.create(name='rebels') aliens = Group.create(name='aliens') allGroups = [aliens, rebels] # https://en.wikipedia.org/wiki/They_Live user = User.create(name='doubtful') user.groups.add(allGroups) belongsID = [g.id for g in user.groups] allGroupsID = [g.id for g in allGroups] eq_((set(belongsID), len(belongsID)), (set(allGroupsID), len(allGroupsID)))
def populate(self): with self.udb.atomic(): cap1 = Capability.create(domain='res1', action=Action.READ) cap2 = Capability.create(domain='res2', action=Action.UPDATE) grp1 = Group.create(name='grp2') grp2 = Group.create(name='grp1') usr = User.create(name='usr') grp1.capabilities.add(cap1) grp2.capabilities.add(cap2) usr.groups.add([grp1, grp2]) return usr, [cap1, cap2]
def test_user_can(self): cap1 = Capability.create(domain=Capability.simToReg('volumes/*'), action=Action.CREATE | Action.READ) cap2 = Capability.create(domain=Capability.simToReg('volumes/123'), action=Action.UPDATE) grp1 = Group.create(name='grp2') grp2 = Group.create(name='grp1') usr = User.create(name='usr') grp1.capabilities.add(cap1) grp2.capabilities.add(cap2) usr.groups.add([grp1, grp2]) self.assertTrue(usr.can('volumes/61273', action=Action.CREATE)) self.assertTrue(usr.can('volumes/123', Action.CREATE | Action.READ)) self.assertFalse(usr.can('volumes/82828', Action.DELETE)) self.assertFalse(usr.can('volumes/123', Action.DELETE))
def test_assign_user_to_group(self): anons = Group.create(name='anons') jhondohe = User.create(name='jhondhoe', password='******') anons.users.add(jhondohe) anons.save() eq_(jhondohe.groups.count(), 1) eq_(jhondohe.groups.get().id, anons.id)
def test_assign_capability_to_group(self): cap = Capability.create(domain='res', action=Action.DELETE) anons = Group.create(name='anons') anons.capabilities.add(cap) anons.save() eq_(anons.capabilities.count(), 1) eq_(anons.capabilities.get(), cap)
def test_remove_user_from_group(self): anons = Group.create(name='anons') jhondohe = User.create(name='jhondhoe', password='******') anons.users.add(jhondohe) anons.save() anons.users.remove(jhondohe) anons.save() eq_(jhondohe.groups.count(), 0) eq_(UserToGroup.select().count(), 0)
def test_assign_duplicate_user_to_group(self): anons = Group.create(name='anons') jhondohe = User.create(name='jhondhoe', password='******') anons.users.add(jhondohe) anons.save() with self.assertRaises(IntegrityError): anons.users.add(jhondohe) anons.save() eq_(jhondohe.groups.count(), 1) eq_(jhondohe.groups.get().id, anons.id)
def test_assign_same_capability_to_group(self): cap = Capability.create(domain='res', action=Action.DELETE) anons = Group.create(name='anons') anons.capabilities.add(cap) anons.save() with self.assertRaises(IntegrityError): anons.capabilities.add(cap) anons.save() eq_(anons.capabilities.count(), 1) eq_(anons.capabilities.get(), cap)
def test_group_can(self): cap1 = Capability.create(domain=Capability.simToReg('volumes/*'), action=Action.CREATE | Action.READ) cap2 = Capability.create(domain=Capability.simToReg('users/123'), action=Action.CREATE | Action.DELETE) grp = Group.create(name='grp2') grp.capabilities.add([cap1, cap2]) self.assertTrue(grp.can('volumes/123', Action.CREATE | Action.READ)) self.assertFalse(grp.can('volumes/82828', Action.DELETE)) self.assertTrue(grp.can('users/123', Action.DELETE)) self.assertFalse(grp.can('users/123', Action.UPDATE))
def test_group_unique_name(self): Group.create(name='cyclists') with self.assertRaises(IntegrityError): Group.create(name='cyclists')
def test_group_creation(self): newGroup = Group.create(name='chefs') Group.get(Group.name == newGroup.name) eq_(Group.select().count(), 1)
def add_group(name): try: return Group.create(name=name) except IntegrityError: raise ConflictException("a group with the same name already exists")
def delete_group(id): if not Group.delete().where(Group.id == id).execute(): raise NotFoundException('no group could be found with this id')
def get_groups(): return Group.select()