Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 1df3a85

Browse files
committed
fix: Add test case for roles & permissions
1 parent fdbb7d7 commit 1df3a85

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

tests/test_roles.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/python3
2+
3+
# Copyright (c) 2023 Humanitarian OpenStreetMap Team
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as
7+
# published by the Free Software Foundation, either version 3 of the
8+
# License, or (at your option) any later version.
9+
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
# Humanitarian OpenStreetmap Team
19+
# 1100 13th Street NW Suite 800 Washington, D.C. 20005
20+
21+
22+
import argparse
23+
import logging
24+
import sys
25+
import os
26+
from sys import argv
27+
import asyncio
28+
from codetiming import Timer
29+
from tm_admin.access import Roles, Operation, Access
30+
31+
# Instantiate logger
32+
log = logging.getLogger(__name__)
33+
34+
import tm_admin as tma
35+
rootdir = tma.__path__[0]
36+
37+
async def test_mapper(acl: Access):
38+
hits = 0
39+
if await acl.check('projects', Roles.MAPPER, Operation.READ):
40+
hits += 1
41+
if await acl.check('users', Roles.MAPPER, Operation.READ):
42+
hits += 1
43+
if await acl.check('tasks', Roles.MAPPER, Operation.READ):
44+
hits += 1
45+
if await acl.check('messages', Roles.MAPPER, Operation.READ):
46+
hits += 1
47+
if await acl.check('campaigns', Roles.MAPPER, Operation.READ):
48+
hits += 1
49+
# This is supposed to fail
50+
if not await acl.check('campaigns', Roles.MAPPER, Operation.CREATE):
51+
hits += 1
52+
53+
assert hits == 5
54+
55+
async def test_manager(acl: Access):
56+
hits = 0
57+
if await acl.check('tasks', Roles.PROJECT_MANAGER, Operation.READ):
58+
hits += 1
59+
if await acl.check('projects', Roles.PROJECT_MANAGER, Operation.UPDATE):
60+
hits += 1
61+
if await acl.check('users', Roles.PROJECT_MANAGER, Operation.DELETE):
62+
hits += 1
63+
64+
assert hits == 3
65+
66+
async def test_validator(acl: Access):
67+
hits = 0
68+
if await acl.check('tasks', Roles.VALIDATOR, Operation.READ):
69+
hits += 1
70+
# This is supossed to fail
71+
if not await acl.check('tasks', Roles.VALIDATOR, Operation.DELETE):
72+
hits += 1
73+
74+
assert hits == 2
75+
76+
async def main():
77+
parser = argparse.ArgumentParser()
78+
parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output")
79+
parser.add_argument("-u", "--uri", default='localhost/testdata', help="Database URI")
80+
parser.add_argument("-c", "--config", default='tmroles.yaml', help="The config file for this project")
81+
82+
args = parser.parse_args()
83+
# if verbose, dump to the terminal.
84+
log_level = os.getenv("LOG_LEVEL", default="INFO")
85+
if args.verbose is not None:
86+
log_level = logging.DEBUG
87+
88+
logging.basicConfig(
89+
level=log_level,
90+
format=("%(asctime)s.%(msecs)03d [%(levelname)s] " "%(name)s | %(funcName)s:%(lineno)d | %(message)s"),
91+
datefmt="%y-%m-%d %H:%M:%S",
92+
stream=sys.stdout,
93+
)
94+
95+
acl = Access(args.config)
96+
await test_mapper(acl)
97+
await test_manager(acl)
98+
await test_validator(acl)
99+
100+
if __name__ == "__main__":
101+
"""This is just a hook so this file can be run standalone during development."""
102+
loop = asyncio.new_event_loop()
103+
asyncio.set_event_loop(loop)
104+
loop.run_until_complete(main())

0 commit comments

Comments
 (0)