|
| 1 | +# spec/mailers/staging_email_interceptor_spec.rb |
| 2 | +require 'rails_helper' |
| 3 | + |
| 4 | +RSpec.describe StagingEmailInterceptor do |
| 5 | + def build_message(to:, subject:, cc: nil, bcc: nil) |
| 6 | + Mail.new do |
| 7 | + to to |
| 8 | + cc cc if cc |
| 9 | + bcc bcc if bcc |
| 10 | + from 'flextensions@berkeley.edu' |
| 11 | + subject subject |
| 12 | + body 'Hello' |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + it 'redirects the recipient to the default staging mailbox' do |
| 17 | + message = build_message(to: 'student@example.com', subject: 'Your extension') |
| 18 | + described_class.delivering_email(message) |
| 19 | + |
| 20 | + expect(message.to).to eq([ described_class::DEFAULT_OVERRIDE ]) |
| 21 | + end |
| 22 | + |
| 23 | + it 'honors STAGING_EMAIL_OVERRIDE when set' do |
| 24 | + message = build_message(to: 'student@example.com', subject: 'Your extension') |
| 25 | + |
| 26 | + allow(ENV).to receive(:fetch).and_call_original |
| 27 | + allow(ENV).to receive(:fetch) |
| 28 | + .with('STAGING_EMAIL_OVERRIDE', described_class::DEFAULT_OVERRIDE) |
| 29 | + .and_return('someone@example.com') |
| 30 | + |
| 31 | + described_class.delivering_email(message) |
| 32 | + |
| 33 | + expect(message.to).to eq([ 'someone@example.com' ]) |
| 34 | + end |
| 35 | + |
| 36 | + it 'preserves the original recipient(s) in the subject line' do |
| 37 | + message = build_message(to: %w[a@example.com b@example.com], subject: 'Your extension') |
| 38 | + described_class.delivering_email(message) |
| 39 | + |
| 40 | + expect(message.subject).to eq('[STAGING -> a@example.com, b@example.com] Your extension') |
| 41 | + end |
| 42 | + |
| 43 | + it 'strips cc and bcc so no real person is reached' do |
| 44 | + message = build_message( |
| 45 | + to: 'student@example.com', |
| 46 | + subject: 'Your extension', |
| 47 | + cc: 'ta@example.com', |
| 48 | + bcc: 'instructor@example.com' |
| 49 | + ) |
| 50 | + described_class.delivering_email(message) |
| 51 | + |
| 52 | + expect(message.cc).to be_nil |
| 53 | + expect(message.bcc).to be_nil |
| 54 | + end |
| 55 | +end |
0 commit comments