def test_group_delete(app): """ Test deletion of a group. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin, Membership from invenio_accounts.models import User g1 = Group.create(name="test1") g2 = Group.create(name="test2", admins=[g1]) u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() g2.add_member(u) # Group is admin of another group, which will be left without admins g1.delete() assert Group.query.count() == 1 assert GroupAdmin.query.count() == 0 assert Membership.query.count() == 1 g2.delete() assert Group.query.count() == 0 assert GroupAdmin.query.count() == 0 assert Membership.query.count() == 0
def test_group_creation_signals(app): """ Test signals sent after creation. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group Group.called = False @event.listens_for(Group, 'after_insert') def _receiver(mapper, connection, target): Group.called = True assert isinstance(target, Group) assert target.name == 'signaltest' Group.create(name="signaltest") assert Group.called Group.called = False with pytest.raises(IntegrityError): Group.create(name="signaltest") assert not Group.called event.remove(Group, 'after_insert', _receiver)
def test_group_invite(app): """ Test group invite. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ MembershipState from invenio_accounts.models import User g = Group.create(name="test") u = User(email="*****@*****.**", password="******") u2 = User(email="*****@*****.**", password="******") db.session.add(u) db.session.add(u2) db.session.commit() m = g.invite(u) assert Membership.query.count() == 1 assert m.state == MembershipState.PENDING_USER a = Group.create(name="admin") g2 = Group.create(name="test2", admins=[a]) assert g2.invite(u2, admin=g) is None m = g2.invite(u2, admin=a) assert Membership.query.count() == 2 assert m.state == MembershipState.PENDING_USER
def test_group_subscribe(app): """ Test group subscribe. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, SubscriptionPolicy, \ Membership, MembershipState from invenio_accounts.models import User g_o = Group.create(name="test_open", subscription_policy=SubscriptionPolicy.OPEN) g_a = Group.create(name="test_approval", subscription_policy=SubscriptionPolicy.APPROVAL) g_c = Group.create(name="test_closed", subscription_policy=SubscriptionPolicy.CLOSED) u = User(email="test", password="******") db.session.add(u) db.session.commit() m_o = g_o.subscribe(u) m_c = g_c.subscribe(u) m_a = g_a.subscribe(u) assert m_c is None assert m_a.state == MembershipState.PENDING_ADMIN assert m_o.state == MembershipState.ACTIVE assert Membership.query.count() == 2
def test_group_creation(app): """Test creation of groups.""" with app.app_context(): from weko_groups.models import Group, \ GroupAdmin, Membership, SubscriptionPolicy, PrivacyPolicy g = Group.create(name="test") assert g.name == 'test' assert g.description == '' assert g.subscription_policy == SubscriptionPolicy.APPROVAL assert g.privacy_policy == PrivacyPolicy.MEMBERS assert not g.is_managed assert g.created assert g.modified assert GroupAdmin.query.count() == 0 assert Membership.query.count() == 0 g2 = Group.create(name="admintest", description="desc", subscription_policy=SubscriptionPolicy.OPEN, privacy_policy=PrivacyPolicy.PUBLIC, is_managed=True, admins=[g]) assert g2.name == 'admintest' assert g2.description == 'desc' assert g2.subscription_policy == SubscriptionPolicy.OPEN assert g2.privacy_policy == PrivacyPolicy.PUBLIC assert g2.is_managed assert g2.created assert g2.modified assert GroupAdmin.query.count() == 1 admin = g2.admins[0] assert admin.admin_type == 'Group' assert admin.admin_id == g.id assert Membership.query.count() == 0
def test_group_creation_existing_name(app): """ Test what happens if group with identical name is created. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group g = Group.create(name="test", ) with pytest.raises(IntegrityError): Group.create(name="test", admins=[g])
def test_group_search(example_group): """ Test group search. :param example_group: """ app = example_group with app.app_context(): group = app.get_group() assert group == Group.search(Group.query, 'test_group').one() assert group == Group.search(Group.query, '_group').one() assert group == Group.search(Group.query, '_group').one() assert group == Group.search(Group.query, 'st_gro').one()
def test_group_update_duplicated_names(app): """ Test duplicated name of group update. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group g = Group.create(name="test") Group.create(name="test-change") assert Group.query.count() == 2 with pytest.raises(IntegrityError): g.update(name="test-change")
def test_group_update(app): """ Test update of a group. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, SubscriptionPolicy, \ PrivacyPolicy g = Group.create(name="test") m = g.modified g.update( name="test-change", description="changed", subscription_policy=SubscriptionPolicy.OPEN, privacy_policy=PrivacyPolicy.MEMBERS, is_managed=True, ) assert g.name == 'test-change' assert g.description == 'changed' assert g.subscription_policy == SubscriptionPolicy.OPEN assert g.privacy_policy == PrivacyPolicy.MEMBERS assert g.is_managed assert m is not g.modified assert g.created
def test_invite_by_emails(app): """ Test invite by emails. :param app: The flask application. """ with app.app_context(): u1 = User(email='*****@*****.**', password='******') u2 = User(email='*****@*****.**', password='******') g = Group.create(name='test_group') db.session.add(u1) db.session.add(u2) db.session.commit() result = g.invite_by_emails( [u1.email, u2.email, '*****@*****.**']) assert result[0].state.code is MembershipState.PENDING_USER assert result[1].state.code is MembershipState.PENDING_USER assert result[2] is None assert g.is_member(u1, with_pending=True) assert g.is_member(u2, with_pending=True) assert not g.is_member('*****@*****.**', with_pending=True)
def example_group(app): """ Create example groups. :param app: An instance of :class:`~flask.Flask`. :return: App object. """ with app.app_context(): admin = User(email='*****@*****.**', password='******') member = User(email='*****@*****.**', password='******') non_member = User(email='*****@*****.**', password='******') db.session.add(admin) db.session.add(member) db.session.add(non_member) group = Group.create(name='test_group', admins=[admin]) membership = group.invite(member) membership.accept() admin_id = admin.id member_id = member.id non_member_id = non_member.id group_id = group.id db.session.commit() app.get_admin = lambda: User.query.get(admin_id) app.get_member = lambda: User.query.get(member_id) app.get_non_member = lambda: User.query.get(non_member_id) app.get_group = lambda: Group.query.get(group_id) return app
def test_group_admin_query_by_admin(app): """ Test group admin query by admin. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin from flask_sqlalchemy import BaseQuery a = Group.create(name="admin") g = Group.create(name="test", admins=[a]) assert isinstance(GroupAdmin.query_by_admin(a), BaseQuery) assert GroupAdmin.query_by_admin(a).count() == 1 assert GroupAdmin.query_by_admin(g).count() == 0
def test_group_is_member(app): """ Test if group is member. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group from invenio_accounts.models import User g = Group.create(name='test_group') u = User(email='*****@*****.**', password='******') u2 = User(email='*****@*****.**', password='******') u3 = User(email='*****@*****.**', password='******') db.session.add(u) db.session.add(u2) db.session.add(u3) db.session.commit() g.add_member(u) g.add_member(u2, state=MembershipState.PENDING_USER) g.add_member(u3, state=MembershipState.PENDING_ADMIN) assert g.is_member(u) assert not g.is_member(u2) assert g.is_member(u2, with_pending=True) assert not g.is_member(u3) assert g.is_member(u3, with_pending=True)
def test_membership_query_invitations(app): """ Test membership query invitations. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ MembershipState from invenio_accounts.models import User from flask_sqlalchemy import BaseQuery g = Group.create(name="test") u1 = User(email="*****@*****.**", password="******") u2 = User(email="[email protected]", password="******") u3 = User(email="[email protected]", password="******") db.session.add_all([u1, u2, u3]) db.session.commit() Membership.create(g, u1, MembershipState.ACTIVE) Membership.create(g, u2, MembershipState.PENDING_USER) Membership.create(g, u3, MembershipState.PENDING_ADMIN) assert isinstance(Membership.query_by_user(u1), BaseQuery) assert Membership.query_invitations(u1).count() == 0 assert Membership.query_invitations(u2).count() == 1 assert Membership.query_invitations(u3).count() == 0
def test_group_admin_create(app): """ Test group admin create. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin a = Group.create(name="admin") g = Group.create(name="test") ga = GroupAdmin.create(g, a) assert ga.admin_type == 'Group' assert ga.admin_id == a.id assert ga.group.id == g.id assert GroupAdmin.query.count() == 1
def test_group_add_admin(app): """ Test add group admin. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin a = Group.create(name="admin") g = Group.create(name="test") obj = g.add_admin(a) assert isinstance(obj, GroupAdmin) assert GroupAdmin.query.count() == 1 with pytest.raises(IntegrityError): g.add_admin(a)
def test_group_admin_query_admins_by_group_ids(app): """ Test group admin query admins by group ids. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin from sqlalchemy.orm.query import Query a = Group.create(name="admin") g = Group.create(name="test", admins=[a]) assert isinstance(GroupAdmin.query_admins_by_group_ids([g.id]), Query) assert 1 == GroupAdmin.query_admins_by_group_ids([g.id]).count() assert 0 == GroupAdmin.query_admins_by_group_ids([a.id]).count() with pytest.raises(AssertionError): GroupAdmin.query_admins_by_group_ids('invalid')
def test_group_remove_admin(app): """ Test remove group admin. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, GroupAdmin a = Group.create(name="admin") g = Group.create(name="test", admins=[a]) assert GroupAdmin.query.count() == 1 g.remove_admin(a) assert GroupAdmin.query.count() == 0 with pytest.raises(NoResultFound): g.remove_admin(a)
def get_schema_form(item_type_id=0): """Get schema form. :param item_type_id: Item type ID. (Default: 0) :return: The json object. """ try: cur_lang = current_i18n.language result = None if item_type_id > 0: result = ItemTypes.get_by_id(item_type_id) if result is None: return '["*"]' schema_form = result.form filemeta_form = schema_form[0] if 'filemeta' == filemeta_form.get('key'): group_list = Group.get_group_list() filemeta_form_group = filemeta_form.get('items')[-1] filemeta_form_group['type'] = 'select' filemeta_form_group['titleMap'] = group_list if 'default' != cur_lang: for elem in schema_form: if 'title_i18n' in elem and cur_lang in elem['title_i18n']\ and len(elem['title_i18n'][cur_lang]) > 0: elem['title'] = elem['title_i18n'][cur_lang] if 'items' in elem: for sub_elem in elem['items']: if 'title_i18n' in sub_elem and cur_lang in \ sub_elem['title_i18n'] and len( sub_elem['title_i18n'][cur_lang]) > 0: sub_elem['title'] = sub_elem['title_i18n'][ cur_lang] if sub_elem.get('title') == 'Group/Price': for sub_item in sub_elem['items']: if sub_item['title'] == "価格" and \ 'validationMessage_i18n' in sub_item and \ cur_lang in sub_item[ 'validationMessage_i18n'] and\ len(sub_item['validationMessage_i18n'] [cur_lang]) > 0: sub_item['validationMessage'] = sub_item[ 'validationMessage_i18n'][cur_lang] if 'items' in sub_elem: for sub_item in sub_elem['items']: if 'title_i18n' in sub_item and cur_lang in \ sub_item['title_i18n'] and len( sub_item['title_i18n'][cur_lang]) > 0: sub_item['title'] = sub_item['title_i18n'][ cur_lang] return jsonify(schema_form) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def add_signals_info(record, obj): """Add event signals info. Add user role, site license flag, item index list. :param record: the record metadate. :param obj: send object. """ # Add user role info to send_obj userrole = 'guest' userid = 0 user_groups = [] if hasattr(current_user, 'id'): userid = current_user.id user_groups = Group.query_by_user(current_user).all() if len(current_user.roles) == 0: userrole = 'user' elif len(current_user.roles) == 1: userrole = current_user.roles[0].name else: max_power_role_id = 999 for r in current_user.roles: if max_power_role_id > r.id: max_power_role_id = r.id userrole = r.name obj.userrole = userrole obj.userid = userid # Add groups of current users groups = [{'group_id': g.id, 'group_name': g.name} for g in user_groups] obj.user_group_list = groups if groups else None # Check whether billing file or not obj.is_billing_item = is_billing_item(record['item_type_id']) # Add site license flag to send_obj obj.site_license_flag = True if hasattr(current_user, 'site_licese_flag') \ else False obj.site_license_name = current_user.site_license_name \ if hasattr(current_user, 'site_license_name') else '' # Add index list info to send_obj index_list = '' if len(record.navi) > 0: for index in record.navi: index_list += index[3] + '|' obj.index_list = index_list[:len(index_list) - 1] # Add item info to send_obj obj.item_title = record['item_title'] obj.item_id = record['_deposit']['id']
def test_group_is_admin(app): """ Test if group is admin. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group from invenio_accounts.models import User g = Group.create(name="test") u = User(email="test", password="******") db.session.add(u) db.session.commit() g.add_admin(u) assert g.is_admin(u) a = Group.create(name="admin") g = Group.create(name="test2", admins=[a]) assert g.is_admin(a)
def test_membership_query_by_group(app): """ Test membership query by group. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ MembershipState from invenio_accounts.models import User from flask_sqlalchemy import BaseQuery g = Group.create(name="test") Group.create(name="test2") u = User(email="*****@*****.**", password="******") u2 = User(email="[email protected]", password="******") db.session.add(u) db.session.add(u2) db.session.commit() Membership.create(g, u, MembershipState.ACTIVE) assert isinstance(Membership.query_by_group(g), BaseQuery) assert 1 == Membership.query_by_group(g).count() assert 0 == Membership.query_by_user(u2).count()
def test_membership_query_requests(app): """ Test membership query requests. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ MembershipState from invenio_accounts.models import User from flask_sqlalchemy import BaseQuery a = User(email="*****@*****.**", password="******") u1 = User(email="*****@*****.**", password="******") u2 = User(email="[email protected]", password="******") db.session.add_all([a, u1, u2]) db.session.commit() g = Group.create(name="test", admins=[a]) Membership.create(g, u1, MembershipState.PENDING_ADMIN) Membership.create(g, u2, MembershipState.PENDING_USER) assert isinstance(Membership.query_requests(u1), BaseQuery) assert Membership.query_requests(a).count() == 1 ad = Group.create(name="admin") g2 = Group.create(name="test2", admins=[ad]) u3 = User(email="[email protected]", password="******") u4 = User(email="[email protected]", password="******") u5 = User(email="[email protected]", password="******") db.session.add_all([u3, u4, u5]) db.session.commit() Membership.create(ad, u3, MembershipState.ACTIVE) Membership.create(g2, u4, MembershipState.PENDING_ADMIN) Membership.create(g2, u5, MembershipState.PENDING_USER) assert Membership.query_requests(u3).count() == 1
def test_group_query_by_user(app): """ Test query by user. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ GroupAdmin, MembershipState from invenio_accounts.models import User u1 = User(email="[email protected]", password="******") u2 = User(email="[email protected]", password="******") u3 = User(email="[email protected]", password="******") db.session.add(u1) db.session.add(u2) db.session.add(u3) db.session.commit() g1 = Group.create(name="test1", admins=[u1]) g2 = Group.create(name="test2", admins=[u1]) g1.add_member(u2, state=MembershipState.PENDING_ADMIN) g1.add_member(u3, state=MembershipState.ACTIVE) g2.add_member(u2, state=MembershipState.ACTIVE) assert Group.query.count() == 2 assert GroupAdmin.query.count() == 2 assert Membership.query.count() == 3 assert Group.query_by_user(u1).count() == 2 assert Group.query_by_user(u1, with_pending=True).count() == 2 assert Group.query_by_user(u2).count() == 1 assert Group.query_by_user(u2, with_pending=True).count() == 2 assert Group.query_by_user(u3).count() == 1 assert Group.query_by_user(u3, with_pending=True).count() == 1 assert 1 == Group.query_by_user(u3, with_pending=True, eager=[Group.members]).count()
def test_membership_reject(app): """ Test membership reject. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership from invenio_accounts.models import User g = Group.create(name="test") u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() m = Membership.create(g, u) m.reject() assert Membership.query.count() == 0
def test_group_get_by_name(app): """ Test group get bu name.. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group Group.create(name="test1") Group.create(name="test2") assert Group.get_by_name("test1").name == "test1" assert Group.get_by_name("invalid") is None
def test_membership_accept(app): """ Test membership accept. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group, Membership, \ MembershipState from invenio_accounts.models import User g = Group.create(name="test") u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() m = Membership.create(g, u, MembershipState.PENDING_ADMIN) m.accept() assert m.state == MembershipState.ACTIVE assert m.is_active()
def test_group_creation_invalid_data(app): """ Test what happens if group with invalid data is created. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group with pytest.raises(AssertionError): Group.create(name="") with pytest.raises(AssertionError): Group.create(name="test", privacy_policy='invalid') with pytest.raises(AssertionError): Group.create(name="test", subscription_policy='invalid') assert Group.query.count() == 0
def get_schema_form(item_type_id=0): """Get schema form. :param item_type_id: Item type ID. (Default: 0) :return: The json object. """ try: # cur_lang = 'default' # if current_app.config['I18N_SESSION_KEY'] in session: # cur_lang = session[current_app.config['I18N_SESSION_KEY']] cur_lang = current_i18n.language result = None if item_type_id > 0: result = ItemTypes.get_by_id(item_type_id) if result is None: return '["*"]' schema_form = result.form filemeta_form = schema_form[0] if 'filemeta' == filemeta_form.get('key'): group_list = Group.get_group_list() filemeta_form_group = filemeta_form.get('items')[-1] filemeta_form_group['type'] = 'select' filemeta_form_group['titleMap'] = group_list if 'default' != cur_lang: for elem in schema_form: if 'title_i18n' in elem: if cur_lang in elem['title_i18n']: if len(elem['title_i18n'][cur_lang]) > 0: elem['title'] = elem['title_i18n'][cur_lang] if 'items' in elem: for sub_elem in elem['items']: if 'title_i18n' in sub_elem: if cur_lang in sub_elem['title_i18n']: if len(sub_elem['title_i18n'][cur_lang]) > 0: sub_elem['title'] = sub_elem['title_i18n'][ cur_lang] return jsonify(schema_form) except: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)
def get_json_schema(item_type_id=0): """Get json schema. :param item_type_id: Item type ID. (Default: 0) :return: The json object. """ try: result = None json_schema = None cur_lang = current_i18n.language if item_type_id > 0: if item_type_id == 20: result = ItemTypes.get_by_id(item_type_id) if result is None: return '{}' json_schema = result.schema properties = json_schema.get('properties') for key, value in properties.items(): if 'validationMessage_i18n' in value: value['validationMessage'] =\ value['validationMessage_i18n'][cur_lang] else: result = ItemTypes.get_record(item_type_id) if 'filemeta' in json.dumps(result): group_list = Group.get_group_list() group_enum = list(group_list.keys()) filemeta_group = result.get('properties').get( 'filemeta').get('items').get('properties').get( 'groups') filemeta_group['enum'] = group_enum json_schema = result if result is None: return '{}' return jsonify(json_schema) except BaseException: current_app.logger.error('Unexpected error: ', sys.exc_info()[0]) return abort(400)