diff --git a/app/mailers/emails/admin_notification.rb b/app/mailers/emails/admin_notification.rb index 9d02d4132a1403f196cb529ab9b3951597ff3f63..3766b4447d1d106c83cfa41158c449dfd8c0c7bc 100644 --- a/app/mailers/emails/admin_notification.rb +++ b/app/mailers/emails/admin_notification.rb @@ -15,23 +15,7 @@ def send_unsubscribed_notification(user_id) email = user.notification_email_or_default mail to: email, subject: "Unsubscribed from GitLab administrator notifications" end - - def user_auto_banned_email(admin_id, user_id, max_project_downloads:, within_seconds:, group: nil) - admin = User.find(admin_id) - @user = User.find(user_id) - @max_project_downloads = max_project_downloads - @within_minutes = within_seconds / 60 - @ban_scope = if group.present? - _('your group (%{group_name})' % { group_name: group.name }) - else - _('your GitLab instance') - end - - Gitlab::I18n.with_locale(admin.preferred_language) do - email_with_layout( - to: admin.notification_email_or_default, - subject: subject(_("We've detected unusual activity"))) - end - end end end + +Emails::AdminNotification.prepend_mod diff --git a/ee/app/mailers/ee/emails/admin_notification.rb b/ee/app/mailers/ee/emails/admin_notification.rb new file mode 100644 index 0000000000000000000000000000000000000000..9813721d46ab900e5f708126d413cd93cebc18db --- /dev/null +++ b/ee/app/mailers/ee/emails/admin_notification.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module EE + module Emails + module AdminNotification + def user_auto_banned_email(admin_id, user_id, max_project_downloads:, within_seconds:, group: nil) + admin = ::User.find(admin_id) + @user = ::User.find(user_id) + @max_project_downloads = max_project_downloads + @within_minutes = within_seconds / 60 + + if group.present? + @ban_scope = _('your group (%{group_name})' % { group_name: group.name }) + @settings_page_url = group_settings_reporting_url(group) + @banned_page_url = group_group_members_url(group, tab: 'banned') + else + @ban_scope = _('your GitLab instance') + @settings_page_url = reporting_admin_application_settings_url + @banned_page_url = admin_users_url(filter: 'banned') + end + + ::Gitlab::I18n.with_locale(admin.preferred_language) do + email_with_layout( + to: admin.notification_email_or_default, + subject: subject(_("We've detected unusual activity"))) + end + end + end + end +end diff --git a/app/views/notify/user_auto_banned_email.html.haml b/ee/app/views/notify/user_auto_banned_email.html.haml similarity index 70% rename from app/views/notify/user_auto_banned_email.html.haml rename to ee/app/views/notify/user_auto_banned_email.html.haml index 8c33cd7299db996be99476b4088e8148d45c03a0..8240cb10a1dd5c4d222cfce8f9c88e735102caeb 100644 --- a/app/views/notify/user_auto_banned_email.html.haml +++ b/ee/app/views/notify/user_auto_banned_email.html.haml @@ -4,6 +4,6 @@ %p = _('We want to let you know %{username} has been banned from %{scope} due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes, scope: @ban_scope } %p - = _('If this is a mistake, you can %{link_start}unban them%{link_end}.').html_safe % { link_start: link_start % { url: admin_users_url(filter: 'banned') }, link_end: link_end } + = _('If this is a mistake, you can %{link_start}unban them%{link_end}.').html_safe % { link_start: link_start % { url: @banned_page_url }, link_end: link_end } %p - = _('You can adjust rules on auto-banning %{link_start}here%{link_end}.').html_safe % { link_start: link_start % { url: network_admin_application_settings_url(anchor: 'js-ip-limits-settings') }, link_end: link_end } + = _('You can adjust rules on auto-banning %{link_start}here%{link_end}.').html_safe % { link_start: link_start % { url: @settings_page_url }, link_end: link_end } diff --git a/app/views/notify/user_auto_banned_email.text.erb b/ee/app/views/notify/user_auto_banned_email.text.erb similarity index 80% rename from app/views/notify/user_auto_banned_email.text.erb rename to ee/app/views/notify/user_auto_banned_email.text.erb index 336973c2e42e0b904c7d8534c0d1a16b612d6ec9..5ad3f4a7aae01b69c2f1c1c399adc427c1ddb34d 100644 --- a/app/views/notify/user_auto_banned_email.text.erb +++ b/ee/app/views/notify/user_auto_banned_email.text.erb @@ -2,6 +2,6 @@ <%= _('We want to let you know %{username} has been banned from %{scope} due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes, scope: @ban_scope } %> -<%= _('If this is a mistake, you can unban them: %{url}.') % { url: admin_users_url(filter: 'banned') } %> +<%= _('If this is a mistake, you can unban them: %{url}.') % { url: @banned_page_url } %> -<%= _('You can adjust rules on auto-banning here: %{url}.') % { url: network_admin_application_settings_url(anchor: 'js-ip-limits-settings') } %> +<%= _('You can adjust rules on auto-banning here: %{url}.') % { url: @settings_page_url } %> diff --git a/ee/spec/mailers/ee/emails/admin_notification_spec.rb b/ee/spec/mailers/ee/emails/admin_notification_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..2f00e9fb6125602ebedd303ef5e60e07e9394d34 --- /dev/null +++ b/ee/spec/mailers/ee/emails/admin_notification_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Emails::AdminNotification do + include EmailSpec::Matchers + include_context 'gitlab email notification' + + describe 'user_auto_banned_email' do + let_it_be(:admin) { create(:user) } + let_it_be(:user) { create(:user) } + + let(:max_project_downloads) { 5 } + let(:time_period) { 600 } + let(:group) { nil } + + subject do + Notify.user_auto_banned_email( + admin.id, user.id, + max_project_downloads: max_project_downloads, + within_seconds: time_period, + group: group + ) + end + + it_behaves_like 'an email sent from GitLab' + it_behaves_like 'it should not have Gmail Actions links' + it_behaves_like 'a user cannot unsubscribe through footer link' + it_behaves_like 'appearance header and footer enabled' + it_behaves_like 'appearance header and footer not enabled' + + it 'is sent to the administrator' do + is_expected.to deliver_to admin.email + end + + it 'has the correct subject' do + is_expected.to have_subject "We've detected unusual activity" + end + + it 'includes the name of the user' do + is_expected.to have_body_text user.name + end + + it 'includes the scope of the ban' do + is_expected.to have_body_text "banned from your GitLab instance" + end + + it 'includes the reason' do + is_expected.to have_body_text "due to them downloading more than 5 project repositories within 10 minutes" + end + + it 'includes a link to unban the user' do + is_expected.to have_body_text admin_users_url(filter: 'banned') + end + + it 'includes a link to change the settings' do + is_expected.to have_body_text reporting_admin_application_settings_url + end + + it 'includes the email reason' do + is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>} + end + + context 'when scoped to a group' do + let(:group) { create(:group) } + + it 'includes the scope of the ban' do + is_expected.to have_body_text "banned from your group (#{group.name})" + end + + it 'includes a link to unban the user' do + is_expected.to have_body_text group_group_members_url(group, tab: 'banned') + end + + it 'includes a link to change the settings' do + is_expected.to have_body_text group_settings_reporting_url(group) + end + end + end +end diff --git a/spec/mailers/emails/admin_notification_spec.rb b/spec/mailers/emails/admin_notification_spec.rb index 1b770d6d4a26b50a53b8653df69720204ae075f3..33b8558bfa3ab881885a0c77c136d232e8ea208b 100644 --- a/spec/mailers/emails/admin_notification_spec.rb +++ b/spec/mailers/emails/admin_notification_spec.rb @@ -11,68 +11,4 @@ expect(Notify).to be_respond_to(email_method) end end - - describe 'user_auto_banned_email' do - let_it_be(:admin) { create(:user) } - let_it_be(:user) { create(:user) } - - let(:max_project_downloads) { 5 } - let(:time_period) { 600 } - let(:group) { nil } - - subject do - Notify.user_auto_banned_email( - admin.id, user.id, - max_project_downloads: max_project_downloads, - within_seconds: time_period, - group: group - ) - end - - it_behaves_like 'an email sent from GitLab' - it_behaves_like 'it should not have Gmail Actions links' - it_behaves_like 'a user cannot unsubscribe through footer link' - it_behaves_like 'appearance header and footer enabled' - it_behaves_like 'appearance header and footer not enabled' - - it 'is sent to the administrator' do - is_expected.to deliver_to admin.email - end - - it 'has the correct subject' do - is_expected.to have_subject "We've detected unusual activity" - end - - it 'includes the name of the user' do - is_expected.to have_body_text user.name - end - - it 'includes the scope of the ban' do - is_expected.to have_body_text "banned from your GitLab instance" - end - - it 'includes the reason' do - is_expected.to have_body_text "due to them downloading more than 5 project repositories within 10 minutes" - end - - it 'includes a link to unban the user' do - is_expected.to have_body_text admin_users_url(filter: 'banned') - end - - it 'includes a link to change the settings' do - is_expected.to have_body_text network_admin_application_settings_url(anchor: 'js-ip-limits-settings') - end - - it 'includes the email reason' do - is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>} - end - - context 'when scoped to a group' do - let(:group) { create(:group) } - - it 'includes the scope of the ban' do - is_expected.to have_body_text "banned from your group (#{group.name})" - end - end - end end