|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2024 Northwestern University. |
| 4 | +# |
| 5 | +# Invenio-RDM-Records is free software; you can redistribute it |
| 6 | +# and/or modify it under the terms of the MIT License; see LICENSE file for |
| 7 | +# more details. |
| 8 | + |
| 9 | +"""Test permissions.""" |
| 10 | + |
| 11 | +from invenio_communities.permissions import CommunityPermissionPolicy |
| 12 | + |
| 13 | + |
| 14 | +def test_can_request_membership( |
| 15 | + app, community, owner, anon_identity, any_user, superuser_identity |
| 16 | +): |
| 17 | + |
| 18 | + policy = CommunityPermissionPolicy |
| 19 | + community_record = community._record |
| 20 | + authenticated_identity = any_user.identity |
| 21 | + |
| 22 | + allow_membership_requests_orig = app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"] |
| 23 | + |
| 24 | + # Case - feature disabled |
| 25 | + app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"] = False |
| 26 | + assert not ( |
| 27 | + policy(action="request_membership", record=community_record).allows( |
| 28 | + superuser_identity |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"] = True |
| 33 | + |
| 34 | + # Case - setting disabled |
| 35 | + community_record.access.member_policy = "closed" |
| 36 | + assert not ( |
| 37 | + policy(action="request_membership", record=community_record).allows( |
| 38 | + superuser_identity |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + community_record.access.member_policy = "open" |
| 43 | + |
| 44 | + # Case - unlogged user |
| 45 | + assert not ( |
| 46 | + policy(action="request_membership", record=community_record).allows( |
| 47 | + anon_identity |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + # Case - logged user not part of community |
| 52 | + assert policy(action="request_membership", record=community_record).allows( |
| 53 | + authenticated_identity |
| 54 | + ) |
| 55 | + |
| 56 | + # Case - member of community |
| 57 | + assert not ( |
| 58 | + policy(action="request_membership", record=community_record).allows( |
| 59 | + owner.identity |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + app.config["COMMUNITIES_ALLOW_MEMBERSHIP_REQUESTS"] = allow_membership_requests_orig |
0 commit comments