Commit ba0a4d19 authored by Max Woolf's avatar Max Woolf

Fixes ssh key deletion notification

As part of the work to notify users
when an administrator deletes their
PAT or SSH key, this commit fixes
that this feature did not work for SSH keys
as the ActiveJob is querying a database object
which no longer exists.
parent 85bf09ff
...@@ -58,7 +58,14 @@ module CredentialsInventoryActions ...@@ -58,7 +58,14 @@ module CredentialsInventoryActions
return unless Feature.enabled?(:credentials_inventory_revocation_emails, credential.user) return unless Feature.enabled?(:credentials_inventory_revocation_emails, credential.user)
if credential.is_a?(Key) if credential.is_a?(Key)
CredentialsInventoryMailer.ssh_key_deleted_email(key: credential, deleted_by: current_user).deliver_later CredentialsInventoryMailer.ssh_key_deleted_email(
params: {
notification_email: credential.user.notification_email,
title: credential.title,
last_used_at: credential.last_used_at,
created_at: credential.created_at
}, deleted_by: current_user
).deliver_later
elsif credential.is_a?(PersonalAccessToken) elsif credential.is_a?(PersonalAccessToken)
CredentialsInventoryMailer.personal_access_token_revoked_email(token: credential, revoked_by: current_user).deliver_later CredentialsInventoryMailer.personal_access_token_revoked_email(token: credential, revoked_by: current_user).deliver_later
end end
......
...@@ -15,12 +15,12 @@ class CredentialsInventoryMailer < ApplicationMailer ...@@ -15,12 +15,12 @@ class CredentialsInventoryMailer < ApplicationMailer
) )
end end
def ssh_key_deleted_email(key:, deleted_by:) def ssh_key_deleted_email(params:, deleted_by:)
@deleted_by = deleted_by @deleted_by = deleted_by
@key = key @params = params
mail( mail(
to: key.user.notification_email, to: params[:notification_email],
subject: _('Your SSH key was deleted') subject: _('Your SSH key was deleted')
) )
end end
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
%ul %ul
%li %li
= @key.title = @params[:title]
%li %li
= _("Created on %{created_at}") % { created_at: @key.created_at.to_date.to_s(:medium) } = _("Created on %{created_at}") % { created_at: @params[:created_at].to_date.to_s(:medium) }
- if @key.last_used_at - if @params[:last_used_at]
%li %li
= _("Last used %{last_used_at} ago") % { last_used_at: time_ago_in_words(@key.last_used_at) } = _("Last used %{last_used_at} ago") % { last_used_at: time_ago_in_words(@params[:last_used_at]) }
%p %p
= html_escape(_("You can create a new %{link}.")) % { link: link_to(_('SSH key'), profile_keys_url.html_safe) } = html_escape(_("You can create a new %{link}.")) % { link: link_to(_('SSH key'), profile_keys_url.html_safe) }
= _("The following SSH key was deleted by an administrator, %{username}.") % { username: @deleted_by.username } = _("The following SSH key was deleted by an administrator, %{username}.") % { username: @deleted_by.username }
= @key.title = @params[:title]
= _("Created on %{created_at}") % { created_at: @key.created_at.to_date.to_s(:medium) } = _("Created on %{created_at}") % { created_at: @params[:created_at].to_date.to_s(:medium) }
- if @key.last_used_at - if @params[:last_used_at]
= _("Last used %{last_used_at} ago") % { last_used_at: time_ago_in_words(@key.last_used_at) } = _("Last used %{last_used_at} ago") % { last_used_at: time_ago_in_words(@params[:last_used_at]) }
= html_escape(_("You can create a new SSH key by visiting %{link}")) % { link: profile_keys_url.html_safe } = html_escape(_("You can create a new SSH key by visiting %{link}")) % { link: profile_keys_url.html_safe }
...@@ -23,8 +23,16 @@ RSpec.describe CredentialsInventoryMailer do ...@@ -23,8 +23,16 @@ RSpec.describe CredentialsInventoryMailer do
describe '#ssh_key_deleted_email' do describe '#ssh_key_deleted_email' do
let_it_be(:ssh_key) { create(:personal_key, last_used_at: 3.weeks.ago) } let_it_be(:ssh_key) { create(:personal_key, last_used_at: 3.weeks.ago) }
let(:params) do
subject(:email) { described_class.ssh_key_deleted_email(key: ssh_key, deleted_by: administrator) } {
notification_email: ssh_key.user.notification_email,
title: ssh_key.title,
last_used_at: ssh_key.last_used_at,
created_at: ssh_key.created_at
}
end
subject(:email) { described_class.ssh_key_deleted_email(params: params, deleted_by: administrator) }
it { is_expected.to have_subject 'Your SSH key was deleted' } it { is_expected.to have_subject 'Your SSH key was deleted' }
it { is_expected.to have_body_text 'The following SSH key was deleted by an administrator, Revoker' } it { is_expected.to have_body_text 'The following SSH key was deleted by an administrator, Revoker' }
......
...@@ -35,9 +35,9 @@ RSpec.shared_examples_for 'credentials inventory controller delete SSH key' do | ...@@ -35,9 +35,9 @@ RSpec.shared_examples_for 'credentials inventory controller delete SSH key' do |
end end
it 'notifies the key owner' do it 'notifies the key owner' do
expect(CredentialsInventoryMailer).to receive_message_chain(:ssh_key_deleted_email, :deliver_later) perform_enqueued_jobs do
expect { subject }.to change { ActionMailer::Base.deliveries.size }.by(1)
subject end
end end
context 'when credentials_inventory_revocation_emails is disabled' do context 'when credentials_inventory_revocation_emails is disabled' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment