Skip to content

Commit 296758b

Browse files
committed
Authorize showing project
1 parent faa727c commit 296758b

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

app/controllers/api/scratch/projects_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ProjectsController < ScratchController
99
before_action :ensure_create_is_a_remix, only: %i[create]
1010

1111
def show
12+
authorize! :show, @project
1213
render json: @project.scratch_component.content_with_stage_first
1314
end
1415

spec/features/scratch/showing_a_scratch_project_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
let(:school) { create(:school) }
77
let(:teacher) { create(:teacher, school:) }
88
let(: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 'returns scratch project JSON' do
1113
authenticated_in_hydra_as(teacher)
1214
project = create(
1315
:project,
1416
project_type: Project::Types::CODE_EDITOR_SCRATCH,
15-
locale: 'en'
17+
locale: 'en',
18+
school: school,
19+
user_id: teacher.id,
20+
lesson: lesson
1621
)
1722
create(:scratch_component, project: project)
1823

@@ -29,7 +34,10 @@
2934
project = create(
3035
:project,
3136
project_type: Project::Types::CODE_EDITOR_SCRATCH,
32-
locale: 'en'
37+
locale: 'en',
38+
school: school,
39+
lesson: lesson,
40+
user_id: teacher.id
3341
)
3442
create(
3543
:scratch_component,
@@ -71,4 +79,18 @@
7179

7280
expect(response).to have_http_status(:unauthorized)
7381
end
82+
83+
it 'returns a 403 forbidden if user does not have access to the project' do
84+
authenticated_in_hydra_as(teacher)
85+
project = create(
86+
:project,
87+
project_type: Project::Types::CODE_EDITOR_SCRATCH,
88+
locale: 'en'
89+
)
90+
create(:scratch_component, project: project)
91+
92+
get "/api/scratch/projects/#{project.identifier}", headers: headers
93+
94+
expect(response).to have_http_status(:forbidden)
95+
end
7496
end

0 commit comments

Comments
 (0)