diff options
-rw-r--r-- | gn3/auth/authorisation/resources.py | 30 | ||||
-rw-r--r-- | tests/unit/auth/test_resources.py | 18 |
2 files changed, 45 insertions, 3 deletions
diff --git a/gn3/auth/authorisation/resources.py b/gn3/auth/authorisation/resources.py index f0a4b3a..2ddd865 100644 --- a/gn3/auth/authorisation/resources.py +++ b/gn3/auth/authorisation/resources.py @@ -1,6 +1,6 @@ """Handle the management of resources.""" from uuid import UUID, uuid4 -from typing import NamedTuple +from typing import Sequence, NamedTuple from gn3.auth import db from .checks import authorised_p @@ -22,6 +22,7 @@ class Resource(NamedTuple): resource_id: UUID resource_name: str resource_category: ResourceCategory + public: bool @authorised_p(("create-resource",), error_message="Could not create resource") def create_resource( @@ -41,3 +42,30 @@ def create_resource( str(resource.resource_category.resource_category_id))) return resource + +def resource_categories(conn: db.DbConnection) -> Sequence[ResourceCategory]: + """Retrieve all available resource categories""" + with db.cursor(conn) as cursor: + cursor.execute("SELECT * FROM resource_categories") + return tuple( + ResourceCategory(UUID(row[0]), row[1], row[2]) + for row in cursor.fetchall()) + return tuple() + +def public_resources(conn: db.DbConnection) -> Sequence[Resource]: + """List all resources marked as public""" + categories = { + str(cat.resource_category_id): cat for cat in resource_categories(conn) + } + with db.cursor(conn) as cursor: + cursor.execute("SELECT * FROM resources WHERE public=1") + results = cursor.fetchall() + group_uuids = tuple(row[0] for row in results) + query = ("SELECT * FROM groups WHERE group_id IN " + f"({', '.join(['?'] * len(group_uuids))})") + cursor.execute(query, group_uuids) + groups = {row[0]: Group(UUID(row[0]), row[1]) for row in cursor.fetchall()} + return tuple( + Resource(groups[row[0]], UUID(row[1]), row[2], categories[row[3]], + bool(row[4])) + for row in results) diff --git a/tests/unit/auth/test_resources.py b/tests/unit/auth/test_resources.py index aaf22e6..6dc0e98 100644 --- a/tests/unit/auth/test_resources.py +++ b/tests/unit/auth/test_resources.py @@ -5,7 +5,8 @@ import pytest from gn3.auth.authorisation.groups import Group from gn3.auth.authorisation.resources import ( - Resource, create_resource, ResourceCategory) + Resource, user_resources, create_resource, ResourceCategory, + public_resources) from tests.unit.auth import conftest @@ -25,7 +26,7 @@ uuid_fn = lambda : uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") conftest.TEST_USERS, (Resource( group, uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b"), - "test_resource", resource_category), + "test_resource", resource_category, False), create_resource_failure, create_resource_failure, create_resource_failure, @@ -37,3 +38,16 @@ def test_create_resource(mocker, test_app, test_users_in_group, user, expected): with test_app.app_context() as flask_context: flask_context.g.user = user assert create_resource(conn, "test_resource", resource_category) == expected + +SORTKEY = lambda resource: resource.resource_id + +@pytest.mark.unit_test +def test_public_resources(test_resources): + """ + GIVEN: some resources in the database + WHEN: public resources are requested + THEN: only list the resources that are public + """ + conn, _res = test_resources + assert sorted(public_resources(conn), key=SORTKEY) == sorted(tuple( + res for res in conftest.TEST_RESOURCES if res.public), key=SORTKEY) |