From 7fabca4679d00d59b368963db0a711c047f09c3f Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Wed, 2 Oct 2024 12:07:56 -0400 Subject: [PATCH] Simplify self host settings controller (#1230) --- .../settings/hostings_controller.rb | 74 ++++------- app/mailers/application_mailer.rb | 13 -- app/mailers/notification_mailer.rb | 5 - app/models/concerns/providable.rb | 11 +- app/models/family.rb | 6 + app/models/provider/synth.rb | 33 +++++ app/models/setting.rb | 14 -- .../hostings/_invite_code_settings.html.erb | 34 +++++ .../hostings/_provider_settings.html.erb | 9 ++ .../hostings/_synth_settings.html.erb | 44 +++++++ .../hostings/_upgrade_settings.html.erb | 41 ++++++ app/views/settings/hostings/show.html.erb | 123 ++---------------- config/application.rb | 2 - config/environments/development.rb | 12 -- .../locales/views/notification_mailer/en.yml | 7 - config/locales/views/settings/en.yml | 55 -------- config/locales/views/settings/hostings/en.yml | 39 ++++++ config/routes.rb | 4 +- .../settings/hostings_controller_test.rb | 59 +-------- test/mailers/.keep | 0 test/mailers/application_mailer_test.rb | 43 ------ test/models/setting_test.rb | 23 ---- 22 files changed, 252 insertions(+), 399 deletions(-) delete mode 100644 app/mailers/notification_mailer.rb create mode 100644 app/views/settings/hostings/_invite_code_settings.html.erb create mode 100644 app/views/settings/hostings/_provider_settings.html.erb create mode 100644 app/views/settings/hostings/_synth_settings.html.erb create mode 100644 app/views/settings/hostings/_upgrade_settings.html.erb delete mode 100644 config/locales/views/notification_mailer/en.yml create mode 100644 config/locales/views/settings/hostings/en.yml delete mode 100644 test/mailers/.keep delete mode 100644 test/mailers/application_mailer_test.rb delete mode 100644 test/models/setting_test.rb diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rb index 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_hosted def show + @synth_usage = Current.family.synth_usage end def 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 - end - end - - def send_test_email - unless Setting.smtp_settings_populated? - flash[:alert] = t(".missing_smtp_setting_error") - render(:show, status: :unprocessable_entity) - return + Setting.upgrades_mode = mode + Setting.upgrades_target = target 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?(:render_deploy_hook) + Setting.render_deploy_hook = hosting_params[:render_deploy_hook] + end + + if hosting_params.key?(:require_invite_for_signup) + Setting.require_invite_for_signup = hosting_params[:require_invite_for_signup] + end + + if hosting_params.key?(:synth_api_key) + Setting.synth_api_key = hosting_params[:synth_api_key] end redirect_to settings_hosting_path, notice: t(".success") + rescue ActiveRecord::RecordInvalid => error + flash.now[:alert] = t(".failure") + render :show, status: :unprocessable_entity end private - 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? end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index adc394b4..47cdc622 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,16 +1,3 @@ class ApplicationMailer < ActionMailer::Base layout "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" }) - end end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb deleted file mode 100644 index 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 -end diff --git a/app/models/concerns/providable.rb b/app/models/concerns/providable.rb index fcc8a80f..5ba88932 100644 --- a/app/models/concerns/providable.rb +++ b/app/models/concerns/providable.rb @@ -18,13 +18,12 @@ module Providable Provider::Github.new 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 + 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 self_hosted? Rails.application.config.app_mode.self_hosted? end diff --git a/app/models/family.rb b/app/models/family.rb index 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: :destroy has_many :tags, dependent: :destroy has_many :accounts, dependent: :destroy @@ -119,4 +121,8 @@ class Family < ApplicationRecord def needs_sync? last_synced_at.nil? || last_synced_at.to_date < Date.current end + + def synth_usage + self.class.synth_provider&.usage + end end diff --git a/app/models/provider/synth.rb b/app/models/provider/synth.rb index 8a0a8302..17d6954e 100644 --- a/app/models/provider/synth.rb +++ b/app/models/provider/synth.rb @@ -9,6 +9,38 @@ class Provider::Synth response = 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 + ) + end def fetch_security_prices(ticker:, start_date:, end_date:) prices = paginate( @@ -96,6 +128,7 @@ class Provider::Synth ExchangeRateResponse = Struct.new :rate, :success?, :error, :raw_response, keyword_init: true SecurityPriceResponse = Struct.new :prices, :success?, :error, :raw_response, keyword_init: true ExchangeRatesResponse = Struct.new :rates, :success?, :error, :raw_response, keyword_init: true + UsageResponse = Struct.new :used, :limit, :utilization, :plan, :success?, :error, :raw_response, keyword_init: true def base_url "https://api.synthfinance.com" diff --git a/app/models/setting.rb b/app/models/setting.rb index 2d177b31..d576fbea 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -17,21 +17,7 @@ class Setting < RailsSettings::Base default: 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?) - end end diff --git a/app/views/settings/hostings/_invite_code_settings.html.erb b/app/views/settings/hostings/_invite_code_settings.html.erb new file mode 100644 index 00000000..49828365 --- /dev/null +++ b/app/views/settings/hostings/_invite_code_settings.html.erb @@ -0,0 +1,34 @@ +
+
+
+

<%= t(".title") %>

+

<%= t(".description") %>

+
+ + <%= 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.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" %> +
+ <% end %> +
+ + <% if Setting.require_invite_for_signup %> +
+
+ <%= t(".generated_tokens") %> +
+
+ <%= 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 %> + <%= t(".generate_tokens") %> + <% end %> +
+
+ +
+ <%= turbo_frame_tag :invite_codes, src: invite_codes_path %> +
+ <% end %> +
diff --git a/app/views/settings/hostings/_provider_settings.html.erb b/app/views/settings/hostings/_provider_settings.html.erb new file mode 100644 index 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| %> +
+

<%= t(".title") %>

+

<%= t(".description") %>

+ <%= 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" } %> +
+ <% end %> +<% end %> diff --git a/app/views/settings/hostings/_synth_settings.html.erb b/app/views/settings/hostings/_synth_settings.html.erb new file mode 100644 index 00000000..30445b00 --- /dev/null +++ b/app/views/settings/hostings/_synth_settings.html.erb @@ -0,0 +1,44 @@ +
+
+

<%= t(".title") %>

+

<%= t(".description") %>

+
+ + <%= 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? %> +
+
+

+ <%= 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)) %> +

+
+
+
+
+
+

+ <%= t(".plan", plan: @synth_usage.plan) %> +

+
+
+ <% end %> +
diff --git a/app/views/settings/hostings/_upgrade_settings.html.erb b/app/views/settings/hostings/_upgrade_settings.html.erb new file mode 100644 index 00000000..50594584 --- /dev/null +++ b/app/views/settings/hostings/_upgrade_settings.html.erb @@ -0,0 +1,41 @@ +<% if ENV["HOSTING_PLATFORM"] == "render" %> +
+

<%= t(".title") %>

+

<%= t(".description") %>

+ + <%= 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.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 %> + <%= t(".manual_title") %> +
+ + <%= t(".manual_description") %> + + <% end %> +
+
+ <%= 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 %> + <%= t(".latest_release_title") %> +
+ + <%= t(".latest_release_description") %> + + <% end %> +
+
+ <%= 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 %> + <%= t(".latest_commit_title") %> +
+ + <%= t(".latest_commit_description") %> + + <% end %> +
+
+ <% end %> +
+<% end %> diff --git a/app/views/settings/hostings/show.html.erb b/app/views/settings/hostings/show.html.erb index a96e68cb..ba4b7d5d 100644 --- a/app/views/settings/hostings/show.html.erb +++ b/app/views/settings/hostings/show.html.erb @@ -3,122 +3,19 @@ <% end %>
-

<%= t(".page_title") %>

+

<%= t(".title") %>

- <% 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| %> -

<%= t(".upgrades.title") %>

-

<%= t(".upgrades.description") %>

- -
-
- <%= 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 %> - <%= t(".upgrades.manual.title") %> -
- - <%= t(".upgrades.manual.description") %> - - <% end %> -
-
- <%= 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 %> - <%= t(".upgrades.latest_release.title") %> -
- - <%= t(".upgrades.latest_release.description") %> - - <% end %> -
-
- <%= 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 %> - <%= t(".upgrades.latest_commit.title") %> -
- - <%= t(".upgrades.latest_commit.description") %> - - <% end %> -
-
- -
-

<%= t(".provider_settings.title") %>

-

<%= t(".render_deploy_hook_description") %>

- <%= 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" } %> -
- <% 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| %> -

<%= t(".smtp_settings.description") %>

-
-
- <%= 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" } %> -
-
-
-
- <%= lucide_icon "mails", class: "w-6 h-6 text-gray-500" %> -
-
-

<%= t(".smtp_settings.send_test_email") %>

-

<%= t(".smtp_settings.send_test_email_description") %>

-
-
-
- <%= 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" %> -
-
-
- <% end %> - <% end %> - - <%= settings_section title: t(".invite_settings.title") do %> -
-
-
-

<%= t(".invite_settings.require_invite_for_signup") %>

-

<%= t(".invite_settings.invite_code_description") %>

-
- - <%= 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.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" %> -
- <% end %> -
- - <% if Setting.require_invite_for_signup %> -
-
- <%= t(".invite_settings.generated_tokens") %> -
-
- <%= 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 %> - <%= t(".invite_settings.generate_tokens") %> - <% end %> -
-
- -
- <%= turbo_frame_tag :invite_codes, src: invite_codes_path %> -
- <% end %> + <%= settings_section title: t(".general") do %> +
+ <%= render "settings/hostings/upgrade_settings" %> + <%= render "settings/hostings/provider_settings" %> + <%= render "settings/hostings/synth_settings" %>
<% end %> + <%= 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.rb index 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_job config.app_mode = (ENV["SELF_HOSTING_ENABLED"] == "true" ? "self_hosted" : "managed").inquiry diff --git a/config/environments/development.rb b/config/environments/development.rb index 907b2b93..2dcd4b05 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -40,18 +40,6 @@ Rails.application.configure do config.action_mailer.raise_delivery_errors = false config.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 = false config.action_mailer.perform_deliveries = true diff --git a/config/locales/views/notification_mailer/en.yml b/config/locales/views/notification_mailer/en.yml deleted file mode 100644 index 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.yml index 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: user@mydomain.com - 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: username@gmail.com - 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: Accounts categories_label: Categories diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.yml new file mode 100644 index 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 Upgrade diff --git a/config/routes.rb b/config/routes.rb index cab9eeab..9db1cc04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,9 +16,7 @@ Rails.application.routes.draw do namespace :settings do resource :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] end resources :tags, except: %i[show destroy] do diff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rb index 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::IntegrationTest end test "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" } } + end end test "should get edit when self hosting is enabled" do @@ -31,63 +33,16 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest end end - 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" do with_self_hosting do NEW_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_mode assert_equal "release", Setting.upgrades_target assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook end end - - 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 - end end diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/mailers/application_mailer_test.rb b/test/mailers/application_mailer_test.rb deleted file mode 100644 index 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: "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 - with_self_hosting do - 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 - 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 -end diff --git a/test/models/setting_test.rb b/test/models/setting_test.rb deleted file mode 100644 index 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 = "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