Commit 1cd7e801 authored by Rajendra Kadam's avatar Rajendra Kadam Committed by Peter Leitzen

Make follow up fixes for enable error tracking api

parent 90fd52b0
...@@ -27,6 +27,8 @@ module ErrorTracking ...@@ -27,6 +27,8 @@ module ErrorTracking
validates :api_url, length: { maximum: 255 }, public_url: { enforce_sanitization: true, ascii_only: true }, allow_nil: true validates :api_url, length: { maximum: 255 }, public_url: { enforce_sanitization: true, ascii_only: true }, allow_nil: true
validates :enabled, inclusion: { in: [true, false] }
validates :api_url, presence: { message: 'is a required field' }, if: :enabled validates :api_url, presence: { message: 'is a required field' }, if: :enabled
validate :validate_api_url_path, if: :enabled validate :validate_api_url_path, if: :enabled
......
---
title: Refactor error tracking specs and add validation to enabled field in error tracking model
merge_request: 24892
author: Rajendra Kadam
type: added
...@@ -19,6 +19,19 @@ describe ErrorTracking::ProjectErrorTrackingSetting do ...@@ -19,6 +19,19 @@ describe ErrorTracking::ProjectErrorTrackingSetting do
it { is_expected.to allow_value("http://gitlab.com/api/0/projects/project1/something").for(:api_url) } 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 { 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 it 'rejects invalid api_urls' do
is_expected.not_to allow_values( is_expected.not_to allow_values(
"https://replaceme.com/'><script>alert(document.cookie)</script>", # unsafe "https://replaceme.com/'><script>alert(document.cookie)</script>", # unsafe
......
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
require 'spec_helper' require 'spec_helper'
describe API::ErrorTracking do describe API::ErrorTracking do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:setting) { create(:project_error_tracking_setting) } let(:setting) { create(:project_error_tracking_setting) }
let(:project) { setting.project } let(:project) { setting.project }
shared_examples 'returns project settings' do shared_examples 'returns project settings' do
it 'returns correct project settings' do it 'returns correct project settings' do
subject make_request
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq( expect(json_response).to eq(
...@@ -23,7 +23,7 @@ describe API::ErrorTracking do ...@@ -23,7 +23,7 @@ describe API::ErrorTracking do
shared_examples 'returns 404' do shared_examples 'returns 404' do
it 'returns correct project settings' do it 'returns correct project settings' do
subject make_request
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']) expect(json_response['message'])
...@@ -32,7 +32,9 @@ describe API::ErrorTracking do ...@@ -32,7 +32,9 @@ describe API::ErrorTracking do
end end
describe "PATCH /projects/:id/error_tracking/settings" do describe "PATCH /projects/:id/error_tracking/settings" do
def make_patch_request(**params) let(:params) { { active: false } }
def make_request
patch api("/projects/#{project.id}/error_tracking/settings", user), params: params patch api("/projects/#{project.id}/error_tracking/settings", user), params: params
end end
...@@ -42,26 +44,39 @@ describe API::ErrorTracking do ...@@ -42,26 +44,39 @@ describe API::ErrorTracking do
end end
context 'patch settings' do context 'patch settings' do
subject do it_behaves_like 'returns project settings'
make_patch_request(active: false)
it 'updates enabled flag' do
expect(setting).to be_enabled
make_request
expect(json_response).to include('active' => false)
expect(setting.reload).not_to be_enabled
end end
it_behaves_like 'returns project settings' context 'active is invalid' do
let(:params) { { active: "randomstring" } }
it 'returns active is invalid if non boolean' do it 'returns active is invalid if non boolean' do
make_patch_request(active: "randomstring") make_request
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']) expect(json_response['error'])
.to eq('active is invalid') .to eq('active is invalid')
end
end end
it 'returns 400 if active is empty' do context 'active is empty' do
make_patch_request(active: '') let(:params) { { active: '' } }
it 'returns 400' do
make_request
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']) expect(json_response['error'])
.to eq('active is empty') .to eq('active is empty')
end
end end
end end
...@@ -73,10 +88,6 @@ describe API::ErrorTracking do ...@@ -73,10 +88,6 @@ describe API::ErrorTracking do
end end
context 'patch settings' do context 'patch settings' do
subject do
make_patch_request(active: true)
end
it_behaves_like 'returns 404' it_behaves_like 'returns 404'
end end
end end
...@@ -87,10 +98,12 @@ describe API::ErrorTracking do ...@@ -87,10 +98,12 @@ describe API::ErrorTracking do
project.add_reporter(user) project.add_reporter(user)
end end
it 'returns 403 for update request' do context 'patch request' do
make_patch_request(active: true) it 'returns 403' do
make_request
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
end
end end
end end
...@@ -99,28 +112,34 @@ describe API::ErrorTracking do ...@@ -99,28 +112,34 @@ describe API::ErrorTracking do
project.add_developer(user) project.add_developer(user)
end end
it 'returns 403 for update request' do context 'patch request' do
make_patch_request(active: true) it 'returns 403' do
make_request
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
end
end end
end end
context 'when authenticated as non-member' do context 'when authenticated as non-member' do
it 'returns 404 for update request' do context 'patch request' do
make_patch_request(active: false) it 'returns 404' do
make_request
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end
end end
end end
context 'when unauthenticated' do context 'when unauthenticated' do
let(:user) { nil } let(:user) { nil }
it 'returns 401 for update request' do context 'patch request' do
make_patch_request(active: true) it 'returns 401 for update request' do
make_request
expect(response).to have_gitlab_http_status(:unauthorized) expect(response).to have_gitlab_http_status(:unauthorized)
end
end end
end end
end end
...@@ -136,10 +155,6 @@ describe API::ErrorTracking do ...@@ -136,10 +155,6 @@ describe API::ErrorTracking do
end end
context 'get settings' do context 'get settings' do
subject do
make_request
end
it_behaves_like 'returns project settings' it_behaves_like 'returns project settings'
end end
end end
...@@ -152,10 +167,6 @@ describe API::ErrorTracking do ...@@ -152,10 +167,6 @@ describe API::ErrorTracking do
end end
context 'get settings' do context 'get settings' do
subject do
make_request
end
it_behaves_like 'returns 404' it_behaves_like 'returns 404'
end end
end end
......
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