From ea610ee1857dec0358dae4ca909d6e59f2913a72 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 1 Aug 2018 17:06:20 +0200 Subject: [PATCH] Notifications grouped of checks in error to user email. This is not dependent of Notificatioon model. --- app/mailers/notifications_mailer.rb | 34 +-- app/services/notifier/channels/base.rb | 20 +- app/services/notifier/channels/email.rb | 18 +- app/services/notifier/processor.rb | 18 +- app/services/notifier/resolver.rb | 14 +- .../_check_comment_vendor.en.html.erb | 8 +- .../_check_comment_vendor.en.text.erb | 10 +- .../_check_comment_vendor.fr.html.erb | 8 +- .../_check_comment_vendor.fr.text.erb | 11 +- .../_domain_recurrent_failure.en.html.erb | 23 ++ .../_domain_recurrent_failure.en.text.erb | 15 ++ .../_domain_recurrent_failure.fr.html.erb | 18 ++ .../_domain_recurrent_failure.fr.text.erb | 15 ++ .../_footer_recurrent_failures.en.html.erb | 7 +- .../_footer_recurrent_failures.en.text.erb | 6 +- .../_footer_recurrent_failures.fr.html.erb | 6 +- .../_footer_recurrent_failures.fr.text.erb | 6 +- .../_ssl_recurrent_failure.en.html.erb | 22 ++ .../_ssl_recurrent_failure.en.text.erb | 15 ++ .../_ssl_recurrent_failure.fr.html.erb | 20 ++ .../_ssl_recurrent_failure.fr.text.erb | 15 ++ .../domain_expires_soon.en.html.erb | 2 +- .../domain_expires_soon.en.text.erb | 2 +- .../domain_expires_soon.fr.html.erb | 2 +- .../domain_expires_soon.fr.text.erb | 2 +- .../domain_recurrent_failures.en.html.erb | 30 --- .../domain_recurrent_failures.en.text.erb | 20 -- .../domain_recurrent_failures.fr.html.erb | 29 --- .../domain_recurrent_failures.fr.text.erb | 20 -- .../recurrent_failures.en.html.erb | 22 ++ .../recurrent_failures.en.text.erb | 14 ++ .../recurrent_failures.fr.html.erb | 22 ++ .../recurrent_failures.fr.text.erb | 14 ++ .../ssl_expires_soon.en.html.erb | 2 +- .../ssl_expires_soon.en.text.erb | 2 +- .../ssl_expires_soon.fr.html.erb | 2 +- .../ssl_expires_soon.fr.text.erb | 2 +- .../ssl_recurrent_failures.en.html.erb | 31 --- .../ssl_recurrent_failures.en.text.erb | 22 -- .../ssl_recurrent_failures.fr.html.erb | 35 --- .../ssl_recurrent_failures.fr.text.erb | 22 -- config/locales/en.yml | 9 +- config/locales/fr.yml | 13 +- db/seeds.rb | 2 + test/factories/checks.rb | 2 +- test/factories/users.rb | 4 + test/mailers/notifications_mailer_test.rb | 210 ++++++------------ .../previews/notifications_mailer_preview.rb | 15 +- test/services/notifier/channels/base_test.rb | 30 ++- test/services/notifier/processor_test.rb | 4 +- test/services/notifier/resolver_test.rb | 68 +++--- 51 files changed, 451 insertions(+), 512 deletions(-) create mode 100644 app/views/notifications_mailer/_domain_recurrent_failure.en.html.erb create mode 100644 app/views/notifications_mailer/_domain_recurrent_failure.en.text.erb create mode 100644 app/views/notifications_mailer/_domain_recurrent_failure.fr.html.erb create mode 100644 app/views/notifications_mailer/_domain_recurrent_failure.fr.text.erb create mode 100644 app/views/notifications_mailer/_ssl_recurrent_failure.en.html.erb create mode 100644 app/views/notifications_mailer/_ssl_recurrent_failure.en.text.erb create mode 100644 app/views/notifications_mailer/_ssl_recurrent_failure.fr.html.erb create mode 100644 app/views/notifications_mailer/_ssl_recurrent_failure.fr.text.erb delete mode 100644 app/views/notifications_mailer/domain_recurrent_failures.en.html.erb delete mode 100644 app/views/notifications_mailer/domain_recurrent_failures.en.text.erb delete mode 100644 app/views/notifications_mailer/domain_recurrent_failures.fr.html.erb delete mode 100644 app/views/notifications_mailer/domain_recurrent_failures.fr.text.erb create mode 100644 app/views/notifications_mailer/recurrent_failures.en.html.erb create mode 100644 app/views/notifications_mailer/recurrent_failures.en.text.erb create mode 100644 app/views/notifications_mailer/recurrent_failures.fr.html.erb create mode 100644 app/views/notifications_mailer/recurrent_failures.fr.text.erb delete mode 100644 app/views/notifications_mailer/ssl_recurrent_failures.en.html.erb delete mode 100644 app/views/notifications_mailer/ssl_recurrent_failures.en.text.erb delete mode 100644 app/views/notifications_mailer/ssl_recurrent_failures.fr.html.erb delete mode 100644 app/views/notifications_mailer/ssl_recurrent_failures.fr.text.erb diff --git a/app/mailers/notifications_mailer.rb b/app/mailers/notifications_mailer.rb index 31a4677..8f79612 100644 --- a/app/mailers/notifications_mailer.rb +++ b/app/mailers/notifications_mailer.rb @@ -1,34 +1,38 @@ class NotificationsMailer < ApplicationMailer helper :application - before_action do + before_action except: :recurrent_failures do @notification = params.fetch(:notification) @check = @notification.check end - default to: -> { @notification.recipient } - def domain_expires_soon @expire_in_days = Integer(@check.domain_expires_at.to_date - Date.today) - subject = t(".subject", domain: @check.domain, count: @expire_in_days) - mail subject: subject - end - - def domain_recurrent_failures - subject = t(".subject", domain: @check.domain) - mail subject: subject + I18n.with_locale params&.fetch(:locale) { @check.user.locale } do + subject = t(".subject", domain: @check.domain, count: @expire_in_days) + mail subject: subject, to: @notification.recipient + end end def ssl_expires_soon @expire_in_days = Integer(@check.domain_expires_at.to_date - Date.today) - subject = t(".subject", domain: @check.domain, count: @expire_in_days) - mail subject: subject + I18n.with_locale params&.fetch(:locale) { @check.user.locale } do + subject = t(".subject", domain: @check.domain, count: @expire_in_days) + mail subject: subject, to: @notification.recipient + end end - def ssl_recurrent_failures - subject = t(".subject", domain: @check.domain) - mail subject: subject + def recurrent_failures(user, checks) + @checks = checks + + # params generally not set, except for preview mailer + params_locale = (params[:locale] if params.present?) + + I18n.with_locale params_locale || user.locale do + subject = t(".subject", count: checks.count, domain: checks.first.domain) + mail subject: subject, to: user.email + end end end diff --git a/app/services/notifier/channels/base.rb b/app/services/notifier/channels/base.rb index a3e9586..5c879aa 100644 --- a/app/services/notifier/channels/base.rb +++ b/app/services/notifier/channels/base.rb @@ -1,39 +1,33 @@ module Notifier module Channels class Base - def notify(reason, notification) # rubocop:disable Metrics/MethodLength - return unless supports?(reason, notification) + def notify(notification) # rubocop:disable Metrics/MethodLength + return unless supports?(notification) notification.ongoing! - case [notification.check.kind.to_sym, reason] - when [:domain, :expires_soon] + case notification.check.kind.to_sym + when :domain domain_notify_expires_soon(notification) - when [:domain, :recurrent_failures] - domain_notify_recurrent_failures(notification) - when [:ssl, :expires_soon] + when :ssl ssl_notify_expires_soon(notification) - when [:ssl, :recurrent_failures] - ssl_notify_recurrent_failures(notification) else fail ArgumentError, - "Invalid notification reason `#{reason}` for check kind `#{notification.check.kind}`." + "Invalid notification for check kind `#{notification.check.kind}`." end end private # :nocov: - def supports?(_reason, _notification) + def supports?(_notification) fail NotImplementedError, "#{self.class.name} channel did not implemented method #{__callee__}" end %i[ domain_notify_expires_soon - domain_notify_recurrent_failures ssl_notify_expires_soon - ssl_notify_recurrent_failures ].each do |method| define_method(method) do fail NotImplementedError, diff --git a/app/services/notifier/channels/email.rb b/app/services/notifier/channels/email.rb index 9243b7d..9141ee0 100644 --- a/app/services/notifier/channels/email.rb +++ b/app/services/notifier/channels/email.rb @@ -1,29 +1,25 @@ module Notifier module Channels class Email < Base - REASONS = %i[expires_soon recurrent_failures].freeze + # Error notifications - all checks grouped by user + def notify_recurrent_failures(user, checks) + NotificationsMailer.recurrent_failures(user, checks).deliver_now + end protected - def supports?(reason, _notification) - REASONS.include?(reason) + def supports?(_notification) + true end + # Expiration notifications def domain_notify_expires_soon(notification) NotificationsMailer.with(notification: notification).domain_expires_soon.deliver_now end - def domain_notify_recurrent_failures(notification) - NotificationsMailer.with(notification: notification).domain_recurrent_failures.deliver_now - end - def ssl_notify_expires_soon(notification) NotificationsMailer.with(notification: notification).ssl_expires_soon.deliver_now end - - def ssl_notify_recurrent_failures(notification) - NotificationsMailer.with(notification: notification).ssl_recurrent_failures.deliver_now - end end end end diff --git a/app/services/notifier/processor.rb b/app/services/notifier/processor.rb index e2893a4..8d3cad3 100644 --- a/app/services/notifier/processor.rb +++ b/app/services/notifier/processor.rb @@ -16,16 +16,19 @@ module Notifier end def process_expires_soon - resolver.resolve_expires_soon.find_each do |notification| - notifier_channel_for(notification).notify(:expires_soon, notification) + resolver.notifications_expiring_soon.find_each do |notification| + notifier_channel_for(notification).notify(notification) sleep configuration.interval end end + # Notify checks in error by email to the check owner adress email. + # A single email contains all checks for a same user. def process_recurrent_failures - resolver.resolve_check_failed(configuration.consecutive_failures).find_each do |notification| - notifier_channel_for(notification).notify(:recurrent_failures, notification) + failed_checks = resolver.checks_recurrent_failures(configuration.consecutive_failures) + failed_checks.group_by(&:user).each_pair do |user, checks| + channels[:email].notify_recurrent_failures(user, checks) sleep configuration.interval end @@ -34,12 +37,7 @@ module Notifier private def default_configuration - config = Rails.configuration.chexpire.fetch("notifier", {}) - - Configuration.new( - config.fetch("interval") { 0.00 }, - config.fetch("consecutive_failures") { 3 }, - ) + Rails.configuration.chexpire.fetch("notifier") end def notifier_channel_for(notification) diff --git a/app/services/notifier/resolver.rb b/app/services/notifier/resolver.rb index a157860..25c3592 100644 --- a/app/services/notifier/resolver.rb +++ b/app/services/notifier/resolver.rb @@ -1,18 +1,18 @@ module Notifier class Resolver - def resolve_expires_soon + def notifications_expiring_soon scope .where("checks.domain_expires_at >= CURDATE()") .where("DATE(checks.domain_expires_at) <= DATE_ADD(CURDATE(), INTERVAL notifications.interval DAY)") end - def resolve_check_failed - # Only gets here the checks having its last run in error - # Logical rules are in plain ruby inside processor - scope - .includes(check: :logs) - .merge(Check.last_run_failed) + def checks_recurrent_failures(min_consecutive) + Check + .active + .consecutive_failures(min_consecutive) + .includes(:user) + .where.not(user: ignore_users) end private diff --git a/app/views/notifications_mailer/_check_comment_vendor.en.html.erb b/app/views/notifications_mailer/_check_comment_vendor.en.html.erb index 9c96749..8394dd9 100644 --- a/app/views/notifications_mailer/_check_comment_vendor.en.html.erb +++ b/app/views/notifications_mailer/_check_comment_vendor.en.html.erb @@ -1,13 +1,13 @@ -<%- if @check.comment.present? -%> +<%- if check.comment.present? -%>

You wrote the following comment with this domain:

- <%= @check.comment -%> + <%= check.comment -%>

<%- end -%> -<%- if @check.vendor.present? -%> -Vendor: <%= @check.vendor %> +<%- if check.vendor.present? -%> +Vendor: <%= check.vendor %> <% end %> diff --git a/app/views/notifications_mailer/_check_comment_vendor.en.text.erb b/app/views/notifications_mailer/_check_comment_vendor.en.text.erb index 7407cc1..2f62655 100644 --- a/app/views/notifications_mailer/_check_comment_vendor.en.text.erb +++ b/app/views/notifications_mailer/_check_comment_vendor.en.text.erb @@ -1,11 +1,9 @@ -<%- if @check.comment.present? -%> +<%- if check.comment.present? -%> You wrote the following comment with this domain: - <%= @check.comment -%> + <%= check.comment -%> <%- end -%> - - -<%- if @check.vendor.present? -%> -Vendor: <%= @check.vendor %> +<%- if check.vendor.present? -%> +Vendor: <%= check.vendor %> <% end %> diff --git a/app/views/notifications_mailer/_check_comment_vendor.fr.html.erb b/app/views/notifications_mailer/_check_comment_vendor.fr.html.erb index c2d7576..a2772ff 100644 --- a/app/views/notifications_mailer/_check_comment_vendor.fr.html.erb +++ b/app/views/notifications_mailer/_check_comment_vendor.fr.html.erb @@ -1,13 +1,13 @@ -<%- if @check.comment.present? -%> +<%- if check.comment.present? -%>

Vous avez saisi le commentaire suivant pour ce domaine :

- <%= @check.comment -%> + <%= check.comment -%>

<%- end -%> -<%- if @check.vendor.present? -%> -Fournisseur : <%= @check.vendor %> +<%- if check.vendor.present? -%> +Fournisseur : <%= check.vendor %> <% end %> diff --git a/app/views/notifications_mailer/_check_comment_vendor.fr.text.erb b/app/views/notifications_mailer/_check_comment_vendor.fr.text.erb index b52e291..6bddf75 100644 --- a/app/views/notifications_mailer/_check_comment_vendor.fr.text.erb +++ b/app/views/notifications_mailer/_check_comment_vendor.fr.text.erb @@ -1,11 +1,8 @@ -<%- if @check.comment.present? -%> +<%- if check.comment.present? -%> Vous avez saisi le commentaire suivant pour ce domaine : - - <%= @check.comment -%> + <%= check.comment -%> <%- end -%> - - -<%- if @check.vendor.present? -%> -Fournisseur : <%= @check.vendor %> +<%- if check.vendor.present? -%> +Fournisseur : <%= check.vendor %> <% end %> diff --git a/app/views/notifications_mailer/_domain_recurrent_failure.en.html.erb b/app/views/notifications_mailer/_domain_recurrent_failure.en.html.erb new file mode 100644 index 0000000..2b47a94 --- /dev/null +++ b/app/views/notifications_mailer/_domain_recurrent_failure.en.html.erb @@ -0,0 +1,23 @@ +

