Commit 4e3981c6 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Add more tests for alert field types

Add more tests for different time format and extract types into
constants
parent e7f900a2
......@@ -7,7 +7,10 @@ module AlertManagement
attr_accessor :project, :path, :label, :type
SUPPORTED_TYPES = %w[array datetime string].freeze
ARRAY_TYPE = 'array'
DATETIME_TYPE = 'datetime'
STRING_TYPE = 'string'
SUPPORTED_TYPES = [ARRAY_TYPE, DATETIME_TYPE, STRING_TYPE].freeze
validates :project, presence: true
validates :label, presence: true
......
......@@ -51,12 +51,12 @@ module Gitlab
def type_of(value)
case value
when Array
::AlertManagement::AlertPayloadField::ARRAY_TYPE
when /^\d{4}/ # assume it's a datetime
'datetime'
::AlertManagement::AlertPayloadField::DATETIME_TYPE
when String
'string'
when Array
'array'
::AlertManagement::AlertPayloadField::STRING_TYPE
end
end
end
......
......@@ -23,6 +23,10 @@ RSpec.describe Gitlab::AlertManagement::AlertPayloadFieldExtractor do
}
},
time: '2020-12-09T12:34:56',
time_iso_8601_and_rfc_3339: '2021-01-27T13:01:11+00:00', # ISO 8601 and RFC 3339
time_iso_8601: '2021-01-27T13:01:11Z', # ISO 8601
time_iso_8601_short: '20210127T130111Z', # ISO 8601
time_rfc_3339: '2021-01-27 13:01:11+00:00', # RFC 3339
discarded_null: nil,
discarded_bool_true: true,
discarded_bool_false: false,
......@@ -36,6 +40,10 @@ RSpec.describe Gitlab::AlertManagement::AlertPayloadFieldExtractor do
a_field(%w(nested key), 'Key', 'string'),
a_field(%w(nested deep key), 'Key', 'string'),
a_field(['time'], 'Time', 'datetime'),
a_field(['time_iso_8601_and_rfc_3339'], 'Time iso 8601 and rfc 3339', 'datetime'),
a_field(['time_iso_8601'], 'Time iso 8601', 'datetime'),
a_field(['time_iso_8601_short'], 'Time iso 8601 short', 'datetime'),
a_field(['time_rfc_3339'], 'Time rfc 3339', 'datetime'),
a_field(['arr'], 'Arr', 'array')
)
end
......
......@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe AlertManagement::AlertPayloadField do
let(:alert_payload_field) { build(:alert_management_alert_payload_field) }
describe '.validations' do
describe 'validations' do
subject { alert_payload_field }
it { is_expected.to validate_presence_of(:project) }
......
......@@ -32,8 +32,8 @@ RSpec.describe AlertManagement::ExtractAlertPayloadFieldsService do
project.add_maintainer(user_with_permissions)
end
context 'when payload is a valid JSON' do
context 'when payloa has an acceptable size' do
context 'when payload is valid JSON' do
context 'when payload has an acceptable size' do
it 'responds with success' do
is_expected.to be_success
end
......@@ -75,6 +75,8 @@ RSpec.describe AlertManagement::ExtractAlertPayloadFieldsService do
end
context 'without permissions' do
let_it_be(:user) { user_without_permissions }
it 'returns insufficient permissions error' do
is_expected.to be_error
expect(response.message).to eq('Insufficient permissions')
......
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