diff options
author | Frederick Muriuki Muriithi | 2022-12-08 08:04:14 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-12-08 08:35:42 +0300 |
commit | b2e4621ba69a4fbbb9bd310aae6c9dee7339a3f5 (patch) | |
tree | e7baa7066858d1a41d0c584ad918b5e121426ba5 /gn3/auth/authorisation/resources.py | |
parent | 2be81e59ff416cb8764aaf041a3b8febae4d8875 (diff) | |
download | genenetwork3-b2e4621ba69a4fbbb9bd310aae6c9dee7339a3f5.tar.gz |
auth: add test to retrieve public resources
* gn3/auth/authorisation/resources.py: new functions
* resource_categories: retrieves all resource categories
* public_resources: retrieves all public resources
* tests/unit/auth/test_resources.py: test `public_resources` function
Diffstat (limited to 'gn3/auth/authorisation/resources.py')
-rw-r--r-- | gn3/auth/authorisation/resources.py | 30 |
1 files changed, 29 insertions, 1 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) |