Skip to content

Commit 4acbcea

Browse files
committed
Return a 404 if a Scratch project doesn't exist
Fixes RaspberryPiFoundation/digital-editor-issues#1269 Previously a 500 was raised because we would try to access the scratch component of an `nil` object. I've also added another case for if the project exists but isn't a scratch project as I expect that would error too.
1 parent 38be3fb commit 4acbcea

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

app/controllers/api/scratch/scratch_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def check_scratch_feature
2020
def load_project
2121
project_loader = ProjectLoader.new(params[:id], [params[:locale]])
2222
@project = project_loader.load
23+
24+
raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless @project
25+
raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless @project.scratch_project?
2326
end
2427
end
2528
end

spec/features/scratch/showing_a_scratch_project_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@
1818
data = JSON.parse(response.body, symbolize_names: true)
1919
expect(data).to have_key(:targets)
2020
end
21+
22+
it 'returns a 404 if project does not exist' do
23+
get '/api/scratch/projects/non_existent_project'
24+
25+
expect(response).to have_http_status(:not_found)
26+
end
27+
28+
it 'returns a 404 if project is not a scratch project' do
29+
project = create(:project, project_type: Project::Types::PYTHON, locale: 'en')
30+
31+
get "/api/scratch/projects/#{project.identifier}"
32+
33+
expect(response).to have_http_status(:not_found)
34+
end
2135
end

0 commit comments

Comments
 (0)