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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AlertManagement::HttpIntegration, feature_category: :incident_management do
include ::Gitlab::Routing.url_helpers
let_it_be(:project) { create(:project) }
subject(:integration) { build(:alert_management_http_integration, project: project) }
describe 'associations' do
it { is_expected.to belong_to(:project) }
end
describe 'default values' do
it { expect(described_class.new.endpoint_identifier).to be_present }
it { expect(described_class.new(endpoint_identifier: 'test').endpoint_identifier).to eq('test') }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:type_identifier) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
context 'when active' do
# Using `create` instead of `build` the integration so `token` is set.
# Uniqueness spec saves integration with `validate: false` otherwise.
subject { create(:alert_management_http_integration, :legacy) }
it { is_expected.to validate_uniqueness_of(:endpoint_identifier).scoped_to(:project_id) }
end
context 'when inactive' do
subject { create(:alert_management_http_integration, :legacy, :inactive) }
it { is_expected.to validate_uniqueness_of(:endpoint_identifier).scoped_to(:project_id) }
end
context 'payload_attribute_mapping' do
subject { build(:alert_management_http_integration, payload_attribute_mapping: attribute_mapping) }
context 'with valid JSON schema' do
let(:attribute_mapping) do
{
title: { path: %w[a b c], type: 'string', label: 'Title' },
description: { path: %w[a], type: 'string' }
}
end
it { is_expected.to be_valid }
end
context 'with invalid JSON schema' do
shared_examples 'is invalid record' do
it do
expect(subject).to be_invalid
expect(subject.errors.messages[:payload_attribute_mapping]).to eq(['must be a valid json schema'])
end
end
context 'when property is not an object' do
let(:attribute_mapping) do
{ title: 'That is not a valid schema' }
end
it_behaves_like 'is invalid record'
end
context 'when property missing required attributes' do
let(:attribute_mapping) do
{ title: { type: 'string' } }
end
it_behaves_like 'is invalid record'
end
context 'when property has extra attributes' do
let(:attribute_mapping) do
{ title: { path: %w[a b c], type: 'string', extra: 'property' } }
end
it_behaves_like 'is invalid record'
end
end
end
end
describe 'scopes' do
let_it_be(:integration_1) { create(:alert_management_http_integration) }
let_it_be(:integration_2) { create(:alert_management_http_integration, :inactive, project: project) }
let_it_be(:integration_3) { create(:alert_management_prometheus_integration, project: project) }
let_it_be(:integration_4) { create(:alert_management_http_integration, :legacy, :inactive) }
describe '.for_endpoint_identifier' do
let(:identifier) { integration_1.endpoint_identifier }
subject { described_class.for_endpoint_identifier(identifier) }
it { is_expected.to contain_exactly(integration_1) }
end
describe '.for_type' do
let(:type) { :prometheus }
subject { described_class.for_type(type) }
it { is_expected.to contain_exactly(integration_3) }
end
describe '.for_project' do
let(:project) { integration_2.project }
subject { described_class.for_project(project) }
it { is_expected.to contain_exactly(integration_2, integration_3) }
context 'with project_ids array' do
let(:project) { [integration_1.project_id] }
it { is_expected.to contain_exactly(integration_1) }
end
end
describe '.active' do
subject { described_class.active }
it { is_expected.to contain_exactly(integration_1, integration_3) }
end
describe '.ordered_by_type_and_id' do
before do
# Rearrange cache by saving to avoid false-positives
integration_2.touch
end
subject { described_class.ordered_by_type_and_id }
it { is_expected.to eq([integration_1, integration_2, integration_4, integration_3]) }
end
end
describe 'before validation' do
describe '#ensure_payload_example_not_nil' do
subject(:integration) { build(:alert_management_http_integration, payload_example: payload_example) }
context 'when the payload_example is nil' do
let(:payload_example) { nil }
it 'sets the payload_example to empty JSON' do
integration.valid?
expect(integration.payload_example).to eq({})
end
end
context 'when the payload_example is not nil' do
let(:payload_example) { { 'key' => 'value' } }
it 'sets the payload_example to specified value' do
integration.valid?
expect(integration.payload_example).to eq(payload_example)
end
end
end
end
describe '#token' do
subject { integration.token }
shared_context 'assign token' do |token|
let!(:previous_token) { integration.token }
before do
integration.token = token
integration.valid?
end
end
shared_examples 'valid token' do
it { is_expected.to match(/\A\h{32}\z/) }
end
context 'when unsaved' do
context 'when assigned' do
include_context 'assign token', 'random_token'
it_behaves_like 'valid token'
it { is_expected.not_to eq('random_token') }
end
end
context 'when persisted' do
before do
integration.save!
integration.reload
end
it_behaves_like 'valid token'
context 'when resetting' do
include_context 'assign token', ''
it_behaves_like 'valid token'
it { is_expected.not_to eq(previous_token) }
end
context 'when reassigning' do
include_context 'assign token', 'random_token'
it_behaves_like 'valid token'
it { is_expected.to eq(previous_token) }
end
end
end
describe '#endpoint_identifier' do
subject { integration.endpoint_identifier }
context 'when defined on initialize' do
let(:integration) { described_class.new }
it { is_expected.to match(/\A\h{16}\z/) }
end
context 'when included in initialization args' do
let(:required_args) { { project: project, name: 'Name' } }
AlertManagement::HttpIntegration::LEGACY_IDENTIFIERS.each do |identifier|
context "for endpoint identifier \"#{identifier}\"" do
let(:integration) { described_class.new(**required_args, endpoint_identifier: identifier) }
it { is_expected.to eq(identifier) }
specify { expect(integration).to be_valid }
end
end
end
context 'when reassigning' do
let(:integration) { create(:alert_management_http_integration) }
let!(:starting_identifier) { subject }
it 'does not allow reassignment' do
integration.endpoint_identifier = 'newValidId'
integration.save!
expect(integration.reload.endpoint_identifier).to eq(starting_identifier)
end
end
end
describe '#url' do
subject { integration.url }
it do
is_expected.to eq(
project_alert_http_integration_url(
integration.project,
'datadog',
integration.endpoint_identifier,
format: :json
)
)
end
context 'when name is not defined' do
let(:integration) { described_class.new(project: project) }
it do
is_expected.to eq(
project_alert_http_integration_url(
integration.project,
'http-endpoint',
integration.endpoint_identifier,
format: :json
)
)
end
end
context 'for a legacy integration' do
let(:integration) { build(:alert_management_http_integration, :legacy) }
it do
is_expected.to eq(
project_alerts_notify_url(
integration.project,
format: :json
)
)
end
end
context 'for a prometheus integration' do
let(:integration) { build(:alert_management_prometheus_integration) }
it do
is_expected.to eq(
project_alert_http_integration_url(
integration.project,
'datadog',
integration.endpoint_identifier,
format: :json
)
)
end
context 'for a legacy integration' do
let(:integration) { build(:alert_management_prometheus_integration, :legacy) }
it do
is_expected.to eq(
notify_project_prometheus_alerts_url(
integration.project,
format: :json
)
)
end
end
end
end
end
|