def test_users(self): g_primary = get_primary_field('Group') u_primary = get_primary_field('User') g1 = model.provider.create(app_model.Group, { 'group_name': 'editors', 'display_name': 'Editors' }) g1_primary_value = getattr(g1, g_primary) u1 = model.provider.create( app_model.User, { 'user_name': 'alpha', 'display_name': 'Alpha', 'email_address': '*****@*****.**', 'created': datetime.utcnow(), 'groups': [g1_primary_value] }) u1_primary_value = getattr(u1, u_primary) flush_db_changes() resp = self.app.get('/tgapppermissions/users', extra_environ={'REMOTE_USER': '******'}) assert 'alpha' in resp.text, resp assert 'Alpha' in resp.text, resp assert '*****@*****.**' in resp.text, resp
def test_toggle_group_exlusive(self): g_primary = get_primary_field('Group') u_primary = get_primary_field('User') u1 = model.provider.create( app_model.User, { 'user_name': 'alpha', 'display_name': 'Alpha', 'email_address': '*****@*****.**', 'created': datetime.utcnow() }) u1_primary_value = getattr(u1, u_primary) g1 = model.provider.create(app_model.Group, { 'group_name': 'editors', 'display_name': 'Editors' }) g1_primary_value = getattr(g1, g_primary) g2 = model.provider.create(app_model.Group, { 'group_name': 'contributors', 'display_name': 'Contributors' }) g2_primary_value = getattr(g2, g_primary) flush_db_changes() resp = self.app.get( '/tgapppermissions/toggle_group', { 'user': u1_primary_value, 'group': g1_primary_value }, extra_environ={'REMOTE_USER': '******'}, status=302, ) __, user = model.provider.query(app_model.User, filters=dict(user_name='alpha')) user = user[0] assert len(user.groups) == 1, user.groups assert user.groups[0].group_name == 'editors', user.groups resp = self.app.get( '/tgapppermissions/toggle_group', { 'user': u1_primary_value, 'group': g2_primary_value }, extra_environ={'REMOTE_USER': '******'}, status=302, ) __, user = model.provider.query(app_model.User, filters=dict(user_name='alpha')) user = user[0] assert len(user.groups) == 1, user.groups assert user.groups[0].group_name == 'contributors', user.groups
def test_update_permission(self): g_primary = get_primary_field('Group') p_primary = get_primary_field('Permission') g1 = model.provider.create(app_model.Group, { 'group_name': 'editors', 'dispaly_name': 'Editors' }) g1_primary_value = getattr(g1, g_primary) g2 = model.provider.create(app_model.Group, { 'group_name': 'users', 'dispaly_name': 'Users' }) g2_primary_value = getattr(g2, g_primary) flush_db_changes() self.app.get( '/tgapppermissions/create_permission', params={ 'permission_name': 'pname', 'description': 'descr', 'groups': [] }, extra_environ={'REMOTE_USER': '******'}, status=302, ) count, perms = model.provider.query( app_model.Permission, filters=dict(permission_name='pname')) assert count == 1 perm = perms[0] self.app.get( '/tgapppermissions/update_permission/' + str(getattr(perm, p_primary)), params={ 'permission_name': 'view', 'description': 'perm to view things', 'groups': [g1_primary_value, g2_primary_value] }, extra_environ={'REMOTE_USER': '******'}, status=302, ) count, perms = model.provider.query( app_model.Permission, filters=dict(permission_name='view')) assert count == 1 perm = perms[0] assert 'view' == perm.permission_name, perm.permission_name assert 'perm to view things' == perm.description, perm.description # assert perm.groups == [g1, g2], perm.groups _, groups = model.provider.query(app_model.Group) assert set([getattr(g, g_primary) for g in perm.groups ]) == set([getattr(g, g_primary) for g in groups])
def test_create_permission(self): g_primary = get_primary_field('Group') g = model.provider.create(app_model.Group, { 'group_name': 'editors', 'dispaly_name': 'Editors' }) g_primary_value = getattr(g, g_primary) flush_db_changes() self.app.get( '/tgapppermissions/create_permission', params={ 'permission_name': 'pname', 'description': 'descr', 'groups': [g_primary_value] }, extra_environ={'REMOTE_USER': '******'}, status=302, ) count, perms = model.provider.query( app_model.Permission, filters=dict(permission_name='pname')) assert count == 1 perm = perms[0] assert 'pname' == perm.permission_name, perm.permission_name assert 'descr' == perm.description, perm.description assert g_primary_value == getattr(perm.groups[0], g_primary), perm.groups[0]
def test_edit_permission_form(self): p_primary = get_primary_field('Permission') self.app.get( '/tgapppermissions/create_permission', params={ 'permission_name': 'pname', 'description': 'descr', 'groups': [] }, extra_environ={'REMOTE_USER': '******'}, status=302, ) count, perms = model.provider.query(app_model.Permission) assert count == 1 perm = perms[0] resp = self.app.get('/tgapppermissions/edit_permission/' + str(getattr(perm, p_primary)), extra_environ={'REMOTE_USER': '******'}) assert 'name="permission_name"' in resp.text, resp assert 'name="description"' in resp.text, resp assert 'name="groups' in resp.text, resp assert '/update_permission' in resp.text, resp
def delete_permission(self, permission_id): primary_field = get_primary_field('Permission') try: model.provider.delete(app_model.Permission, {primary_field: permission_id}) except AttributeError: abort(404) flash(_('Permission deleted')) return redirect(url(self.mount_point))
def update_permission(self, permission_id, **kwargs): primary_field = get_primary_field('Permission') model.provider.update(app_model.Permission, {primary_field: permission_id, 'permission_name': kwargs.get('permission_name'), 'description': kwargs.get('description'), 'groups': kwargs.get('groups')}) flash(_('Permission updated.')) return redirect(url(self.mount_point))
def edit_permission(self, permission_id, **_): primary_field = get_primary_field('Permission') permission = model.provider.get_obj(app_model.Permission, {primary_field: permission_id}) or abort(404) values = model.provider.dictify(permission) values['groups'] = [instance_primary_key(g, True) for g in permission.groups] return dict(form=get_edit_permission_form(), mount_point=self.mount_point, action=plug_url('tgapppermissions', '/update_permission/' + permission_id), values=values)
def toggle_group(self, **kwargs): group_id = kwargs.get('group') user_id = kwargs.get('user') user = model.provider.get_obj(app_model.User, {get_primary_field('User'): user_id}) or abort(404) groups_list = [instance_primary_key(g, True) for g in user.groups] if group_id in groups_list: groups_list.remove(group_id) model.provider.update(app_model.User, {get_primary_field('User'): user_id, 'groups': groups_list}) else: if config['_pluggable_tgapppermissions_config']['exclusive_permissions']: groups_list = [group_id] else: groups_list.append(group_id) model.provider.update(app_model.User, {get_primary_field('User'): user_id, 'groups': groups_list}) return redirect(url(self.mount_point + '/users'))
def test_delete_permission(self): p_primary = get_primary_field('Permission') self.app.get( '/tgapppermissions/create_permission', params={ 'permission_name': 'pname', 'description': 'descr', 'groups': [] }, extra_environ={'REMOTE_USER': '******'}, status=302, ) count, perms = model.provider.query(app_model.Permission) assert count == 1 perm = perms[0] self.app.get('/tgapppermissions/delete_permission/' + str(getattr(perm, p_primary)), extra_environ={'REMOTE_USER': '******'}, status=302) count, perms = model.provider.query(app_model.Permission) assert count == 0