Skip to content

Commit a689dd6

Browse files
committed
Add authorization for updating scratch projects
1 parent 296758b commit a689dd6

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

app/controllers/api/scratch/projects_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def create
4848
end
4949

5050
def update
51+
authorize! :update, @project
5152
@project.scratch_component&.content = scratch_content_params
5253
@project.save!
5354
render json: { status: 'ok' }, status: :ok

spec/features/scratch/updating_a_scratch_project_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
let(:school) { create(:school) }
77
let(:teacher) { create(:teacher, school:) }
88
let(:auth_headers) { { 'Authorization' => UserProfileMock::TOKEN } }
9+
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id]) }
10+
let(:lesson) { create(:lesson, school:, school_class:, user_id: teacher.id) }
911

1012
it 'responds 401 Unauthorized when no Authorization header is provided' do
1113
put '/api/scratch/projects/any-identifier', params: { project: { targets: [] } }
@@ -18,7 +20,10 @@
1820
project = create(
1921
:project,
2022
project_type: Project::Types::CODE_EDITOR_SCRATCH,
21-
locale: 'en'
23+
locale: 'en',
24+
school: school,
25+
lesson: lesson,
26+
user_id: teacher.id
2227
)
2328
create(:scratch_component, project: project)
2429

@@ -31,4 +36,18 @@
3136

3237
expect(project.reload.scratch_component.content.to_h['targets']).to eq(['some update'])
3338
end
39+
40+
it 'returns 403 Forbidden when trying to update a project user does not have access to' do
41+
authenticated_in_hydra_as(teacher)
42+
project = create(
43+
:project,
44+
project_type: Project::Types::CODE_EDITOR_SCRATCH,
45+
locale: 'en'
46+
)
47+
create(:scratch_component, project: project)
48+
49+
put "/api/scratch/projects/#{project.identifier}", params: { targets: ['some update'] }, headers: auth_headers
50+
51+
expect(response).to have_http_status(:forbidden)
52+
end
3453
end

0 commit comments

Comments
 (0)