diff --git a/app/controllers/api/projects/remixes_controller.rb b/app/controllers/api/projects/remixes_controller.rb index 222279f7e..1df7a366a 100644 --- a/app/controllers/api/projects/remixes_controller.rb +++ b/app/controllers/api/projects/remixes_controller.rb @@ -44,7 +44,9 @@ def project end def load_and_authorize_remix - @project = Project.find_by!(remixed_from_id: project.id, user_id: current_user&.id) + @project = Project.where(remixed_from_id: project.id, user_id: current_user&.id) + .order(created_at: :desc, updated_at: :desc) + .first! authorize! :show, @project end diff --git a/spec/requests/projects/remix_spec.rb b/spec/requests/projects/remix_spec.rb index 1443deca5..0caefd1be 100644 --- a/spec/requests/projects/remix_spec.rb +++ b/spec/requests/projects/remix_spec.rb @@ -68,6 +68,24 @@ expect(response).to have_http_status(:not_found) end + + context 'when multiple remixes exist for the same user and project' do + before do + create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id, + created_at: 2.days.ago, updated_at: 2.days.ago) + end + + let!(:newer_remix) do + create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id, + created_at: 1.hour.from_now, updated_at: 1.hour.from_now) + end + + it 'returns the most recently created remix' do + get("/api/projects/#{original_project.identifier}/remix", headers:) + + expect(response.parsed_body['identifier']).to eq(newer_remix.identifier) + end + end end describe('#show_identifier') do @@ -97,6 +115,23 @@ get("/api/projects/#{original_project.identifier}/remix/identifier", headers:) expect(response).to have_http_status(:not_found) end + + context 'when multiple remixes exist for the same user and project' do + before do + create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id, + created_at: 2.days.ago, updated_at: 2.days.ago) + end + + let!(:newer_remix) do + create(:project, remixed_from_id: original_project.id, user_id: authenticated_user.id, + created_at: 1.hour.from_now, updated_at: 1.hour.from_now) + end + + it 'returns the identifier of the most recently created remix' do + get("/api/projects/#{original_project.identifier}/remix/identifier", headers:) + expect(response.parsed_body['identifier']).to eq(newer_remix.identifier) + end + end end describe '#create' do