The personal finance app for everyone
Simplify self host settings controller (#1230)
Zach Gollwitzer7fabca4679d0
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex dbf5177a..222ae018 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -1,71 +1,43 @@class Settings::HostingsController < SettingsController- before_action :verify_hosting_mode+ before_action :raise_if_not_self_hosteddef show+ @synth_usage = Current.family.synth_usageenddef update- if all_updates_valid?- hosting_params.keys.each do |key|- Setting.send("#{key}=", hosting_params[key].strip)- end+ if hosting_params[:upgrades_setting].present?+ mode = hosting_params[:upgrades_setting] == "manual" ? "manual" : "auto"+ target = hosting_params[:upgrades_setting] == "commit" ? "commit" : "release"- redirect_to settings_hosting_path, notice: t(".success")- else- flash.now[:error] = @errors.first.message- render :show, status: :unprocessable_entity+ Setting.upgrades_mode = mode+ Setting.upgrades_target = target+ end++ if hosting_params.key?(:render_deploy_hook)+ Setting.render_deploy_hook = hosting_params[:render_deploy_hook]end- end- def send_test_email- unless Setting.smtp_settings_populated?- flash[:alert] = t(".missing_smtp_setting_error")- render(:show, status: :unprocessable_entity)- return+ if hosting_params.key?(:require_invite_for_signup)+ Setting.require_invite_for_signup = hosting_params[:require_invite_for_signup]end- begin- NotificationMailer.with(user: Current.user).test_email.deliver_now- rescue => _e- flash[:alert] = t(".error")- render :show, status: :unprocessable_entity- return+ if hosting_params.key?(:synth_api_key)+ Setting.synth_api_key = hosting_params[:synth_api_key]endredirect_to settings_hosting_path, notice: t(".success")+ rescue ActiveRecord::RecordInvalid => error+ flash.now[:alert] = t(".failure")+ render :show, status: :unprocessable_entityendprivate- def all_updates_valid?- @errors = ActiveModel::Errors.new(Setting)- hosting_params.keys.each do |key|- setting = Setting.new(var: key)- setting.value = hosting_params[key].strip-- unless setting.valid?- @errors.merge!(setting.errors)- end- end-- if hosting_params[:upgrades_mode] == "auto" && hosting_params[:render_deploy_hook].blank?- @errors.add(:render_deploy_hook, t("settings.hostings.update.render_deploy_hook_error"))- end-- @errors.empty?- end-def hosting_params- permitted_params = params.require(:setting).permit(:render_deploy_hook, :upgrades_mode, :email_sender, :app_domain, :smtp_host, :smtp_port, :smtp_username, :smtp_password, :require_invite_for_signup)-- result = {}- result[:upgrades_mode] = permitted_params[:upgrades_mode] == "manual" ? "manual" : "auto" if permitted_params.key?(:upgrades_mode)- result[:render_deploy_hook] = permitted_params[:render_deploy_hook] if permitted_params.key?(:render_deploy_hook)- result[:upgrades_target] = permitted_params[:upgrades_mode] unless permitted_params[:upgrades_mode] == "manual" if permitted_params.key?(:upgrades_mode)- result.merge!(permitted_params.slice(:email_sender, :app_domain, :smtp_host, :smtp_port, :smtp_username, :smtp_password, :require_invite_for_signup))- result+ params.require(:setting).permit(:render_deploy_hook, :upgrades_setting, :require_invite_for_signup, :synth_api_key)end- def verify_hosting_mode- head :not_found unless self_hosted?+ def raise_if_not_self_hosted+ raise "Settings not available on non-self-hosted instance" unless self_hosted?endenddiff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rbindex adc394b4..47cdc622 100644--- a/app/mailers/application_mailer.rb+++ b/app/mailers/application_mailer.rb@@ -1,16 +1,3 @@class ApplicationMailer < ActionMailer::Baselayout "mailer"-- after_action :set_self_host_settings, if: -> { Rails.configuration.app_mode.self_hosted? }-- private-- def set_self_host_settings- mail.from = Setting.email_sender- mail.delivery_method.settings.merge!({ address: Setting.smtp_host,- port: Setting.smtp_port,- user_name: Setting.smtp_username,- password: Setting.smtp_password,- tls: ENV.fetch("SMTP_TLS_ENABLED", "true") == "true" })- endenddiff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rbdeleted file mode 100644index 025aa8e9..00000000--- a/app/mailers/notification_mailer.rb+++ /dev/null@@ -1,5 +0,0 @@-class NotificationMailer < ApplicationMailer- def test_email- mail(to: params[:user].email, subject: t(".test_email_subject"), body: t(".test_email_body"))- end-enddiff --git a/app/models/concerns/providable.rb b/app/models/concerns/providable.rbindex fcc8a80f..5ba88932 100644--- a/app/models/concerns/providable.rb+++ b/app/models/concerns/providable.rb@@ -18,13 +18,12 @@ module ProvidableProvider::Github.newend- private-- def synth_provider- api_key = self_hosted? ? Setting.synth_api_key : ENV["SYNTH_API_KEY"]- api_key.present? ? Provider::Synth.new(api_key) : nil- end+ def synth_provider+ api_key = self_hosted? ? Setting.synth_api_key : ENV["SYNTH_API_KEY"]+ api_key.present? ? Provider::Synth.new(api_key) : nil+ end+ privatedef self_hosted?Rails.application.config.app_mode.self_hosted?enddiff --git a/app/models/family.rb b/app/models/family.rbindex b7ffdbc9..e7307bad 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -1,4 +1,6 @@class Family < ApplicationRecord+ include Providable+has_many :users, dependent: :destroyhas_many :tags, dependent: :destroyhas_many :accounts, dependent: :destroy@@ -119,4 +121,8 @@ class Family < ApplicationRecorddef needs_sync?last_synced_at.nil? || last_synced_at.to_date < Date.currentend++ def synth_usage+ self.class.synth_provider&.usage+ endenddiff --git a/app/models/provider/synth.rb b/app/models/provider/synth.rbindex 8a0a8302..17d6954e 100644--- a/app/models/provider/synth.rb+++ b/app/models/provider/synth.rb@@ -9,6 +9,38 @@ class Provider::Synthresponse = client.get("#{base_url}/user")JSON.parse(response.body).dig("id").present?end+ def usage+ response = client.get("#{base_url}/user")++ if response.status == 401+ return UsageResponse.new(+ success?: false,+ error: "Unauthorized: Invalid API key",+ raw_response: response+ )+ end++ parsed = JSON.parse(response.body)++ remaining = parsed.dig("api_calls_remaining")+ limit = parsed.dig("api_limit")+ used = limit - remaining++ UsageResponse.new(+ used: used,+ limit: limit,+ utilization: used.to_f / limit * 100,+ plan: parsed.dig("plan"),+ success?: true,+ raw_response: response+ )+ rescue StandardError => error+ UsageResponse.new(+ success?: false,+ error: error,+ raw_response: error+ )+ enddef fetch_security_prices(ticker:, start_date:, end_date:)prices = paginate(@@ -96,6 +128,7 @@ class Provider::SynthExchangeRateResponse = Struct.new :rate, :success?, :error, :raw_response, keyword_init: trueSecurityPriceResponse = Struct.new :prices, :success?, :error, :raw_response, keyword_init: trueExchangeRatesResponse = Struct.new :rates, :success?, :error, :raw_response, keyword_init: true+ UsageResponse = Struct.new :used, :limit, :utilization, :plan, :success?, :error, :raw_response, keyword_init: truedef base_url"https://api.synthfinance.com"diff --git a/app/models/setting.rb b/app/models/setting.rbindex 2d177b31..d576fbea 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -17,21 +17,7 @@ class Setting < RailsSettings::Basedefault: ENV.fetch("UPGRADES_TARGET", "release"),validates: { inclusion: { in: %w[release commit] } }- field :app_domain, type: :string, default: ENV["APP_DOMAIN"]- field :email_sender, type: :string, default: ENV["EMAIL_SENDER"]-field :synth_api_key, type: :string, default: ENV["SYNTH_API_KEY"]field :require_invite_for_signup, type: :boolean, default: false-- scope :smtp_settings do- field :smtp_host, type: :string, read_only: true, default: ENV["SMTP_ADDRESS"]- field :smtp_port, type: :string, read_only: true, default: ENV["SMTP_PORT"]- field :smtp_username, type: :string, read_only: true, default: ENV["SMTP_USERNAME"]- field :smtp_password, type: :string, read_only: true, default: ENV["SMTP_PASSWORD"]- end-- def self.smtp_settings_populated?- Setting.defined_fields.select { |f| f.scope == :smtp_settings }.map(&:read).all?(&:present?)- endenddiff --git a/app/views/settings/hostings/_invite_code_settings.html.erb b/app/views/settings/hostings/_invite_code_settings.html.erbnew file mode 100644index 00000000..49828365--- /dev/null+++ b/app/views/settings/hostings/_invite_code_settings.html.erb@@ -0,0 +1,34 @@+<div class="space-y-4">+ <div class="flex items-center justify-between">+ <div class="space-y-1">+ <p class="text-sm"><%= t(".title") %></p>+ <p class="text-gray-500 text-sm"><%= t(".description") %></p>+ </div>++ <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>+ <div class="relative inline-block select-none">+ <%= form.check_box :require_invite_for_signup, class: "sr-only peer", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "input" %>+ <%= form.label :require_invite_for_signup, " ".html_safe, class: "maybe-switch" %>+ </div>+ <% end %>+ </div>++ <% if Setting.require_invite_for_signup %>+ <div class="flex items-center justify-between mb-4">+ <div>+ <span class="text-gray-900 text-base font-medium"><%= t(".generated_tokens") %></span>+ </div>+ <div>+ <%= button_to invite_codes_path,+ method: :post,+ class: "flex gap-1 bg-gray-50 text-gray-900 text-sm rounded-lg px-3 py-2" do %>+ <span><%= t(".generate_tokens") %></span>+ <% end %>+ </div>+ </div>++ <div>+ <%= turbo_frame_tag :invite_codes, src: invite_codes_path %>+ </div>+ <% end %>+</div>diff --git a/app/views/settings/hostings/_provider_settings.html.erb b/app/views/settings/hostings/_provider_settings.html.erbnew file mode 100644index 00000000..c4aef548--- /dev/null+++ b/app/views/settings/hostings/_provider_settings.html.erb@@ -0,0 +1,9 @@+<% if ENV["HOSTING_PLATFORM"] == "render" %>+ <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>+ <div class="space-y-4">+ <h2 class="font-medium mb-1"><%= t(".title") %></h2>+ <p class="text-gray-500 text-sm mb-4"><%= t(".description") %></p>+ <%= form.url_field :render_deploy_hook, label: t(".render_deploy_hook_label"), placeholder: t(".render_deploy_hook_placeholder"), value: Setting.render_deploy_hook, data: { "auto-submit-form-target" => "auto" } %>+ </div>+ <% end %>+<% end %>diff --git a/app/views/settings/hostings/_synth_settings.html.erb b/app/views/settings/hostings/_synth_settings.html.erbnew file mode 100644index 00000000..30445b00--- /dev/null+++ b/app/views/settings/hostings/_synth_settings.html.erb@@ -0,0 +1,44 @@+<div class="space-y-4">+ <div>+ <h2 class="font-medium mb-1"><%= t(".title") %></h2>+ <p class="text-gray-500 text-sm mb-4"><%= t(".description") %></p>+ </div>++ <%= styled_form_with model: Setting.new,+ url: settings_hosting_path,+ method: :patch,+ data: {+ controller: "auto-submit-form",+ "auto-submit-form-trigger-event-value": "blur"+ } do |form| %>+ <%= form.text_field :synth_api_key,+ label: t(".label"),+ type: "password",+ placeholder: t(".placeholder"),+ value: Setting.synth_api_key,+ container_class: @synth_usage.present? && !@synth_usage.success? ? "border-red-500" : "",+ data: { "auto-submit-form-target": "auto" } %>+ <% end %>++ <% if @synth_usage.present? && @synth_usage.success? %>+ <div class="space-y-4">+ <div class="space-y-2">+ <p class="text-sm text-gray-500">+ <%= t(".api_calls_used",+ used: number_with_delimiter(@synth_usage.used),+ limit: number_with_delimiter(@synth_usage.limit),+ percentage: number_to_percentage(@synth_usage.utilization, precision: 1)) %>+ </p>+ <div class="w-52 h-1.5 bg-gray-100 rounded-2xl">+ <div class="h-full bg-green-500 rounded-2xl"+ style="width: <%= [@synth_usage.utilization, 2].max %>%;"></div>+ </div>+ </div>+ <div class="bg-gray-100 rounded-md px-1.5 py-0.5 w-fit">+ <p class="text-xs font-medium text-gray-500 uppercase">+ <%= t(".plan", plan: @synth_usage.plan) %>+ </p>+ </div>+ </div>+ <% end %>+</div>diff --git a/app/views/settings/hostings/_upgrade_settings.html.erb b/app/views/settings/hostings/_upgrade_settings.html.erbnew file mode 100644index 00000000..50594584--- /dev/null+++ b/app/views/settings/hostings/_upgrade_settings.html.erb@@ -0,0 +1,41 @@+<% if ENV["HOSTING_PLATFORM"] == "render" %>+ <div>+ <h2 class="font-medium mb-1"><%= t(".title") %></h2>+ <p class="text-gray-500 text-sm mb-4"><%= t(".description") %></p>++ <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>+ <div class="space-y-4">+ <div class="flex items-center gap-4">+ <%= form.radio_button :upgrades_setting, "manual", checked: Setting.upgrades_mode == "manual", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>+ <%= form.label :upgrades_mode_manual, t(".manual_title"), class: "text-gray-900 text-sm" do %>+ <span class="font-medium"><%= t(".manual_title") %></span>+ <br>+ <span class="text-gray-500">+ <%= t(".manual_description") %>+ </span>+ <% end %>+ </div>+ <div class="flex items-center gap-4">+ <%= form.radio_button :upgrades_setting, "release", checked: Setting.upgrades_mode == "auto" && Setting.upgrades_target == "release", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>+ <%= form.label :upgrades_mode_release, t(".latest_release_title"), class: "text-gray-900 text-sm" do %>+ <span class="font-medium"><%= t(".latest_release_title") %></span>+ <br>+ <span class="text-gray-500">+ <%= t(".latest_release_description") %>+ </span>+ <% end %>+ </div>+ <div class="flex items-center gap-4">+ <%= form.radio_button :upgrades_setting, "commit", checked: Setting.upgrades_mode == "auto" && Setting.upgrades_target == "commit", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>+ <%= form.label :upgrades_mode_commit, t(".latest_commit_title"), class: "text-gray-900 text-sm" do %>+ <span class="font-medium"><%= t(".latest_commit_title") %></span>+ <br>+ <span class="text-gray-500">+ <%= t(".latest_commit_description") %>+ </span>+ <% end %>+ </div>+ </div>+ <% end %>+ </div>+<% end %>diff --git a/app/views/settings/hostings/show.html.erb b/app/views/settings/hostings/show.html.erbindex a96e68cb..ba4b7d5d 100644--- a/app/views/settings/hostings/show.html.erb+++ b/app/views/settings/hostings/show.html.erb@@ -3,121 +3,18 @@<% end %><div class="space-y-4 pb-32">- <h1 class="text-gray-900 text-xl font-medium mb-4"><%= t(".page_title") %></h1>+ <h1 class="text-gray-900 text-xl font-medium mb-4"><%= t(".title") %></h1>- <% if ENV["HOSTING_PLATFORM"] == "render" %>- <%= settings_section title: t(".general_settings_title") do %>- <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>- <h2 class="font-medium mb-1"><%= t(".upgrades.title") %></h2>- <p class="text-gray-500 text-sm mb-4"><%= t(".upgrades.description") %></p>-- <div class="space-y-4">- <div class="flex items-center gap-4">- <%= form.radio_button :upgrades_mode, "manual", checked: Setting.upgrades_mode == "manual", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>- <%= form.label :upgrades_mode_manual, t(".upgrades.manual.title"), class: "text-gray-900 text-sm" do %>- <span class="font-medium"><%= t(".upgrades.manual.title") %></span>- <br>- <span class="text-gray-500">- <%= t(".upgrades.manual.description") %>- </span>- <% end %>- </div>- <div class="flex items-center gap-4">- <%= form.radio_button :upgrades_mode, "release", checked: Setting.upgrades_mode == "auto" && Setting.upgrades_target == "release", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>- <%= form.label :upgrades_mode_release, t(".upgrades.latest_release.title"), class: "text-gray-900 text-sm" do %>- <span class="font-medium"><%= t(".upgrades.latest_release.title") %></span>- <br>- <span class="text-gray-500">- <%= t(".upgrades.latest_release.description") %>- </span>- <% end %>- </div>- <div class="flex items-center gap-4">- <%= form.radio_button :upgrades_mode, "commit", checked: Setting.upgrades_mode == "auto" && Setting.upgrades_target == "commit", data: { "auto-submit-form-target" => "auto", "autosubmit-trigger-event": "input" } %>- <%= form.label :upgrades_mode_commit, t(".upgrades.latest_commit.title"), class: "text-gray-900 text-sm" do %>- <span class="font-medium"><%= t(".upgrades.latest_commit.title") %></span>- <br>- <span class="text-gray-500">- <%= t(".upgrades.latest_commit.description") %>- </span>- <% end %>- </div>- </div>-- <div>- <h2 class="font-medium mb-1"><%= t(".provider_settings.title") %></h2>- <p class="text-gray-500 text-sm mb-4"><%= t(".render_deploy_hook_description") %></p>- <%= form.url_field :render_deploy_hook, label: t(".render_deploy_hook_label"), placeholder: t(".render_deploy_hook_placeholder"), value: Setting.render_deploy_hook, data: { "auto-submit-form-target" => "auto" } %>- </div>- <% end %>- <% end %>- <% end %>-- <%= settings_section title: t(".smtp_settings.title") do %>- <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>- <p class="text-gray-500 text-sm mb-4"><%= t(".smtp_settings.description") %></p>- <div class="space-y-4">- <div class="space-y-3">- <%= form.text_field :email_sender, label: t(".email_sender"), placeholder: t(".email_sender_placeholder"), value: Setting.email_sender, data: { "auto-submit-form-target" => "auto" } %>- <%= form.text_field :app_domain, label: t(".domain"), placeholder: t(".domain_placeholder"), value: Setting.app_domain, data: { "auto-submit-form-target" => "auto" } %>- <%= form.text_field :smtp_host, label: t(".smtp_settings.host"), placeholder: t(".smtp_settings.host_placeholder"), value: Setting.smtp_host, data: { "auto-submit-form-target" => "auto" } %>- <%= form.number_field :smtp_port, label: t(".smtp_settings.port"), placeholder: t(".smtp_settings.port_placeholder"), value: Setting.smtp_port, data: { "auto-submit-form-target" => "auto" } %>- <%= form.text_field :smtp_username, label: t(".smtp_settings.username"), placeholder: t(".smtp_settings.username_placeholder"), value: Setting.smtp_username, data: { "auto-submit-form-target" => "auto" } %>- <%= form.password_field :smtp_password, label: t(".smtp_settings.password"), placeholder: t(".smtp_settings.password_placeholder"), value: Setting.smtp_password, data: { "auto-submit-form-target" => "auto" } %>- </div>- <div class="flex items-center justify-between bg-white border border-alpha-black-100 p-4 rounded-lg">- <div class="flex items-center gap-3">- <div class="w-10 h-10 rounded-full bg-gray-25 flex items-center justify-center">- <%= lucide_icon "mails", class: "w-6 h-6 text-gray-500" %>- </div>- <div>- <p class="text-gray-900 font-medium text-sm"><%= t(".smtp_settings.send_test_email") %></p>- <p class="text-gray-500 text-sm"><%= t(".smtp_settings.send_test_email_description") %></p>- </div>- </div>- <div>- <%= link_to t(".smtp_settings.send_test_email_button"), send_test_email_settings_hosting_path, data: { turbo_method: :post }, class: "bg-gray-50 text-gray-900 text-sm font-medium rounded-lg px-3 py-2" %>- </div>- </div>- </div>- <% end %>+ <%= settings_section title: t(".general") do %>+ <div class="space-y-6">+ <%= render "settings/hostings/upgrade_settings" %>+ <%= render "settings/hostings/provider_settings" %>+ <%= render "settings/hostings/synth_settings" %>+ </div><% end %>- <%= settings_section title: t(".invite_settings.title") do %>- <div class="space-y-4">- <div class="flex items-center justify-between">- <div class="space-y-1">- <p class="text-sm"><%= t(".invite_settings.require_invite_for_signup") %></p>- <p class="text-gray-500 text-sm"><%= t(".invite_settings.invite_code_description") %></p>- </div>-- <%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>- <div class="relative inline-block select-none">- <%= form.check_box :require_invite_for_signup, class: "sr-only peer", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "input" %>- <%= form.label :require_invite_for_signup, " ".html_safe, class: "maybe-switch" %>- </div>- <% end %>- </div>-- <% if Setting.require_invite_for_signup %>- <div class="flex items-center justify-between mb-4">- <div>- <span class="text-gray-900 text-base font-medium"><%= t(".invite_settings.generated_tokens") %></span>- </div>- <div>- <%= button_to invite_codes_path,- method: :post,- class: "flex gap-1 bg-gray-50 text-gray-900 text-sm rounded-lg px-3 py-2" do %>- <span><%= t(".invite_settings.generate_tokens") %></span>- <% end %>- </div>- </div>-- <div>- <%= turbo_frame_tag :invite_codes, src: invite_codes_path %>- </div>- <% end %>- </div>+ <%= settings_section title: t(".invites") do %>+ <%= render "settings/hostings/invite_code_settings" %><% end %><%= settings_nav_footer %>diff --git a/config/application.rb b/config/application.rbindex 0be8f797..80e8d4ed 100644--- a/config/application.rb+++ b/config/application.rb@@ -24,8 +24,6 @@ module Maybe# config.time_zone = "Central Time (US & Canada)"# config.eager_load_paths << Rails.root.join("extras")- config.action_mailer.default_options = { from: ENV["MAILER_SENDER"] }-config.active_job.queue_adapter = :good_jobconfig.app_mode = (ENV["SELF_HOSTING_ENABLED"] == "true" ? "self_hosted" : "managed").inquirydiff --git a/config/environments/development.rb b/config/environments/development.rbindex 907b2b93..2dcd4b05 100644--- a/config/environments/development.rb+++ b/config/environments/development.rb@@ -40,18 +40,6 @@ Rails.application.configure doconfig.action_mailer.raise_delivery_errors = falseconfig.action_mailer.delivery_method = :letter_opener- # Uncomment to send emails in development- # config.action_mailer.raise_delivery_errors = true- # config.action_mailer.delivery_method = :smtp- # config.action_mailer.smtp_settings = {- # address: ENV["SMTP_ADDRESS"],- # port: ENV["SMTP_PORT"],- # user_name: ENV["SMTP_USERNAME"],- # password: ENV["SMTP_PASSWORD"],- # tls: ENV.fetch("SMTP_TLS_ENABLED", "true") == "true"- # }--config.action_mailer.perform_caching = falseconfig.action_mailer.perform_deliveries = truediff --git a/config/locales/views/notification_mailer/en.yml b/config/locales/views/notification_mailer/en.ymldeleted file mode 100644index c15894c1..00000000--- a/config/locales/views/notification_mailer/en.yml+++ /dev/null@@ -1,7 +0,0 @@-----en:- notification_mailer:- test_email:- test_email_body: Congratulation ! Connection to the SMTP server is now correctly- configured.- test_email_subject: SMTP settings verified !diff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex e4bbb1bb..83f067ff 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -1,61 +1,6 @@---en:settings:- hostings:- send_test_email:- error: 'Configuration error: Test email could not be sent'- missing_smtp_setting_error: Ensure that all smtp settings are filled in- success: Test email has been sent successfully- show:- domain: App Domain- domain_placeholder: mydomain.com- email_sender: Email Sender- email_sender_placeholder: [email protected]- general_settings_title: General Settings- invite_settings:- generate_tokens: Generate new code- generated_tokens: Generated codes- invite_code_description: Every new user that joins your instance if Maybe- can only do so via an invite code- require_invite_for_signup: Require invite code for new sign ups- title: Invite Codes- page_title: Self-Hosting- provider_settings:- title: Provider Settings- render_deploy_hook_description: Input the deploy hook URL provided by Render- render_deploy_hook_label: Render Deploy Hook URL- render_deploy_hook_placeholder: https://api.render.com/deploy/srv-xyz...- smtp_settings:- description: Configure outgoing mail server settings for notifications and- alerts- host: SMTP Host- host_placeholder: smtp.gmail.com- password: Password- password_placeholder: "*******"- port: Port- port_placeholder: 587- send_test_email: Send test email- send_test_email_button: Send test email- send_test_email_description: Verify SMTP settings by sending a test email- title: SMTP Email Configuration- username: Username- username_placeholder: [email protected]- upgrades:- description: Choose how your application receives updates- latest_commit:- description: Automatically update to the latest commit (unstable)- title: Latest Commit- latest_release:- description: Automatically update to the most recent release (stable)- title: Latest Release- manual:- description: You control when to download and install updates- title: Manual- title: Auto upgrade- update:- render_deploy_hook_error: Render deploy hook must be provided to enable auto- upgrades- success: Settings updated successfully.nav:accounts_label: Accountscategories_label: Categoriesdiff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlnew file mode 100644index 00000000..0e4a5b2d--- /dev/null+++ b/config/locales/views/settings/hostings/en.yml@@ -0,0 +1,39 @@+---+en:+ settings:+ hostings:+ invite_code_settings:+ description: Every new user that joins your instance if Maybe can only do+ so via an invite code+ generate_tokens: Generate new code+ generated_tokens: Generated codes+ title: Require invite code for new sign ups+ provider_settings:+ description: Configure settings for your hosting provider+ render_deploy_hook_label: Render Deploy Hook URL+ render_deploy_hook_placeholder: https://api.render.com/deploy/srv-xyz...+ title: Provider Settings+ show:+ general: General Settings+ invites: Invite Codes+ title: Self-Hosting+ synth_settings:+ api_calls_used: "%{used} / %{limit} API calls used (%{percentage})"+ description: Input the API key provided by Synth+ label: API Key+ placeholder: Enter your API key here+ plan: "%{plan} plan"+ title: Synth Settings+ update:+ failure: Invalid setting value+ success: Settings updated+ upgrade_settings:+ description: Configure how your application receives updates+ latest_commit_description: Automatically update to the latest commit (unstable)+ latest_commit_title: Latest Commit+ latest_release_description: Automatically update to the most recent release+ (stable)+ latest_release_title: Latest Release+ manual_description: You control when to download and install updates+ manual_title: Manual+ title: Auto Upgradediff --git a/config/routes.rb b/config/routes.rbindex cab9eeab..9db1cc04 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -16,9 +16,7 @@ Rails.application.routes.draw donamespace :settings doresource :profile, only: %i[show update destroy]resource :preferences, only: %i[show update]- resource :hosting, only: %i[show update] do- post :send_test_email, on: :collection- end+ resource :hosting, only: %i[show update]endresources :tags, except: %i[show destroy] dodiff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex 05e70789..283b70bc 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -6,11 +6,13 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestendtest "cannot edit when self hosting is disabled" do- get settings_hosting_url- assert :not_found+ assert_raises(RuntimeError, "Settings not available on non-self-hosted instance") do+ get settings_hosting_url+ end- patch settings_hosting_url, params: { setting: { render_deploy_hook: "https://example.com" } }- assert :not_found+ assert_raises(RuntimeError, "Settings not available on non-self-hosted instance") do+ patch settings_hosting_url, params: { setting: { render_deploy_hook: "https://example.com" } }+ endendtest "should get edit when self hosting is enabled" do@@ -31,63 +33,16 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestendend- test "cannot set auto upgrades mode without a deploy hook" do- with_self_hosting do- patch settings_hosting_url, params: { setting: { upgrades_mode: "auto" } }- assert_response :unprocessable_entity- end- end-test "can choose auto upgrades mode with a deploy hook" dowith_self_hosting doNEW_RENDER_DEPLOY_HOOK = "https://api.render.com/deploy/srv-abc123"assert_nil Setting.render_deploy_hook- patch settings_hosting_url, params: { setting: { render_deploy_hook: NEW_RENDER_DEPLOY_HOOK, upgrades_mode: "release" } }+ patch settings_hosting_url, params: { setting: { render_deploy_hook: NEW_RENDER_DEPLOY_HOOK, upgrades_setting: "release" } }assert_equal "auto", Setting.upgrades_modeassert_equal "release", Setting.upgrades_targetassert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hookendend-- test " #send_test_email if smtp settings are populated try to send an email and redirect with notice" do- with_self_hosting 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- end-- test "#send_test_email with one blank smtp setting" do- with_self_hosting 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[:alert].present?- end- end-- test "#send_test_email when sending the email raise an error" do- with_self_hosting 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[:alert].present?- end- endenddiff --git a/test/mailers/.keep b/test/mailers/.keepdeleted file mode 100644index e69de29b..00000000diff --git a/test/mailers/application_mailer_test.rb b/test/mailers/application_mailer_test.rbdeleted file mode 100644index a4edeb17..00000000--- a/test/mailers/application_mailer_test.rb+++ /dev/null@@ -1,43 +0,0 @@-require "test_helper"--class ApplicationMailerTest < ActionMailer::TestCase- setup do- class TestMailer < ApplicationMailer- def test_email- mail(to: "[email protected]", from: "[email protected]", subject: "Test email subject", body: "Test email body")- end- end- end-- test "should use self host settings when self host enabled" do- with_self_hosting do- smtp_host = "smtp.example.com"- smtp_port = 466- smtp_username = "[email protected]"- smtp_password = "password"- email_sender = "[email protected]"-- 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- end-- test "should use regular env settings when self host disabled" do- TestMailer.test_email.deliver_now-- assert_emails 1- assert_nil ActionMailer::Base.deliveries.first.delivery_method.settings[:address]- end-enddiff --git a/test/models/setting_test.rb b/test/models/setting_test.rbdeleted file mode 100644index a205b8ce..00000000--- a/test/models/setting_test.rb+++ /dev/null@@ -1,23 +0,0 @@-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 = "[email protected]"- Setting.smtp_password = "[email protected]"- 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 = "[email protected]"- Setting.smtp_password = "[email protected]"- Setting.email_sender = "password"-- assert_not Setting.smtp_settings_populated?- end-end