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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ErrorTracking::ProjectErrorTrackingSetting do
include ReactiveCachingHelpers
include Gitlab::Routing
let_it_be(:project) { create(:project) }
let(:sentry_client) { instance_double(ErrorTracking::SentryClient) }
let(:sentry_project_id) { 10 }
subject(:setting) { build(:project_error_tracking_setting, project: project, sentry_project_id: sentry_project_id) }
describe 'Associations' do
it { is_expected.to belong_to(:project) }
end
describe 'Validations' do
it { is_expected.to validate_length_of(:api_url).is_at_most(255) }
it { is_expected.to allow_value("http://gitlab.com/api/0/projects/project1/something").for(:api_url) }
it { is_expected.not_to allow_values("http://gitlab.com/api/0/projects/project1/something€").for(:api_url) }
it 'disallows non-booleans in enabled column' do
is_expected.not_to allow_value(
nil
).for(:enabled)
end
it 'allows booleans in enabled column' do
is_expected.to allow_value(
true,
false
).for(:enabled)
end
it 'rejects invalid api_urls' do
is_expected.not_to allow_values(
"https://replaceme.com/'><script>alert(document.cookie)</script>", # unsafe
"http://gitlab.com/project1/something", # missing api/0/projects
"http://gitlab.com/api/0/projects/org/proj/something", # extra segments
"http://gitlab.com/api/0/projects/org" # too few segments
).for(:api_url).with_message('is invalid')
end
it 'fails validation without org and project slugs' do
subject.api_url = 'http://gitlab.com/api/0/projects/'
expect(subject).not_to be_valid
expect(subject.errors.messages[:project]).to include('is a required field')
end
describe 'presence validations' do
using RSpec::Parameterized::TableSyntax
valid_api_url = 'http://example.com/api/0/projects/org-slug/proj-slug/'
valid_token = 'token'
where(:enabled, :integrated, :token, :api_url, :valid?) do
true | true | nil | nil | true
true | false | nil | nil | false
true | false | nil | valid_api_url | false
true | false | valid_token | nil | false
true | false | valid_token | valid_api_url | true
false | false | nil | nil | true
false | false | nil | valid_api_url | true
false | false | valid_token | nil | true
false | false | valid_token | valid_api_url | true
end
with_them do
before do
subject.enabled = enabled
subject.integrated = integrated
subject.token = token
subject.api_url = api_url
end
it { expect(subject.valid?).to eq(valid?) }
end
end
end
describe 'Callbacks' do
describe 'after_save :create_client_key!' do
subject { build(:project_error_tracking_setting, :integrated, project: project) }
context 'without client key' do
it 'creates a new client key' do
expect { subject.save! }.to change { ErrorTracking::ClientKey.count }.by(1)
end
context 'with sentry backend' do
before do
subject.integrated = false
end
it 'does not create a new client key' do
expect { subject.save! }.not_to change { ErrorTracking::ClientKey.count }
end
end
context 'when feature disabled' do
before do
subject.enabled = false
end
it 'does not create a new client key' do
expect { subject.save! }.not_to change { ErrorTracking::ClientKey.count }
end
end
end
context 'when client key already exists' do
let!(:client_key) { create(:error_tracking_client_key, project: project) }
it 'does not create a new client key' do
expect { subject.save! }.not_to change { ErrorTracking::ClientKey.count }
end
end
end
end
describe 'before_validation :reset_token' do
context 'when a token was previously set' do
subject { create(:project_error_tracking_setting, project: project) }
it 'resets token if url changed' do
subject.api_url = 'http://sentry.com/api/0/projects/org-slug/proj-slug/'
expect(subject).not_to be_valid
expect(subject.token).to be_nil
end
it "does not reset token if new url is set together with the same token" do
subject.api_url = 'http://sentrytest.com/api/0/projects/org-slug/proj-slug/'
current_token = subject.token
subject.token = current_token
expect(subject).to be_valid
expect(subject.token).to eq(current_token)
expect(subject.api_url).to eq('http://sentrytest.com/api/0/projects/org-slug/proj-slug/')
end
it 'does not reset token if new url is set together with a new token' do
subject.api_url = 'http://sentrytest.com/api/0/projects/org-slug/proj-slug/'
subject.token = 'token'
expect(subject).to be_valid
expect(subject.token).to eq('token')
expect(subject.api_url).to eq('http://sentrytest.com/api/0/projects/org-slug/proj-slug/')
end
end
end
describe '.extract_sentry_external_url' do
subject { described_class.extract_sentry_external_url(sentry_url) }
context 'when passing a URL' do
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
it { is_expected.to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project') }
end
context 'when passing nil' do
let(:sentry_url) { nil }
it { is_expected.to be_nil }
end
end
describe '#sentry_external_url' do
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
before do
subject.api_url = sentry_url
end
it 'returns the correct url' do
expect(subject.class).to receive(:extract_sentry_external_url).with(sentry_url).and_call_original
result = subject.sentry_external_url
expect(result).to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project')
end
end
describe '#sentry_client' do
it 'returns sentry client' do
expect(subject.sentry_client).to be_a(ErrorTracking::SentryClient)
end
end
describe '#list_sentry_issues' do
let(:issues) { [:list, :of, :issues] }
let(:result) { subject.list_sentry_issues(**opts) }
let(:opts) { { issue_status: 'unresolved', limit: 10 } }
context 'when cached' do
before do
stub_reactive_cache(subject, issues, opts)
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
end
it 'returns cached issues' do
expect(sentry_client).to receive(:list_issues).with(opts)
.and_return(issues: issues, pagination: {})
expect(result).to eq(issues: issues, pagination: {})
end
end
context 'when not cached' do
it 'returns nil' do
expect(subject).not_to receive(:sentry_client)
expect(result).to be_nil
end
end
context 'when sentry client raises ErrorTracking::SentryClient::Error' do
before do
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(ErrorTracking::SentryClient::Error, 'error message')
end
it 'returns error' do
expect(result).to eq(
error: 'error message',
error_type: ErrorTracking::ProjectErrorTrackingSetting::SENTRY_API_ERROR_TYPE_NON_20X_RESPONSE
)
end
end
context 'when sentry client raises ErrorTracking::SentryClient::MissingKeysError' do
before do
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(ErrorTracking::SentryClient::MissingKeysError,
'Sentry API response is missing keys. key not found: "id"')
end
it 'returns error' do
expect(result).to eq(
error: 'Sentry API response is missing keys. key not found: "id"',
error_type: ErrorTracking::ProjectErrorTrackingSetting::SENTRY_API_ERROR_TYPE_MISSING_KEYS
)
end
end
context 'when sentry client raises ErrorTracking::SentryClient::ResponseInvalidSizeError' do
let(:error_msg) { "Sentry API response is too big. Limit is #{Gitlab::Utils::DeepSize.human_default_max_size}." }
before do
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts)
.and_raise(ErrorTracking::SentryClient::ResponseInvalidSizeError, error_msg)
end
it 'returns error' do
expect(result).to eq(
error: error_msg,
error_type: ErrorTracking::ProjectErrorTrackingSetting::SENTRY_API_ERROR_INVALID_SIZE
)
end
end
context 'when sentry client raises StandardError' do
before do
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:list_issues).with(opts).and_raise(StandardError)
end
it 'returns error' do
expect(result).to eq(error: 'Unexpected Error')
end
end
end
describe '#list_sentry_projects' do
let(:projects) { [:list, :of, :projects] }
it 'calls sentry client' do
expect(subject).to receive(:sentry_client).and_return(sentry_client)
expect(sentry_client).to receive(:projects).and_return(projects)
result = subject.list_sentry_projects
expect(result).to eq(projects: projects)
end
end
describe '#issue_details' do
let(:issue) { build(:error_tracking_sentry_detailed_error, project_id: sentry_project_id) }
let(:commit_id) { issue.first_release_version }
let(:result) { subject.issue_details(opts) }
let(:opts) { { issue_id: 1 } }
context 'when cached' do
before do
stub_reactive_cache(subject, issue, {})
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:issue_details).with(opts).and_return(issue)
end
it { expect(result).to eq(issue: issue) }
it { expect(result[:issue].first_release_version).to eq(commit_id) }
it { expect(result[:issue].gitlab_commit).to eq(nil) }
it { expect(result[:issue].gitlab_commit_path).to eq(nil) }
context 'when release version is nil' do
before do
issue.first_release_version = nil
end
it { expect(result[:issue].gitlab_commit).to eq(nil) }
it { expect(result[:issue].gitlab_commit_path).to eq(nil) }
end
context 'when repo commit matches first relase version' do
let(:commit) { instance_double(Commit, id: commit_id) }
let(:repository) { instance_double(Repository, commit: commit) }
before do
allow(project).to receive(:repository).and_return(repository)
end
it { expect(result[:issue].gitlab_commit).to eq(commit_id) }
it { expect(result[:issue].gitlab_commit_path).to eq(project_commit_path(project, commit_id)) }
end
end
context 'when not cached' do
it { expect(subject).not_to receive(:sentry_client) }
it { expect(result).to be_nil }
end
end
describe '#issue_latest_event' do
let(:error_event) { build(:error_tracking_sentry_error_event, project_id: sentry_project_id) }
let(:result) { subject.issue_latest_event(opts) }
let(:opts) { { issue_id: 1 } }
before do
stub_reactive_cache(subject, error_event, {})
synchronous_reactive_cache(subject)
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:issue_latest_event).with(opts).and_return(error_event)
end
it 'returns the error event' do
expect(result[:latest_event].project_id).to eq(sentry_project_id)
end
end
describe '#update_issue' do
let(:result) { subject.update_issue(**opts) }
let(:opts) { { issue_id: 1, params: {} } }
before do
allow(subject).to receive(:sentry_client).and_return(sentry_client)
allow(sentry_client).to receive(:issue_details)
.with({ issue_id: 1 })
.and_return(Gitlab::ErrorTracking::DetailedError.new(project_id: sentry_project_id))
end
context 'when sentry response is successful' do
before do
allow(sentry_client).to receive(:update_issue).with(opts).and_return(true)
end
it 'returns the successful response' do
expect(result).to eq(updated: true)
end
end
context 'when sentry raises an error' do
before do
allow(sentry_client).to receive(:update_issue).with(opts).and_raise(StandardError)
end
it 'returns the successful response' do
expect(result).to eq(error: 'Unexpected Error')
end
end
context 'when sentry_project_id is not set' do
let(:sentry_projects) do
[
Gitlab::ErrorTracking::Project.new(
id: 1111,
name: 'Some Project',
organization_name: 'Org'
),
Gitlab::ErrorTracking::Project.new(
id: sentry_project_id,
name: setting.project_name,
organization_name: setting.organization_name
)
]
end
context 'when sentry_project_id is not set' do
before do
setting.update!(sentry_project_id: nil)
allow(sentry_client).to receive(:projects).and_return(sentry_projects)
allow(sentry_client).to receive(:update_issue).with(opts).and_return(true)
end
it 'tries to backfill it from sentry API' do
expect(result).to eq(updated: true)
expect(setting.reload.sentry_project_id).to eq(sentry_project_id)
end
context 'when the project cannot be found on sentry' do
before do
sentry_projects.pop
end
it 'raises error' do
expect { result }.to raise_error(/Couldn't find project/)
end
end
end
context 'when mismatching sentry_project_id is detected' do
it 'raises error' do
setting.update!(sentry_project_id: sentry_project_id + 1)
expect { result }.to raise_error(/The Sentry issue appers to be outside/)
end
end
end
end
describe 'slugs' do
shared_examples_for 'slug from api_url' do |method, slug|
context 'when api_url is correct' do
before do
subject.api_url = 'http://gitlab.com/api/0/projects/org-slug/project-slug/'
end
it 'returns slug' do
expect(subject.public_send(method)).to eq(slug)
end
end
context 'when api_url is blank' do
before do
subject.api_url = nil
end
it 'returns nil' do
expect(subject.public_send(method)).to be_nil
end
end
end
it_behaves_like 'slug from api_url', :project_slug, 'project-slug'
it_behaves_like 'slug from api_url', :organization_slug, 'org-slug'
end
describe 'names from api_url' do
shared_examples_for 'name from api_url' do |name, titleized_slug|
context 'when name is present in DB' do
it 'returns name from DB' do
subject[name] = 'Sentry name'
subject.api_url = 'http://gitlab.com/api/0/projects/org-slug/project-slug'
expect(subject.public_send(name)).to eq('Sentry name')
end
end
context 'when name is null in DB' do
it 'titleizes and returns slug from api_url' do
subject[name] = nil
subject.api_url = 'http://gitlab.com/api/0/projects/org-slug/project-slug'
expect(subject.public_send(name)).to eq(titleized_slug)
end
it 'returns nil when api_url is incorrect' do
subject[name] = nil
subject.api_url = 'http://gitlab.com/api/0/projects/'
expect(subject.public_send(name)).to be_nil
end
it 'returns nil when api_url is blank' do
subject[name] = nil
subject.api_url = nil
expect(subject.public_send(name)).to be_nil
end
end
end
it_behaves_like 'name from api_url', :organization_name, 'Org Slug'
it_behaves_like 'name from api_url', :project_name, 'Project Slug'
end
describe '.build_api_url_from' do
it 'correctly builds api_url with slugs' do
api_url = described_class.build_api_url_from(
api_host: 'http://sentry.com/',
organization_slug: 'org-slug',
project_slug: 'proj-slug'
)
expect(api_url).to eq('http://sentry.com/api/0/projects/org-slug/proj-slug/')
end
it 'correctly builds api_url without slugs' do
api_url = described_class.build_api_url_from(
api_host: 'http://sentry.com/',
organization_slug: nil,
project_slug: nil
)
expect(api_url).to eq('http://sentry.com/api/0/projects/')
end
it 'does not raise exception with invalid url' do
api_url = described_class.build_api_url_from(
api_host: ':::',
organization_slug: 'org-slug',
project_slug: 'proj-slug'
)
expect(api_url).to eq(':::')
end
it 'returns nil when api_host is blank' do
api_url = described_class.build_api_url_from(
api_host: '',
organization_slug: 'org-slug',
project_slug: 'proj-slug'
)
expect(api_url).to be_nil
end
end
describe '#api_host' do
context 'when api_url exists' do
before do
subject.api_url = 'https://example.com/api/0/projects/org-slug/proj-slug/'
end
it 'extracts the api_host from api_url' do
expect(subject.api_host).to eq('https://example.com/')
end
end
context 'when api_url is nil' do
before do
subject.api_url = nil
end
it 'returns nil' do
expect(subject.api_url).to eq(nil)
end
end
end
describe '#expire_issues_cache', :use_clean_rails_redis_caching do
let(:issues) { [:some, :issues] }
let(:opt) { 'list_issues' }
let(:params) { { issue_status: 'unresolved', limit: 20, sort: 'last_seen' } }
before do
start_reactive_cache_lifetime(subject, opt, params.stringify_keys)
stub_reactive_cache(subject, issues, opt, params.stringify_keys)
end
it 'clears the cache' do
expect(subject.list_sentry_issues(params)).to eq(issues)
subject.expire_issues_cache
expect(subject.list_sentry_issues(params)).to eq(nil)
end
end
describe '#sentry_enabled' do
using RSpec::Parameterized::TableSyntax
where(:enabled, :integrated, :sentry_enabled) do
true | false | true
true | true | false
true | true | false
false | false | false
end
with_them do
before do
subject.enabled = enabled
subject.integrated = integrated
end
it { expect(subject.sentry_enabled).to eq(sentry_enabled) }
end
end
describe '#integrated_enabled?' do
using RSpec::Parameterized::TableSyntax
where(:enabled, :integrated, :integrated_enabled) do
true | false | false
false | true | false
true | true | true
end
with_them do
before do
subject.enabled = enabled
subject.integrated = integrated
end
it { expect(subject.integrated_enabled?).to eq(integrated_enabled) }
end
end
describe '#gitlab_dsn' do
let!(:client_key) { create(:error_tracking_client_key, project: project) }
it { expect(subject.gitlab_dsn).to eq(client_key.sentry_dsn) }
end
end
|