2018-08-02 00:09:29 +02:00
|
|
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
|
|
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
|
|
|
|
2018-06-04 14:06:37 +02:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
module Notifier
|
|
|
|
class ResolverTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
|
|
|
@resolver = Notifier::Resolver.new
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon ignores user having notification disabled" do
|
2018-06-04 14:06:37 +02:00
|
|
|
n1 = create(:notification, check: build(:check, :expires_next_week))
|
|
|
|
n1.check.user.update_attribute(:notifications_enabled, false)
|
|
|
|
n2 = create(:notification, check: build(:check, :expires_next_week))
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_not_includes notifications, n1
|
|
|
|
assert_includes notifications, n2
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon ignores inactive checks" do
|
2018-06-04 14:06:37 +02:00
|
|
|
n1 = create(:notification, check: build(:check, :expires_next_week, :inactive))
|
|
|
|
n2 = create(:notification, check: build(:check, :expires_next_week))
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_not_includes notifications, n1
|
|
|
|
assert_includes notifications, n2
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon gets only checks inside interval" do
|
2018-06-05 22:44:48 +02:00
|
|
|
n1 = create(:notification, check: build(:check, :expires_next_week), interval: 6)
|
|
|
|
n2 = create(:notification, check: build(:check, :expires_next_week), interval: 7)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_not_includes notifications, n1
|
|
|
|
assert_includes notifications, n2
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon can gets several notifications for a same check" do
|
2018-06-04 14:06:37 +02:00
|
|
|
check = create(:check, :expires_next_week)
|
2018-06-05 22:44:48 +02:00
|
|
|
n1 = create(:notification, check: check, interval: 3)
|
|
|
|
n2 = create(:notification, check: check, interval: 10)
|
|
|
|
n3 = create(:notification, check: check, interval: 30)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_not_includes notifications, n1
|
|
|
|
assert_includes notifications, n2
|
|
|
|
assert_includes notifications, n3
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon takes care of the status" do
|
2018-06-04 14:06:37 +02:00
|
|
|
check = create(:check, :expires_next_week)
|
|
|
|
n1 = create(:notification, check: check)
|
|
|
|
n2 = create(:notification, :failed, check: check)
|
|
|
|
n3 = create(:notification, :ongoing, check: check)
|
|
|
|
n4 = create(:notification, :succeed, check: check)
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_includes notifications, n1
|
|
|
|
assert_includes notifications, n2
|
|
|
|
assert_not_includes notifications, n3
|
|
|
|
assert_not_includes notifications, n4
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#notifications_expiring_soon ignores checks expired and without date" do
|
2018-06-04 14:06:37 +02:00
|
|
|
n1 = create(:notification, check: build(:check, :expires_next_week))
|
|
|
|
n2 = create(:notification, check: build(:check, domain_expires_at: 1.week.ago))
|
|
|
|
n3 = create(:notification, check: build(:check, :nil_dates))
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
notifications = @resolver.notifications_expiring_soon
|
2018-06-04 14:06:37 +02:00
|
|
|
|
|
|
|
assert_includes notifications, n1
|
|
|
|
assert_not_includes notifications, n2
|
|
|
|
assert_not_includes notifications, n3
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#checks_recurrent_failures ignores inactive checks" do
|
|
|
|
c1 = create(:check, :last_runs_failed, :inactive)
|
|
|
|
c2 = create(:check, :last_runs_failed)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
checks = @resolver.checks_recurrent_failures(4)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
assert_not_includes checks, c1
|
|
|
|
assert_includes checks, c2
|
2018-06-04 14:06:37 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#checks_recurrent_failures ignores user having notification disabled" do
|
|
|
|
c1 = create(:check, :last_runs_failed)
|
|
|
|
c1.user.update_attribute(:notifications_enabled, false)
|
|
|
|
c2 = create(:check, :last_runs_failed)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
checks = @resolver.checks_recurrent_failures(4)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
assert_not_includes checks, c1
|
|
|
|
assert_includes checks, c2
|
2018-06-04 14:06:37 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
test "#checks_recurrent_failures gets only checks having consecutive failures" do
|
|
|
|
c1 = create(:check, :nil_dates)
|
|
|
|
c2 = create(:check, :last_run_succeed)
|
|
|
|
c3 = create(:check, :last_runs_failed, consecutive_failures: 5)
|
|
|
|
c4 = create(:check, :last_runs_failed, consecutive_failures: 3)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
checks = @resolver.checks_recurrent_failures(4)
|
2018-06-04 14:06:37 +02:00
|
|
|
|
2018-08-01 17:06:20 +02:00
|
|
|
assert_not_includes checks, c1
|
|
|
|
assert_not_includes checks, c2
|
|
|
|
assert_not_includes checks, c4
|
|
|
|
assert_includes checks, c3
|
2018-06-04 14:06:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|