|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class EmailForwardingRouterTest < ActiveSupport::TestCase |
| 4 | + let(:convention) { create(:convention, domain: "example.com") } |
| 5 | + let(:user1) { create(:user) } |
| 6 | + let(:user2) { create(:user) } |
| 7 | + let(:user_con_profile1) { create(:user_con_profile, convention:, user: user1) } |
| 8 | + let(:user_con_profile2) { create(:user_con_profile, convention:, user: user2) } |
| 9 | + |
| 10 | + describe "all_staff_position_mappings" do |
| 11 | + describe "with email aliases" do |
| 12 | + let(:staff_position) do |
| 13 | + create( |
| 14 | + :staff_position, |
| 15 | + convention:, |
| 16 | + email: "staff@example.com", |
| 17 | + email_aliases: %w[help support], |
| 18 | + cc_addresses: ["cc@example.com"] |
| 19 | + ) |
| 20 | + end |
| 21 | + |
| 22 | + setup do |
| 23 | + staff_position.user_con_profiles << user_con_profile1 |
| 24 | + staff_position.user_con_profiles << user_con_profile2 |
| 25 | + end |
| 26 | + |
| 27 | + it "creates mappings for the primary email and all aliases" do |
| 28 | + mappings = EmailForwardingRouter.all_staff_position_mappings |
| 29 | + mapping_emails = mappings.values.map(&:inbound_email) |
| 30 | + |
| 31 | + assert_includes mapping_emails, "staff@example.com" |
| 32 | + assert_includes mapping_emails, "help@example.com" |
| 33 | + assert_includes mapping_emails, "support@example.com" |
| 34 | + end |
| 35 | + |
| 36 | + it "maps all aliases to the same destination addresses" do |
| 37 | + mappings = EmailForwardingRouter.all_staff_position_mappings |
| 38 | + primary_mapping = mappings.values.find { |m| m.inbound_email == "staff@example.com" } |
| 39 | + help_mapping = mappings.values.find { |m| m.inbound_email == "help@example.com" } |
| 40 | + support_mapping = mappings.values.find { |m| m.inbound_email == "support@example.com" } |
| 41 | + |
| 42 | + expected_destinations = [user1.email, user2.email, "cc@example.com"].map do |addr| |
| 43 | + EmailRoute.normalize_address(addr) |
| 44 | + end |
| 45 | + |
| 46 | + assert_equal expected_destinations.sort, primary_mapping.destination_addresses.sort |
| 47 | + assert_equal expected_destinations.sort, help_mapping.destination_addresses.sort |
| 48 | + assert_equal expected_destinations.sort, support_mapping.destination_addresses.sort |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe "without email aliases" do |
| 53 | + let(:staff_position) do |
| 54 | + create( |
| 55 | + :staff_position, |
| 56 | + convention:, |
| 57 | + email: "staff@example.com", |
| 58 | + email_aliases: [] |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + setup { staff_position.user_con_profiles << user_con_profile1 } |
| 63 | + |
| 64 | + it "creates only the primary email mapping" do |
| 65 | + mappings = EmailForwardingRouter.all_staff_position_mappings |
| 66 | + mapping_emails = mappings.values.map(&:inbound_email) |
| 67 | + |
| 68 | + assert_includes mapping_emails, "staff@example.com" |
| 69 | + assert_equal 1, mapping_emails.count("staff@example.com") |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "with multiple staff positions with aliases" do |
| 74 | + let(:staff_position1) do |
| 75 | + create( |
| 76 | + :staff_position, |
| 77 | + convention:, |
| 78 | + email: "staff1@example.com", |
| 79 | + email_aliases: %w[help support] |
| 80 | + ) |
| 81 | + end |
| 82 | + |
| 83 | + let(:staff_position2) do |
| 84 | + create( |
| 85 | + :staff_position, |
| 86 | + convention:, |
| 87 | + email: "staff2@example.com", |
| 88 | + email_aliases: ["info"] |
| 89 | + ) |
| 90 | + end |
| 91 | + |
| 92 | + setup do |
| 93 | + staff_position1.user_con_profiles << user_con_profile1 |
| 94 | + staff_position2.user_con_profiles << user_con_profile2 |
| 95 | + end |
| 96 | + |
| 97 | + it "creates all mappings for both staff positions" do |
| 98 | + mappings = EmailForwardingRouter.all_staff_position_mappings |
| 99 | + mapping_emails = mappings.values.map(&:inbound_email) |
| 100 | + |
| 101 | + # staff_position1 mappings |
| 102 | + assert_includes mapping_emails, "staff1@example.com" |
| 103 | + assert_includes mapping_emails, "help@example.com" |
| 104 | + assert_includes mapping_emails, "support@example.com" |
| 105 | + |
| 106 | + # staff_position2 mappings |
| 107 | + assert_includes mapping_emails, "staff2@example.com" |
| 108 | + assert_includes mapping_emails, "info@example.com" |
| 109 | + |
| 110 | + assert_equal 5, mapping_emails.size |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments