1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 07:39:39 +02:00

Allow a self-hosted user to configure their SMTP settings directly from within the UI (#682)

* Add setting fields to model

* Allow to configure SMTP settings

* Normalize locales

* Cleanup locales

* Remove 'coming soon'

* fix test

* Reset credentials

* Reset development config

* Check smtp spelling

* Use post instead of get method

* TLS ENV variable is more descriptive

* Rework application mailer

* Follow rails convention for mailer action params

* Reset schema.rb to main

* Test WIP

* Add test for controller and mailer

* Move tests from controller to model

* Custom error message if settings are not all present

* Comment smtp config in development env

* Add default tls enabled value

* Rubocop

* Fix controller test

* Reset credentials

* Normalize locales

* Test

* fix test

* Fix application mailer test that fails randomly

* Error flash message instead of notice

* Rework application mailer tests
This commit is contained in:
Thibaut Gorioux 2024-04-29 22:44:24 +02:00 committed by GitHub
parent f0480e7ab7
commit 6fdb8e8d69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 200 additions and 15 deletions

View file

@ -44,4 +44,38 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_equal "release", Setting.upgrades_target
assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook
end
test " #send_test_email if smtp settings are populated try to send an email and redirect with notice" do
Setting.stubs(:smtp_settings_populated?).returns(true)
test_email_mock = mock
test_email_mock.expects(:deliver_now)
mailer_mock = mock
mailer_mock.expects(:test_email).returns(test_email_mock)
NotificationMailer.expects(:with).with(user: users(:family_admin)).returns(mailer_mock)
post send_test_email_settings_hosting_path
assert_response :found
assert controller.flash[:notice].present?
end
test "#send_test_email with one blank smtp setting" do
Setting.stubs(:smtp_settings_populated?).returns(false)
NotificationMailer.expects(:with).never
post send_test_email_settings_hosting_path
assert_response :unprocessable_entity
assert controller.flash[:error].present?
end
test "#send_test_email when sending the email raise an error" do
Setting.stubs(:smtp_settings_populated?).returns(true)
NotificationMailer.stubs(:with).raises(StandardError)
post send_test_email_settings_hosting_path
assert_response :unprocessable_entity
assert controller.flash[:error].present?
end
end

View file

@ -0,0 +1,45 @@
require "test_helper"
class ApplicationMailerTest < ActionMailer::TestCase
setup do
class TestMailer < ApplicationMailer
def test_email
mail(to: "testto@email.com", from: "testfrom@email.com", subject: "Test email subject", body: "Test email body")
end
end
end
test "should use self host settings when self host enabled" do
ENV["SELF_HOSTING_ENABLED"] = "true"
smtp_host = "smtp.example.com"
smtp_port = 466
smtp_username = "user@example.com"
smtp_password = "password"
email_sender = "notification@example.com"
smtp_settings_from_settings = { address: smtp_host,
port: smtp_port,
user_name: smtp_username,
password: smtp_password }
Setting.stubs(:smtp_host).returns(smtp_host)
Setting.stubs(:smtp_port).returns(smtp_port)
Setting.stubs(:smtp_username).returns(smtp_username)
Setting.stubs(:smtp_password).returns(smtp_password)
Setting.stubs(:email_sender).returns(email_sender)
TestMailer.test_email.deliver_now
assert_emails 1
assert_equal smtp_settings_from_settings, ActionMailer::Base.deliveries.first.delivery_method.settings.slice(:address, :port, :user_name, :password)
end
test "should use regular env settings when self host disabled" do
ENV["SELF_HOSTING_ENABLED"] = "false"
TestMailer.test_email.deliver_now
assert_emails 1
assert_nil ActionMailer::Base.deliveries.first.delivery_method.settings[:address]
end
end

View file

@ -0,0 +1,23 @@
require "test_helper"
class AccountTest < ActiveSupport::TestCase
test "#send_test_email return true if all smtp settings are populated" do
Setting.smtp_host = "smtp.example.com"
Setting.smtp_port = 466
Setting.smtp_username = "user@example.com"
Setting.smtp_password = "notification@example.com"
Setting.email_sender = "password"
assert Setting.smtp_settings_populated?
end
test "#send_test_email return false if one smtp settings is not populated" do
Setting.smtp_host = ""
Setting.smtp_port = 466
Setting.smtp_username = "user@example.com"
Setting.smtp_password = "notification@example.com"
Setting.email_sender = "password"
assert_not Setting.smtp_settings_populated?
end
end