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
|
# frozen_string_literal: true
RSpec.shared_examples 'issuable link creation' do
describe '#execute' do
subject { described_class.new(issuable, user, params).execute }
context 'when the reference list is empty' do
let(:params) do
{ issuable_references: [] }
end
it 'returns error' do
is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
end
end
context 'when Issuable not found' do
let(:params) do
{ issuable_references: ["##{non_existing_record_iid}"] }
end
it 'returns error' do
is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
end
it 'no relationship is created' do
expect { subject }.not_to change { issuable_link_class.count }
end
end
context 'when user has no permission to target issuable' do
let(:params) do
{ issuable_references: [restricted_issuable.to_reference(issuable_parent)] }
end
it 'returns error' do
if issuable_type == :issue
is_expected.to eq(message: "Couldn't link #{issuable_type}. You must have at least the Reporter role in both projects.", status: :error, http_status: 403)
else
is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
end
end
it 'no relationship is created' do
expect { subject }.not_to change { issuable_link_class.count }
end
end
context 'source and target are the same issuable' do
let(:params) do
{ issuable_references: [issuable.to_reference] }
end
it 'does not create notes' do
expect(SystemNoteService).not_to receive(:relate_issuable)
subject
end
it 'no relationship is created' do
expect { subject }.not_to change { issuable_link_class.count }
end
end
context 'when there is an issuable to relate' do
let(:params) do
{ issuable_references: [issuable2.to_reference, issuable3.to_reference(issuable_parent)] }
end
it 'creates relationships' do
expect { subject }.to change { issuable_link_class.count }.by(2)
expect(issuable_link_class.find_by!(target: issuable2)).to have_attributes(source: issuable, link_type: 'relates_to')
expect(issuable_link_class.find_by!(target: issuable3)).to have_attributes(source: issuable, link_type: 'relates_to')
end
it 'returns success status and created links', :aggregate_failures do
expect(subject.keys).to match_array([:status, :created_references])
expect(subject[:status]).to eq(:success)
expect(subject[:created_references].map(&:target_id)).to match_array([issuable2.id, issuable3.id])
end
it 'creates notes' do
# First two-way relation notes
expect(SystemNoteService).to receive(:relate_issuable)
.with(issuable, issuable2, user)
expect(SystemNoteService).to receive(:relate_issuable)
.with(issuable2, issuable, user)
# Second two-way relation notes
expect(SystemNoteService).to receive(:relate_issuable)
.with(issuable, issuable3, user)
expect(SystemNoteService).to receive(:relate_issuable)
.with(issuable3, issuable, user)
subject
end
end
context 'when reference of any already related issue is present' do
let(:params) do
{
issuable_references: [
issuable_a.to_reference,
issuable_b.to_reference
],
link_type: IssueLink::TYPE_RELATES_TO
}
end
it 'creates notes only for new relations' do
expect(SystemNoteService).to receive(:relate_issuable).with(issuable, issuable_a, anything)
expect(SystemNoteService).to receive(:relate_issuable).with(issuable_a, issuable, anything)
expect(SystemNoteService).not_to receive(:relate_issuable).with(issuable, issuable_b, anything)
expect(SystemNoteService).not_to receive(:relate_issuable).with(issuable_b, issuable, anything)
subject
end
end
context 'when there are invalid references' do
let(:params) do
{ issuable_references: [issuable.to_reference, issuable_a.to_reference] }
end
it 'creates links only for valid references' do
expect { subject }.to change { issuable_link_class.count }.by(1)
end
it 'returns error status' do
expect(subject).to eq(
status: :error,
http_status: 422,
message: "#{issuable.to_reference} cannot be added: cannot be related to itself"
)
end
end
end
end
|