|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +describe ::Portus::Mail::Utils do |
| 6 | + let(:no_smtp) do |
| 7 | + { |
| 8 | + |
| 9 | + "name" => "test", |
| 10 | + "smtp" => { "enabled": false } |
| 11 | + }.freeze |
| 12 | + end |
| 13 | + |
| 14 | + let(:basic) do |
| 15 | + { |
| 16 | + |
| 17 | + "name" => "test", |
| 18 | + "smtp" => { |
| 19 | + "enabled" => true, |
| 20 | + |
| 21 | + "port" => 567, |
| 22 | + "domain" => "example.com", |
| 23 | + "enable_starttls_auto" => false, |
| 24 | + "openssl_verify_mode" => "none" |
| 25 | + } |
| 26 | + }.freeze |
| 27 | + end |
| 28 | + |
| 29 | + let(:authentication) do |
| 30 | + { |
| 31 | + |
| 32 | + "name" => "test", |
| 33 | + "smtp" => { |
| 34 | + "enabled" => true, |
| 35 | + |
| 36 | + "port" => 567, |
| 37 | + "domain" => "example.com", |
| 38 | + "enable_starttls_auto" => false, |
| 39 | + "openssl_verify_mode" => "none", |
| 40 | + "user_name" => "mssola", |
| 41 | + "password" => "password", |
| 42 | + "authentication" => "login" |
| 43 | + } |
| 44 | + }.freeze |
| 45 | + end |
| 46 | + |
| 47 | + let(:tls_noca) do |
| 48 | + { |
| 49 | + |
| 50 | + "name" => "test", |
| 51 | + "smtp" => { |
| 52 | + "enabled" => true, |
| 53 | + |
| 54 | + "port" => 567, |
| 55 | + "domain" => "example.com", |
| 56 | + "enable_starttls_auto" => true, |
| 57 | + "openssl_verify_mode" => "peer", |
| 58 | + "ssl_tls" => "tls" |
| 59 | + } |
| 60 | + }.freeze |
| 61 | + end |
| 62 | + |
| 63 | + let(:notls_ca) do |
| 64 | + { |
| 65 | + |
| 66 | + "name" => "test", |
| 67 | + "smtp" => { |
| 68 | + "enabled" => true, |
| 69 | + |
| 70 | + "port" => 567, |
| 71 | + "domain" => "example.com", |
| 72 | + "enable_starttls_auto" => true, |
| 73 | + "openssl_verify_mode" => "peer", |
| 74 | + "ca_path" => "/lala", |
| 75 | + "ca_file" => "/lala" |
| 76 | + } |
| 77 | + }.freeze |
| 78 | + end |
| 79 | + |
| 80 | + let(:ssl_ca) do |
| 81 | + { |
| 82 | + |
| 83 | + "name" => "test", |
| 84 | + "smtp" => { |
| 85 | + "enabled" => true, |
| 86 | + |
| 87 | + "port" => 567, |
| 88 | + "domain" => "example.com", |
| 89 | + "enable_starttls_auto" => true, |
| 90 | + "openssl_verify_mode" => "peer", |
| 91 | + "ca_path" => "/lala", |
| 92 | + "ca_file" => "/lala", |
| 93 | + "ssl_tls" => "ssl" |
| 94 | + } |
| 95 | + }.freeze |
| 96 | + end |
| 97 | + |
| 98 | + describe "#check_email_configuration!" do |
| 99 | + it "raises an exception on malformed 'from'" do |
| 100 | + expect do |
| 101 | + described_class.new("from" => "!").check_email_configuration! |
| 102 | + end.to raise_error(::Portus::Mail::ConfigurationError) |
| 103 | + end |
| 104 | + |
| 105 | + it "raises an exception on malformed 'reply_to'" do |
| 106 | + expect do |
| 107 | + described_class.new("from" => "[email protected]", "reply_to" => "!").check_email_configuration! |
| 108 | + end.to raise_error(::Portus::Mail::ConfigurationError) |
| 109 | + end |
| 110 | + |
| 111 | + it "does not raise an exception when everything is alright" do |
| 112 | + expect do |
| 113 | + hsh = { "from" => "[email protected]", "reply_to" => "[email protected]" } |
| 114 | + described_class.new(hsh).check_email_configuration! |
| 115 | + end.not_to raise_error |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + describe "#smtp_settings" do |
| 120 | + it "returns nil when disabled" do |
| 121 | + res = described_class.new(no_smtp).smtp_settings |
| 122 | + expect(res).to be_nil |
| 123 | + end |
| 124 | + |
| 125 | + it "returns a basic smtp configuration" do |
| 126 | + res = described_class.new(basic).smtp_settings |
| 127 | + %i[address port domain enable_starttls_auto openssl_verify_mode].each do |key| |
| 128 | + expect(res[key]).not_to be_nil |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + it "returns a configuration with authentication" do |
| 133 | + res = described_class.new(authentication).smtp_settings |
| 134 | + %i[address port domain enable_starttls_auto openssl_verify_mode |
| 135 | + user_name password authentication].each do |key| |
| 136 | + expect(res[key]).not_to be_nil |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + it "returns a configuration with SSL (ssl/tls and no ca)" do |
| 141 | + res = described_class.new(tls_noca).smtp_settings |
| 142 | + %i[address port domain enable_starttls_auto openssl_verify_mode tls].each do |key| |
| 143 | + expect(res[key]).not_to be_nil |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + it "returns a configuration with SSL (ssl/tls and ca)" do |
| 148 | + res = described_class.new(ssl_ca).smtp_settings |
| 149 | + %i[address port domain enable_starttls_auto openssl_verify_mode ssl |
| 150 | + ca_file ca_path].each do |key| |
| 151 | + expect(res[key]).not_to be_nil |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + it "returns a configuration with SSL (no ssl/tls and ca)" do |
| 156 | + res = described_class.new(notls_ca).smtp_settings |
| 157 | + %i[address port domain enable_starttls_auto openssl_verify_mode |
| 158 | + ca_file ca_path].each do |key| |
| 159 | + expect(res[key]).not_to be_nil |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments