Skip to content

Let owners toggle Scratch for their school#839

Open
zetter-rpf wants to merge 9 commits into
mainfrom
enable-scratch-endpoint
Open

Let owners toggle Scratch for their school#839
zetter-rpf wants to merge 9 commits into
mainfrom
enable-scratch-endpoint

Conversation

@zetter-rpf
Copy link
Copy Markdown
Contributor

@zetter-rpf zetter-rpf commented May 28, 2026

Status

What's changed?

Previously members of schools could access Scratch projects if they had the cat_mode feature was enabled.

This change allows members of schools to view/remix/update existing scratch projects they have access to, regardless of the feature flag. There is a new scratch_enabled flag in the database that decides if school members can create new scratch projects.

As part of this we have:

  • Re-purposed the existing update school endpoint to allow for updating the new scratch_enabled flag
  • Changed the logic in the scratch base controller to just check for school membership instead of the feature
  • Checked the scratch_enabled flag in the lessons controller
  • Made the lessons controller safer by restricting what params are accepted in updates
  • Removed duplicated asset tests
  • Made sure the Scratch controllers authorizes resources using our standard pattern

As part of deploy

We should deploy the frontend changes soon after this to make it easier to toggle the Scratch feature

@cla-bot cla-bot Bot added the cla-signed label May 28, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

Test coverage

91.34% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/26826478259

@zetter-rpf zetter-rpf force-pushed the enable-scratch-endpoint branch 2 times, most recently from 9fd2598 to f486e88 Compare June 2, 2026 09:59
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-mws1rw June 2, 2026 09:59 Inactive
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-mws1rw June 2, 2026 10:00 Inactive
@zetter-rpf zetter-rpf force-pushed the enable-scratch-endpoint branch from 34c6dd7 to 5215472 Compare June 2, 2026 10:39
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-mws1rw June 2, 2026 10:39 Inactive
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-mws1rw June 2, 2026 11:13 Inactive
This update schools endpoint isn't used, and there were no
requests to it  the last 30 days.

We've chosen to limit the params so we don't get any unexpected
behaviour, but can add old ones back in if we want to allow
school owners to update them in the future.

We've also removed the 422 test as can't seem to make a similar failing test.

We've also checked the abilities and only school owners can update
the school, but owners, teachers and students can view it.
@zetter-rpf zetter-rpf force-pushed the enable-scratch-endpoint branch from a689dd6 to e351e91 Compare June 2, 2026 11:22
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-mws1rw June 2, 2026 11:22 Inactive
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-4uywtk June 2, 2026 12:58 Inactive
@zetter-rpf zetter-rpf changed the title Enable scratch endpoint Endpoints to let owners toggle Scratch for their school Jun 2, 2026
@zetter-rpf zetter-rpf force-pushed the enable-scratch-endpoint branch from b406d4b to 8b57ca5 Compare June 2, 2026 13:13
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-4uywtk June 2, 2026 13:13 Inactive
@zetter-rpf zetter-rpf marked this pull request as ready for review June 2, 2026 13:14
Copilot AI review requested due to automatic review settings June 2, 2026 13:14
@zetter-rpf zetter-rpf changed the title Endpoints to let owners toggle Scratch for their school Let owners toggle Scratch for their school Jun 2, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Replaces the cat_mode Flipper feature flag with a new per-school scratch_enabled boolean column. School members can now view/remix/update existing Scratch projects they have access to regardless of any feature flag; only the creation of new Scratch lessons is gated by scratch_enabled. The schools update endpoint is repurposed to toggle this flag, the lessons controller restricts what params can be updated, and the Scratch controllers are refactored to use the standard CanCan authorize_resource pattern.

Changes:

  • New scratch_enabled boolean on schools (migration + schema + jbuilder view), exposed via PUT /api/schools/:id (now only permitting scratch_enabled).
  • Scratch controllers: replaced the cat_mode filter with a school-membership check and switched to authorize_resource for Project / original_project / project_from_header; added a verify_can_create_scratch_projects guard in the lessons controller and split lesson_params into create_params / update_params.
  • Updated and reorganized request specs to reflect the new behavior; removed the now-duplicate creating_a_scratch_asset_spec.rb file.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
