Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@ A project remix is created via a `POST` request to `projects/{original_project_i
Experience CS synchronizes public curriculum projects and their global Scratch
assets asynchronously. Configure `EXPERIENCE_CS_API_KEY` to the same secret as
Experience CS's `EDITOR_API_SYNC_API_KEY`. The corresponding request header is
accepted only for project create/update and global Scratch asset upload; it does
not authorize user-project operations.
accepted for public project create/update and global Scratch asset upload.

`PUT /api/experience-cs/projects/:identifier/migrate` lets the service replace
an exact locale-less Experience CS user-project stub with its Markdown
instructions and Scratch content. Project-scoped migration assets use
`POST /api/experience-cs/projects/:identifier/assets/:filename` and remain
subject to the project's normal viewing permissions.

### Code Editor for Education

Expand Down
48 changes: 48 additions & 0 deletions app/controllers/api/experience_cs_project_migrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Api
class ExperienceCsProjectMigrationsController < ApiController

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Handling this in a new controller is a good idea and makes it easier to remove it when we have done the migration.

prepend_before_action :load_experience_cs_service_user
before_action :authorize_user
before_action :load_project

def update
migrate_project!
render json: {
identifier: @project.identifier,
locale: @project.locale,
project_type: @project.project_type
}
rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing => e
render json: { error: e.message }, status: :unprocessable_content
end

private

def load_project
@project = Project.find_by!(identifier: params.expect(:id), locale: nil)
end

def migrate_project!
attributes = migration_params
@project.with_lock do
authorize! :migrate_from_experience_cs, @project
@project.update!(
attributes.slice(:name, :instructions).merge(
project_type: Project::Types::CODE_EDITOR_SCRATCH
)
)
scratch_component = @project.scratch_component || @project.build_scratch_component
scratch_component.update!(attributes.require(:scratch_component).slice(:content))
end
end

def migration_params
params.fetch(:project, {}).permit(
:name,
{ instructions: [:markdown_content] },
{ scratch_component: { content: {} } }
)
end
end
end
41 changes: 32 additions & 9 deletions app/controllers/api/scratch/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Scratch
class AssetsController < ApiController
include ActiveStorage::SetCurrent

prepend_before_action :load_experience_cs_service_user, only: %i[create_global]
prepend_before_action :load_experience_cs_service_user, only: %i[create_global create_migration]
before_action :authorize_user, except: %i[show]
prepend_before_action :load_project_from_header, only: %i[show create]
authorize_resource :project_from_header, except: %i[create_global]
authorize_resource :project_from_header, except: %i[create_global create_migration]
before_action :load_migration_project, only: :create_migration
before_action :authorize_migration_asset, only: :create_migration

def show
filename_with_extension = "#{params[:id]}.#{params[:format]}"
Expand All @@ -34,6 +36,15 @@ def create
)
end

def create_migration
create_asset(
project: @migration_project,
uploaded_user_id: @migration_project.user_id,
filename: "#{params[:id]}.#{params[:format]}",
reject_conflicting_content: true
)
end

def create_global
authorize! :create_global, ScratchAsset

Expand All @@ -60,20 +71,20 @@ def create_asset(reject_conflicting_content: false, **attributes)
end

if reject_conflicting_content
return if attach_global_file(scratch_asset, attributes.fetch(:filename)) == :conflict
return if attach_file_with_conflict_check(scratch_asset, attributes.fetch(:filename)) == :conflict
else
attach_file_unless_present(scratch_asset, attributes.fetch(:filename))
end

render json: { status: 'ok', 'content-name': params[:id] }, status: :created
end

def attach_global_file(scratch_asset, filename)
def attach_file_with_conflict_check(scratch_asset, filename)
scratch_asset.with_lock do
if scratch_asset.file.attached?
next :unchanged if global_file_matches?(scratch_asset)
next :unchanged if file_matches?(scratch_asset)

next reject_conflicting_global_file
next reject_conflicting_file(scratch_asset)
end

scratch_asset.file.attach(io: request.body, filename:)
Expand All @@ -85,12 +96,13 @@ def attach_file_unless_present(scratch_asset, filename)
scratch_asset.file.attach(io: request.body, filename:) unless scratch_asset.file.attached?
end

def global_file_matches?(scratch_asset)
def file_matches?(scratch_asset)
scratch_asset.file.blob.checksum == request_body_checksum
end

def reject_conflicting_global_file
render json: { error: 'Asset content conflicts with the existing global asset' }, status: :conflict
def reject_conflicting_file(scratch_asset)
scope = scratch_asset.global? ? 'global' : 'project'
render json: { error: "Asset content conflicts with the existing #{scope} asset" }, status: :conflict
:conflict
end

Expand All @@ -109,6 +121,17 @@ def load_project_from_header
project_type: Project::Types::CODE_EDITOR_SCRATCH
)
end

def load_migration_project
@migration_project = Project.find_by!(
identifier: params.expect(:project_id),
locale: nil
)
end

def authorize_migration_asset
authorize! :upload_migration_asset, @migration_project
end
end
end
end
13 changes: 13 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(user)

define_editor_admin_abilities(user)
define_experience_cs_admin_abilities(user)
define_experience_cs_service_abilities(user)
end

private
Expand Down Expand Up @@ -72,6 +73,12 @@ def define_school_owner_abilities(school:)
can(%i[read], :school_member)
can(%i[read create import update destroy regenerate_join_code], SchoolClass, school: { id: school.id })
can(%i[read update show_context], Project, school_id: school.id, lesson: { visibility: %w[teachers students] })
can(
%i[read show_context],
Project,
school_id: school.id,
parent: { school_id: school.id, lesson: { visibility: %w[teachers students] } }
)
can(%i[read create create_batch destroy], ClassStudent, school_class: { school: { id: school.id } })
can(%i[read create destroy], :school_owner)
can(%i[read create destroy], :school_teacher)
Expand Down Expand Up @@ -152,6 +159,12 @@ def define_experience_cs_admin_abilities(user)
define_school_import_abilities(user)
end

def define_experience_cs_service_abilities(user)
return unless user&.experience_cs_service_account?

can %i[migrate_from_experience_cs upload_migration_asset], Project, &:experience_cs_migration_target?
end

def school_teacher_can_manage_lesson?(user:, school:, lesson:)
is_my_lesson = lesson.school_id == school.id && lesson.user_id == user.id
is_my_class = lesson.school_class&.teacher_ids&.include?(user.id)
Expand Down
4 changes: 4 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def public_experience_cs_project?
user_id.nil? && school_id.nil? && EXPERIENCE_CS_PROJECT_TYPES.include?(project_type)
end

def experience_cs_migration_target?
user_id.present? && school_id.present? && project_type == Types::SCRATCH
end

def self_and_ancestors
projects = []
current_project = self
Expand Down
2 changes: 1 addition & 1 deletion app/models/scratch_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def response_content_type
private

def belongs_to_scratch_project
return if project.blank? || project.scratch_project?
return if project.blank? || Project::EXPERIENCE_CS_PROJECT_TYPES.include?(project.project_type)

errors.add(:project, 'must be a Scratch project')
end
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
mount GraphiQL::Rails::Engine, at: '/graphql', graphql_path: '/graphql#execute' unless Rails.env.production?

namespace :api do
put '/experience-cs/projects/:id/migrate', to: 'experience_cs_project_migrations#update'
post '/experience-cs/projects/:project_id/assets/:id.:format', to: 'scratch/assets#create_migration'

namespace :scratch do
resources :projects, only: %i[show update create]
get '/assets/internalapi/asset/:id.:format/get/' => 'assets#show'
Expand Down
140 changes: 138 additions & 2 deletions spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,42 @@

describe 'POST #create' do
let(:upload) { File.binread(file_fixture(filename)) }
let(:request_path) { '/api/scratch/assets/test_image_1.png' }
let(:request_headers) do
{ 'Content-Type' => 'application/octet-stream', 'X-Project-ID' => project.identifier }.merge(auth_headers)
end
let(:make_request) do
post '/api/scratch/assets/test_image_1.png', headers: request_headers, params: upload

def make_request
post request_path, headers: request_headers, params: upload
end

context 'when a teacher is logged in' do
before do
authenticated_in_hydra_as(teacher)
end

context 'when another project type has the same identifier' do
let(:identifier) { 'shared-project-identifier' }
let(:project) { create_scratch_project(identifier:, user_id: teacher.id) }

before do
create(
:project,
identifier:,
locale: 'en',
project_type: Project::Types::PYTHON,
user_id: nil
)
end

it 'uploads the asset to the Scratch project' do
make_request

expect(ScratchAsset.find_by!(filename:).project).to eq(project)
expect(response).to have_http_status(:created)
end
end

it 'responds 400 Bad Request when X-Project-ID is not provided' do
post '/api/scratch/assets/test_image_1.png',
headers: { 'Content-Type' => 'application/octet-stream' }.merge(auth_headers),
Expand Down Expand Up @@ -420,13 +444,125 @@
end
end

context 'when the Experience CS service uploads a migration asset' do
let(:request_headers) do
{
'Content-Type' => 'application/octet-stream',
ExperienceCsServiceAuthenticator::HEADER => 'service-api-key'
}
end
let(:request_path) { "/api/experience-cs/projects/#{project.identifier}/assets/test_image_1.png" }
let(:project) do
create(
:project,
school:,
user_id: teacher.id,
locale: nil,
project_type: Project::Types::SCRATCH
)
end

before do
allow(Rails.configuration.x.experience_cs).to receive(:service_api_key).and_return('service-api-key')
end

it 'stores the asset against the existing stub as an upload by its owner' do
expect { make_request }.to change(ScratchAsset, :count).by(1)

asset = ScratchAsset.find_by!(filename:, project:)
expect(asset.uploaded_user_id).to eq(teacher.id)
expect(asset.file.download).to eq(upload)
expect(response).to have_http_status(:created)
end

it 'accepts repeated uploads with identical bytes' do
make_request

expect { make_request }.not_to change(ScratchAsset, :count)

expect(response).to have_http_status(:created)
expect(ScratchAsset.find_by!(filename:, project:).file.download).to eq(upload)
end

it 'rejects repeated uploads with conflicting bytes' do
existing_asset = create_uploaded_scratch_asset(
filename:,
project:,
uploaded_user_id: teacher.id,
body: 'existing bytes'
)

expect { make_request }.not_to change(ScratchAsset, :count)

expect(response).to have_http_status(:conflict)
expect(response.parsed_body).to eq(
'error' => 'Asset content conflicts with the existing project asset'
)
expect(existing_asset.reload.file.download).to eq('existing bytes')
end

it 'rejects uploads after the stub has been converted' do
project.update!(project_type: Project::Types::CODE_EDITOR_SCRATCH)

expect { make_request }.not_to change(ScratchAsset, :count)

expect(response).to have_http_status(:forbidden)
end
end

it 'responds 401 unauthorized when user is not signed in' do
post '/api/scratch/assets/example.svg', headers: { 'X-Project-ID' => project.identifier }

expect(response).to have_http_status(:unauthorized)
end
end

describe 'visibility of a migrated project asset' do
let(:student) { create(:student, school:) }
let(:class_teacher) { create(:teacher, school:) }
let(:school_owner) { create(:owner, school:) }
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id, class_teacher.id]) }
let(:lesson) { create(:lesson, school:, school_class:, user_id: teacher.id, visibility: 'students') }
let(:lesson_project) { create_scratch_project(school:, lesson:, user_id: teacher.id) }
let(:project) { create_scratch_project(school:, user_id: student.id, remixed_from_id: lesson_project.id) }

before do
create(:class_student, school_class:, student_id: student.id)
create_uploaded_scratch_asset(filename:, project:, uploaded_user_id: student.id, body: 'private migration bytes')
end

it 'is available to the student, class teachers, and school owners' do
[student, teacher, class_teacher, school_owner].each do |viewer|
authenticated_in_hydra_as(viewer)

get(
'/api/scratch/assets/internalapi/asset/test_image_1.png/get/',
headers: { Authorization: UserProfileMock::TOKEN, 'X-Project-ID' => project.identifier }
)
follow_redirect! while response.redirect?

expect(response.body).to eq('private migration bytes')
end
end

it 'is unavailable to unrelated and unauthenticated callers' do
unrelated_teacher = create(:teacher, school: create(:school))
authenticated_in_hydra_as(unrelated_teacher)

get(
'/api/scratch/assets/internalapi/asset/test_image_1.png/get/',
headers: { Authorization: UserProfileMock::TOKEN, 'X-Project-ID' => project.identifier }
)
expect(response).to have_http_status(:forbidden)

get(
'/api/scratch/assets/internalapi/asset/test_image_1.png/get/',
headers: { 'X-Project-ID' => project.identifier }
)
expect(response).to have_http_status(:unauthorized)
end
end

describe 'POST #create_global' do
let(:upload) { File.binread(file_fixture(filename)) }
let(:project) { create_scratch_project(locale: 'en', user_id: nil) }
Expand Down
Loading
Loading