diff options
Diffstat (limited to 'spec/requests/api/issues/post_projects_issues_spec.rb')
-rw-r--r-- | spec/requests/api/issues/post_projects_issues_spec.rb | 157 |
1 files changed, 134 insertions, 23 deletions
diff --git a/spec/requests/api/issues/post_projects_issues_spec.rb b/spec/requests/api/issues/post_projects_issues_spec.rb index 9d3bd26a200..82692366589 100644 --- a/spec/requests/api/issues/post_projects_issues_spec.rb +++ b/spec/requests/api/issues/post_projects_issues_spec.rb @@ -8,15 +8,15 @@ RSpec.describe API::Issues do create(:project, :public, creator_id: user.id, namespace: user.namespace) end - let(:user2) { create(:user) } - let(:non_member) { create(:user) } - let_it_be(:guest) { create(:user) } - let_it_be(:author) { create(:author) } - let_it_be(:assignee) { create(:assignee) } - let(:admin) { create(:user, :admin) } - let(:issue_title) { 'foo' } - let(:issue_description) { 'closed' } - let!(:closed_issue) do + let_it_be(:user2) { create(:user) } + let_it_be(:non_member) { create(:user) } + let_it_be(:guest) { create(:user) } + let_it_be(:author) { create(:author) } + let_it_be(:milestone) { create(:milestone, title: '1.0.0', project: project) } + let_it_be(:assignee) { create(:assignee) } + let_it_be(:admin) { create(:user, :admin) } + + let_it_be(:closed_issue) do create :closed_issue, author: user, assignees: [user], @@ -28,7 +28,7 @@ RSpec.describe API::Issues do closed_at: 1.hour.ago end - let!(:confidential_issue) do + let_it_be(:confidential_issue) do create :issue, :confidential, project: project, @@ -38,7 +38,7 @@ RSpec.describe API::Issues do updated_at: 2.hours.ago end - let!(:issue) do + let_it_be(:issue) do create :issue, author: user, assignees: [user], @@ -46,22 +46,21 @@ RSpec.describe API::Issues do milestone: milestone, created_at: generate(:past_time), updated_at: 1.hour.ago, - title: issue_title, - description: issue_description + title: 'foo', + description: 'closed' end + let_it_be(:note) { create(:note_on_issue, author: user, project: project, noteable: issue) } + let_it_be(:label) do create(:label, title: 'label', color: '#FFAABB', project: project) end let!(:label_link) { create(:label_link, label: label, target: issue) } - let(:milestone) { create(:milestone, title: '1.0.0', project: project) } let_it_be(:empty_milestone) do create(:milestone, title: '2.0.0', project: project) end - let!(:note) { create(:note_on_issue, author: user, project: project, noteable: issue) } - let(:no_milestone_title) { 'None' } let(:any_milestone_title) { 'Any' } @@ -400,16 +399,15 @@ RSpec.describe API::Issues do end context 'when request exceeds the rate limit' do - before do + it 'prevents users from creating more issues' do allow(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true) - end - it 'prevents users from creating more issues' do post api("/projects/#{project.id}/issues", user), params: { title: 'new issue', labels: 'label, label2', weight: 3, assignee_ids: [user2.id] } - expect(response).to have_gitlab_http_status(:too_many_requests) expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.') + + expect(response).to have_gitlab_http_status(:too_many_requests) end end end @@ -517,7 +515,7 @@ RSpec.describe API::Issues do end context 'when using the issue ID instead of iid' do - it 'returns 404 when trying to move an issue' do + it 'returns 404 when trying to move an issue', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/341520' do post api("/projects/#{project.id}/issues/#{issue.id}/move", user), params: { to_project_id: target_project.id } @@ -556,6 +554,114 @@ RSpec.describe API::Issues do end end + describe '/projects/:id/issues/:issue_iid/clone' do + let_it_be(:valid_target_project) { create(:project) } + let_it_be(:invalid_target_project) { create(:project) } + + before_all do + valid_target_project.add_maintainer(user) + end + + context 'when user can admin the issue' do + context 'when the user can admin the target project' do + it 'clones the issue' do + expect do + post_clone_issue(user, issue, valid_target_project) + end.to change { valid_target_project.issues.count }.by(1) + + cloned_issue = Issue.last + + expect(cloned_issue.notes.count).to eq(2) + expect(cloned_issue.notes.pluck(:note)).not_to include(issue.notes.first.note) + expect(response).to have_gitlab_http_status(:created) + expect(json_response['id']).to eq(cloned_issue.id) + expect(json_response['project_id']).to eq(valid_target_project.id) + end + + context 'when target project is the same source project' do + it 'clones the issue' do + expect do + post_clone_issue(user, issue, issue.project) + end.to change { issue.reset.project.issues.count }.by(1) + + cloned_issue = Issue.last + + expect(cloned_issue.notes.count).to eq(2) + expect(cloned_issue.notes.pluck(:note)).not_to include(issue.notes.first.note) + expect(response).to have_gitlab_http_status(:created) + expect(json_response['id']).to eq(cloned_issue.id) + expect(json_response['project_id']).to eq(issue.project.id) + end + end + end + end + + context 'when the user does not have the permission to clone issues' do + it 'returns 400' do + post api("/projects/#{project.id}/issues/#{issue.iid}/clone", user), + params: { to_project_id: invalid_target_project.id } + + expect(response).to have_gitlab_http_status(:bad_request) + expect(json_response['message']).to eq(s_('CloneIssue|Cannot clone issue due to insufficient permissions!')) + end + end + + context 'when using the issue ID instead of iid' do + it 'returns 404', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/341520' do + post api("/projects/#{project.id}/issues/#{issue.id}/clone", user), + params: { to_project_id: valid_target_project.id } + + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq('404 Issue Not Found') + end + end + + context 'when issue does not exist' do + it 'returns 404' do + post api("/projects/#{project.id}/issues/12300/clone", user), + params: { to_project_id: valid_target_project.id } + + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq('404 Issue Not Found') + end + end + + context 'when source project does not exist' do + it 'returns 404' do + post api("/projects/0/issues/#{issue.iid}/clone", user), + params: { to_project_id: valid_target_project.id } + + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq('404 Project Not Found') + end + end + + context 'when target project does not exist' do + it 'returns 404' do + post api("/projects/#{project.id}/issues/#{issue.iid}/clone", user), + params: { to_project_id: 0 } + + expect(response).to have_gitlab_http_status(:not_found) + expect(json_response['message']).to eq('404 Project Not Found') + end + end + + it 'clones the issue with notes when with_notes is true' do + expect do + post api("/projects/#{project.id}/issues/#{issue.iid}/clone", user), + params: { to_project_id: valid_target_project.id, with_notes: true } + end.to change { valid_target_project.issues.count }.by(1) + + cloned_issue = Issue.last + + expect(cloned_issue.notes.count).to eq(3) + expect(cloned_issue.notes.pluck(:note)).to include(issue.notes.first.note) + expect(response).to have_gitlab_http_status(:created) + expect(json_response['id']).to eq(cloned_issue.id) + expect(json_response['project_id']).to eq(valid_target_project.id) + end + end + describe 'POST :id/issues/:issue_iid/subscribe' do it 'subscribes to an issue' do post api("/projects/#{project.id}/issues/#{issue.iid}/subscribe", user2) @@ -576,7 +682,7 @@ RSpec.describe API::Issues do expect(response).to have_gitlab_http_status(:not_found) end - it 'returns 404 if the issue ID is used instead of the iid' do + it 'returns 404 if the issue ID is used instead of the iid', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/341520' do post api("/projects/#{project.id}/issues/#{issue.id}/subscribe", user) expect(response).to have_gitlab_http_status(:not_found) @@ -609,7 +715,7 @@ RSpec.describe API::Issues do expect(response).to have_gitlab_http_status(:not_found) end - it 'returns 404 if using the issue ID instead of iid' do + it 'returns 404 if using the issue ID instead of iid', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/341520' do post api("/projects/#{project.id}/issues/#{issue.id}/unsubscribe", user) expect(response).to have_gitlab_http_status(:not_found) @@ -621,4 +727,9 @@ RSpec.describe API::Issues do expect(response).to have_gitlab_http_status(:not_found) end end + + def post_clone_issue(current_user, issue, target_project) + post api("/projects/#{issue.project.id}/issues/#{issue.iid}/clone", current_user), + params: { to_project_id: target_project.id } + end end |