diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index 9449b56..e80320f 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -116,7 +116,7 @@ class ChecksController < ApplicationController # rubocop:disable Metrics/ClassLe last_notification = @check.notifications.last # user has filled a new notification: we use it for the form - if last_notification.new_record? + if last_notification.present? && last_notification.new_record? @new_notification = last_notification else # otherwise, set a new empty notification build_empty_notification diff --git a/app/models/check.rb b/app/models/check.rb index bd76fe7..87fbe5c 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -87,7 +87,7 @@ class Check < ApplicationRecord def increment_consecutive_failures! self.consecutive_failures += 1 - save! + save!(validate: false) end def supported? @@ -128,7 +128,7 @@ class Check < ApplicationRecord end def reset_consecutive_failures - return unless last_success_at_changed? + return unless last_success_at_changed? || mode_changed? || domain_changed? return if consecutive_failures_changed? self.consecutive_failures = 0 diff --git a/test/models/check_test.rb b/test/models/check_test.rb index 909b2ff..22fecc1 100644 --- a/test/models/check_test.rb +++ b/test/models/check_test.rb @@ -55,6 +55,27 @@ class CheckTest < ActiveSupport::TestCase assert_nil check_notification.sent_at end + test "consecutive failures are resetted when domain changed" do + check = create(:check, consecutive_failures: 1) + + check.domain = "mynewdomain.fr" + check.save! + + assert_equal 0, check.consecutive_failures + end + + test "consecutive failures are resetted when mode changed" do + check = create(:check, consecutive_failures: 1, mode: :auto) + check.manual! + + assert_equal 0, check.consecutive_failures + + check = create(:check, domain: "x.wxyz", consecutive_failures: 1, mode: :manual) + check.auto! + + assert_equal 0, check.consecutive_failures + end + test "days_from_last_success without any success" do check = build(:check) assert_nil check.days_from_last_success