Skip to content

Show users with STAFF_AREA_ACCESS as copilot choices.#5752

Open
mikerkelly wants to merge 9 commits intomainfrom
mikerkelly/project-create/form-reduce-copilot-list
Open

Show users with STAFF_AREA_ACCESS as copilot choices.#5752
mikerkelly wants to merge 9 commits intomainfrom
mikerkelly/project-create/form-reduce-copilot-list

Conversation

@mikerkelly
Copy link
Copy Markdown
Contributor

@mikerkelly mikerkelly commented Apr 1, 2026

Fixes #5750.

The list of all users displayed for the copilot field in the ProjectCreateForm and ProjectEditForm is very long in production -- anyone with a GitHub account can log in. Shortening it to a shorter valid set makes it slightly easier to navigate. This also makes manual errors less likely.

Ideally we would use a copilot-specific permission or role but that does not exist (yet?). So let's use STAFF_AREA_ACCESS as in practice all copilots do currently have that access for accessing the copilot dashboards (and formerly the application process).

Adjust the form field QuerySet to reduce the set of choices to those with roles that have the required permission. A function that returns User QuerySet is a User manager method so put it there, and this means that with_permission can be used more widely.

potential_copilots has the permission as a parameter to help with testing.

Add ALL_ROLES and roles_with_permission utility for making the QuerySet without hardcoding in the StaffAreaAdministrator role. Ideally all application behaviour should be driven by permissions, not roles, to decouple changes to the roles from the specific views et cetera.

Add user_with_fake_role_factory test fixture to aid with making User with fake permissions, to help with decoupling tests from specific roles and permissions. I updated existing tests using this where needed due to the copilot field change to show how this can be used, and also applied it to the rest of the integration tests and the ProjectAddMember unit tests for illustrative purposes.

The form tests use a fixture to patch the QuerySet returned by the User.objects.potential_copilots so that the other tests can be decoupled from the specific permission.

@mikerkelly mikerkelly force-pushed the mikerkelly/project-create/form-reduce-copilot-list branch 2 times, most recently from 0551040 to d9619f3 Compare April 1, 2026 15:09
To mirror GLOBAL_ROLES. These are not used yet but I intend to use them in
future commits, breaking up the changes.

The tests were using the & intersection set operator, which tests for
intersection, not equality. Checking for equality seems more robust. Therefore
the test was missing some new roles. Added these.
This function gets a set of Roles with a specific permission.

This can be useful when querying objects with role fields, the permissions
cannot be queried directly as they are not represented in the database.
Instead, you can query on something like `roles__overlap(permission)`.

This is not used yet, but I intend to use it soon.
The list of all users displayed for the `copilot` field in the
`ProjectCreateForm` and `ProjectEditForm` is very long in production -- anyone
with a GitHub account can log in. Shortening it to a shorter valid set makes it
slightly easier to navigate. This also makes manual errors less likely.

Ideally we would use a copilot-specific permission or role but that does not
exist (yet?). So let's use `STAFF_AREA_ACCESS` as in practice all copilots do
currently have that access for accessing the copilot dashboards (and formerly
the application process).

Adjust the form field QuerySet to reduce the set of choices to those with roles
that have the required permission. A function that returns User QuerySet is a
User manager method so put it there, and this means that `with_permission` can
be used more widely.

`potential_copilots` has the permission as a parameter to help with testing.

Add `user_with_fake_role_factory` test fixture to aid with making `User` with
fake permissions, to help with decoupling tests from specific roles and
permissions.

The form tests use a fixture to patch the `QuerySet` returned by the
`User.objects.potential_copilots` so that the other tests can be decoupled from
the specific permission.
This does not seem to break anything and generally it is helpful to delay
conversion to string as late as possible in case something downstream wants to
handle the original data type. Presumably the form handling code casts to
string itself.
We can use the `user_with_fake_role_factory` fixture to generate fake roles
with a given permission, for testing purposes. This decouples from the specific
roles and focuses on what actually drives access in the application, the
permission. The roles are really data represented as code, I feel they should
not be under test.
This allows creating a fake role with multiple permissions by passing them in.

This is a bit more ergonomic than:

```python
UserFactory(
    roles=[
        role_factory(permission=Permission.RELEASE_FILE_VIEW),
        role_factory(permission=Permission.SNAPSHOT_PUBLISH),
    ]
)

```

It also enables the extension to the `user_with_fake_role_factory` fixture. Now
we can just do:

```python
user = user_with_fake_role_factory(
    permission=[Permission.RELEASE_FILE_VIEW, Permission.SNAPSHOT_PUBLISH]
)
```

Not changing the function signature to `permissions` when there are many
existing examples. Easiest to accept either single or iterable parameter.
We can use the `user_with_fake_role_factory` fixture to generate fake roles
with given permissions, for testing purposes. This decouples from the specific
roles and focuses on what actually drives access in the application, the
permission. The roles are really data represented as code, I feel they should
not be under test.
This will make later commits have a smaller diff.
The ProjectAddMember view is driven by the USER_EDIT_PROJECT_ROLES permission,
not a specific role.

We can use the `user_with_fake_role_factory` fixture to generate fake roles
with a given permission,for testing purposes. This decouples from the specific
roles and focuses on what actually drives access in the application, the
permission. The roles are really data represented as code, I feel they should
not be under test.
@mikerkelly mikerkelly force-pushed the mikerkelly/project-create/form-reduce-copilot-list branch from d9619f3 to ed959fe Compare April 1, 2026 16:44
@mikerkelly mikerkelly marked this pull request as ready for review April 1, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce list of users shown in the copilot field on the project create form

1 participant