Commit 3e7f967a authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add missing tests for alert payload fields

Add tests for `AlertPayloadField` and `ExtractAlertPayloadFieldsService`
parent 80bd2df4
......@@ -7,20 +7,24 @@ module AlertManagement
attr_accessor :project, :path, :label, :type
SUPPORTED_TYPES = %w[string numeric datetime].freeze
SUPPORTED_TYPES = %w[array datetime string].freeze
validates :project, presence: true
validates :label, presence: true
validates :type, inclusion: { in: SUPPORTED_TYPES }
validate :path_is_list_of_strings
validate :ensure_path_is_non_empty_list_of_strings
private
def path_is_list_of_strings
unless path.is_a?(Array) && path.all? { |segment| segment.is_a?(String) }
def ensure_path_is_non_empty_list_of_strings
return if path_is_non_empty_list_of_strings?
errors.add(:path, 'must be a list of strings')
end
def path_is_non_empty_list_of_strings?
path.is_a?(Array) && !path.empty? && path.all? { |segment| segment.is_a?(String) }
end
end
end
......@@ -38,19 +38,11 @@ module AlertManagement
end
def available?
feature_enabled? && license_available?
::Gitlab::AlertManagement.custom_mapping_available?(project)
end
def allowed?
current_user&.can?(:admin_operations, project)
end
def feature_enabled?
Feature.enabled?(:multiple_http_integrations_custom_mapping, project)
end
def license_available?
project&.feature_available?(:multiple_alert_http_integrations)
end
end
end
......@@ -3,22 +3,40 @@
require 'spec_helper'
RSpec.describe AlertManagement::AlertPayloadField do
let(:alert_payload_field) { build_stubbed(:alert_management_alert_payload_field) }
let(:alert_payload_field) { build(:alert_management_alert_payload_field) }
describe '.validations' do
subject { alert_payload_field }
describe 'Validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:label) }
it { is_expected.to validate_inclusion_of(:type).in_array(described_class::SUPPORTED_TYPES) }
describe '#path_is_list_of_strings' do
before do
alert_management_alert_payload_field.path = path
alert_management_alert_payload_field.valid?
context 'validates path' do
shared_examples 'has invalid path' do
it 'is invalid' do
expect(alert_payload_field.valid?).to eq(false)
expect(alert_payload_field.errors.full_messages).to eq(['Path must be a list of strings'])
end
end
context 'when path is nil' do
let(:alert_payload_field) { build(:alert_management_alert_payload_field, path: nil) }
it_behaves_like 'has invalid path'
end
context 'when path is nil'
context 'when path is empty array'
context 'when path does not contain only strings'
context 'when path is empty array' do
let(:alert_payload_field) { build(:alert_management_alert_payload_field, path: []) }
it_behaves_like 'has invalid path'
end
context 'when path does not contain only strings' do
let(:alert_payload_field) { build(:alert_management_alert_payload_field, path: ['title', 1]) }
it_behaves_like 'has invalid path'
end
end
end
end
......@@ -3,11 +3,13 @@
require 'spec_helper'
RSpec.describe AlertManagement::ExtractAlertPayloadFieldsService do
let(:project) { build_stubbed(:project) }
let(:user) { build_stubbed(:user) }
let(:params) { { payload: payload_json } }
let(:payload_json) { Gitlab::Json.generate(payload) }
let_it_be_with_reload(:project) { create(:project) }
let_it_be(:user_with_permissions) { create(:user) }
let_it_be(:user_without_permissions) { create(:user) }
let_it_be(:user) { user_with_permissions }
let(:payload) { { foo: 'bar' } }
let(:payload_json) { Gitlab::Json.generate(payload) }
let(:params) { { payload: payload_json } }
let(:service) do
described_class.new(container: project, current_user: user, params: params)
......@@ -15,18 +17,80 @@ RSpec.describe AlertManagement::ExtractAlertPayloadFieldsService do
subject(:response) { service.execute }
context 'with license' do
before do
stub_licensed_features(multiple_alert_http_integrations: true)
allow(user).to receive(:can?).with(:admin_operations, project).and_return(true)
end
it 'works' do
expect(response).to be_success
context 'with feature flag enabled' do
before do
stub_feature_flags(multiple_http_integrations_custom_mapping: project)
end
context 'with permissions' do
before do
project.add_maintainer(user_with_permissions)
end
context 'when payload is a valid JSON' do
context 'when payloa has an acceptable size' do
it 'responds with success' do
is_expected.to be_success
end
it 'returns parsed fields' do
fields = response.payload[:payload_alert_fields]
field = fields.first
expect(fields.count).to eq(1)
expect(field.label).to eq('Foo')
expect(field.type).to eq('string')
expect(field.path).to eq(%w[foo])
end
end
context 'when limits are exceeded' do
before do
allow(Gitlab::Utils::DeepSize)
.to receive(:new)
.with(Gitlab::Json.parse(payload_json))
.and_return(double(valid?: false))
end
it 'returns payload size exceeded error' do
is_expected.to be_error
expect(response.message).to eq('Payload size exceeded')
end
end
end
context 'when payload is not a valid JSON' do
let(:payload) { 'not a JSON' }
it 'returns payload parse failure error' do
is_expected.to be_error
expect(response.message).to eq('Failed to parse payload')
end
end
end
context 'without permissions' do
it 'returns insufficient permissions error' do
is_expected.to be_error
expect(response.message).to eq('Insufficient permissions')
end
end
end
context 'fails when limits are exceeded'
context 'fails with invalid payload'
context 'without license'
context 'without feature flag'
context 'without permission'
context 'with feature flag disabled' do
before do
stub_feature_flags(multiple_http_integrations_custom_mapping: false)
end
it 'returns feature not available error' do
is_expected.to be_error
expect(response.message).to eq('Feature not available')
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