Skip to content

Commit 971e52f

Browse files
committed
Allow creating & showing remix for scratch project
The extends the `project_type` param mechanism introduced in #513 into the `Api::Projects::RemixesController` so that the experience-cs app [1] can create a remix of a Scratch project and then load the remix. See this pull request [2] for more details. [1]: https://github.com/RaspberryPiFoundation/experience-cs [2]: RaspberryPiFoundation/experience-cs#290
1 parent d635cef commit 971e52f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

app/controllers/api/projects/remixes_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ def create
3636
private
3737

3838
def project
39-
@project ||= Project.find_by!(identifier: params[:project_id])
39+
@project ||= project_scope.find_by!(identifier: params[:project_id])
4040
end
4141

4242
def load_and_authorize_remix
43-
@project = Project.find_by!(remixed_from_id: project.id, user_id: current_user&.id)
43+
@project = project_scope.find_by!(remixed_from_id: project.id, user_id: current_user&.id)
4444
authorize! :show, @project
4545
end
4646

47+
def project_scope
48+
Project.only_scratch(params[:project_type] == Project::Types::SCRATCH)
49+
end
50+
4751
def remix_params
4852
params.require(:project)
4953
.permit(:name,

spec/requests/projects/remix_spec.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require 'rails_helper'
44

55
RSpec.describe 'Remix requests' do
6-
let!(:original_project) { create(:project) }
6+
let!(:original_project) { create(:project, project_type: original_project_type) }
7+
let(:original_project_type) { Project::Types::PYTHON }
78
let(:project_params) do
89
{
910
name: original_project.name,
@@ -53,8 +54,10 @@
5354
end
5455

5556
describe '#show' do
57+
let(:project_type) { Project::Types::PYTHON }
58+
5659
before do
57-
create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id)
60+
create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id, project_type:)
5861
end
5962

6063
it 'returns success response' do
@@ -68,6 +71,23 @@
6871

6972
expect(response).to have_http_status(:not_found)
7073
end
74+
75+
context 'when original project and remix are scratch projects' do
76+
let(:original_project_type) { Project::Types::SCRATCH }
77+
let(:project_type) { Project::Types::SCRATCH }
78+
79+
it 'returns 404 response if scratch projects are not explicitly included' do
80+
get("/api/projects/#{original_project.identifier}/remix", headers:)
81+
82+
expect(response).to have_http_status(:not_found)
83+
end
84+
85+
it 'returns success response if scratch projects are explicitly included' do
86+
get("/api/projects/#{original_project.identifier}/remix?project_type=scratch", headers:)
87+
88+
expect(response).to have_http_status(:ok)
89+
end
90+
end
7191
end
7292

7393
describe '#create' do
@@ -104,6 +124,22 @@
104124
expect(response.body).to eq({ error: 'Something went wrong' }.to_json)
105125
end
106126
end
127+
128+
context 'when original project is scratch project' do
129+
let(:original_project_type) { Project::Types::SCRATCH }
130+
131+
it 'returns 404 response if scratch projects are not explicitly included' do
132+
post("/api/projects/#{original_project.identifier}/remix", params: { project: project_params }, headers:)
133+
134+
expect(response).to have_http_status(:not_found)
135+
end
136+
137+
it 'returns success response if scratch projects are explicitly included' do
138+
post("/api/projects/#{original_project.identifier}/remix?project_type=scratch", params: { project: project_params }, headers:)
139+
140+
expect(response).to have_http_status(:ok)
141+
end
142+
end
107143
end
108144
end
109145

0 commit comments

Comments
 (0)