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(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_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_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_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_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 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 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_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_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_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_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_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_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 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_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_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_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_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_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 test_group_add_member(app): """ Test add group member. :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="test1") u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() obj = g.add_member(u) assert isinstance(obj, Membership) assert Group.query.count() == 1 assert Membership.query.count() == 1 with pytest.raises(FlushError): g.add_member(u)
def test_membership_create(app): """ Test membership create. :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) assert m.state == MembershipState.ACTIVE assert m.group.name == g.name assert m.user.id == u.id with pytest.raises(FlushError): Membership.create(g, u)
def test_group_remove_member(app): """ Test remove group member. :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="test1") u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() g.add_member(u) assert Membership.query.count() == 1 g.remove_member(u) assert Membership.query.count() == 0 assert g.remove_member(u) is None
def test_membership_get(app): """ Test membership get. :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="******") u2 = User(email="[email protected]", password="******") db.session.add(u) db.session.add(u2) db.session.commit() Membership.create(g, u) m = Membership.get(g, u) m2 = Membership.get(g, u2) assert m.group.id == g.id assert m.user.id == u.id assert m2 is None
def test_group_query_by_names(app): """ Test query by names. :param app: The flask application. """ with app.app_context(): from weko_groups.models import Group Group.create(name="test1") Group.create(name="test2") Group.create(name="test3") with pytest.raises(AssertionError): Group.query_by_names('test1') assert Group.query_by_names(["invalid"]).count() == 0 assert Group.query_by_names(["test1"]).count() == 1 assert Group.query_by_names(["test2", "invalid"]).count() == 1 assert Group.query_by_names(["test1", "test2"]).count() == 2