1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 21:29:38 +02:00

Test environment stability improvements (#703)

* Add climate_control gem and test helper

* Replace ENV mods in upgrades test

* Replace ENV mods in registrations test

* Remove ENV references in hostings controller

* Update ENV refs in mailer test

* ActiveStorage cleanup

* Consolidate queue config so appropriate adapter runs in test environment

* Make test environment more explicit

* Centralize self hosting config

* Remove flaky system test
This commit is contained in:
Zach Gollwitzer 2024-05-02 13:18:18 -04:00 committed by GitHub
parent 98df7ccb11
commit 5dfbba403a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 137 additions and 137 deletions

View file

@ -51,12 +51,12 @@ group :development, :test do
gem "debug", platforms: %i[ mri windows ] gem "debug", platforms: %i[ mri windows ]
gem "brakeman", require: false gem "brakeman", require: false
gem "rubocop-rails-omakase", require: false gem "rubocop-rails-omakase", require: false
gem "dotenv-rails"
gem "i18n-tasks" gem "i18n-tasks"
gem "erb_lint" gem "erb_lint"
end end
group :development do group :development do
gem "dotenv-rails"
gem "hotwire-livereload" gem "hotwire-livereload"
gem "letter_opener" gem "letter_opener"
gem "ruby-lsp-rails" gem "ruby-lsp-rails"
@ -69,4 +69,5 @@ group :test do
gem "mocha" gem "mocha"
gem "vcr" gem "vcr"
gem "webmock" gem "webmock"
gem "climate_control"
end end

View file

@ -149,6 +149,7 @@ GEM
regexp_parser (>= 1.5, < 3.0) regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2) xpath (~> 3.2)
childprocess (5.0.0) childprocess (5.0.0)
climate_control (1.2.0)
concurrent-ruby (1.2.3) concurrent-ruby (1.2.3)
connection_pool (2.4.1) connection_pool (2.4.1)
crack (1.0.0) crack (1.0.0)
@ -455,6 +456,7 @@ DEPENDENCIES
bootsnap bootsnap
brakeman brakeman
capybara capybara
climate_control
debug debug
dotenv-rails dotenv-rails
erb_lint erb_lint

View file

@ -7,6 +7,6 @@ module SelfHostable
private private
def self_hosted? def self_hosted?
ENV["SELF_HOSTING_ENABLED"] == "true" Rails.configuration.app_mode.self_hosted?
end end
end end

View file

@ -1,7 +1,7 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
layout "mailer" layout "mailer"
after_action :set_self_host_settings, if: -> { ENV["SELF_HOSTING_ENABLED"] == "true" } after_action :set_self_host_settings, if: -> { Rails.configuration.app_mode.self_hosted? }
private private

View file

@ -25,5 +25,9 @@ module Maybe
# config.eager_load_paths << Rails.root.join("extras") # config.eager_load_paths << Rails.root.join("extras")
config.action_mailer.default_options = { from: ENV["MAILER_SENDER"] } 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
end end
end end

View file

@ -76,10 +76,6 @@ Rails.application.configure do
# Highlight code that enqueued background job in logs. # Highlight code that enqueued background job in logs.
config.active_job.verbose_enqueue_logs = true config.active_job.verbose_enqueue_logs = true
# Set Active Job queue adapter
config.active_job.queue_adapter = :good_job
# Raises error for missing translations. # Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true # config.i18n.raise_on_missing_translations = true

View file

@ -61,10 +61,6 @@ Rails.application.configure do
# Use a different cache store in production. # Use a different cache store in production.
# config.cache_store = :mem_cache_store # config.cache_store = :mem_cache_store
# Use a real queuing backend for Active Job (and separate queues per environment).
config.active_job.queue_adapter = :good_job
# config.active_job.queue_name_prefix = "maybe_production"
config.action_mailer.perform_caching = false config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: ENV["APP_DOMAIN"] } config.action_mailer.default_url_options = { host: ENV["APP_DOMAIN"] }

View file

