Commit 02b1c3e9 authored by allison.browne's avatar allison.browne

Refactor error tracking spec

Use rspec validation matchers
parent 7fa306b9
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe ErrorTracking::ProjectErrorTrackingSetting do describe ErrorTracking::ProjectErrorTrackingSetting do
include ReactiveCachingHelpers include ReactiveCachingHelpers
set(:project) { create(:project) } let_it_be(:project) { create(:project) }
subject { create(:project_error_tracking_setting, project: project) } subject { create(:project_error_tracking_setting, project: project) }
...@@ -14,23 +14,23 @@ describe ErrorTracking::ProjectErrorTrackingSetting do ...@@ -14,23 +14,23 @@ describe ErrorTracking::ProjectErrorTrackingSetting do
end end
describe 'Validations' do describe 'Validations' do
context 'when api_url is over 255 chars' do it { is_expected.to validate_length_of(:api_url).is_at_most(255) }
before do it { is_expected.to allow_values("http://gitlab.com/api/0/projects/project1/something").for(:api_url) }
subject.api_url = 'https://' + 'a' * 250
end
it 'fails validation' do it 'rejects invalid api_urls' do
expect(subject).not_to be_valid is_expected.not_to allow_values(
expect(subject.errors.messages[:api_url]).to include('is too long (maximum is 255 characters)') "https://replaceme.com/'><script>alert(document.cookie)</script>", # unsafe
end "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 end
context 'With unsafe url' do it 'fails validation without org and project slugs' do
it 'fails validation' do subject.api_url = 'http://gitlab.com/api/0/projects/'
subject.api_url = "https://replaceme.com/'><script>alert(document.cookie)</script>"
expect(subject).not_to be_valid expect(subject).not_to be_valid
end expect(subject.errors.messages[:project]).to include('is a required field')
end end
context 'presence validations' do context 'presence validations' do
...@@ -60,52 +60,6 @@ describe ErrorTracking::ProjectErrorTrackingSetting do ...@@ -60,52 +60,6 @@ describe ErrorTracking::ProjectErrorTrackingSetting do
it { expect(subject.valid?).to eq(valid?) } it { expect(subject.valid?).to eq(valid?) }
end end
end end
context 'URL path' do
it 'fails validation without api/0/projects' do
subject.api_url = 'http://gitlab.com/project1/something'
expect(subject).not_to be_valid
expect(subject.errors.messages[:api_url]).to include('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
it 'fails validation when api_url has extra parts' do
subject.api_url = 'http://gitlab.com/api/0/projects/org/proj/something'
expect(subject).not_to be_valid
expect(subject.errors.messages[:api_url]).to include("is invalid")
end
it 'fails validation when api_url has less parts' do
subject.api_url = 'http://gitlab.com/api/0/projects/org'
expect(subject).not_to be_valid
expect(subject.errors.messages[:api_url]).to include("is invalid")
end
it 'passes validation with correct path' do
subject.api_url = 'http://gitlab.com/api/0/projects/project1/something'
expect(subject).to be_valid
end
end
context 'non ascii chars in api_url' do
before do
subject.api_url = 'http://gitlab.com/api/0/projects/project1/something€'
end
it 'fails validation' do
expect(subject).not_to be_valid
end
end
end end
describe '#sentry_external_url' do describe '#sentry_external_url' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment