From da33d719105d67afb1ee6b040380211cfa8be23d Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 3 Jan 2023 05:49:53 +0300 Subject: auth: rename fixtures: test_* -> fxtr_* Since test functions are defined starting with "test_", rename the fixture to more clearly indicate it is a fixture (fxtr_*), an not a test in itself. --- tests/unit/auth/fixtures/group_fixtures.py | 16 ++++++++-------- tests/unit/auth/fixtures/migration_fixtures.py | 8 ++++---- tests/unit/auth/fixtures/oauth2_client_fixtures.py | 6 +++--- tests/unit/auth/fixtures/resource_fixtures.py | 4 ++-- tests/unit/auth/fixtures/role_fixtures.py | 2 +- tests/unit/auth/fixtures/user_fixtures.py | 6 +++--- tests/unit/auth/test_groups.py | 20 ++++++++++---------- tests/unit/auth/test_privileges.py | 2 +- tests/unit/auth/test_resources.py | 14 +++++++------- tests/unit/auth/test_roles.py | 4 ++-- tests/unit/auth/test_token.py | 6 +++--- tests/unit/conftest.py | 10 +++++----- 12 files changed, 49 insertions(+), 49 deletions(-) (limited to 'tests') diff --git a/tests/unit/auth/fixtures/group_fixtures.py b/tests/unit/auth/fixtures/group_fixtures.py index d17d5cb..72b96d1 100644 --- a/tests/unit/auth/fixtures/group_fixtures.py +++ b/tests/unit/auth/fixtures/group_fixtures.py @@ -46,7 +46,7 @@ TEST_RESOURCES = TEST_RESOURCES_GROUP_01 + TEST_RESOURCES_GROUP_02 TEST_RESOURCES_PUBLIC = (TEST_RESOURCES_GROUP_01[0], TEST_RESOURCES_GROUP_02[1]) @pytest.fixture(scope="function") -def test_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] +def fxtr_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] """Fixture: setup a test group.""" query = "INSERT INTO groups(group_id, group_name) VALUES (?, ?)" with db.cursor(conn_after_auth_migrations) as cursor: @@ -58,9 +58,9 @@ def test_group(conn_after_auth_migrations):# pylint: disable=[redefined-outer-na yield (conn_after_auth_migrations, TEST_GROUPS[0]) @pytest.fixture(scope="function") -def test_users_in_group(test_group, test_users):# pylint: disable=[redefined-outer-name, unused-argument] +def fxtr_users_in_group(fxtr_group, fxtr_users):# pylint: disable=[redefined-outer-name, unused-argument] """Link the users to the groups.""" - conn, all_users = test_users + conn, all_users = fxtr_users users = tuple( user for user in all_users if user.email not in ("unaff@iliated.user",)) query_params = tuple( @@ -78,7 +78,7 @@ def test_users_in_group(test_group, test_users):# pylint: disable=[redefined-out query_params) @pytest.fixture(scope="function") -def fixture_group_roles(test_group):# pylint: disable=[redefined-outer-name] +def fxtr_group_roles(fxtr_group):# pylint: disable=[redefined-outer-name] """Link roles to group""" from .role_fixtures import RESOURCE_EDITOR_ROLE, RESOURCE_READER_ROLE# pylint: disable=[import-outside-toplevel] group_roles = ( @@ -86,7 +86,7 @@ def fixture_group_roles(test_group):# pylint: disable=[redefined-outer-name] TEST_GROUP_01, RESOURCE_EDITOR_ROLE), GroupRole(uuid.UUID("82aed039-fe2f-408c-ab1e-81cd1ba96630"), TEST_GROUP_02, RESOURCE_READER_ROLE)) - conn, groups = test_group + conn, groups = fxtr_group with db.cursor(conn) as cursor: cursor.executemany( "INSERT INTO group_roles VALUES (?, ?, ?)", @@ -97,11 +97,11 @@ def fixture_group_roles(test_group):# pylint: disable=[redefined-outer-name] yield conn, groups, group_roles @pytest.fixture(scope="function") -def fixture_group_user_roles(test_users_in_group, fixture_group_roles, fixture_resources):#pylint: disable=[redefined-outer-name,unused-argument] +def fxtr_group_user_roles(fxtr_users_in_group, fxtr_group_roles, fxtr_resources):#pylint: disable=[redefined-outer-name,unused-argument] """Assign roles to users.""" from .role_fixtures import RESOURCE_EDITOR_ROLE # pylint: disable=[import-outside-toplevel] - conn, _groups, _group_roles = fixture_group_roles - _conn, _group, group_users = test_users_in_group + conn, _groups, _group_roles = fxtr_group_roles + _conn, _group, group_users = fxtr_users_in_group users = tuple(user for user in group_users if user.email not in ("unaff@iliated.user", "group@lead.er")) users_roles_resources = ( diff --git a/tests/unit/auth/fixtures/migration_fixtures.py b/tests/unit/auth/fixtures/migration_fixtures.py index 3e511b1..eb42c2b 100644 --- a/tests/unit/auth/fixtures/migration_fixtures.py +++ b/tests/unit/auth/fixtures/migration_fixtures.py @@ -8,14 +8,14 @@ from gn3.auth import db from gn3.migrations import apply_migrations, rollback_migrations @pytest.fixture(scope="session") -def auth_testdb_path(test_app_config): # pylint: disable=redefined-outer-name +def auth_testdb_path(fxtr_app_config): # pylint: disable=redefined-outer-name """Get the test application's auth database file""" - return test_app_config["AUTH_DB"] + return fxtr_app_config["AUTH_DB"] @pytest.fixture(scope="session") -def auth_migrations_dir(test_app_config): # pylint: disable=redefined-outer-name +def auth_migrations_dir(fxtr_app_config): # pylint: disable=redefined-outer-name """Get the test application's auth database file""" - return test_app_config["AUTH_MIGRATIONS"] + return fxtr_app_config["AUTH_MIGRATIONS"] def apply_single_migration(backend: DatabaseBackend, migration: Migration):# pylint: disable=[redefined-outer-name] """Utility to apply a single migration""" diff --git a/tests/unit/auth/fixtures/oauth2_client_fixtures.py b/tests/unit/auth/fixtures/oauth2_client_fixtures.py index 03a53d7..5f11e92 100644 --- a/tests/unit/auth/fixtures/oauth2_client_fixtures.py +++ b/tests/unit/auth/fixtures/oauth2_client_fixtures.py @@ -9,13 +9,13 @@ from gn3.auth import db from gn3.auth.authentication.oauth2.models.oauth2client import OAuth2Client @pytest.fixture(autouse=True) -def fixture_patch_envvars(monkeypatch): +def fxtr_patch_envvars(monkeypatch): monkeypatch.setenv("AUTHLIB_INSECURE_TRANSPORT", "true") @pytest.fixture -def fixture_oauth2_clients(fixture_users_with_passwords): +def fxtr_oauth2_clients(fxtr_users_with_passwords): """Fixture: Create the OAuth2 clients for use with tests.""" - conn, users = fixture_users_with_passwords + conn, users = fxtr_users_with_passwords now = datetime.datetime.now() clients = tuple( diff --git a/tests/unit/auth/fixtures/resource_fixtures.py b/tests/unit/auth/fixtures/resource_fixtures.py index 9287936..12d8bce 100644 --- a/tests/unit/auth/fixtures/resource_fixtures.py +++ b/tests/unit/auth/fixtures/resource_fixtures.py @@ -6,9 +6,9 @@ from gn3.auth import db from .group_fixtures import TEST_RESOURCES @pytest.fixture(scope="function") -def fixture_resources(test_group):# pylint: disable=[redefined-outer-name] +def fxtr_resources(fxtr_group):# pylint: disable=[redefined-outer-name] """fixture: setup test resources in the database""" - conn, _group = test_group + conn, _group = fxtr_group with db.cursor(conn) as cursor: cursor.executemany( "INSERT INTO resources VALUES (?,?,?,?,?)", diff --git a/tests/unit/auth/fixtures/role_fixtures.py b/tests/unit/auth/fixtures/role_fixtures.py index befa6b0..e1e1680 100644 --- a/tests/unit/auth/fixtures/role_fixtures.py +++ b/tests/unit/auth/fixtures/role_fixtures.py @@ -22,7 +22,7 @@ RESOURCE_EDITOR_ROLE = Role( TEST_ROLES = (RESOURCE_READER_ROLE, RESOURCE_EDITOR_ROLE) @pytest.fixture(scope="function") -def fixture_roles(conn_after_auth_migrations): +def fxtr_roles(conn_after_auth_migrations): """Setup some example roles.""" with db.cursor(conn_after_auth_migrations) as cursor: cursor.executemany( diff --git a/tests/unit/auth/fixtures/user_fixtures.py b/tests/unit/auth/fixtures/user_fixtures.py index 843d575..89b7e62 100644 --- a/tests/unit/auth/fixtures/user_fixtures.py +++ b/tests/unit/auth/fixtures/user_fixtures.py @@ -18,7 +18,7 @@ TEST_USERS = ( "unaff@iliated.user", "Unaffiliated User")) @pytest.fixture(scope="function") -def test_users(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] +def fxtr_users(conn_after_auth_migrations):# pylint: disable=[redefined-outer-name] """Fixture: setup test users.""" query = "INSERT INTO users(user_id, email, name) VALUES (?, ?, ?)" query_user_roles = "INSERT INTO user_roles(user_id, role_id) VALUES (?, ?)" @@ -44,9 +44,9 @@ def test_users(conn_after_auth_migrations):# pylint: disable=[redefined-outer-na ("9a0c7ce5-2f40-4e78-979e-bf3527a59579",))) @pytest.fixture(scope="function") -def fixture_users_with_passwords(test_users): # pylint: disable=[redefined-outer-name] +def fxtr_users_with_passwords(fxtr_users): # pylint: disable=[redefined-outer-name] """Fixture: add passwords to the users""" - conn, users = test_users + conn, users = fxtr_users user_passwords_params = tuple( (str(user.user_id), bcrypt.hashpw( f"password_for_user_{idx:03}".encode("utf8"), diff --git a/tests/unit/auth/test_groups.py b/tests/unit/auth/test_groups.py index 5a27c71..2345b2a 100644 --- a/tests/unit/auth/test_groups.py +++ b/tests/unit/auth/test_groups.py @@ -35,7 +35,7 @@ PRIVILEGES = ( create_group_failure, create_group_failure, create_group_failure, create_group_failure)))) def test_create_group(# pylint: disable=[too-many-arguments] - test_app, auth_testdb_path, mocker, test_users, user, expected):# pylint: disable=[unused-argument] + fxtr_app, auth_testdb_path, mocker, fxtr_users, user, expected):# pylint: disable=[unused-argument] """ GIVEN: an authenticated user WHEN: the user attempts to create a group @@ -43,7 +43,7 @@ def test_create_group(# pylint: disable=[too-many-arguments] appropriate privileges """ mocker.patch("gn3.auth.authorisation.groups.uuid4", uuid_fn) - with test_app.app_context() as flask_context: + with fxtr_app.app_context() as flask_context: flask_context.g.user = user with db.connection(auth_testdb_path) as conn: assert create_group(conn, "a_test_group", user) == expected @@ -63,7 +63,7 @@ create_role_failure = { "ResourceEditor", PRIVILEGES)), create_role_failure, create_role_failure, create_role_failure, create_role_failure)))) -def test_create_group_role(mocker, test_users_in_group, test_app, user, expected): +def test_create_group_role(mocker, fxtr_users_in_group, fxtr_app, user, expected): """ GIVEN: an authenticated user WHEN: the user attempts to create a role, attached to a group @@ -72,14 +72,14 @@ def test_create_group_role(mocker, test_users_in_group, test_app, user, expected """ mocker.patch("gn3.auth.authorisation.groups.uuid4", uuid_fn) mocker.patch("gn3.auth.authorisation.roles.uuid4", uuid_fn) - conn, _group, _users = test_users_in_group - with test_app.app_context() as flask_context: + conn, _group, _users = fxtr_users_in_group + with fxtr_app.app_context() as flask_context: flask_context.g.user = user assert create_group_role( conn, GROUP, "ResourceEditor", PRIVILEGES) == expected @pytest.mark.unit_test -def test_create_multiple_groups(mocker, test_app, test_users): +def test_create_multiple_groups(mocker, fxtr_app, fxtr_users): """ GIVEN: An authenticated user with appropriate authorisation WHEN: The user attempts to create a new group, while being a member of an @@ -91,8 +91,8 @@ def test_create_multiple_groups(mocker, test_app, test_users): user = User( UUID("ecb52977-3004-469e-9428-2a1856725c7f"), "group@lead.er", "Group Leader") - conn, _test_users = test_users - with test_app.app_context() as flask_context: + conn, _test_users = fxtr_users + with fxtr_app.app_context() as flask_context: flask_context.g.user = user # First time, successfully creates the group assert create_group(conn, "a_test_group", user) == Group( @@ -108,7 +108,7 @@ def test_create_multiple_groups(mocker, test_app, test_users): conftest.TEST_USERS, (([Group(UUID("9988c21d-f02f-4d45-8966-22c968ac2fbf"), "TheTestGroup")] * 3) + [Nothing])))) -def test_user_group(test_users_in_group, user, expected): +def test_user_group(fxtr_users_in_group, user, expected): """ GIVEN: A bunch of registered users, some of whom are members of a group, and others are not @@ -116,7 +116,7 @@ def test_user_group(test_users_in_group, user, expected): THEN: return a Maybe containing the group that the user belongs to, or Nothing """ - conn, _group, _users = test_users_in_group + conn, _group, _users = fxtr_users_in_group with db.cursor(conn) as cursor: assert ( user_group(cursor, user).maybe(Nothing, lambda val: val) diff --git a/tests/unit/auth/test_privileges.py b/tests/unit/auth/test_privileges.py index 3586e90..1c2ba24 100644 --- a/tests/unit/auth/test_privileges.py +++ b/tests/unit/auth/test_privileges.py @@ -37,7 +37,7 @@ PRIVILEGES = sorted( @pytest.mark.parametrize( "user,expected", tuple(zip( conftest.TEST_USERS, (PRIVILEGES, [], [], [], [])))) -def test_user_privileges(auth_testdb_path, test_users, user, expected):# pylint: disable=[unused-argument] +def test_user_privileges(auth_testdb_path, fxtr_users, user, expected):# pylint: disable=[unused-argument] """ GIVEN: A user WHEN: An attempt is made to fetch the user's privileges diff --git a/tests/unit/auth/test_resources.py b/tests/unit/auth/test_resources.py index 824062d..b756cd9 100644 --- a/tests/unit/auth/test_resources.py +++ b/tests/unit/auth/test_resources.py @@ -31,24 +31,24 @@ uuid_fn = lambda : uuid.UUID("d32611e3-07fc-4564-b56c-786c6db6de2b") create_resource_failure, create_resource_failure, create_resource_failure)))) -def test_create_resource(mocker, test_app, test_users_in_group, user, expected): +def test_create_resource(mocker, fxtr_app, fxtr_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: + conn, _group, _users = fxtr_users_in_group + with fxtr_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(fixture_resources): +def test_public_resources(fxtr_resources): """ GIVEN: some resources in the database WHEN: public resources are requested THEN: only list the resources that are public """ - conn, _res = fixture_resources + conn, _res = fxtr_resources assert sorted(public_resources(conn), key=SORTKEY) == sorted(tuple( res for res in conftest.TEST_RESOURCES if res.public), key=SORTKEY) @@ -68,11 +68,11 @@ PUBLIC_RESOURCES = sorted(conftest.TEST_RESOURCES_PUBLIC, key=SORTKEY) conftest.TEST_RESOURCES_PUBLIC), key=SORTKEY), PUBLIC_RESOURCES, PUBLIC_RESOURCES)))) -def test_user_resources(fixture_group_user_roles, user, expected): +def test_user_resources(fxtr_group_user_roles, user, expected): """ GIVEN: some resources in the database WHEN: a particular user's resources are requested THEN: list only the resources for which the user can access """ - conn = fixture_group_user_roles + conn = fxtr_group_user_roles assert sorted(user_resources(conn, user), key=SORTKEY) == expected diff --git a/tests/unit/auth/test_roles.py b/tests/unit/auth/test_roles.py index b6e681d..92384bb 100644 --- a/tests/unit/auth/test_roles.py +++ b/tests/unit/auth/test_roles.py @@ -30,7 +30,7 @@ PRIVILEGES = ( PRIVILEGES), create_role_failure, create_role_failure, create_role_failure, create_role_failure)))) def test_create_role(# pylint: disable=[too-many-arguments] - test_app, auth_testdb_path, mocker, test_users, user, expected):# pylint: disable=[unused-argument] + fxtr_app, auth_testdb_path, mocker, fxtr_users, user, expected):# pylint: disable=[unused-argument] """ GIVEN: an authenticated user WHEN: the user attempts to create a role @@ -38,7 +38,7 @@ def test_create_role(# pylint: disable=[too-many-arguments] appropriate privileges """ mocker.patch("gn3.auth.authorisation.roles.uuid4", uuid_fn) - with test_app.app_context() as flask_context: + with fxtr_app.app_context() as flask_context: flask_context.g.user = user with db.connection(auth_testdb_path) as conn, db.cursor(conn) as cursor: the_role = create_role(cursor, "a_test_role", PRIVILEGES) diff --git a/tests/unit/auth/test_token.py b/tests/unit/auth/test_token.py index edf4b19..a1709bf 100644 --- a/tests/unit/auth/test_token.py +++ b/tests/unit/auth/test_token.py @@ -32,7 +32,7 @@ def gen_token(client, grant_type, user, scope): # pylint: disable=[unused-argume (("papa", "yada", 2), USERNAME_PASSWORD_FAIL_RESULT), # (("unaff@iliated.user", "password_for_user_004", 1), USERNAME_PASSWORD_FAIL_RESULT) )) -def test_token(test_app, fixture_oauth2_clients, test_data, expected): +def test_token(fxtr_app, fxtr_oauth2_clients, test_data, expected): """ GIVEN: a registered oauth2 client, a user WHEN: a token is requested via the 'password' grant @@ -42,7 +42,7 @@ def test_token(test_app, fixture_oauth2_clients, test_data, expected): back c) TODO: when user tries to use wrong client, we get error message back """ - _conn, oa2clients = fixture_oauth2_clients + _conn, oa2clients = fxtr_oauth2_clients email, password, client_idx = test_data data = { "grant_type": "password", "scope": "profile nonexistent-scope", @@ -50,7 +50,7 @@ def test_token(test_app, fixture_oauth2_clients, test_data, expected): "client_secret": oa2clients[client_idx].client_secret, "username": email, "password": password} - with test_app.test_client() as client: + with fxtr_app.test_client() as client: res = client.post("/api/oauth2/token", data=data) assert res.status_code == expected["status_code"] for key in expected["result"]: diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 3020f88..8005c8e 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -8,7 +8,7 @@ import pytest from gn3.app import create_app @pytest.fixture(scope="session") -def test_app(): +def fxtr_app(): """Fixture: setup the test app""" # Do some setup with TemporaryDirectory() as testdir: @@ -24,12 +24,12 @@ def test_app(): testdb.unlink(missing_ok=True) @pytest.fixture(scope="session") -def client(test_app): # pylint: disable=redefined-outer-name +def client(fxtr_app): # pylint: disable=redefined-outer-name """Create a test client fixture for tests""" - with test_app.app_context(): - yield test_app.test_client() + with fxtr_app.app_context(): + yield fxtr_app.test_client() @pytest.fixture(scope="session") -def test_app_config(client): # pylint: disable=redefined-outer-name +def fxtr_app_config(client): # pylint: disable=redefined-outer-name """Return the test application's configuration object""" return client.application.config -- cgit v1.2.3