1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::IssueLinks, feature_category: :team_planning do
let_it_be(:non_member) { create(:user) }
let_it_be(:guest_user) { create(:user) }
let_it_be(:public_project) { create(:project, :public, guests: guest_user) }
let_it_be(:private_project) { create(:project, :private, guests: guest_user) }
let_it_be(:project) { public_project }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:target_issue) { create(:issue, project: project) }
describe 'GET /links' do
let_it_be(:issue_link) { create(:issue_link, source: issue, target: target_issue) }
def perform_request(user = nil, params = {})
get api("/projects/#{project.id}/issues/#{issue.iid}/links", user), params: params
end
context 'when unauthenticated' do
it 'returns 401' do
perform_request
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when authenticated' do
let_it_be(:target_issue2) { create(:issue, project: project) }
let_it_be(:issue_link2) { create(:issue_link, source: issue, target: target_issue2) }
it 'returns related issues' do
perform_request(guest_user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_an Array
expect(json_response.length).to eq(2)
expect(response).to match_response_schema('public_api/v4/related_issues')
end
it 'returns multiple links without N + 1' do
perform_request(guest_user)
control = ActiveRecord::QueryRecorder.new { perform_request(guest_user) }
create(:issue_link, source: issue, target: create(:issue, project: project))
expect { perform_request(guest_user) }.not_to exceed_query_limit(control)
end
end
end
describe 'POST /links' do
def perform_request(user = nil, params: {})
post api("/projects/#{project.id}/issues/#{issue.iid}/links", user), params: params
end
context 'when unauthenticated' do
it 'returns 401' do
perform_request(params: { target_project_id: target_issue.project.id, target_issue_iid: target_issue.iid })
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when authenticated' do
context 'given target project not found' do
it 'returns 404' do
perform_request(guest_user, params: { target_project_id: -1, target_issue_iid: target_issue.iid })
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Project Not Found')
end
end
context 'given target issue not found' do
it 'returns 404' do
perform_request(
guest_user,
params: { target_project_id: project.id, target_issue_iid: non_existing_record_iid }
)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Not found')
end
end
context 'when user does not have access to given issue' do
it 'returns 403' do
perform_request(non_member, params: { target_project_id: project.id, target_issue_iid: target_issue.iid })
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response['message'])
.to eq("Couldn't link issues. You must have at least the Guest role in both projects.")
end
end
context 'when trying to relate to a confidential issue' do
let_it_be(:target_issue) { create(:issue, :confidential, project: project) }
it 'returns 404' do
perform_request(guest_user, params: { target_project_id: project.id, target_issue_iid: target_issue.iid })
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Not found')
end
end
context 'when trying to relate to a private project issue' do
let_it_be(:project) { private_project }
it 'returns 404' do
perform_request(non_member, params: { target_project_id: project.id, target_issue_iid: target_issue.iid })
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Project Not Found')
end
end
context 'when user has ability to create an issue link' do
it 'returns 201 status and contains the expected link response' do
perform_request(
guest_user,
params: { target_project_id: project.id, target_issue_iid: target_issue.iid, link_type: 'relates_to' }
)
expect_link_response(link_type: 'relates_to')
expect(json_response['source_issue']['id']).to eq(issue.id)
expect(json_response['target_issue']['id']).to eq(target_issue.id)
end
it 'returns 201 when sending full path of target project' do
perform_request(
guest_user,
params: { target_project_id: project.full_path, target_issue_iid: target_issue.iid }
)
expect_link_response
end
def expect_link_response(link_type: 'relates_to')
expect(response).to have_gitlab_http_status(:created)
expect(response).to match_response_schema('public_api/v4/issue_link')
expect(json_response['link_type']).to eq(link_type)
end
end
end
end
describe 'GET /links/:issue_link_id' do
let_it_be(:issue_link) { create(:issue_link, source: issue, target: target_issue) }
def perform_request(issue_link_id, user = nil, params = {})
get api("/projects/#{project.id}/issues/#{issue.iid}/links/#{issue_link_id}", user), params: params
end
context 'when unauthenticated' do
context 'when accessing an issue of a private project' do
let_it_be(:project) { private_project }
it 'returns 401' do
perform_request(issue_link.id)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
# This isn't ideal, see https://gitlab.com/gitlab-org/gitlab/-/issues/364077
context 'when accessing an issue of a public project' do
it 'returns 401' do
perform_request(issue_link.id)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
context 'when authenticated' do
context 'when issue link does not exist' do
it 'returns 404' do
perform_request(non_existing_record_id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when issue link does not belong to the specified issue' do
it 'returns 404' do
other_issue = create(:issue, project: project)
# source is different than the given API route issue
other_link = create(:issue_link, source: other_issue, target: target_issue)
perform_request(other_link.id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when user has ability to read the issue link' do
it 'returns 200' do
perform_request(issue_link.id, guest_user)
aggregate_failures "testing response" do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('public_api/v4/issue_link')
end
end
end
context 'when user cannot read issue link' do
context 'when the issue link targets an issue in a non-accessible project' do
it 'returns 404' do
private_issue = create(:issue, project: create(:project, :private))
private_link = create(:issue_link, source: issue, target: private_issue)
perform_request(private_link.id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when issue link targets a non-accessible issue' do
it 'returns 404' do
confidential_issue = create(:issue, :confidential, project: public_project)
confidential_link = create(:issue_link, source: issue, target: confidential_issue)
perform_request(confidential_link.id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
end
describe 'DELETE /links/:issue_link_id' do
let_it_be(:issue_link) { create(:issue_link, source: issue, target: target_issue) }
def perform_request(issue_link_id, user = nil)
delete api("/projects/#{project.id}/issues/#{issue.iid}/links/#{issue_link_id}", user)
end
context 'when unauthenticated' do
it 'returns 401' do
perform_request(issue_link.id)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when authenticated' do
context 'when user does not have write access to given issue link' do
it 'returns 404' do
external_public_issue = create(:issue, project: create(:project, :public))
external_link = create(:issue_link, source: issue, target: external_public_issue)
perform_request(external_link.id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('No Issue Link found')
end
end
context 'issue link not found' do
it 'returns 404' do
perform_request(non_existing_record_id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Not found')
end
end
context 'when trying to delete a link with a private project issue' do
let_it_be(:project) { private_project }
it 'returns 404' do
perform_request(issue_link.id, non_member)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Project Not Found')
end
end
context 'when user has ability to delete the issue link' do
it 'returns 200' do
perform_request(issue_link.id, guest_user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('public_api/v4/issue_link')
end
it 'returns 404 when the issue link does not belong to the specified issue' do
other_issue = create(:issue, project: project)
other_link = create(:issue_link, source: other_issue, target: target_issue)
perform_request(other_link.id, guest_user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Not found')
end
end
end
end
end
|