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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::MigrateIssueTrackersSensitiveData, schema: 20200130145430 do
let(:services) { table(:services) }
before do
# we need to define the classes due to encryption
issue_tracker_data = Class.new(ApplicationRecord) do
self.table_name = 'issue_tracker_data'
def self.encryption_options
{
key: Settings.attr_encrypted_db_key_base_32,
encode: true,
mode: :per_attribute_iv,
algorithm: 'aes-256-gcm'
}
end
attr_encrypted :project_url, encryption_options
attr_encrypted :issues_url, encryption_options
attr_encrypted :new_issue_url, encryption_options
end
jira_tracker_data = Class.new(ApplicationRecord) do
self.table_name = 'jira_tracker_data'
def self.encryption_options
{
key: Settings.attr_encrypted_db_key_base_32,
encode: true,
mode: :per_attribute_iv,
algorithm: 'aes-256-gcm'
}
end
attr_encrypted :url, encryption_options
attr_encrypted :api_url, encryption_options
attr_encrypted :username, encryption_options
attr_encrypted :password, encryption_options
end
stub_const('IssueTrackerData', issue_tracker_data)
stub_const('JiraTrackerData', jira_tracker_data)
end
let(:url) { 'http://base-url.tracker.com' }
let(:new_issue_url) { 'http://base-url.tracker.com/new_issue' }
let(:issues_url) { 'http://base-url.tracker.com/issues' }
let(:api_url) { 'http://api.tracker.com' }
let(:password) { 'passw1234' }
let(:username) { 'user9' }
let(:title) { 'Issue tracker' }
let(:description) { 'Issue tracker description' }
let(:jira_properties) do
{
'api_url' => api_url,
'jira_issue_transition_id' => '5',
'password' => password,
'url' => url,
'username' => username,
'title' => title,
'description' => description,
'other_field' => 'something'
}
end
let(:tracker_properties) do
{
'project_url' => url,
'new_issue_url' => new_issue_url,
'issues_url' => issues_url,
'title' => title,
'description' => description,
'other_field' => 'something'
}
end
let(:tracker_properties_no_url) do
{
'new_issue_url' => new_issue_url,
'issues_url' => issues_url,
'title' => title,
'description' => description
}
end
subject { described_class.new.perform(1, 100) }
shared_examples 'handle properties' do
it 'does not clear the properties' do
expect { subject }.not_to change { service.reload.properties}
end
end
context 'with Jira service' do
let!(:service) do
services.create!(id: 10, type: 'JiraService', title: nil, properties: jira_properties.to_json, category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'migrates data' do
expect { subject }.to change { JiraTrackerData.count }.by(1)
service.reload
data = JiraTrackerData.find_by(service_id: service.id)
expect(data.url).to eq(url)
expect(data.api_url).to eq(api_url)
expect(data.username).to eq(username)
expect(data.password).to eq(password)
expect(service.title).to eq(title)
expect(service.description).to eq(description)
end
end
context 'with bugzilla service' do
let!(:service) do
services.create!(id: 11, type: 'BugzillaService', title: nil, properties: tracker_properties.to_json, category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'migrates data' do
expect { subject }.to change { IssueTrackerData.count }.by(1)
service.reload
data = IssueTrackerData.find_by(service_id: service.id)
expect(data.project_url).to eq(url)
expect(data.issues_url).to eq(issues_url)
expect(data.new_issue_url).to eq(new_issue_url)
expect(service.title).to eq(title)
expect(service.description).to eq(description)
end
end
context 'with youtrack service' do
let!(:service) do
services.create!(id: 12, type: 'YoutrackService', title: nil, properties: tracker_properties_no_url.to_json, category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'migrates data' do
expect { subject }.to change { IssueTrackerData.count }.by(1)
service.reload
data = IssueTrackerData.find_by(service_id: service.id)
expect(data.project_url).to be_nil
expect(data.issues_url).to eq(issues_url)
expect(data.new_issue_url).to eq(new_issue_url)
expect(service.title).to eq(title)
expect(service.description).to eq(description)
end
end
context 'with gitlab service with no properties' do
let!(:service) do
services.create!(id: 13, type: 'GitlabIssueTrackerService', title: nil, properties: {}, category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'does not migrate data' do
expect { subject }.not_to change { IssueTrackerData.count }
end
end
context 'with redmine service already with data fields' do
let!(:service) do
services.create!(id: 14, type: 'RedmineService', title: nil, properties: tracker_properties_no_url.to_json, category: 'issue_tracker').tap do |service|
IssueTrackerData.create!(service_id: service.id, project_url: url, new_issue_url: new_issue_url, issues_url: issues_url)
end
end
it_behaves_like 'handle properties'
it 'does not create new data fields record' do
expect { subject }.not_to change { IssueTrackerData.count }
end
end
context 'with custom issue tracker which has data fields record inconsistent with properties field' do
let!(:service) do
services.create!(id: 15, type: 'CustomIssueTrackerService', title: 'Existing title', properties: jira_properties.to_json, category: 'issue_tracker').tap do |service|
IssueTrackerData.create!(service_id: service.id, project_url: 'http://other_url', new_issue_url: 'http://other_url/new_issue', issues_url: 'http://other_url/issues')
end
end
it_behaves_like 'handle properties'
it 'does not update the data fields record' do
expect { subject }.not_to change { IssueTrackerData.count }
service.reload
data = IssueTrackerData.find_by(service_id: service.id)
expect(data.project_url).to eq('http://other_url')
expect(data.issues_url).to eq('http://other_url/issues')
expect(data.new_issue_url).to eq('http://other_url/new_issue')
expect(service.title).to eq('Existing title')
end
end
context 'with Jira service which has data fields record inconsistent with properties field' do
let!(:service) do
services.create!(id: 16, type: 'CustomIssueTrackerService', description: 'Existing description', properties: jira_properties.to_json, category: 'issue_tracker').tap do |service|
JiraTrackerData.create!(service_id: service.id, url: 'http://other_jira_url')
end
end
it_behaves_like 'handle properties'
it 'does not update the data fields record' do
expect { subject }.not_to change { JiraTrackerData.count }
service.reload
data = JiraTrackerData.find_by(service_id: service.id)
expect(data.url).to eq('http://other_jira_url')
expect(data.password).to be_nil
expect(data.username).to be_nil
expect(data.api_url).to be_nil
expect(service.description).to eq('Existing description')
end
end
context 'non issue tracker service' do
let!(:service) do
services.create!(id: 17, title: nil, description: nil, type: 'OtherService', properties: tracker_properties.to_json)
end
it_behaves_like 'handle properties'
it 'does not migrate any data' do
expect { subject }.not_to change { IssueTrackerData.count }
service.reload
expect(service.title).to be_nil
expect(service.description).to be_nil
end
end
context 'Jira service with empty properties' do
let!(:service) do
services.create!(id: 18, type: 'JiraService', properties: '', category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'does not migrate any data' do
expect { subject }.not_to change { JiraTrackerData.count }
end
end
context 'Jira service with nil properties' do
let!(:service) do
services.create!(id: 18, type: 'JiraService', properties: nil, category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'does not migrate any data' do
expect { subject }.not_to change { JiraTrackerData.count }
end
end
context 'Jira service with invalid properties' do
let!(:service) do
services.create!(id: 18, type: 'JiraService', properties: 'invalid data', category: 'issue_tracker')
end
it_behaves_like 'handle properties'
it 'does not migrate any data' do
expect { subject }.not_to change { JiraTrackerData.count }
end
end
context 'with Jira service with invalid properties, valid Jira service and valid bugzilla service' do
let!(:jira_integration_invalid) do
services.create!(id: 19, title: 'invalid - title', description: 'invalid - description', type: 'JiraService', properties: 'invalid data', category: 'issue_tracker')
end
let!(:jira_integration_valid) do
services.create!(id: 20, type: 'JiraService', properties: jira_properties.to_json, category: 'issue_tracker')
end
let!(:bugzilla_integration_valid) do
services.create!(id: 11, type: 'BugzillaService', title: nil, properties: tracker_properties.to_json, category: 'issue_tracker')
end
it 'migrates data for the valid service' do
subject
jira_integration_invalid.reload
expect(JiraTrackerData.find_by(service_id: jira_integration_invalid.id)).to be_nil
expect(jira_integration_invalid.title).to eq('invalid - title')
expect(jira_integration_invalid.description).to eq('invalid - description')
expect(jira_integration_invalid.properties).to eq('invalid data')
jira_integration_valid.reload
data = JiraTrackerData.find_by(service_id: jira_integration_valid.id)
expect(data.url).to eq(url)
expect(data.api_url).to eq(api_url)
expect(data.username).to eq(username)
expect(data.password).to eq(password)
expect(jira_integration_valid.title).to eq(title)
expect(jira_integration_valid.description).to eq(description)
bugzilla_integration_valid.reload
data = IssueTrackerData.find_by(service_id: bugzilla_integration_valid.id)
expect(data.project_url).to eq(url)
expect(data.issues_url).to eq(issues_url)
expect(data.new_issue_url).to eq(new_issue_url)
expect(bugzilla_integration_valid.title).to eq(title)
expect(bugzilla_integration_valid.description).to eq(description)
end
end
end
|