@ -12,14 +12,16 @@ amazon:
secret_access_key: <%= ENV["S3_SECRET_ACCESS_KEY"] %> secret_access_key: <%= ENV["S3_SECRET_ACCESS_KEY"] %>
region: <%= ENV["S3_REGION"] || "us-east-1" %> region: <%= ENV["S3_REGION"] || "us-east-1" %>
bucket: <%= ENV["S3_BUCKET"] %> bucket: <%= ENV["S3_BUCKET"] %>
google:
service: GCS
project: <%= ENV["GCS_PROJECT"] %>
credentials: <%= Rails.root.join("gcp-storage-keyfile.json") %>
bucket: <%= ENV["GCS_BUCKET"] %>
azure: # Removed in #702. Uncomment, add gems, update .env.example to enable.
service: AzureStorage #google:
storage_account_name: <%= ENV["AZURE_STORAGE_ACCOUNT_NAME"] %> # service: GCS
storage_access_key: <%= ENV["AZURE_STORAGE_ACCESS_KEY"] %> # project: <%#= ENV["GCS_PROJECT"] %>
container: <%= ENV["AZURE_STORAGE_CONTAINER"] %> # credentials: <%#= Rails.root.join("gcp-storage-keyfile.json") %>
# bucket: <%#= ENV["GCS_BUCKET"] %>
#azure:
# service: AzureStorage
# storage_account_name: <%#= ENV["AZURE_STORAGE_ACCOUNT_NAME"] %>
# storage_access_key: <%#= ENV["AZURE_STORAGE_ACCESS_KEY"] %>
# container: <%#= ENV["AZURE_STORAGE_CONTAINER"] %>

View file

