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_membership_search(example_group): """ Test membership search. :param example_group: """ app = example_group with app.app_context(): member = app.get_member() assert member.get_id() == str( Membership.search(Membership.query, '*****@*****.**').one().user_id) assert member.get_id() == str( Membership.search(Membership.query, '@example.com').one().user_id) assert member.get_id() == str( Membership.search(Membership.query, 'test2@example').one().user_id) assert member.get_id() == str( Membership.search(Membership.query, '@example').one().user_id)
def test_membership_delete(app): """ Test membership delete. :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() Membership.create(g, u) assert Membership.query.count() == 1 Membership.delete(g, u) assert Membership.query.count() == 0 assert Membership.delete(g, u) is None
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_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_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_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_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