debian-mirror-gitlab/spec/requests/projects/issue_links_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.5 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe Projects::IssueLinksController, feature_category: :team_planning do
2020-11-24 15:15:51 +05:30
let(:user) { create :user }
let(:project) { create(:project_empty_repo) }
let(:issue) { create :issue, project: project }
describe 'GET /*namespace_id/:project_id/issues/:issue_id/links' do
let(:issue_b) { create :issue, project: project }
let!(:issue_link) { create :issue_link, source: issue, target: issue_b }
before do
project.add_guest(user)
login_as user
end
it 'returns JSON response' do
list_service_response = IssueLinks::ListService.new(issue, user).execute
get namespace_project_issue_links_path(issue_links_params)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq(list_service_response.as_json)
end
2022-07-16 23:28:13 +05:30
context 'when linked issue is a task' do
let(:issue_b) { create :issue, :task, project: project }
2023-01-13 00:05:48 +05:30
it 'returns a work item path for the linked task using the iid in the path' do
2022-07-16 23:28:13 +05:30
get namespace_project_issue_links_path(issue_links_params)
expect(json_response.count).to eq(1)
2022-07-23 23:45:48 +05:30
expect(json_response.first).to include(
2023-07-09 08:55:56 +05:30
'path' => project_work_items_path(issue_b.project, issue_b.iid),
2022-07-23 23:45:48 +05:30
'type' => 'TASK'
)
2022-07-16 23:28:13 +05:30
end
end
2020-11-24 15:15:51 +05:30
end
describe 'POST /*namespace_id/:project_id/issues/:issue_id/links' do
let(:issue_b) { create :issue, project: project }
before do
project.add_role(user, user_role)
login_as user
end
context 'with success' do
let(:user_role) { :developer }
let(:issuable_references) { [issue_b.to_reference] }
it 'returns success JSON' do
post namespace_project_issue_links_path(issue_links_params(issuable_references: issuable_references))
list_service_response = IssueLinks::ListService.new(issue, user).execute
expect(response).to have_gitlab_http_status(:ok)
2023-07-09 08:55:56 +05:30
expect(json_response).to eq('message' => nil, 'issuables' => list_service_response.as_json)
2020-11-24 15:15:51 +05:30
end
end
context 'with failure' do
context 'when unauthorized' do
let(:user_role) { :guest }
let(:issuable_references) { [issue_b.to_reference] }
it 'returns 403' do
post namespace_project_issue_links_path(issue_links_params(issuable_references: issuable_references))
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when failing service result' do
let(:user_role) { :developer }
let(:issuable_references) { ["##{non_existing_record_iid}"] }
it 'returns failure JSON' do
post namespace_project_issue_links_path(issue_links_params(issuable_references: issuable_references))
list_service_response = IssueLinks::ListService.new(issue, user).execute
expect(response).to have_gitlab_http_status(:not_found)
2021-04-29 21:17:54 +05:30
expect(json_response).to eq('message' => 'No matching issue found. Make sure that you are adding a valid issue URL.', 'issuables' => list_service_response.as_json)
2020-11-24 15:15:51 +05:30
end
end
end
end
describe 'DELETE /*namespace_id/:project_id/issues/:issue_id/link/:id' do
let(:issue_link) { create :issue_link, source: issue, target: referenced_issue }
before do
project.add_role(user, user_role)
login_as user
end
context 'when unauthorized' do
context 'when no authorization on current project' do
let(:referenced_issue) { create :issue, project: project }
let(:user_role) { :guest }
it 'returns 403' do
delete namespace_project_issue_link_path(issue_links_params(id: issue_link.id))
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when no authorization on the related issue project' do
# unauthorized project issue
let(:referenced_issue) { create :issue }
let(:user_role) { :developer }
it 'returns 404' do
delete namespace_project_issue_link_path(issue_links_params(id: issue_link.id))
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'when authorized' do
let(:referenced_issue) { create :issue, project: project }
let(:user_role) { :developer }
it 'returns success JSON' do
delete namespace_project_issue_link_path(issue_links_params(id: issue_link.id))
list_service_response = IssueLinks::ListService.new(issue, user).execute
expect(json_response).to eq('issuables' => list_service_response.as_json)
end
end
context 'when non of issues of the link is not the issue requested in the path' do
let(:referenced_issue) { create(:issue, project: project) }
let(:another_issue) { create(:issue, project: project) }
let(:issue) { create(:issue, project: project) }
let(:user_role) { :developer }
let!(:issue_link) { create :issue_link, source: another_issue, target: referenced_issue }
subject do
delete namespace_project_issue_link_path(issue_links_params(id: issue_link.id))
end
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
it 'does not delete the link' do
expect { subject }.not_to change { IssueLink.count }.from(1)
end
end
end
def issue_links_params(opts = {})
2023-07-09 08:55:56 +05:30
opts.reverse_merge(namespace_id: issue.project.namespace, project_id: issue.project, issue_id: issue, format: :json)
2020-11-24 15:15:51 +05:30
end
end