@ -25,7 +25,7 @@ class RegistrationsControllerTest < ActionDispatch::IntegrationTest
end end
test "create when hosted requires an invite code" do test "create when hosted requires an invite code" do
in_invited_app do with_env_overrides REQUIRE_INVITE_CODE: "true" do
assert_no_difference "User.count" do assert_no_difference "User.count" do
post registration_url, params: { user: { post registration_url, params: { user: {
email: "john@example.com", email: "john@example.com",
@ -51,13 +51,4 @@ class RegistrationsControllerTest < ActionDispatch::IntegrationTest
end end
end end
end end
private
def in_invited_app
ENV["REQUIRE_INVITE_CODE"] = "true"
yield
ensure
ENV["REQUIRE_INVITE_CODE"] = nil
end
end end

View file

@ -2,25 +2,26 @@ require "test_helper"
class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
setup do setup do
ENV["SELF_HOSTING_ENABLED"] = "true"
sign_in users(:family_admin) sign_in users(:family_admin)
end end
test "cannot edit when self hosting is disabled" do test "cannot edit when self hosting is disabled" do
ENV["SELF_HOSTING_ENABLED"] = "false"
get settings_hosting_url get settings_hosting_url
assert :not_found assert :not_found
patch settings_hosting_url, params: { setting: { render_deploy_hook: "https://example.com" } } patch settings_hosting_url, params: { setting: { render_deploy_hook: "https://example.com" } }
assert :not_found assert :not_found
end end
test "should get edit when self hosting is enabled" do test "should get edit when self hosting is enabled" do
with_self_hosting do
get settings_hosting_url get settings_hosting_url
assert_response :success assert_response :success
end end
end
test "can update settings when self hosting is enabled" do test "can update settings when self hosting is enabled" do
with_self_hosting do
NEW_RENDER_DEPLOY_HOOK = "https://api.render.com/deploy/srv-abc123" NEW_RENDER_DEPLOY_HOOK = "https://api.render.com/deploy/srv-abc123"
assert_nil Setting.render_deploy_hook assert_nil Setting.render_deploy_hook
@ -28,13 +29,17 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook
end end
end
test "cannot set auto upgrades mode without a deploy hook" do test "cannot set auto upgrades mode without a deploy hook" do
with_self_hosting do
patch settings_hosting_url, params: { setting: { upgrades_mode: "auto" } } patch settings_hosting_url, params: { setting: { upgrades_mode: "auto" } }
assert_response :unprocessable_entity assert_response :unprocessable_entity
end end
end
test "can choose auto upgrades mode with a deploy hook" do 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" NEW_RENDER_DEPLOY_HOOK = "https://api.render.com/deploy/srv-abc123"
assert_nil Setting.render_deploy_hook assert_nil Setting.render_deploy_hook
@ -44,8 +49,10 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_equal "release", Setting.upgrades_target assert_equal "release", Setting.upgrades_target
assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook assert_equal NEW_RENDER_DEPLOY_HOOK, Setting.render_deploy_hook
end end
end
test " #send_test_email if smtp settings are populated try to send an email and redirect with notice" do 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) Setting.stubs(:smtp_settings_populated?).returns(true)
test_email_mock = mock test_email_mock = mock
@ -60,8 +67,10 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_response :found assert_response :found
assert controller.flash[:notice].present? assert controller.flash[:notice].present?
end end
end
test "#send_test_email with one blank smtp setting" do test "#send_test_email with one blank smtp setting" do
with_self_hosting do
Setting.stubs(:smtp_settings_populated?).returns(false) Setting.stubs(:smtp_settings_populated?).returns(false)
NotificationMailer.expects(:with).never NotificationMailer.expects(:with).never
@ -69,8 +78,10 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert controller.flash[:error].present? assert controller.flash[:error].present?
end end
end
test "#send_test_email when sending the email raise an error" do test "#send_test_email when sending the email raise an error" do
with_self_hosting do
Setting.stubs(:smtp_settings_populated?).returns(true) Setting.stubs(:smtp_settings_populated?).returns(true)
NotificationMailer.stubs(:with).raises(StandardError) NotificationMailer.stubs(:with).raises(StandardError)
@ -78,4 +89,5 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert controller.flash[:error].present? assert controller.flash[:error].present?
end end
end
end end

View file

@ -4,8 +4,6 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
setup do setup do
sign_in @user = users(:family_admin) sign_in @user = users(:family_admin)
ENV["UPGRADES_ENABLED"] = "true"
@completed_upgrade = Upgrader::Upgrade.new( @completed_upgrade = Upgrader::Upgrade.new(
"commit", "commit",
commit_sha: "47bb430954292d2fdcc81082af731a16b9587da3", commit_sha: "47bb430954292d2fdcc81082af731a16b9587da3",
@ -28,16 +26,17 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
end end
test "controller not available when upgrades are disabled" do test "controller not available when upgrades are disabled" do
ENV["UPGRADES_ENABLED"] = "false" MOCK_COMMIT = "47bb430954292d2fdcc81082af731a16b9587da3"
post "/upgrades/acknowledge/47bb430954292d2fdcc81082af731a16b9587da3" post acknowledge_upgrade_url(MOCK_COMMIT)
assert_response :not_found assert_response :not_found
post "/upgrades/deploy/47bb430954292d2fdcc81082af731a16b9587da3" post deploy_upgrade_url(MOCK_COMMIT)
assert_response :not_found assert_response :not_found
end end
test "should acknowledge an upgrade prompt" do test "should acknowledge an upgrade prompt" do
with_env_overrides UPGRADES_ENABLED: "true" do
Upgrader.stubs(:find_upgrade).returns(@available_upgrade) Upgrader.stubs(:find_upgrade).returns(@available_upgrade)
post acknowledge_upgrade_url(@available_upgrade.commit_sha) post acknowledge_upgrade_url(@available_upgrade.commit_sha)
@ -46,8 +45,10 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
assert_equal @user.last_prompted_upgrade_commit_sha, @available_upgrade.commit_sha assert_equal @user.last_prompted_upgrade_commit_sha, @available_upgrade.commit_sha
assert :redirect assert :redirect
end end
end
test "should acknowledge an upgrade alert" do test "should acknowledge an upgrade alert" do
with_env_overrides UPGRADES_ENABLED: "true" do
Upgrader.stubs(:find_upgrade).returns(@completed_upgrade) Upgrader.stubs(:find_upgrade).returns(@completed_upgrade)
post acknowledge_upgrade_url(@completed_upgrade.commit_sha) post acknowledge_upgrade_url(@completed_upgrade.commit_sha)
@ -56,8 +57,10 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
assert_equal @user.last_alerted_upgrade_commit_sha, @completed_upgrade.commit_sha assert_equal @user.last_alerted_upgrade_commit_sha, @completed_upgrade.commit_sha
assert :redirect assert :redirect
end end
end
test "should deploy an upgrade" do test "should deploy an upgrade" do
with_env_overrides UPGRADES_ENABLED: "true" do
Upgrader.stubs(:find_upgrade).returns(@available_upgrade) Upgrader.stubs(:find_upgrade).returns(@available_upgrade)
post deploy_upgrade_path(@available_upgrade.commit_sha) post deploy_upgrade_path(@available_upgrade.commit_sha)
@ -66,8 +69,10 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
assert_equal @user.last_prompted_upgrade_commit_sha, @available_upgrade.commit_sha assert_equal @user.last_prompted_upgrade_commit_sha, @available_upgrade.commit_sha
assert :redirect assert :redirect
end end
end
test "should rollback user state if upgrade fails" do test "should rollback user state if upgrade fails" do
with_env_overrides UPGRADES_ENABLED: "true" do
PRIOR_COMMIT = "47bb430954292d2fdcc81082af731a16b9587da2" PRIOR_COMMIT = "47bb430954292d2fdcc81082af731a16b9587da2"
@user.update!(last_prompted_upgrade_commit_sha: PRIOR_COMMIT) @user.update!(last_prompted_upgrade_commit_sha: PRIOR_COMMIT)
@ -80,4 +85,5 @@ class UpgradesControllerTest < ActionDispatch::IntegrationTest
assert_equal @user.last_prompted_upgrade_commit_sha, PRIOR_COMMIT assert_equal @user.last_prompted_upgrade_commit_sha, PRIOR_COMMIT
assert :redirect assert :redirect
end end
end
end end

View file

@ -10,8 +10,7 @@ class ApplicationMailerTest < ActionMailer::TestCase
end end
test "should use self host settings when self host enabled" do test "should use self host settings when self host enabled" do
ENV["SELF_HOSTING_ENABLED"] = "true" with_self_hosting do
smtp_host = "smtp.example.com" smtp_host = "smtp.example.com"
smtp_port = 466 smtp_port = 466
smtp_username = "user@example.com" smtp_username = "user@example.com"
@ -33,10 +32,9 @@ class ApplicationMailerTest < ActionMailer::TestCase
assert_emails 1 assert_emails 1
assert_equal smtp_settings_from_settings, ActionMailer::Base.deliveries.first.delivery_method.settings.slice(:address, :port, :user_name, :password) assert_equal smtp_settings_from_settings, ActionMailer::Base.deliveries.first.delivery_method.settings.slice(:address, :port, :user_name, :password)
end end
end
test "should use regular env settings when self host disabled" do test "should use regular env settings when self host disabled" do
ENV["SELF_HOSTING_ENABLED"] = "false"
TestMailer.test_email.deliver_now TestMailer.test_email.deliver_now
assert_emails 1 assert_emails 1

View file

@ -26,25 +26,6 @@ class SettingsTest < ApplicationSystemTestCase
assert_current_path settings_profile_path assert_current_path settings_profile_path
end end
test "all settings views and links are accessible" do
open_settings_from_sidebar
@settings_links.each_with_index do |(link_text, header_text, path), index|
next_setting_path = @settings_links[index + 1][2] if index < @settings_links.size - 1
prev_setting_path = @settings_links[index - 1][2] if index > 0
find_link(link_text, exact: true).click
assert_selector "h1", text: header_text
assert_current_path path
assert_link "Next", href: next_setting_path if next_setting_path.present?
assert_link "Back", href: prev_setting_path if prev_setting_path.present?
end
# Conditional nav items don't show by default
assert_no_text "Self-Hosting"
end
private private
def open_settings_from_sidebar def open_settings_from_sidebar
find("#user-menu").click find("#user-menu").click

View file

@ -1,8 +1,10 @@
# Require individual test files to enable these as needed # Test ENV setup:
# By default, all features should be disabled
# Use the `with_env_overrides` helper to enable features for individual tests
ENV["SELF_HOSTING_ENABLED"] = "false" ENV["SELF_HOSTING_ENABLED"] = "false"
ENV["UPGRADES_ENABLED"] = "false" ENV["UPGRADES_ENABLED"] = "false"
ENV["RAILS_ENV"] ||= "test" ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment" require_relative "../config/environment"
require "rails/test_help" require "rails/test_help"
require "minitest/mock" require "minitest/mock"
@ -29,6 +31,15 @@ module ActiveSupport
def sign_in(user) def sign_in(user)
post session_path, params: { email: user.email, password: "password" } post session_path, params: { email: user.email, password: "password" }
end end
def with_env_overrides(overrides = {}, &block)
ClimateControl.modify(**overrides, &block)
end
def with_self_hosting
Rails.configuration.stubs(:app_mode).returns("self_hosted".inquiry)
yield
end
end end
end end