db/migrate/20260528141937_add_scratch_enabled_to_school.rb Adds scratch_enabled boolean column to schools.
db/schema.rb Reflects the new column and bumps schema version.
app/views/api/schools/_school.json.jbuilder Exposes scratch_enabled in school JSON.
app/controllers/api/schools_controller.rb Splits permitted params into create_params / update_params (update is restricted to scratch_enabled).
app/controllers/api/lessons_controller.rb Adds verify_can_create_scratch_projects filter, splits create_params/update_params, and routes update through the restricted set.
app/controllers/api/scratch/scratch_controller.rb Replaces check_scratch_feature Flipper check with a school-membership check; drops load_project (moved to projects controller).
app/controllers/api/scratch/projects_controller.rb Switches to authorize_resource :project / :original_project; refactors load_original_project.
app/controllers/api/scratch/assets_controller.rb Uses authorize_resource :project_from_header instead of inline authorize! calls.
spec/features/my_school/showing_my_school_spec.rb Adds scratch_enabled to expected JSON.
spec/features/school/updating_a_school_spec.rb Reworks update spec to assert scratch_enabled flips; removes invalid-name test.
spec/features/lesson/creating_a_lesson_spec.rb Adds tests for Scratch project creation gated by scratch_enabled.
spec/features/lesson/updating_a_lesson_spec.rb Updates expectation: re-assigning class is now silently ignored rather than 422.
spec/features/scratch/creating_a_scratch_project_spec.rb Replaces cat_mode flag setup with school-membership scenarios; updates expected status code to 403.
spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb Removes cat_mode flag setup; adds 401/404 cases for unauthenticated / non-school users.
spec/features/scratch/showing_a_scratch_project_spec.rb Requires authentication and adds 401/403 coverage.
spec/features/scratch/updating_a_scratch_project_spec.rb Removes cat_mode setup; adds 403 case for inaccessible project.
spec/features/scratch/creating_a_scratch_asset_spec.rb Deletes duplicate spec file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/controllers/api/lessons_controller.rb
Comment thread db/migrate/20260528141937_add_scratch_enabled_to_school.rb
Comment thread app/controllers/api/scratch/scratch_controller.rb Outdated
Comment thread spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb Outdated
The behaviour we want is to allow any existing scratch projects to be accessed updated and so I've removed all the checks from the Scratch Controller, except that they are logged in and part of a school. The school check could be removed in the future if we want to open up access to Scratch.

Note that some controllers were missing the logged in check so I've updated the code and tests
We're adding a new setting to allow Scratch to be toggled by school owners since Flipper is not designed for gating large amounts of individual actors.
Previously any params could be updated in lessons which was dangerous - we don't expect many of these params to be updated to changing them could create odd behaviour or allow other data to be accessed.

I've checked the frontend, and only the name and visibility can be updated.

I've done this now so that users can't turn their projects into Scratch projects unexpectedly.

We could add attributes back in in the future if we want to.
This is only relevant to the projects controller, so there's no need to have it in the shared controller
While we have been working on this we have ended up with two test files that cover creating assets. Use the more comprehensive file for now and move in the only one that wasn't covered.
Using authorize_resource directly makes it easier to remove authorization concerns from controller actions, provide better defaults for the controller, and give us consistent behaviour for unauthorized requests.
@zetter-rpf zetter-rpf force-pushed the enable-scratch-endpoint branch from 8b57ca5 to 2b204ba Compare June 2, 2026 14:28
@zetter-rpf zetter-rpf temporarily deployed to editor-api-p-enable-scr-e2zsdh June 2, 2026 14:29 Inactive
@zetter-rpf
Copy link
Copy Markdown
Contributor Author

zetter-rpf commented Jun 2, 2026

This is deployable while I'm off once reviewed, I suggest deploying it just before @DNR500's work for https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1375 otherwise it will be hard for us to test enabling scratch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants