foreman_expire_hosts 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +28 -2
- data/.rubocop_todo.yml +10 -47
- data/README.md +1 -0
- data/app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb +11 -13
- data/app/helpers/concerns/foreman_expire_hosts/audits_helper_extensions.rb +2 -2
- data/app/helpers/concerns/foreman_expire_hosts/hosts_helper_extensions.rb +8 -11
- data/app/mailers/expire_hosts_mailer.rb +72 -20
- data/app/models/concerns/foreman_expire_hosts/host_ext.rb +3 -3
- data/app/models/host_status/expiration_status.rb +1 -1
- data/app/models/setting/expire_hosts.rb +2 -2
- data/app/services/foreman_expire_hosts/action/base.rb +57 -0
- data/app/services/foreman_expire_hosts/action/delete_expired_hosts.rb +23 -0
- data/app/services/foreman_expire_hosts/action/stop_expired_hosts.rb +37 -0
- data/app/services/foreman_expire_hosts/expiry_edit_authorizer.rb +21 -0
- data/app/services/foreman_expire_hosts/notification/base.rb +71 -0
- data/app/services/foreman_expire_hosts/notification/deleted_hosts.rb +15 -0
- data/app/services/foreman_expire_hosts/notification/expiry_warning.rb +26 -0
- data/app/services/foreman_expire_hosts/notification/failed_deleted_hosts.rb +15 -0
- data/app/services/foreman_expire_hosts/notification/failed_stopped_hosts.rb +15 -0
- data/app/services/foreman_expire_hosts/notification/stopped_hosts.rb +26 -0
- data/app/services/foreman_expire_hosts/safe_destroy.rb +23 -0
- data/app/services/foreman_expire_hosts/ui_notifications/hosts/base.rb +68 -0
- data/app/services/foreman_expire_hosts/ui_notifications/hosts/expiry_warning.rb +28 -0
- data/app/services/foreman_expire_hosts/ui_notifications/hosts/stopped_host.rb +11 -0
- data/app/views/expire_hosts_mailer/expiry_warning_notification.html.erb +4 -1
- data/app/views/expire_hosts_mailer/stopped_hosts_notification.html.erb +4 -1
- data/db/seeds.d/80_expire_hosts_ui_notification.rb +30 -0
- data/foreman_expire_hosts.gemspec +2 -2
- data/lib/expire_hosts_notifications.rb +7 -104
- data/lib/foreman_expire_hosts/engine.rb +14 -8
- data/lib/foreman_expire_hosts/version.rb +1 -1
- data/lib/tasks/expired_hosts.rake +7 -5
- data/test/factories/foreman_expire_hosts_factories.rb +1 -1
- data/test/functional/concerns/hosts_controller_extensions_test.rb +8 -6
- data/test/lib/expire_hosts_notifications_test.rb +128 -32
- data/test/test_plugin_helper.rb +8 -3
- data/test/unit/concerns/host_extensions_test.rb +17 -17
- data/test/unit/expire_hosts_mailer_test.rb +47 -27
- data/test/unit/expiry_edit_authorizer_test.rb +55 -0
- data/test/unit/host_status/expiration_status_test.rb +1 -1
- data/test/unit/safe_destroy_test.rb +26 -0
- metadata +30 -11
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
|
19
19
|
s.require_paths = ['lib']
|
20
20
|
|
21
|
-
s.add_dependency 'deface'
|
22
21
|
s.add_dependency 'bootstrap-datepicker-rails'
|
22
|
+
s.add_dependency 'deface'
|
23
23
|
|
24
|
-
s.add_development_dependency 'rubocop'
|
25
24
|
s.add_development_dependency 'rdoc'
|
25
|
+
s.add_development_dependency 'rubocop', '0.51.0'
|
26
26
|
end
|
@@ -1,120 +1,23 @@
|
|
1
1
|
module ExpireHostsNotifications
|
2
2
|
class << self
|
3
|
-
|
4
|
-
def admin_email
|
5
|
-
[(Setting[:host_expiry_email_recipients] || Setting[:administrator])]
|
6
|
-
end
|
7
|
-
|
8
|
-
def days_to_delete_after_expired
|
9
|
-
Setting[:days_to_delete_after_host_expiration].to_i
|
10
|
-
end
|
11
|
-
|
12
|
-
def catch_delivery_errors(message, hosts = [])
|
13
|
-
yield
|
14
|
-
rescue => error
|
15
|
-
message = _('%{message} for Hosts %{hosts}') % {:message => message, :hosts => hosts.map(&:name).to_sentence}
|
16
|
-
Foreman::Logging.exception(message, error)
|
17
|
-
end
|
18
|
-
|
19
|
-
# This method to deliver deleted host details to its owner
|
20
3
|
def delete_expired_hosts
|
21
|
-
|
22
|
-
failed_delete_hosts = []
|
23
|
-
deleted_hosts = []
|
24
|
-
deletable_hosts.each do |deletable_host|
|
25
|
-
Rails.logger.info "Deleting expired host #{deletable_host}"
|
26
|
-
deletable_host.audit_comment = _('Destroyed since it got expired on %s') % deletable_host.expired_on
|
27
|
-
if deletable_host.destroy
|
28
|
-
deleted_hosts << deletable_host
|
29
|
-
else
|
30
|
-
failed_delete_hosts << deletable_host
|
31
|
-
end
|
32
|
-
end
|
33
|
-
unless deleted_hosts.empty?
|
34
|
-
ExpireHostsNotifications.hosts_by_user(deleted_hosts).each do |user_id, hosts_hash|
|
35
|
-
catch_delivery_errors(_('Failed to deliver deleted hosts notification'), deleted_hosts) do
|
36
|
-
ExpireHostsMailer.deleted_hosts_notification(hosts_hash['email'], hosts_hash['hosts']).deliver_now
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
return if failed_delete_hosts.empty?
|
41
|
-
catch_delivery_errors(_('Failed to deliver deleted hosts notification failed status'), failed_delete_hosts) do
|
42
|
-
ExpireHostsMailer.failed_to_delete_hosts_notification(self.admin_email, failed_delete_hosts).deliver_now
|
43
|
-
end
|
4
|
+
ForemanExpireHosts::Action::DeleteExpiredHosts.new.engage
|
44
5
|
end
|
45
6
|
|
46
7
|
def stop_expired_hosts
|
47
|
-
|
48
|
-
failed_stop_hosts = []
|
49
|
-
stopped_hosts = []
|
50
|
-
stoppable_hosts.each do |stoppable_host|
|
51
|
-
next unless stoppable_host.supports_power_and_running?
|
52
|
-
Rails.logger.info "Powering down expired host in grace period #{stoppable_host}"
|
53
|
-
host_status = begin
|
54
|
-
stoppable_host.power.stop
|
55
|
-
rescue
|
56
|
-
false
|
57
|
-
end
|
58
|
-
if host_status
|
59
|
-
stopped_hosts << stoppable_host
|
60
|
-
else
|
61
|
-
failed_stop_hosts << stoppable_host
|
62
|
-
end
|
63
|
-
end
|
64
|
-
unless stopped_hosts.empty?
|
65
|
-
delete_date = (Date.today + self.days_to_delete_after_expired.to_i)
|
66
|
-
hosts_by_user(stopped_hosts).each do |user_id, hosts_hash|
|
67
|
-
catch_delivery_errors(_('Failed to deliver stopped hosts notification'), stopped_hosts) do
|
68
|
-
ExpireHostsMailer.stopped_hosts_notification(hosts_hash['email'], delete_date, hosts_hash['hosts']).deliver_now
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
return if failed_stop_hosts.empty?
|
73
|
-
catch_delivery_errors(_('Failed to deliver stopped hosts notification failed status'), failed_stop_hosts) do
|
74
|
-
ExpireHostsMailer.failed_to_stop_hosts_notification(self.admin_email, failed_stop_hosts).deliver_now
|
75
|
-
end
|
8
|
+
ForemanExpireHosts::Action::StopExpiredHosts.new.engage
|
76
9
|
end
|
77
10
|
|
78
11
|
def deliver_expiry_warning_notification(num = 1) # notify1_days_before_expiry
|
79
12
|
return unless [1, 2].include?(num)
|
80
13
|
days_before_expiry = Setting["notify#{num}_days_before_host_expiry"].to_i
|
81
14
|
expiry_date = (Date.today + days_before_expiry)
|
82
|
-
notifiable_hosts = Host.with_expire_date(expiry_date)
|
83
|
-
return if notifiable_hosts.empty?
|
84
|
-
hosts_by_user(notifiable_hosts).each do |user_id, hosts_hash|
|
85
|
-
catch_delivery_errors(_('Failed to deliver expiring hosts notification'), notifiable_hosts) do
|
86
|
-
ExpireHostsMailer.expiry_warning_notification(hosts_hash['email'], expiry_date, hosts_hash['hosts']).deliver_now
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
15
|
+
notifiable_hosts = Host.with_expire_date(expiry_date).preload(:owner)
|
90
16
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
if host.owner_type == 'User'
|
96
|
-
unless hosts_hash.key?(host.owner_id.to_s)
|
97
|
-
email_recipients = emails + [host.owner.mail]
|
98
|
-
hosts_hash[host.owner_id.to_s] = { 'id' => host.owner_id, 'name' => host.owner.name, 'email' => email_recipients, 'hosts' => [] }
|
99
|
-
end
|
100
|
-
hosts_hash[host.owner_id.to_s]['hosts'] << host
|
101
|
-
elsif host.owner_type == 'Usergroup'
|
102
|
-
host.owner.users.each do |owner|
|
103
|
-
unless hosts_hash.key?(owner.id.to_s)
|
104
|
-
email_recipients = emails + [owner.mail]
|
105
|
-
hosts_hash[owner.id.to_s] = { 'id' => owner.id, 'name' => owner.name, 'email' => email_recipients, 'hosts' => [] }
|
106
|
-
end
|
107
|
-
hosts_hash[owner.id.to_s]['hosts'] << host
|
108
|
-
end
|
109
|
-
else
|
110
|
-
email = (!emails.empty? ? emails : [Setting[:administrator]])
|
111
|
-
unless hosts_hash.key?(owner.id.to_s)
|
112
|
-
hosts_hash['admin'] = { 'id' => nil, 'name' => 'Admin', 'email' => email, 'hosts' => [] }
|
113
|
-
end
|
114
|
-
hosts_hash['admin']['hosts'] << host
|
115
|
-
end
|
116
|
-
end
|
117
|
-
hosts_hash
|
17
|
+
ForemanExpireHosts::Notification::ExpiryWarning.new(
|
18
|
+
:hosts => notifiable_hosts,
|
19
|
+
:expiry_date => expiry_date
|
20
|
+
).deliver
|
118
21
|
end
|
119
22
|
end
|
120
23
|
end
|
@@ -9,6 +9,7 @@ module ForemanExpireHosts
|
|
9
9
|
config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
|
10
10
|
config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
|
11
11
|
config.autoload_paths += Dir["#{config.root}/app/helpers/concerns"]
|
12
|
+
config.autoload_paths += Dir["#{config.root}/app/services"]
|
12
13
|
|
13
14
|
# Add any db migrations
|
14
15
|
initializer 'foreman_plugin_template.load_app_instance_data' do |app|
|
@@ -21,22 +22,21 @@ module ForemanExpireHosts
|
|
21
22
|
require_dependency File.expand_path('../../../app/models/setting/expire_hosts.rb', __FILE__) if (Setting.table_exists? rescue(false))
|
22
23
|
end
|
23
24
|
|
24
|
-
initializer 'foreman_expire_hosts.register_plugin', :before => :finisher_hook do |
|
25
|
+
initializer 'foreman_expire_hosts.register_plugin', :before => :finisher_hook do |_app|
|
25
26
|
Foreman::Plugin.register :foreman_expire_hosts do
|
26
|
-
requires_foreman '>= 1.
|
27
|
+
requires_foreman '>= 1.16'
|
27
28
|
register_custom_status HostStatus::ExpirationStatus
|
28
29
|
|
29
30
|
# strong parameters
|
30
31
|
parameter_filter Host::Managed, :expired_on
|
31
32
|
|
32
|
-
security_block :hosts do
|
33
|
-
permission :edit_hosts, {:hosts => [:select_multiple_expiration, :update_multiple_expiration]}
|
34
|
-
end
|
35
|
-
|
36
33
|
security_block :foreman_expire_hosts do
|
37
34
|
permission :edit_host_expiry,
|
38
35
|
{},
|
39
36
|
:resource_type => 'Host'
|
37
|
+
permission :edit_hosts,
|
38
|
+
{ :hosts => [:select_multiple_expiration, :update_multiple_expiration] },
|
39
|
+
:resource_type => 'Host'
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -45,13 +45,19 @@ module ForemanExpireHosts
|
|
45
45
|
begin
|
46
46
|
Host::Managed.send :include, ForemanExpireHosts::HostExt
|
47
47
|
HostsHelper.send :include, ForemanExpireHosts::HostsHelperExtensions
|
48
|
-
HostsController.send :
|
48
|
+
HostsController.send :prepend, ForemanExpireHosts::HostControllerExtensions
|
49
49
|
AuditsHelper.send :include, ForemanExpireHosts::AuditsHelperExtensions
|
50
|
-
rescue => e
|
50
|
+
rescue StandardError => e
|
51
51
|
Rails.logger.warn "ForemanExpireHosts: skipping engine hook (#{e})"
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
rake_tasks do
|
56
|
+
Rake::Task['db:seed'].enhance do
|
57
|
+
ForemanExpireHosts::Engine.load_seed
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
55
61
|
# Precompile any JS or CSS files under app/assets/
|
56
62
|
# If requiring files from each other, list them explicitly here to avoid precompiling the same
|
57
63
|
# content twice.
|
@@ -2,10 +2,12 @@
|
|
2
2
|
namespace :expired_hosts do
|
3
3
|
desc 'Delete all expired hosts, send notification email about expiring hosts'
|
4
4
|
task :deliver_notifications => :environment do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
User.as_anonymous_admin do
|
6
|
+
ExpireHostsNotifications.delete_expired_hosts
|
7
|
+
ExpireHostsNotifications.stop_expired_hosts
|
8
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification(1)
|
9
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification(2)
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
@@ -30,7 +32,7 @@ namespace :foreman_expire_hosts do
|
|
30
32
|
"#{ForemanExpireHosts::Engine.root}/lib/**/*.rb",
|
31
33
|
"#{ForemanExpireHosts::Engine.root}/test/**/*.rb"]
|
32
34
|
end
|
33
|
-
rescue
|
35
|
+
rescue StandardError
|
34
36
|
puts 'Rubocop not loaded.'
|
35
37
|
end
|
36
38
|
|
@@ -40,11 +40,11 @@ class HostsControllerTest < ActionController::TestCase
|
|
40
40
|
end
|
41
41
|
|
42
42
|
describe 'updating a host' do
|
43
|
-
let(:host) {
|
43
|
+
let(:host) { FactoryBot.create(:host) }
|
44
44
|
|
45
45
|
test 'should add expiration date to host' do
|
46
46
|
expiration_date = Date.today + 14
|
47
|
-
put :update, { :id => host.name, :host => {:expired_on => expiration_date} }, set_session_user
|
47
|
+
put :update, { :id => host.name, :host => { :expired_on => expiration_date } }, set_session_user
|
48
48
|
h = Host.find(host.id)
|
49
49
|
assert_equal expiration_date, h.expired_on
|
50
50
|
end
|
@@ -53,16 +53,18 @@ class HostsControllerTest < ActionController::TestCase
|
|
53
53
|
describe 'setting expiration date on multiple hosts' do
|
54
54
|
before do
|
55
55
|
as_admin do
|
56
|
-
@hosts =
|
56
|
+
@hosts = FactoryBot.create_list(:host, 2, :with_puppet)
|
57
57
|
end
|
58
58
|
@request.env['HTTP_REFERER'] = hosts_path
|
59
59
|
end
|
60
60
|
|
61
61
|
test 'show a host selection' do
|
62
62
|
host_ids = @hosts.map(&:id)
|
63
|
-
xhr :post, :select_multiple_expiration, {:host_ids => host_ids}, set_session_user
|
63
|
+
xhr :post, :select_multiple_expiration, { :host_ids => host_ids }, set_session_user
|
64
64
|
assert_response :success
|
65
|
-
|
65
|
+
@hosts.each do |host|
|
66
|
+
assert response.body =~ /#{host.name}/m
|
67
|
+
end
|
66
68
|
end
|
67
69
|
|
68
70
|
test 'should set expiration date' do
|
@@ -90,7 +92,7 @@ class HostsControllerTest < ActionController::TestCase
|
|
90
92
|
assert_empty flash[:error]
|
91
93
|
|
92
94
|
@hosts.each do |host|
|
93
|
-
|
95
|
+
assert_nil host.reload.expired_on
|
94
96
|
end
|
95
97
|
end
|
96
98
|
end
|
@@ -1,76 +1,172 @@
|
|
1
1
|
require 'test_plugin_helper'
|
2
|
+
require 'notifications_test_helper'
|
2
3
|
|
3
4
|
class ExpireHostsNotificationsTest < ActiveSupport::TestCase
|
5
|
+
include NotificationBlueprintSeeds
|
6
|
+
|
4
7
|
setup do
|
5
8
|
setup_settings
|
6
|
-
|
9
|
+
ActionMailer::Base.deliveries.clear
|
7
10
|
end
|
8
11
|
|
12
|
+
let(:user) { FactoryBot.create(:user, :with_usergroup, :with_mail) }
|
13
|
+
let(:usergroup) { user.usergroups.first }
|
14
|
+
|
9
15
|
context '#delete_expired_hosts' do
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
context 'with single owner' do
|
17
|
+
setup do
|
18
|
+
FactoryBot.create_list(:host, 2, :expired, :owner => user)
|
19
|
+
FactoryBot.create_list(:host, 2, :owner => user)
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'should delete expired hosts' do
|
23
|
+
assert_difference 'Host.count', -2 do
|
24
|
+
ExpireHostsNotifications.delete_expired_hosts
|
25
|
+
end
|
26
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
27
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Deleted expired hosts'
|
28
|
+
end
|
13
29
|
end
|
14
30
|
|
15
|
-
|
16
|
-
|
17
|
-
|
31
|
+
context 'with usergroup owner' do
|
32
|
+
setup do
|
33
|
+
FactoryBot.create_list(:host, 2, :expired, :owner => usergroup)
|
34
|
+
FactoryBot.create_list(:host, 2, :owner => usergroup)
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'should delete expired hosts' do
|
38
|
+
assert_difference 'Host.count', -2 do
|
39
|
+
ExpireHostsNotifications.delete_expired_hosts
|
40
|
+
end
|
41
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
42
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Deleted expired hosts'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'without owner' do
|
47
|
+
setup do
|
48
|
+
FactoryBot.create_list(:host, 2, :expired, :without_owner)
|
49
|
+
FactoryBot.create_list(:host, 2, :without_owner)
|
50
|
+
end
|
51
|
+
|
52
|
+
test 'should delete expired hosts' do
|
53
|
+
assert_difference 'Host.count', -2 do
|
54
|
+
ExpireHostsNotifications.delete_expired_hosts
|
55
|
+
end
|
56
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
57
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Deleted expired hosts'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with additional recipients' do
|
62
|
+
setup do
|
63
|
+
Setting[:host_expiry_email_recipients] = 'test@example.com, test2.example.com'
|
64
|
+
FactoryBot.create_list(:host, 2, :expired, :owner => user)
|
65
|
+
end
|
66
|
+
|
67
|
+
test 'should deliver notification to additional recipients' do
|
18
68
|
ExpireHostsNotifications.delete_expired_hosts
|
69
|
+
assert_equal 3, ActionMailer::Base.deliveries.count
|
70
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Deleted expired hosts'
|
71
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), user.mail
|
72
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), 'test@example.com'
|
73
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), 'test2.example.com'
|
19
74
|
end
|
20
75
|
end
|
21
76
|
end
|
22
77
|
|
23
78
|
context '#stop_expired_hosts' do
|
79
|
+
let(:power_mock) { mock('power') }
|
80
|
+
let(:host) { FactoryBot.create(:host, :expired, :on_compute_resource, :owner => user) }
|
81
|
+
let(:blueprint) { NotificationBlueprint.find_by(name: 'expire_hosts_stopped_host') }
|
24
82
|
setup do
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
Host.any_instance.stubs(:power).returns(@power_mock)
|
30
|
-
@unaffected_hosts = FactoryGirl.create_list(:host, 2, :owner => @owner)
|
83
|
+
power_mock.stubs(:ready?).returns(true)
|
84
|
+
host.unstub(:queue_compute)
|
85
|
+
Host.any_instance.stubs(:power).returns(power_mock)
|
86
|
+
FactoryBot.create_list(:host, 2, :owner => user)
|
31
87
|
end
|
32
88
|
|
33
89
|
test 'should stop expired hosts' do
|
34
|
-
|
35
|
-
ExpireHostsMailer.expects(:stopped_hosts_notification).once
|
90
|
+
power_mock.expects(:stop).returns(true)
|
36
91
|
ExpireHostsNotifications.stop_expired_hosts
|
92
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
93
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Stopped expired hosts'
|
94
|
+
end
|
95
|
+
|
96
|
+
test 'should send a ui notification per stopped host' do
|
97
|
+
power_mock.expects(:stop).returns(true)
|
98
|
+
assert_difference('blueprint.notifications.count', 1) do
|
99
|
+
ExpireHostsNotifications.stop_expired_hosts
|
100
|
+
end
|
37
101
|
end
|
38
102
|
|
39
103
|
test 'should send failure message if host cannot be stopped' do
|
40
|
-
|
41
|
-
ExpireHostsMailer.expects(:failed_to_stop_hosts_notification).once
|
104
|
+
power_mock.expects(:stop).returns(false)
|
42
105
|
ExpireHostsNotifications.stop_expired_hosts
|
106
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
107
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Failed to stop expired hosts'
|
43
108
|
end
|
44
109
|
end
|
45
110
|
|
46
111
|
context '#deliver_expiry_warning_notification' do
|
112
|
+
let(:blueprint) { NotificationBlueprint.find_by(name: 'expire_hosts_expiry_warning') }
|
113
|
+
let(:hosts) { FactoryBot.create_list(:host, 2, :expires_in_a_week, :owner => user) }
|
114
|
+
|
47
115
|
setup do
|
48
116
|
Setting['notify1_days_before_host_expiry'] = 7
|
49
|
-
|
117
|
+
hosts
|
118
|
+
end
|
119
|
+
|
120
|
+
test 'should send a ui notification per host' do
|
121
|
+
assert_difference('blueprint.notifications.count', 2) do
|
122
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification
|
123
|
+
end
|
124
|
+
hosts.each do |host|
|
125
|
+
notification = Notification.find_by(
|
126
|
+
notification_blueprint_id: blueprint.id,
|
127
|
+
subject_id: host.id,
|
128
|
+
subject_type: 'Host::Base'
|
129
|
+
)
|
130
|
+
assert_equal 1, notification.notification_recipients.where(user_id: user.id).count
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
test 'should redisplay read ui notification' do
|
135
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification
|
136
|
+
notification = Notification.find_by(notification_blueprint_id: blueprint.id, subject_id: hosts.first.id)
|
137
|
+
assert_not_nil notification
|
138
|
+
assert_equal 1, NotificationRecipient.where(notification_id: notification.id).update_all(seen: true) # rubocop:disable Rails/SkipsModelValidations
|
139
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification
|
140
|
+
assert_equal 1, NotificationRecipient.where(notification_id: notification.id, seen: false).count
|
50
141
|
end
|
51
142
|
|
52
143
|
test 'should send a single notification' do
|
53
|
-
ExpireHostsMailer.expects(:expiry_warning_notification).once
|
54
144
|
ExpireHostsNotifications.deliver_expiry_warning_notification
|
145
|
+
assert_equal 1, ActionMailer::Base.deliveries.count
|
146
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Expiring hosts in foreman'
|
55
147
|
end
|
56
148
|
|
57
149
|
test 'should send two notifications for two users' do
|
58
|
-
owner2 =
|
59
|
-
|
60
|
-
ExpireHostsMailer.expects(:expiry_warning_notification).twice
|
150
|
+
owner2 = FactoryBot.create(:user, :with_mail)
|
151
|
+
FactoryBot.create(:host, :expires_in_a_week, :owner => owner2)
|
61
152
|
ExpireHostsNotifications.deliver_expiry_warning_notification
|
153
|
+
assert_equal 2, ActionMailer::Base.deliveries.count
|
154
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Expiring hosts in foreman'
|
155
|
+
assert_includes ActionMailer::Base.deliveries.last.subject, 'Expiring hosts in foreman'
|
62
156
|
end
|
63
|
-
end
|
64
157
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
158
|
+
test 'should send three notifications for three users' do
|
159
|
+
user2 = FactoryBot.create(:user, :with_mail)
|
160
|
+
user3 = FactoryBot.create(:user, :with_mail, :usergroups => [usergroup])
|
161
|
+
FactoryBot.create(:host, :expires_in_a_week, :owner => user2)
|
162
|
+
FactoryBot.create(:host, :expires_in_a_week, :owner => usergroup)
|
163
|
+
ExpireHostsNotifications.deliver_expiry_warning_notification
|
164
|
+
assert_equal 3, ActionMailer::Base.deliveries.count
|
165
|
+
assert_includes ActionMailer::Base.deliveries.first.subject, 'Expiring hosts in foreman'
|
166
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), user.mail
|
167
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), user2.mail
|
168
|
+
assert_includes ActionMailer::Base.deliveries.flat_map(&:to), user3.mail
|
169
|
+
assert_equal 1, ActionMailer::Base.deliveries.flat_map(&:subject).uniq.count
|
74
170
|
end
|
75
171
|
end
|
76
172
|
end
|