+ Domain: <%= check.domain %> (<%= check.consecutive_failures %> consecutive errors) + +
+ + <%- if check.domain_expires_at.present? %> + Last known expiry date : <%= format_utc(check.domain_expires_at) %>. +
+ <% end %> + + <%- if check.last_success_at.present? %> + Last successful check occured : <%= format_utc(check.last_success_at) %>. +
+ <% end %> +

+ + +

If you have deleted your domain or have not renewed it, please disable +or delete the check by following this link:
+<%= edit_check_url(check) %> +

+ +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_domain_recurrent_failure.en.text.erb b/app/views/notifications_mailer/_domain_recurrent_failure.en.text.erb new file mode 100644 index 0000000..61ec36c --- /dev/null +++ b/app/views/notifications_mailer/_domain_recurrent_failure.en.text.erb @@ -0,0 +1,15 @@ +Domain: <%= check.domain %> (<%= check.consecutive_failures %> consecutive errors) +<%= "-" * (check.domain.length + "Domain: ".length) %> + +<%- if check.domain_expires_at.present? %> +Last known expiry date : <%= format_utc(check.domain_expires_at) %>. +<% end %> +<%- if check.last_success_at.present? %> +Last successful check occured : <%= format_utc(check.last_success_at) %>. +<% end %> + +If you have deleted your domain or have not renewed it, please disable +or delete the check by following this link: +<%= edit_check_url(check) %> + +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_domain_recurrent_failure.fr.html.erb b/app/views/notifications_mailer/_domain_recurrent_failure.fr.html.erb new file mode 100644 index 0000000..ba96bf3 --- /dev/null +++ b/app/views/notifications_mailer/_domain_recurrent_failure.fr.html.erb @@ -0,0 +1,18 @@ +

+ Domaine: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +
+ <%- if check.domain_expires_at.present? %> + Dernière date d'expiration connue : <%= format_utc(check.domain_expires_at) %>. +
+ <% end %> + + <%- if check.last_success_at.present? %> + Dernière vérification réussie : <%= format_utc(check.last_success_at) %>. + <% end %> +

+ +

+Si vous avez supprimé le domaine ou ne l'avez pas renouvellé, merci de désactiver la vérification associée, avec ce lien :
+<%= link_to nil, edit_check_url(check) %> +

+<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_domain_recurrent_failure.fr.text.erb b/app/views/notifications_mailer/_domain_recurrent_failure.fr.text.erb new file mode 100644 index 0000000..82d8c67 --- /dev/null +++ b/app/views/notifications_mailer/_domain_recurrent_failure.fr.text.erb @@ -0,0 +1,15 @@ +Domaine: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +<%= "-" * (check.domain.length + "Domaine: ".length) %> + +<%- if check.domain_expires_at.present? %> +Dernière date d'expiration connue : <%= format_utc(check.domain_expires_at) %>. +<% end %> +<%- if check.last_success_at.present? %> +Dernière vérification réussie : <%= format_utc(check.last_success_at) %>. +<% end %> + +Si vous avez supprimé le domaine ou ne l'avez pas renouvellé, +merci de désactiver la vérification associée, avec ce lien : +<%= edit_check_url(check) %> + +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_footer_recurrent_failures.en.html.erb b/app/views/notifications_mailer/_footer_recurrent_failures.en.html.erb index beeffb8..b5159d8 100644 --- a/app/views/notifications_mailer/_footer_recurrent_failures.en.html.erb +++ b/app/views/notifications_mailer/_footer_recurrent_failures.en.html.erb @@ -1,11 +1,8 @@
-
-- -

You received this email because of the notification <%= interval %> days before -the last known expiry date.
-You can handle the check by following this link:
-<%= link_to nil, edit_check_url(check) %> +

You can handle all your checks by following this link:
+<%= link_to nil, checks_url %>

The Chexpire Team

diff --git a/app/views/notifications_mailer/_footer_recurrent_failures.en.text.erb b/app/views/notifications_mailer/_footer_recurrent_failures.en.text.erb index b5a42ec..e91b7ae 100644 --- a/app/views/notifications_mailer/_footer_recurrent_failures.en.text.erb +++ b/app/views/notifications_mailer/_footer_recurrent_failures.en.text.erb @@ -1,8 +1,6 @@ -- -You received this email because of the notification <%= interval %> days before -the last known expiry date. -You can handle the check by following this link: -<%= edit_check_url(check) %> +You can handle all your checks by following this link: +<%= checks_url %> The Chexpire Team diff --git a/app/views/notifications_mailer/_footer_recurrent_failures.fr.html.erb b/app/views/notifications_mailer/_footer_recurrent_failures.fr.html.erb index bb35480..0f13c9e 100644 --- a/app/views/notifications_mailer/_footer_recurrent_failures.fr.html.erb +++ b/app/views/notifications_mailer/_footer_recurrent_failures.fr.html.erb @@ -1,10 +1,8 @@
-
-- -

Vous avez reçu ce courriel à <%= pluralize(interval, "jour", "jours") %> avant la dernière date d'expiration connue.
-Vous pouvez gérer les notifications pour cette vérification à ce adresse : -<%= link_to nil, edit_check_url(check) %> +

Vous pouvez gérer l'ensemble de vos vérifications à cette adresse :
+<%= link_to nil, checks_url %>

L'équipe Chexpire

diff --git a/app/views/notifications_mailer/_footer_recurrent_failures.fr.text.erb b/app/views/notifications_mailer/_footer_recurrent_failures.fr.text.erb index a77dd21..60c4ef6 100644 --- a/app/views/notifications_mailer/_footer_recurrent_failures.fr.text.erb +++ b/app/views/notifications_mailer/_footer_recurrent_failures.fr.text.erb @@ -1,8 +1,6 @@ -- -Vous avez reçu ce courriel à <%= pluralize(interval, "jour", "jours") %> -avant la dernière date d'expiration connue. -Vous pouvez gérer les notifications pour cette vérification à ce adresse : -<%= edit_check_url(check) %> +Vous pouvez gérer l'ensemble de vos vérifications à cette adresse : +<%= checks_url %> L'équipe Chexpire diff --git a/app/views/notifications_mailer/_ssl_recurrent_failure.en.html.erb b/app/views/notifications_mailer/_ssl_recurrent_failure.en.html.erb new file mode 100644 index 0000000..1ce2fd3 --- /dev/null +++ b/app/views/notifications_mailer/_ssl_recurrent_failure.en.html.erb @@ -0,0 +1,22 @@ +

+ SSL: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +
+ + <%- if check.domain_expires_at.present? %> + Last known expiry date : <%= format_utc(check.domain_expires_at) %>. +
+ <%- end %> + + <%- if check.last_success_at.present? %> + Last successful check : <%= format_utc(check.last_success_at) %>. +
+ <%- end %> +

+ +

+If there is no more SSL endpoint for this domain, please disable +or delete the check by following this link:
+<%= edit_check_url(check) %> +

+ +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_ssl_recurrent_failure.en.text.erb b/app/views/notifications_mailer/_ssl_recurrent_failure.en.text.erb new file mode 100644 index 0000000..52c5179 --- /dev/null +++ b/app/views/notifications_mailer/_ssl_recurrent_failure.en.text.erb @@ -0,0 +1,15 @@ +SSL: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +<%= "-" * (check.domain.length + "SSL: ".length) %> + +<%- if check.domain_expires_at.present? %> +Last known expiry date : <%= format_utc(check.domain_expires_at) %>. +<%- end %> +<%- if check.last_success_at.present? %> +Last successful check : <%= format_utc(check.last_success_at) %>. +<%- end %> + +If there is no more SSL endpoint for this domain, please disable +or delete the check by following this link: +<%= edit_check_url(check) %> + +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_ssl_recurrent_failure.fr.html.erb b/app/views/notifications_mailer/_ssl_recurrent_failure.fr.html.erb new file mode 100644 index 0000000..18d1cfc --- /dev/null +++ b/app/views/notifications_mailer/_ssl_recurrent_failure.fr.html.erb @@ -0,0 +1,20 @@ +

+ SSL: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +
+ + <%- if check.domain_expires_at.present? %> + Dernière date d'expiration connue : <%= format_utc(check.domain_expires_at) %>. +
+ <% end %> + + <%- if check.last_success_at.present? %> + Dernière vérification réussie : <%= format_utc(check.last_success_at) %>. + <% end %> +

+ +

+ S'il n'y a plus de terminaison SSL pour ce site ou s'il n'existe plus, + merci de désactiver la vérification associée, avec ce lien :
+<%= link_to nil, edit_check_url(check) %> +

+<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/_ssl_recurrent_failure.fr.text.erb b/app/views/notifications_mailer/_ssl_recurrent_failure.fr.text.erb new file mode 100644 index 0000000..4492ca8 --- /dev/null +++ b/app/views/notifications_mailer/_ssl_recurrent_failure.fr.text.erb @@ -0,0 +1,15 @@ +SSL: <%= check.domain %> (<%= check.consecutive_failures %> erreurs consécutives) +<%= "-" * (check.domain.length + "SSL: ".length) %> + +<%- if check.domain_expires_at.present? %> +Dernière date d'expiration connue : <%= format_utc(check.domain_expires_at) %>. +<% end %> +<%- if check.last_success_at.present? %> +Dernière vérification réussie : <%= format_utc(check.last_success_at) %>. +<% end %> + +S'il n'y a plus de terminaison SSL pour ce site ou s'il n'existe plus, +merci de désactiver la vérification associée, avec ce lien : +<%= edit_check_url(check) %> + +<%= render "check_comment_vendor", check: check %> diff --git a/app/views/notifications_mailer/domain_expires_soon.en.html.erb b/app/views/notifications_mailer/domain_expires_soon.en.html.erb index d3b087f..9fb05a7 100644 --- a/app/views/notifications_mailer/domain_expires_soon.en.html.erb +++ b/app/views/notifications_mailer/domain_expires_soon.en.html.erb @@ -7,6 +7,6 @@


-<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_expires_soon.en.text.erb b/app/views/notifications_mailer/domain_expires_soon.en.text.erb index c42e60a..6e93342 100644 --- a/app/views/notifications_mailer/domain_expires_soon.en.text.erb +++ b/app/views/notifications_mailer/domain_expires_soon.en.text.erb @@ -3,7 +3,7 @@ Hi, the domain <%= @check.domain %> will expire <%= format_utc(@check.domain_expires_at) %>. -<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_expires_soon.fr.html.erb b/app/views/notifications_mailer/domain_expires_soon.fr.html.erb index 9b39910..6aa8e1c 100644 --- a/app/views/notifications_mailer/domain_expires_soon.fr.html.erb +++ b/app/views/notifications_mailer/domain_expires_soon.fr.html.erb @@ -7,6 +7,6 @@


-<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_expires_soon.fr.text.erb b/app/views/notifications_mailer/domain_expires_soon.fr.text.erb index 2caad62..126ac40 100644 --- a/app/views/notifications_mailer/domain_expires_soon.fr.text.erb +++ b/app/views/notifications_mailer/domain_expires_soon.fr.text.erb @@ -3,7 +3,7 @@ Salut, le domaine <%= @check.domain %> va expirer le <%= format_utc(@check.domain_expires_at) %>. -<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_recurrent_failures.en.html.erb b/app/views/notifications_mailer/domain_recurrent_failures.en.html.erb deleted file mode 100644 index 2146dbc..0000000 --- a/app/views/notifications_mailer/domain_recurrent_failures.en.html.erb +++ /dev/null @@ -1,30 +0,0 @@ -

- Hi, -
-
- - We had recurrent failures while checking the whois database for domain - <%= @check.domain %>. As of today, we can't anymore verify the expiry date. -

- -

- <%- if @check.domain_expires_at.present? %> - Our last known expiry date is <%= format_utc(@check.domain_expires_at) %>. -
- <% end %> - - <%- if @check.last_success_at.present? %> - Our last successful check occured <%= format_utc(@check.last_success_at) %>. - <% end %> -

- -

-If you have deleted the domain or have not renewed it, please disable -or delete the check by following this link:

-<%= link_to nil, edit_check_url(@check) %> -

-
-<%= render "check_comment_vendor" %> - - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_recurrent_failures.en.text.erb b/app/views/notifications_mailer/domain_recurrent_failures.en.text.erb deleted file mode 100644 index b5ce466..0000000 --- a/app/views/notifications_mailer/domain_recurrent_failures.en.text.erb +++ /dev/null @@ -1,20 +0,0 @@ -Hi, - -We had recurrent failures while checking the whois database for domain -<%= @check.domain %>. As of today, we can't anymore verify the expiry date. - -<%- if @check.domain_expires_at.present? %> -The last known expiry date is <%= format_utc(@check.domain_expires_at) %>. -<% end %> -<%- if @check.last_success_at.present? %> -The last successful check occured <%= format_utc(@check.last_success_at) %>. -<% end %> - -If you have deleted your domain or have not renewed it, please disable -or delete the check by following this link: - -<%= edit_check_url(@check) %> - -<%= render "check_comment_vendor" %> - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_recurrent_failures.fr.html.erb b/app/views/notifications_mailer/domain_recurrent_failures.fr.html.erb deleted file mode 100644 index 7da5aed..0000000 --- a/app/views/notifications_mailer/domain_recurrent_failures.fr.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -

- Salut, -
-
- - Nous avons rencontré de multiples erreurs pendant l'exécution des vérifications du domaine <%= @check.domain %>. - Nous ne pouvons plus interroger la base Whois pour vérifier la date d'expiration. -

- -

- <%- if @check.domain_expires_at.present? %> - La dernière date d'expiration connue est le <%= format_utc(@check.domain_expires_at) %>. -
- <% end %> - - <%- if @check.last_success_at.present? %> - Notre dernière vérification réussie a eu lieu le <%= format_utc(@check.last_success_at) %>. - <% end %> -

- -

-Si vous avez supprimé le domaine ou ne l'avez pas renouvellé, merci de désactiver la vérification associée, avec ce lien :

-<%= link_to nil, edit_check_url(@check) %> -

-
-<%= render "check_comment_vendor" %> - - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/domain_recurrent_failures.fr.text.erb b/app/views/notifications_mailer/domain_recurrent_failures.fr.text.erb deleted file mode 100644 index cb8673e..0000000 --- a/app/views/notifications_mailer/domain_recurrent_failures.fr.text.erb +++ /dev/null @@ -1,20 +0,0 @@ -Salut, - -Nous avons rencontré de multiples erreurs pendant l'exécution des vérifications -du domaine <%= @check.domain %>. -Nous ne pouvons plus interroger la base Whois pour vérifier la date d'expiration. - -<%- if @check.domain_expires_at.present? %> -La dernière date d'expiration connue est le <%= format_utc(@check.domain_expires_at) %>. -<% end %> -<%- if @check.last_success_at.present? %> -Notre dernière vérification réussie a eu lieu le <%= format_utc(@check.last_success_at) %>. -<% end %> - -Si vous avez supprimé le domaine ou ne l'avez pas renouvellé, -merci de désactiver la vérification associée, avec ce lien : -<%= edit_check_url(@check) %> - -<%= render "check_comment_vendor" %> - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/recurrent_failures.en.html.erb b/app/views/notifications_mailer/recurrent_failures.en.html.erb new file mode 100644 index 0000000..fee55f1 --- /dev/null +++ b/app/views/notifications_mailer/recurrent_failures.en.html.erb @@ -0,0 +1,22 @@ +

