Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create staff fixture #2928

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")


@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


Expand Down Expand Up @@ -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)
Expand Down