diff --git a/api/conftest.py b/api/conftest.py index b6605d6e4d63..966995c85245 100644 --- a/api/conftest.py +++ b/api/conftest.py @@ -77,9 +77,29 @@ def test_user_client(api_client, test_user): @pytest.fixture() -def organisation(db, admin_user): +def staff_user(django_user_model): + """ + A non-admin user fixture. + To add to an environment with permissions use the fixture + with_environment_permissions. + + This fixture is attached to the organisation fixture. + """ + return django_user_model.objects.create(email="staff@example.com") + + +@pytest.fixture() +def staff_client(staff_user): + client = APIClient() + client.force_authenticate(user=staff_user) + return client + + +@pytest.fixture() +def organisation(db, admin_user, staff_user): org = Organisation.objects.create(name="Test Org") admin_user.add_organisation(org, role=OrganisationRole.ADMIN) + staff_user.add_organisation(org, role=OrganisationRole.USER) return org @@ -156,6 +176,29 @@ def environment(project): return Environment.objects.create(name="Test Environment", project=project) +@pytest.fixture() +def with_environment_permissions( + environment: Environment, staff_user: FFAdminUser +) -> typing.Callable: + """ + Add environment permissions to the staff_user fixture. + Defaults to associating to the environment fixture. + """ + + def _with_environment_permissions( + permission_keys: list[str], environment_id: typing.Optional[int] = None + ) -> UserEnvironmentPermission: + environment_id = environment_id or environment.id + uep, __ = UserEnvironmentPermission.objects.get_or_create( + environment_id=environment_id, user=staff_user + ) + uep.permissions.add(*permission_keys) + + return uep + + return _with_environment_permissions + + @pytest.fixture() def identity(environment): return Identity.objects.create(identifier="test_identity", environment=environment)