+ Hi, +
+
+ + <%= t(".header", count: @checks.count) %> +

+ + +<%- @checks.each_with_index do |check, index| %> + <%- if index > 0 -%> +
+
+
+ <%- end -%> + + <%= render "#{check.kind}_recurrent_failure", check: check %> +<%- end -%> + +
+
+<%= render "footer_recurrent_failures" %> diff --git a/app/views/notifications_mailer/recurrent_failures.en.text.erb b/app/views/notifications_mailer/recurrent_failures.en.text.erb new file mode 100644 index 0000000..d58006b --- /dev/null +++ b/app/views/notifications_mailer/recurrent_failures.en.text.erb @@ -0,0 +1,14 @@ +Hi, + +<%= t(".header", count: @checks.count) %> + +<%- @checks.each_with_index do |check, index| %> + <%- if index > 0 -%> + + <%- end -%> + +<%= render "#{check.kind}_recurrent_failure", check: check %> +<%- end -%> + + +<%= render "footer_recurrent_failures" %> diff --git a/app/views/notifications_mailer/recurrent_failures.fr.html.erb b/app/views/notifications_mailer/recurrent_failures.fr.html.erb new file mode 100644 index 0000000..513a848 --- /dev/null +++ b/app/views/notifications_mailer/recurrent_failures.fr.html.erb @@ -0,0 +1,22 @@ +

+ Salut, +
+
+ + <%= t(".header", count: @checks.count) %> +

+ + +<%- @checks.each_with_index do |check, index| %> + <%- if index > 0 -%> +
+
+
+ <%- end -%> + + <%= render "#{check.kind}_recurrent_failure", check: check %> +<%- end -%> + +
+
+<%= render "footer_recurrent_failures" %> diff --git a/app/views/notifications_mailer/recurrent_failures.fr.text.erb b/app/views/notifications_mailer/recurrent_failures.fr.text.erb new file mode 100644 index 0000000..3726ef9 --- /dev/null +++ b/app/views/notifications_mailer/recurrent_failures.fr.text.erb @@ -0,0 +1,14 @@ +Salut, + +<%= t(".header", count: @checks.count) %> + +<%- @checks.each_with_index do |check, index| %> + <%- if index > 0 -%> + + <%- end -%> + +<%= render "#{check.kind}_recurrent_failure", check: check %> +<%- end -%> + + +<%= render "footer_recurrent_failures" %> diff --git a/app/views/notifications_mailer/ssl_expires_soon.en.html.erb b/app/views/notifications_mailer/ssl_expires_soon.en.html.erb index 401df10..f0a5515 100644 --- a/app/views/notifications_mailer/ssl_expires_soon.en.html.erb +++ b/app/views/notifications_mailer/ssl_expires_soon.en.html.erb @@ -7,6 +7,6 @@


-<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_expires_soon.en.text.erb b/app/views/notifications_mailer/ssl_expires_soon.en.text.erb index a1d1c56..73e13dd 100644 --- a/app/views/notifications_mailer/ssl_expires_soon.en.text.erb +++ b/app/views/notifications_mailer/ssl_expires_soon.en.text.erb @@ -3,7 +3,7 @@ Hi, the SSL certificate for <%= @check.domain %> will expire <%= format_utc(@check.domain_expires_at) %>. -<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_expires_soon.fr.html.erb b/app/views/notifications_mailer/ssl_expires_soon.fr.html.erb index dfc1c3f..6869687 100644 --- a/app/views/notifications_mailer/ssl_expires_soon.fr.html.erb +++ b/app/views/notifications_mailer/ssl_expires_soon.fr.html.erb @@ -7,6 +7,6 @@


-<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_expires_soon.fr.text.erb b/app/views/notifications_mailer/ssl_expires_soon.fr.text.erb index 0664fb5..9f6999c 100644 --- a/app/views/notifications_mailer/ssl_expires_soon.fr.text.erb +++ b/app/views/notifications_mailer/ssl_expires_soon.fr.text.erb @@ -3,7 +3,7 @@ Salut, le certificat SSL pour <%= @check.domain %> va expirer le <%= format_utc(@check.domain_expires_at) %>. -<%= render "check_comment_vendor" %> +<%= render "check_comment_vendor", check: @check %> <%= render "footer_expires_soon", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_recurrent_failures.en.html.erb b/app/views/notifications_mailer/ssl_recurrent_failures.en.html.erb deleted file mode 100644 index 602c32d..0000000 --- a/app/views/notifications_mailer/ssl_recurrent_failures.en.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -

- Hi, -
-
- - We had recurrent failures while checking the SSL certificate for - <%= @check.domain %>. As of today, we can no longer verify the certificate - expiry date. -

- -

- <%- if @check.domain_expires_at.present? %> - Our last known expiry date is <%= format_utc(@check.domain_expires_at) %>. -
- <%- end %> - - <%- if @check.last_success_at.present? %> - Our last successful check occured <%= format_utc(@check.last_success_at) %>. - <%- end %> -

- -

- If there is no more SSL endpoint for this domain, please disable - or delete the check by following this link:

- <%= link_to nil, edit_check_url(@check) %> -

-
-<%= render "check_comment_vendor" %> - - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_recurrent_failures.en.text.erb b/app/views/notifications_mailer/ssl_recurrent_failures.en.text.erb deleted file mode 100644 index 2bb6dd3..0000000 --- a/app/views/notifications_mailer/ssl_recurrent_failures.en.text.erb +++ /dev/null @@ -1,22 +0,0 @@ -Hi, - -We had recurrent failures while checking the SSL certificate for -<%= @check.domain %>. As of today, we can no longer verify the certificate -expiry date. - -<%- if @check.domain_expires_at.present? %> -The last known expiry date is <%= format_utc(@check.domain_expires_at) %>. -<%- end %> - -<%- if @check.last_success_at.present? %> -The last successful check occured <%= format_utc(@check.last_success_at) %>. -<%- end %> - -If there is no more SSL endpoint for this domain, please disable -or delete the check by following this link: - -<%= edit_check_url(@check) %> - -<%= render "check_comment_vendor" %> - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_recurrent_failures.fr.html.erb b/app/views/notifications_mailer/ssl_recurrent_failures.fr.html.erb deleted file mode 100644 index ea05abc..0000000 --- a/app/views/notifications_mailer/ssl_recurrent_failures.fr.html.erb +++ /dev/null @@ -1,35 +0,0 @@ -

- Salut, -
-
- - Nous avons rencontré de multiples erreurs - pendant l'exécution des vérifications du certificat SSL - du site <%= @check.domain %>. - Nous ne pouvons plus vérifier la date d'expiration du certificat - en nous connectant au site. -

- -

- <%- if @check.domain_expires_at.present? %> - La dernière date d'expiration connue est le <%= format_utc(@check.domain_expires_at) %>. -
- <% end %> - - <%- if @check.last_success_at.present? %> - Notre dernière vérification réussie a eu lieu le <%= format_utc(@check.last_success_at) %>. - <% end %> -

- -

- S'il n'y a plus de terminaison SSL pour ce site ou s'il n'existe plus, - merci de désactiver la vérification associée, avec ce lien : -
-
- <%= link_to nil, edit_check_url(@check) %> -

