Add ability to email announcements to all users (#33928)

This commit is contained in:
Claire 2025-03-06 15:05:27 +01:00 committed by GitHub
parent d2ce9a6064
commit 5a100bf38f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 275 additions and 11 deletions

View file

@ -317,4 +317,16 @@ RSpec.describe UserMailer do
.and(have_body_text(I18n.t('user_mailer.terms_of_service_changed.changelog')))
end
end
describe '#announcement_published' do
let(:announcement) { Fabricate :announcement }
let(:mail) { described_class.announcement_published(receiver, announcement) }
it 'renders announcement_published mail' do
expect(mail)
.to be_present
.and(have_subject(I18n.t('user_mailer.announcement_published.subject')))
.and(have_body_text(I18n.t('user_mailer.announcement_published.description', domain: Rails.configuration.x.local_domain)))
end
end
end

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Admin Announcement Mail Distributions' do
let(:user) { Fabricate(:admin_user) }
let(:announcement) { Fabricate(:announcement, notification_sent_at: nil) }
before { sign_in(user) }
describe 'Sending an announcement notification', :inline_jobs do
it 'marks the announcement as notified and sends the email' do
visit admin_announcement_preview_path(announcement)
expect(page)
.to have_title(I18n.t('admin.announcements.preview.title'))
emails = capture_emails do
expect { click_on I18n.t('admin.terms_of_service.preview.send_to_all', count: 1, display_count: 1) }
.to(change { announcement.reload.notification_sent_at })
end
expect(emails.first)
.to be_present
.and(deliver_to(user.email))
expect(page)
.to have_title(I18n.t('admin.announcements.title'))
end
end
end

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Admin Announcements Mail Previews' do
let(:announcement) { Fabricate(:announcement, notification_sent_at: nil) }
before { sign_in(admin_user) }
describe 'Viewing Announcements Mail previews' do
it 'shows the Announcement Mail preview page' do
visit admin_announcement_preview_path(announcement)
expect(page)
.to have_title(I18n.t('admin.announcements.preview.title'))
end
end
end

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Admin TermsOfService Tests' do
let(:user) { Fabricate(:admin_user) }
let(:announcement) { Fabricate(:announcement, notification_sent_at: nil) }
before { sign_in(user) }
describe 'Sending test Announcement email', :inline_jobs do
it 'generates the test email' do
visit admin_announcement_preview_path(announcement)
expect(page)
.to have_title(I18n.t('admin.announcements.preview.title'))
emails = capture_emails { click_on I18n.t('admin.terms_of_service.preview.send_preview', email: user.email) }
expect(emails.first)
.to be_present
.and(deliver_to(user.email))
expect(page)
.to have_title(I18n.t('admin.announcements.title'))
end
end
end

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Admin::DistributeAnnouncementNotificationWorker do
let(:worker) { described_class.new }
describe '#perform' do
context 'with missing record' do
it 'runs without error' do
expect { worker.perform(nil) }.to_not raise_error
end
end
context 'with valid announcement' do
let(:announcement) { Fabricate(:announcement) }
let!(:user) { Fabricate :user, confirmed_at: 3.days.ago }
it 'sends the announcement via email', :inline_jobs do
emails = capture_emails { worker.perform(announcement.id) }
expect(emails.size)
.to eq(1)
expect(emails.first)
.to have_attributes(
to: [user.email],
subject: I18n.t('user_mailer.announcement_published.subject')
)
end
end
end
end