mirror of
https://github.com/Evolix/chexpire.git
synced 2024-05-04 17:55:11 +02:00
Notifier channel email
NB: domain recurrent failures notification conditions needs to be implemented.
This commit is contained in:
parent
26340a9304
commit
9886e978fe
|
@ -1,2 +1,5 @@
|
|||
module ApplicationHelper
|
||||
def format_utc(time, format: :default)
|
||||
l(time.utc, format: format)
|
||||
end
|
||||
end
|
||||
|
|
22
app/mailers/notifications_mailer.rb
Normal file
22
app/mailers/notifications_mailer.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
class NotificationsMailer < ApplicationMailer
|
||||
helper :application
|
||||
|
||||
before_action 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
|
||||
end
|
||||
end
|
21
app/services/notifier/channels/email.rb
Normal file
21
app/services/notifier/channels/email.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Notifier
|
||||
module Channels
|
||||
class Email < Base
|
||||
REASONS = %i[expires_soon recurrent_failures].freeze
|
||||
|
||||
protected
|
||||
|
||||
def supports?(reason, _notification)
|
||||
REASONS.include?(reason)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
<%- if @check.comment.present? -%>
|
||||
<p>
|
||||
You wrote the following comment with this domain:
|
||||
|
||||
<blockquote>
|
||||
<%= @check.comment -%>
|
||||
</blockquote>
|
||||
</p>
|
||||
<%- end -%>
|
||||
|
||||
<%- if @check.vendor.present? -%>
|
||||
Vendor: <%= @check.vendor %>
|
||||
<% end %>
|
|
@ -0,0 +1,11 @@
|
|||
<%- if @check.comment.present? -%>
|
||||
You wrote the following comment with this domain:
|
||||
|
||||
<%= @check.comment -%>
|
||||
<%- end -%>
|
||||
|
||||
|
||||
|
||||
<%- if @check.vendor.present? -%>
|
||||
Vendor: <%= @check.vendor %>
|
||||
<% end %>
|
|
@ -0,0 +1,11 @@
|
|||
<br />
|
||||
<br />
|
||||
--
|
||||
|
||||
<p>
|
||||
You received this email because of the notification <%= delay %> days before the expiry date.<br />
|
||||
You can handle the notifications for this check by following this link:<br />
|
||||
<%= link_to nil, edit_check_url(check) %>
|
||||
</p>
|
||||
|
||||
<p>The Chexpire Team</p>
|
|
@ -0,0 +1,7 @@
|
|||
--
|
||||
|
||||
You received this email because of the notification <%= delay %> days before the expiry date.
|
||||
You can handle the notifications for this check by following this link:
|
||||
<%= edit_check_url(check) %>
|
||||
|
||||
The Chexpire Team
|
|
@ -0,0 +1,11 @@
|
|||
<br />
|
||||
<br />
|
||||
--
|
||||
|
||||
<p>You received this email because of the notification <%= delay %> days before
|
||||
the last known expiry date.<br />
|
||||
You can handle the check by following this link: <br />
|
||||
<%= link_to nil, edit_check_url(check) %>
|
||||
</p>
|
||||
|
||||
<p>The Chexpire Team</p>
|
|
@ -0,0 +1,8 @@
|
|||
--
|
||||
|
||||
You received this email because of the notification <%= delay %> days before
|
||||
the last known expiry date.
|
||||
You can handle the check by following this link:
|
||||
<%= edit_check_url(check) %>
|
||||
|
||||
The Chexpire Team
|
|
@ -0,0 +1,12 @@
|
|||
<p>
|
||||
Hi,
|
||||
<br />
|
||||
<br />
|
||||
the domain <strong><%= @check.domain %></strong> will expire
|
||||
<strong><%= format_utc(@check.domain_expires_at) %></strong>.
|
||||
</p>
|
||||
<br />
|
||||
|
||||
<%= render "check_comment_vendor" %>
|
||||
|
||||
<%= render "footer_expires_soon", delay: @notification.delay, check: @check %>
|
|
@ -0,0 +1,9 @@
|
|||
Hi,
|
||||
|
||||
the domain <%= @check.domain %> will expire <%= format_utc(@check.domain_expires_at) %>.
|
||||
|
||||
|
||||
<%= render "check_comment_vendor" %>
|
||||
|
||||
|
||||
<%= render "footer_expires_soon", delay: @notification.delay, check: @check %>
|
|
@ -0,0 +1,25 @@
|
|||
<p>
|
||||
Hi,
|
||||
<br />
|
||||
<br />
|
||||
|
||||
We had <strong>recurrent failures</strong> while checking the whois database for domain
|
||||
<strong><%= @check.domain %></strong>. As of today, we can't anymore verify the expiry date.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Our last known expiry date is <%= format_utc(@check.domain_expires_at) %>.
|
||||
<br />
|
||||
Our last successful check occured <%= format_utc(@check.last_success_at) %>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
If you have deleted the domain or have not renewed it, please disable
|
||||
or delete the check by following this link: <br /><br />
|
||||
<%= link_to nil, edit_check_url(@check) %>
|
||||
</p>
|
||||
<br />
|
||||
<%= render "check_comment_vendor" %>
|
||||
|
||||
|
||||
<%= render "footer_recurrent_failures", delay: @notification.delay, check: @check %>
|
|
@ -0,0 +1,28 @@
|
|||
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.
|
||||
|
||||
The last known expiry date is <%= format_utc(@check.domain_expires_at) %>.
|
||||
The last successful check occured <%= format_utc(@check.last_success_at) %>.
|
||||
|
||||
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) %>
|
||||
|
||||
|
||||
<%- if @check.comment.present? -%>
|
||||
You wrote the following comment with this domain:
|
||||
|
||||
<%= @check.comment -%>
|
||||
<%- end -%>
|
||||
|
||||
|
||||
|
||||
<%- if @check.vendor.present? -%>
|
||||
Vendor: <%= @check.vendor %>
|
||||
<% end %>
|
||||
|
||||
|
||||
<%= render "footer_recurrent_failures", delay: @notification.delay, check: @check %>
|
|
@ -19,6 +19,18 @@ en:
|
|||
|
||||
flashes:
|
||||
user_not_authorized: "You are not authorized to access to this resource."
|
||||
|
||||
notifications_mailer:
|
||||
domain_expires_soon:
|
||||
subject:
|
||||
zero: "Domain %{domain} expires TODAY !"
|
||||
one: "Domain %{domain} expires TOMORROW !"
|
||||
other: "Domain %{domain} expires in %{count} days"
|
||||
|
||||
domain_recurrent_failures:
|
||||
subject: "Recurrent failures in %{domain} domain expiry check"
|
||||
|
||||
|
||||
shared:
|
||||
navbar:
|
||||
sign_up: "Sign up"
|
||||
|
|
78
test/mailers/notifications_mailer_test.rb
Normal file
78
test/mailers/notifications_mailer_test.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
require "test_helper"
|
||||
|
||||
class NotificationsMailerTest < ActionMailer::TestCase
|
||||
test "domain_expires_soon" do
|
||||
check = create(:check, domain_expires_at: Time.new(2018, 6, 10, 12, 0, 5, "+02:00"))
|
||||
notification = build(:notification, delay: 10, check: check, recipient: "colin@example.org")
|
||||
|
||||
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_match "domain.fr", mail.subject
|
||||
assert_match "in 8 days", mail.subject
|
||||
assert_equal ["colin@example.org"], mail.to
|
||||
assert_equal [Rails.configuration.chexpire.fetch("mailer_default_from")], mail.from
|
||||
|
||||
parts = [mail.text_part.body.to_s, mail.html_part.to_s]
|
||||
|
||||
parts.each do |part|
|
||||
assert_match "domain.fr", part
|
||||
assert_match "Sun, 10 Jun 2018 10:00:05 +0000", part
|
||||
assert_match "10 days", part
|
||||
assert_match "/checks/#{check.id}/edit", part
|
||||
assert_no_match "comment", part
|
||||
assert_no_match "vendor", part
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "domain_expires_soon include comment & vendor" do
|
||||
check = create(:check,
|
||||
domain_expires_at: 1.week.from_now,
|
||||
comment: "My comment",
|
||||
vendor: "The vendor")
|
||||
notification = build(:notification, check: check)
|
||||
|
||||
mail = NotificationsMailer.with(notification: notification).domain_expires_soon
|
||||
|
||||
parts = [mail.text_part.body.to_s, mail.html_part.to_s]
|
||||
|
||||
parts.each do |part|
|
||||
assert_match "My comment", part
|
||||
assert_match "The vendor", part
|
||||
end
|
||||
end
|
||||
|
||||
test "domain_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)
|
||||
|
||||
mail = NotificationsMailer.with(notification: notification).domain_recurrent_failures
|
||||
assert_match "failures", 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
|
||||
|
||||
parts = [mail.text_part.body.to_s, mail.html_part.to_s]
|
||||
|
||||
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
|
||||
end
|
13
test/mailers/previews/notifications_mailer_preview.rb
Normal file
13
test/mailers/previews/notifications_mailer_preview.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Preview all emails at http://localhost:3000/rails/mailers/notifications_mailer
|
||||
class NotificationsMailerPreview < ActionMailer::Preview
|
||||
# Preview this email at http://localhost:3000/rails/mailers/notifications_mailer/domain_expires_soon
|
||||
def domain_expires_soon
|
||||
NotificationsMailer.with(notification: Notification.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.where("last_run_at != last_success_at").limit(1).first
|
||||
NotificationsMailer.with(notification: check.notifications.first).domain_recurrent_failures
|
||||
end
|
||||
end
|
|
@ -2,6 +2,29 @@ require "test_helper"
|
|||
|
||||
module Notifier
|
||||
class ProcessorTest < ActiveSupport::TestCase
|
||||
test "#process_expires_soon sends an email for checks expiring soon" do
|
||||
create_list(:notification, 3, :email, check: build(:check, :expires_next_week))
|
||||
create(:notification, :email, check: build(:check, :nil_dates))
|
||||
create(:notification, :email, check: build(:check, :inactive))
|
||||
|
||||
processor = Processor.new
|
||||
assert_difference "ActionMailer::Base.deliveries.size", +3 do
|
||||
processor.process_expires_soon
|
||||
end
|
||||
|
||||
last_email = ActionMailer::Base.deliveries.last
|
||||
assert_match "expires in 7 days", last_email.subject
|
||||
end
|
||||
|
||||
test "#process_expires_soon respects the interval configuration between sends" do
|
||||
create_list(:notification, 3, :email, check: build(:check, :expires_next_week))
|
||||
test_interval_respected(:process_expires_soon, 3)
|
||||
end
|
||||
|
||||
test "#process_recurrent_failures respects the interval configuration between sends" do
|
||||
create_list(:notification, 3, :email, check: build(:check, :last_runs_failed))
|
||||
test_interval_respected(:process_recurrent_failures, 3)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
|
Loading…
Reference in a new issue