-
-<%= render "check_comment_vendor" %> - - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/app/views/notifications_mailer/ssl_recurrent_failures.fr.text.erb b/app/views/notifications_mailer/ssl_recurrent_failures.fr.text.erb deleted file mode 100644 index fe15787..0000000 --- a/app/views/notifications_mailer/ssl_recurrent_failures.fr.text.erb +++ /dev/null @@ -1,22 +0,0 @@ -Salut, - -Nous avons rencontré de multiples erreurs pendant l'exécution des vérifications -du certificat SSL pour le site <%= @check.domain %>. -Nous ne pouvons plus vérifier la date d'expiration du certificat -en nous connectant au site. - -<%- if @check.domain_expires_at.present? %> -La dernière date d'expiration connue est le <%= format_utc(@check.domain_expires_at) %>. -<% end %> - -<%- if @check.last_success_at.present? %> -Notre dernière vérification réussie a eu lieu le <%= format_utc(@check.last_success_at) %>. -<% end %> - -S'il n'y a plus de terminaison SSL pour ce site ou s'il n'existe plus, -merci de désactiver la vérification associée, avec ce lien : -<%= edit_check_url(@check) %> - -<%= render "check_comment_vendor" %> - -<%= render "footer_recurrent_failures", interval: @notification.interval, check: @check %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 554bb3f..7fb3d01 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -40,8 +40,13 @@ en: one: "Domain %{domain} expires TOMORROW!" other: "Domain %{domain} expires in %{count} days" - domain_recurrent_failures: - subject: "Recurrent failures in %{domain} domain expiry check" + recurrent_failures: + subject: + one: "Recurrent failures for %{domain} expiry check" + other: "%{count} checks in error" + header: + one: "We had recurrent failures during excution of the following check:" + other: "We had recurrent failures during excution of the %{count} following checks:" ssl_expires_soon: subject: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 52241a1..6be381e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -75,8 +75,13 @@ fr: one: "Le domaine %{domain} expire DEMAIN !" other: "Le domaine %{domain} expire dans %{count} jours" - domain_recurrent_failures: - subject: "Erreurs dans la vérification d'expiration du domaine %{domain}" + recurrent_failures: + subject: + one: "Erreurs de vérification d'expiration pour %{domain}" + other: "%{count} vérifications en erreur" + header: + one: "Nous avons rencontré des erreurs pendant l'exécution d'une de vos vérifications :" + other: "Nous avons rencontré des erreurs pendant l'exécution des %{count} vérifications suivantes :" ssl_expires_soon: subject: @@ -84,10 +89,6 @@ fr: one: "Le certificat SSL pour %{domain} expire DEMAIN !" other: "Le certificat SSL pour %{domain} expire dans %{count} jours" - ssl_recurrent_failures: - subject: "Erreurs dans la vérification d'expiration du certificat SSL %{domain}" - - shared: locales: en: Anglais diff --git a/db/seeds.rb b/db/seeds.rb index 996d657..1f353fb 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -43,6 +43,7 @@ check_chexpire_org_error = Check.create!( vendor: "Some random registrar", last_run_at: 20.minutes.ago, created_at: 3.weeks.ago, + consecutive_failures: 4, ) ssl_check_chexpire_org = Check.create!( @@ -67,6 +68,7 @@ ssl_check_chexpire_org_error = Check.create!( vendor: "Some random registrar", last_run_at: 20.minutes.ago, last_success_at: 4.days.ago, + consecutive_failures: 8, ) diff --git a/test/factories/checks.rb b/test/factories/checks.rb index 451c285..a0cf919 100644 --- a/test/factories/checks.rb +++ b/test/factories/checks.rb @@ -65,7 +65,7 @@ FactoryBot.define do end trait :last_runs_failed do - consecutive_failures 4 + consecutive_failures 5 last_run_at 3.days.ago - 90.minutes last_success_at 7.days.ago - 2.hours end diff --git a/test/factories/users.rb b/test/factories/users.rb index 4566543..1f8789b 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -40,5 +40,9 @@ FactoryBot.define do notifications_enabled true locale "en" tos_accepted true + + trait :fr do + locale "fr" + end end end diff --git a/test/mailers/notifications_mailer_test.rb b/test/mailers/notifications_mailer_test.rb index c261131..6c3f7c4 100644 --- a/test/mailers/notifications_mailer_test.rb +++ b/test/mailers/notifications_mailer_test.rb @@ -31,32 +31,32 @@ class NotificationsMailerTest < ActionMailer::TestCase # rubocop:disable Metrics end test "domain_expires_soon FR" do - check = create(:check, domain_expires_at: Time.new(2018, 6, 10, 12, 0, 5, "+02:00")) + check = create(:check, + domain_expires_at: Time.new(2018, 6, 10, 12, 0, 5, "+02:00"), + user: build(:user, :fr)) notification = build(:notification, interval: 10, check: check, recipient: "colin@example.org") - I18n.with_locale :fr do - Date.stub :today, Date.new(2018, 6, 2) do - mail = NotificationsMailer.with(notification: notification).domain_expires_soon + Date.stub :today, Date.new(2018, 6, 2) do + mail = NotificationsMailer.with(notification: notification).domain_expires_soon - assert_emails 1 do - mail.deliver_now - end + assert_emails 1 do + mail.deliver_now + end - assert_match "domain.fr", mail.subject - assert_match "dans 8 jours", mail.subject - assert_equal ["colin@example.org"], mail.to - assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from + assert_match "domain.fr", mail.subject + assert_match "dans 8 jours", mail.subject + assert_equal ["colin@example.org"], mail.to + assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from - parts = [mail.text_part.decode_body, mail.html_part.decode_body] + parts = [mail.text_part.decode_body, mail.html_part.decode_body] - parts.each do |part| - assert_match "domain.fr", part - assert_match "dim 10 juin 2018 10:00:05 +0000", part - assert_match "10 jours", part - assert_match "/checks/#{check.id}/edit", part - assert_no_match "commentaire", part - assert_no_match "fournisseur", part - end + parts.each do |part| + assert_match "domain.fr", part + assert_match "dim 10 juin 2018 10:00:05 +0000", part + assert_match "10 jours", part + assert_match "/checks/#{check.id}/edit", part + assert_no_match "commentaire", part + assert_no_match "fournisseur", part end end end @@ -82,36 +82,34 @@ class NotificationsMailerTest < ActionMailer::TestCase # rubocop:disable Metrics check = create(:check, domain_expires_at: 1.week.from_now, comment: "My comment", - vendor: "The vendor") + vendor: "The vendor", + user: build(:user, :fr)) notification = build(:notification, check: check) - I18n.with_locale :fr do - mail = NotificationsMailer.with(notification: notification).domain_expires_soon + mail = NotificationsMailer.with(notification: notification).domain_expires_soon - parts = [mail.text_part.decode_body, mail.html_part.decode_body] + parts = [mail.text_part.decode_body, mail.html_part.decode_body] - parts.each do |part| - assert_match "commentaire", part - assert_match "Fournisseur", part - end + parts.each do |part| + assert_match "commentaire", part + assert_match "Fournisseur", part end end - test "domain_recurrent_failures" do + test "recurrent_failures" do last_success_at = Time.new(2018, 5, 30, 6, 10, 0, "+00:00") domain_expires_at = Time.new(2018, 10, 10, 7, 20, 0, "+04:00") - check = build(:check, :last_runs_failed, - domain: "invalid-domain.fr", - last_success_at: last_success_at, - domain_expires_at: domain_expires_at, - comment: "My comment") - notification = create(:notification, check: check) + check = create(:check, :last_runs_failed, + domain: "invalid-domain.fr", + last_success_at: last_success_at, + domain_expires_at: domain_expires_at, + comment: "My comment") - mail = NotificationsMailer.with(notification: notification).domain_recurrent_failures + mail = NotificationsMailer.recurrent_failures(check.user, [check]) assert_match "failures", mail.subject assert_match "invalid-domain.fr", mail.subject - assert_equal ["recipient@domain.fr"], mail.to + assert_match(/user-\d+@chexpire.org/, mail.to.first) assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from parts = [mail.text_part.decode_body, mail.html_part.decode_body] @@ -119,41 +117,39 @@ class NotificationsMailerTest < ActionMailer::TestCase # rubocop:disable Metrics parts.each do |part| assert_match "invalid-domain.fr", part assert_match "recurrent failures", part - assert_match(/success[a-z ]+ Wed, 30 May 2018 06:10:00 \+0000/, part) - assert_match(/expiry[a-z ]+ Wed, 10 Oct 2018 03:20:00 \+0000/, part) + assert_match(/success[a-z: ]+ Wed, 30 May 2018 06:10:00 \+0000/, part) + assert_match(/expiry[a-z: ]+ Wed, 10 Oct 2018 03:20:00 \+0000/, part) assert_match "My comment", part assert_match "/checks/#{check.id}/edit", part end end - test "domain_recurrent_failures - FR" do + test "recurrent_failures - FR" do last_success_at = Time.new(2018, 5, 30, 6, 10, 0, "+00:00") domain_expires_at = Time.new(2018, 10, 10, 7, 20, 0, "+04:00") - check = build(:check, :last_runs_failed, - domain: "invalid-domain.fr", - last_success_at: last_success_at, - domain_expires_at: domain_expires_at, - comment: "My comment") - notification = create(:notification, check: check) + check = create(:check, :last_runs_failed, + domain: "invalid-domain.fr", + last_success_at: last_success_at, + domain_expires_at: domain_expires_at, + comment: "My comment", + user: build(:user, :fr)) - I18n.with_locale :fr do - mail = NotificationsMailer.with(notification: notification).domain_recurrent_failures - assert_match "Erreurs", mail.subject - assert_match "invalid-domain.fr", mail.subject + mail = NotificationsMailer.recurrent_failures(check.user, [check]) + assert_match "Erreurs", mail.subject + assert_match "invalid-domain.fr", mail.subject - assert_equal ["recipient@domain.fr"], mail.to - assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from + assert_match(/user-\d+@chexpire.org/, mail.to.first) + assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from - parts = [mail.text_part.decode_body, mail.html_part.decode_body] + parts = [mail.text_part.decode_body, mail.html_part.decode_body] - parts.each do |part| - assert_match "invalid-domain.fr", part - assert_match "erreurs", part - assert_match(/réussie[a-z ]+ mer 30 mai 2018 06:10:00 \+0000/, part) - assert_match(/expiration[a-z ]+ mer 10 oct. 2018 03:20:00 \+0000/, part) - assert_match "commentaire", part - assert_match "/checks/#{check.id}/edit", part - end + parts.each do |part| + assert_match "invalid-domain.fr", part + assert_match "erreurs", part + assert_match(/réussie[a-z: ]+ mer 30 mai 2018 06:10:00 \+0000/, part) + assert_match(/expiration[a-z: ]+ mer 10 oct. 2018 03:20:00 \+0000/, part) + assert_match "commentaire", part + assert_match "/checks/#{check.id}/edit", part end end @@ -188,95 +184,33 @@ class NotificationsMailerTest < ActionMailer::TestCase # rubocop:disable Metrics end test "ssl_expires_soon - FR" do - check = create(:check, :ssl, domain_expires_at: Time.new(2018, 6, 10, 12, 0, 5, "+02:00")) + check = create(:check, :ssl, + domain_expires_at: Time.new(2018, 6, 10, 12, 0, 5, "+02:00"), + user: build(:user, :fr)) notification = build(:notification, interval: 10, check: check, recipient: "colin@example.org") - I18n.with_locale :fr do - Date.stub :today, Date.new(2018, 6, 2) do - mail = NotificationsMailer.with(notification: notification).ssl_expires_soon + Date.stub :today, Date.new(2018, 6, 2) do + mail = NotificationsMailer.with(notification: notification).ssl_expires_soon - assert_emails 1 do - mail.deliver_now - end - - assert_match "domain.fr", mail.subject - assert_match "SSL", mail.subject - assert_match "dans 8 jours", mail.subject - assert_equal ["colin@example.org"], mail.to - assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from - - parts = [mail.text_part.decode_body, mail.html_part.decode_body] - - parts.each do |part| - assert_match "domain.fr", part - assert_match "dim 10 juin 2018 10:00:05 +0000", part - assert_match "10 jours", part - assert_match "/checks/#{check.id}/edit", part - assert_no_match "commentaire", part - assert_no_match "fournisseur", part - end + assert_emails 1 do + mail.deliver_now end - end - end - test "ssl_recurrent_failures" do - last_success_at = Time.new(2018, 5, 30, 6, 10, 0, "+00:00") - domain_expires_at = Time.new(2018, 10, 10, 7, 20, 0, "+04:00") - check = build(:check, :ssl, :last_runs_failed, - domain: "invalid-domain.fr", - last_success_at: last_success_at, - domain_expires_at: domain_expires_at, - comment: "My comment") - notification = create(:notification, check: check) - - mail = NotificationsMailer.with(notification: notification).ssl_recurrent_failures - assert_match "failures", mail.subject - assert_match "invalid-domain.fr", mail.subject - assert_match "SSL", mail.subject - - assert_equal ["recipient@domain.fr"], mail.to - assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from - - parts = [mail.text_part.decode_body, mail.html_part.decode_body] - - parts.each do |part| - assert_match "invalid-domain.fr", part - assert_match "recurrent failures", part - assert_match(/success[a-z ]+ Wed, 30 May 2018 06:10:00 \+0000/, part) - assert_match(/expiry[a-z ]+ Wed, 10 Oct 2018 03:20:00 \+0000/, part) - assert_match "My comment", part - assert_match "/checks/#{check.id}/edit", part - end - end - - test "ssl_recurrent_failures - FR" do - last_success_at = Time.new(2018, 5, 30, 6, 10, 0, "+00:00") - domain_expires_at = Time.new(2018, 10, 10, 7, 20, 0, "+04:00") - check = build(:check, :ssl, :last_runs_failed, - domain: "invalid-domain.fr", - last_success_at: last_success_at, - domain_expires_at: domain_expires_at, - comment: "My comment") - notification = create(:notification, check: check) - - I18n.with_locale :fr do - mail = NotificationsMailer.with(notification: notification).ssl_recurrent_failures - assert_match "Erreurs", mail.subject - assert_match "invalid-domain.fr", mail.subject + assert_match "domain.fr", mail.subject assert_match "SSL", mail.subject - - assert_equal ["recipient@domain.fr"], mail.to + assert_match "dans 8 jours", mail.subject + assert_equal ["colin@example.org"], mail.to assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from parts = [mail.text_part.decode_body, mail.html_part.decode_body] parts.each do |part| - assert_match "invalid-domain.fr", part - assert_match "erreurs", part - assert_match(/réussie[a-z ]+ mer 30 mai 2018 06:10:00 \+0000/, part) - assert_match(/expiration[a-z ]+ mer 10 oct. 2018 03:20:00 \+0000/, part) - assert_match "commentaire", part + assert_match "domain.fr", part + assert_match "dim 10 juin 2018 10:00:05 +0000", part + assert_match "10 jours", part assert_match "/checks/#{check.id}/edit", part + assert_no_match "commentaire", part + assert_no_match "fournisseur", part end end end diff --git a/test/mailers/previews/notifications_mailer_preview.rb b/test/mailers/previews/notifications_mailer_preview.rb index 01f5085..5371b16 100644 --- a/test/mailers/previews/notifications_mailer_preview.rb +++ b/test/mailers/previews/notifications_mailer_preview.rb @@ -6,21 +6,16 @@ class NotificationsMailerPreview < ActionMailer::Preview NotificationsMailer.with(notification: check.notifications.first).domain_expires_soon end - # Preview this email at http://localhost:3000/rails/mailers/notifications_mailer/domain_recurrent_failures - def domain_recurrent_failures - check = Check.domain.where("last_run_at != last_success_at").first - NotificationsMailer.with(notification: check.notifications.first).domain_recurrent_failures - end - # Preview this email at http://localhost:3000/rails/mailers/notifications_mailer/ssl_expires_soon def ssl_expires_soon check = Check.ssl.first NotificationsMailer.with(notification: check.notifications.first).ssl_expires_soon end - # Preview this email at http://localhost:3000/rails/mailers/notifications_mailer/ssl_recurrent_failures - def ssl_recurrent_failures - check = Check.ssl.where("last_run_at != last_success_at").first - NotificationsMailer.with(notification: check.notifications.first).ssl_recurrent_failures + # Preview this email at http://localhost:3000/rails/mailers/notifications_mailer/recurrent_failures + def recurrent_failures + user = User.first + checks = Check.consecutive_failures(2).where(user: user) + NotificationsMailer.recurrent_failures(user, checks) end end diff --git a/test/services/notifier/channels/base_test.rb b/test/services/notifier/channels/base_test.rb index 16856e9..aed7653 100644 --- a/test/services/notifier/channels/base_test.rb +++ b/test/services/notifier/channels/base_test.rb @@ -5,8 +5,8 @@ module Notifier class BaseTest < ActiveSupport::TestCase setup do class FakeChannel < Base - def supports?(reason, _notification) - reason != :unsupported + def supports?(notification) + notification.interval < 1_000 end def domain_notify_expires_soon(*); end @@ -18,7 +18,7 @@ module Notifier test "#notify change the status of the notification" do notification = create(:notification) - @channel.notify(:expires_soon, notification) + @channel.notify(notification) notification.reload @@ -26,18 +26,30 @@ module Notifier assert_just_now notification.sent_at end - test "#notify raises an exception for a invalid reason" do - notification = build(:notification) + test "#notify raises an exception for a non supported check kind" do + notification = Minitest::Mock.new + notification.expect :ongoing!, true + notification.expect :interval, 10 + + check = Minitest::Mock.new + check.expect(:kind, :invalid_kind) + check.expect(:kind, :invalid_kind) # twice (second call for exception message) + + notification.expect :check, check + notification.expect :check, check assert_raises ArgumentError do - @channel.notify(:unknown, notification) + @channel.notify(notification) end + + check.verify + notification.verify end - test "#notify does nothing when channel does not support a reason" do - notification = create(:notification) + test "#notify does nothing when channel doesn't support a notification whatever the reason" do + notification = create(:notification, interval: 10_000) - @channel.notify(:unsupported, notification) + @channel.notify(notification) notification.reload diff --git a/test/services/notifier/processor_test.rb b/test/services/notifier/processor_test.rb index c44ffda..92b41c1 100644 --- a/test/services/notifier/processor_test.rb +++ b/test/services/notifier/processor_test.rb @@ -22,9 +22,9 @@ module Notifier end test "#process_recurrent_failures respects the interval configuration between sends" do - create_list(:notification, 3, :email, check: build(:check, :last_runs_failed)) + create_list(:check, 3, :last_runs_failed) test_interval_respected(:process_recurrent_failures, 3) do |configuration| - configuration.expect(:consecutive_failures, 4) + configuration.expect(:consecutive_failures, 4.2) end end diff --git a/test/services/notifier/resolver_test.rb b/test/services/notifier/resolver_test.rb index e43d474..2b291e7 100644 --- a/test/services/notifier/resolver_test.rb +++ b/test/services/notifier/resolver_test.rb @@ -6,58 +6,58 @@ module Notifier @resolver = Notifier::Resolver.new end - test "#resolve_expires_soon ignores user having notification disabled" do + test "#notifications_expiring_soon ignores user having notification disabled" do 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)) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_not_includes notifications, n1 assert_includes notifications, n2 end - test "#resolve_expires_soon ignores inactive checks" do + test "#notifications_expiring_soon ignores inactive checks" do n1 = create(:notification, check: build(:check, :expires_next_week, :inactive)) n2 = create(:notification, check: build(:check, :expires_next_week)) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_not_includes notifications, n1 assert_includes notifications, n2 end - test "#resolve_expires_soon gets only checks inside interval" do + test "#notifications_expiring_soon gets only checks inside interval" do n1 = create(:notification, check: build(:check, :expires_next_week), interval: 6) n2 = create(:notification, check: build(:check, :expires_next_week), interval: 7) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_not_includes notifications, n1 assert_includes notifications, n2 end - test "#resolve_expires_soon can gets several notifications for a same check" do + test "#notifications_expiring_soon can gets several notifications for a same check" do check = create(:check, :expires_next_week) n1 = create(:notification, check: check, interval: 3) n2 = create(:notification, check: check, interval: 10) n3 = create(:notification, check: check, interval: 30) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_not_includes notifications, n1 assert_includes notifications, n2 assert_includes notifications, n3 end - test "#resolve_expires_soon takes care of the status" do + test "#notifications_expiring_soon takes care of the status" do 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) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_includes notifications, n1 assert_includes notifications, n2 @@ -65,49 +65,51 @@ module Notifier assert_not_includes notifications, n4 end - test "#resolve_expires_soon ignores checks expired and without date" do + test "#notifications_expiring_soon ignores checks expired and without date" do 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)) - notifications = @resolver.resolve_expires_soon + notifications = @resolver.notifications_expiring_soon assert_includes notifications, n1 assert_not_includes notifications, n2 assert_not_includes notifications, n3 end - test "#resolve_check_failed ignores inactive checks" do - n1 = create(:notification, check: build(:check, :last_runs_failed, :inactive)) - n2 = create(:notification, check: build(:check, :last_runs_failed)) + test "#checks_recurrent_failures ignores inactive checks" do + c1 = create(:check, :last_runs_failed, :inactive) + c2 = create(:check, :last_runs_failed) - notifications = @resolver.resolve_check_failed + checks = @resolver.checks_recurrent_failures(4) - assert_not_includes notifications, n1 - assert_includes notifications, n2 + assert_not_includes checks, c1 + assert_includes checks, c2 end - test "#resolve_check_failed ignores user having notification disabled" do - n1 = create(:notification, check: build(:check, :last_runs_failed)) - n1.check.user.update_attribute(:notifications_enabled, false) - n2 = create(:notification, check: build(:check, :last_runs_failed)) + 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) - notifications = @resolver.resolve_check_failed + checks = @resolver.checks_recurrent_failures(4) - assert_not_includes notifications, n1 - assert_includes notifications, n2 + assert_not_includes checks, c1 + assert_includes checks, c2 end - test "#resolve_check_failed gets only checks having last run in error" do - n1 = create(:notification, check: build(:check, :nil_dates)) - n2 = create(:notification, check: build(:check, :last_run_succeed)) - n3 = create(:notification, check: build(:check, :last_runs_failed)) + 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) - notifications = @resolver.resolve_check_failed + checks = @resolver.checks_recurrent_failures(4) - assert_not_includes notifications, n1 - assert_not_includes notifications, n2 - assert_includes notifications, n3 + assert_not_includes checks, c1 + assert_not_includes checks, c2 + assert_not_includes checks, c4 + assert_includes checks, c3 end end end