From 021b8dfcb99928b363e4546f626e3deb5793e392 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 24 Nov 2022 13:42:37 +0300 Subject: auth: Implement `create_resource` function * gn3/auth/authentication/checks.py: new `authenticated_p` decorator to apply on any function that requires the user to be authenticated before it runs. * gn3/auth/authorisation/checks.py: use a `auth.authentication.users.User` object rather than a UUID object in the global `g`. * gn3/auth/authorisation/groups.py: Implement the `authenticated_user_group` function to get the group(s) in which the currently authenticated user belongs. * gn3/auth/authorisation/resources.py: Implement the `create_resource` function correctly. * tests/unit/auth/conftest.py: extract the User objects into a global variable for reusability with the tests. * tests/unit/auth/test_resources.py: Use global user objects from conftest in the tests. Set a User object (rather than UUID) in the global `g` variable. --- tests/unit/auth/test_resources.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'tests/unit/auth/test_resources.py') diff --git a/tests/unit/auth/test_resources.py b/tests/unit/auth/test_resources.py index 04d0017..aaf22e6 100644 --- a/tests/unit/auth/test_resources.py +++ b/tests/unit/auth/test_resources.py @@ -7,6 +7,8 @@ from gn3.auth.authorisation.groups import Group from gn3.auth.authorisation.resources import ( Resource, create_resource, ResourceCategory) +from tests.unit.auth import conftest + group = Group(uuid.UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), "TheTestGroup") resource_category = ResourceCategory( uuid.UUID("fad071a3-2fc8-40b8-992b-cdefe7dcac79"), "mrna", "mRNA Dataset") @@ -14,20 +16,24 @@ create_resource_failure = { "status": "error", "message": "Unauthorised: Could not create resource" } +uuid_fn = lambda : uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") @pytest.mark.unit_test @pytest.mark.parametrize( - "user_id,expected", ( - ("ecb52977-3004-469e-9428-2a1856725c7f", Resource( - group, uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b"), - "test_resource", resource_category)), - ("21351b66-8aad-475b-84ac-53ce528451e3", create_resource_failure), - ("ae9c6245-0966-41a5-9a5e-20885a96bea7", create_resource_failure), - ("9a0c7ce5-2f40-4e78-979e-bf3527a59579", create_resource_failure), - ("e614247d-84d2-491d-a048-f80b578216cb", create_resource_failure))) -def test_create_resource(test_app, test_users_in_group, user_id, expected): + "user,expected", + tuple(zip( + conftest.TEST_USERS, + (Resource( + group, uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b"), + "test_resource", resource_category), + create_resource_failure, + create_resource_failure, + create_resource_failure, + create_resource_failure)))) +def test_create_resource(mocker, test_app, test_users_in_group, user, expected): """Test that resource creation works as expected.""" + mocker.patch("gn3.auth.authorisation.resources.uuid4", uuid_fn) conn, _group, _users = test_users_in_group with test_app.app_context() as flask_context: - flask_context.g.user_id = uuid.UUID(user_id) + flask_context.g.user = user assert create_resource(conn, "test_resource", resource_category) == expected -- cgit v1.2.3