diff --git a/Gemfile b/Gemfile index c72bd8326..9296044c0 100644 --- a/Gemfile +++ b/Gemfile @@ -71,7 +71,7 @@ group :test do end require_relative "lib/bootstrap" -unless Bootstrap.local_authentication? +unless Bootstrap.oss_config? eval_gemfile "gems/fizzy-saas/Gemfile" gem "fizzy-saas", path: "gems/fizzy-saas" end diff --git a/bin/dev b/bin/dev index 9fe70d369..cc4e4ce62 100755 --- a/bin/dev +++ b/bin/dev @@ -14,8 +14,8 @@ if [ -f tmp/solid-queue.txt ]; then export SOLID_QUEUE_IN_PUMA=1 fi -if [ -f tmp/local-auth.txt ]; then - export LOCAL_AUTHENTICATION=1 +if [ -f tmp/oss-config.txt ]; then + export OSS_CONFIG=1 fi exec ./bin/rails server -p 3006 diff --git a/bin/rails b/bin/rails index 61fa81667..ab22dd770 100755 --- a/bin/rails +++ b/bin/rails @@ -1,6 +1,6 @@ #!/usr/bin/env ruby require_relative "../lib/bootstrap" -if !Bootstrap.local_authentication? +if !Bootstrap.oss_config? # default from rails/test_unit/runner.rb but adding the saas gem test files ENV["DEFAULT_TEST"] = "{gems/fizzy-saas/,}test/**/*_test.rb" ENV["DEFAULT_TEST_EXCLUDE"] = "{gems/fizzy-saas/,}test/{system,dummy,fixtures}/**/*_test.rb" diff --git a/bin/setup b/bin/setup index 10f30e733..329858c5b 100755 --- a/bin/setup +++ b/bin/setup @@ -11,7 +11,7 @@ system("bundle config set --local auto_install true") system("bundle check") || system!("bundle install") require_relative "../lib/bootstrap" -load File.expand_path("../gems/fizzy-saas/bin/setup", __dir__) unless Bootstrap.local_authentication? +load File.expand_path("../gems/fizzy-saas/bin/setup", __dir__) unless Bootstrap.oss_config? puts "\n== Preparing database ==" if ARGV.include?("--reset") diff --git a/config/ci.rb b/config/ci.rb index 121deb06a..304d4cd49 100644 --- a/config/ci.rb +++ b/config/ci.rb @@ -9,10 +9,10 @@ CI.run do step "Security: Importmap audit", "bin/importmap audit" step "Security: Brakeman audit", "bin/brakeman --quiet --no-pager --exit-on-warn --exit-on-error" - step "Tests: Rails with 37id auth", "bin/rails test" - step "Tests: Rails with local auth", "LOCAL_AUTHENTICATION=1 bin/rails test" - step "Tests: 37id", "bin/rails 37id:test:units" - step "Tests: System", "bin/rails test:system" + step "Tests: Rails: SaaS config", "bin/rails test" + step "Tests: Rails: OSS config", "OSS_CONFIG=1 bin/rails test" + step "Tests: 37id", "bin/rails 37id:test:units" + step "Tests: System", "bin/rails test:system" if success? step "Signoff: All systems go. Ready for merge and deploy.", "gh signoff" diff --git a/config/initializers/authentication.rb b/config/initializers/authentication.rb index 1ed66cd86..5556a9661 100644 --- a/config/initializers/authentication.rb +++ b/config/initializers/authentication.rb @@ -1,2 +1,2 @@ require "bootstrap" -Rails.application.config.x.local_authentication = Bootstrap.local_authentication? +Rails.application.config.x.oss_config = Bootstrap.oss_config? diff --git a/config/routes.rb b/config/routes.rb index 2c6c8e761..5e187e3e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -221,7 +221,7 @@ Rails.application.routes.draw do root "events#index" - unless Rails.application.config.x.local_authentication + unless Rails.application.config.x.oss_config mount Fizzy::Saas::Engine, at: "/", as: "saas" end diff --git a/db/seeds.rb b/db/seeds.rb index 98954991d..32532d258 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,7 +13,7 @@ end def create_tenant(signal_account_name, bare: false) if bare tenant_id = Digest::SHA256.hexdigest(signal_account_name)[0..8].to_i(16) - elsif Rails.application.config.x.local_authentication + elsif Rails.application.config.x.oss_config tenant_id = ActiveRecord::FixtureSet.identify signal_account_name else signal_account = SignalId::Account.find_by_product_and_name!("fizzy", signal_account_name) @@ -22,7 +22,7 @@ def create_tenant(signal_account_name, bare: false) ApplicationRecord.destroy_tenant tenant_id ApplicationRecord.create_tenant(tenant_id) do - account = if bare || Rails.application.config.x.local_authentication + account = if bare || Rails.application.config.x.oss_config Account.create(name: signal_account_name, tenant_id: tenant_id).tap do User.create!( name: "David Heinemeier Hansson", diff --git a/lib/bootstrap.rb b/lib/bootstrap.rb index 1cfe4042c..6d52f3f49 100644 --- a/lib/bootstrap.rb +++ b/lib/bootstrap.rb @@ -1,5 +1,5 @@ module Bootstrap - def self.local_authentication? - ENV.fetch("LOCAL_AUTHENTICATION", "") != "" || !File.directory?(File.expand_path("../gems/fizzy-saas", __dir__)) + def self.oss_config? + ENV.fetch("OSS_CONFIG", "") != "" || !File.directory?(File.expand_path("../gems/fizzy-saas", __dir__)) end end diff --git a/script/create-local-user.rb b/script/create-local-user.rb deleted file mode 100755 index 7b303774f..000000000 --- a/script/create-local-user.rb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env ruby -# -# This script creates a new user that can be logged into via LOCAL_AUTHENTICATION, avoiding Launchpad/37id integration. -# In order to login as this user, you must set the `LOCAL_AUTHENTICATION` environment variable when running the Rails server: -# -# LOCAL_AUTHENTICATION=1 bin/dev -# - -require_relative "../config/environment" - -unless Rails.env.development? - puts "ERROR: This script is intended to be run in development mode only." - exit 1 -end - -if ARGV.length < 2 - puts "Usage: #{$0} " - exit 1 -end - -email_address = ARGV[0] -tenant = ARGV[1] - -ApplicationRecord.with_tenant(tenant) do - user = User.create!( - name: email_address.split("@").first, - email_address: email_address, - password: "secret123456" - ) - - puts "Created: " - pp user -end diff --git a/test/models/period_highlights_test.rb b/test/models/period_highlights_test.rb index cf7627201..5bcfca99a 100644 --- a/test/models/period_highlights_test.rb +++ b/test/models/period_highlights_test.rb @@ -1,30 +1,27 @@ require "test_helper" class PeriodHighlightTest < ActiveSupport::TestCase - # Skipping when locally authenticating because the VCR cassettes would need to be re-recorded. - unless Rails.application.config.x.local_authentication - include VcrTestHelper + include VcrTestHelper - setup do - @user = users(:david) + setup do + @user = users(:david) + end + + test "generate period highlights" do + period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do + PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) end - test "generate period highlights" do - period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do - PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) - end + assert_match /logo/i, period_highlights.to_html + end - assert_match /logo/i, period_highlights.to_html + test "don't generate highlights for existing periods" do + new_period_highlights = PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) + + existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do + PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) end - test "don't generate highlights for existing periods" do - new_period_highlights = PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) - - existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do - PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) - end - - assert_equal new_period_highlights, existing_period_highlights - end + assert_equal new_period_highlights, existing_period_highlights end end diff --git a/test/models/user/highlights_test.rb b/test/models/user/highlights_test.rb index 0344d02b8..7123f3bc2 100644 --- a/test/models/user/highlights_test.rb +++ b/test/models/user/highlights_test.rb @@ -1,67 +1,64 @@ require "test_helper" class User::HighlightsTest < ActiveSupport::TestCase - # Skipping when locally authenticating because the VCR cassettes would need to be re-recorded. - unless Rails.application.config.x.local_authentication - include VcrTestHelper + include VcrTestHelper - setup do - @user = users(:david) - travel_to 1.week.ago + 2.days - Current.session = sessions(:david) - end + setup do + @user = users(:david) + travel_to 1.week.ago + 2.days + Current.session = sessions(:david) + end - test "generate weekly highlights" do - stub_const(PeriodHighlights::Period, :MIN_EVENTS_TO_BE_INTERESTING, 3) do - period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do - @user.generate_weekly_highlights - end - - assert_match /logo/i, period_highlights.to_html - end - end - - test "don't generate highlights for existing periods" do - stub_const(PeriodHighlights::Period, :MIN_EVENTS_TO_BE_INTERESTING, 3) do - new_period_highlights = @user.generate_weekly_highlights - assert_not_nil new_period_highlights - - existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do - @user.generate_weekly_highlights - end - - assert_equal new_period_highlights, existing_period_highlights - end - end - - test "periods respect user timezone for week boundaries" do - @user.settings.update!(timezone_name: "America/New_York") - - # Sunday Jan 7, 2024 at 2am EST (7am UTC) - this is Sunday in NYC - sunday_in_nyc = Time.zone.parse("2024-01-07 07:00:00 UTC") - - # Saturday Jan 6, 2024 at 11pm EST (Jan 7 4am UTC) - still Saturday in NYC but Sunday in UTC - saturday_in_nyc = Time.zone.parse("2024-01-07 04:00:00 UTC") - - # Event on Saturday evening in NYC (but Sunday in UTC) - saturday_event = travel_to(saturday_in_nyc) { cards(:logo).track_event("Saturday event") } - - # Events throughout the week starting Sunday in NYC - 7.times do |i| - travel_to(sunday_in_nyc + i.days) { cards(:logo).track_event("Event #{i}") } + test "generate weekly highlights" do + stub_const(PeriodHighlights::Period, :MIN_EVENTS_TO_BE_INTERESTING, 3) do + period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do + @user.generate_weekly_highlights end - wednesday = sunday_in_nyc + 3.days - - # The period should start at Sunday in NYC timezone - period_start = wednesday.in_time_zone("America/New_York").beginning_of_week(:sunday) - period = PeriodHighlights::Period.new(@user.collections, starts_at: period_start, duration: 1.week) - - # The Saturday event should NOT be included (it's in the previous week in NYC time) - assert_not_includes period.events, saturday_event - - # Should include 7 events from the current week - assert_equal 7, period.events.count + assert_match /logo/i, period_highlights.to_html end end + + test "don't generate highlights for existing periods" do + stub_const(PeriodHighlights::Period, :MIN_EVENTS_TO_BE_INTERESTING, 3) do + new_period_highlights = @user.generate_weekly_highlights + assert_not_nil new_period_highlights + + existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do + @user.generate_weekly_highlights + end + + assert_equal new_period_highlights, existing_period_highlights + end + end + + test "periods respect user timezone for week boundaries" do + @user.settings.update!(timezone_name: "America/New_York") + + # Sunday Jan 7, 2024 at 2am EST (7am UTC) - this is Sunday in NYC + sunday_in_nyc = Time.zone.parse("2024-01-07 07:00:00 UTC") + + # Saturday Jan 6, 2024 at 11pm EST (Jan 7 4am UTC) - still Saturday in NYC but Sunday in UTC + saturday_in_nyc = Time.zone.parse("2024-01-07 04:00:00 UTC") + + # Event on Saturday evening in NYC (but Sunday in UTC) + saturday_event = travel_to(saturday_in_nyc) { cards(:logo).track_event("Saturday event") } + + # Events throughout the week starting Sunday in NYC + 7.times do |i| + travel_to(sunday_in_nyc + i.days) { cards(:logo).track_event("Event #{i}") } + end + + wednesday = sunday_in_nyc + 3.days + + # The period should start at Sunday in NYC timezone + period_start = wednesday.in_time_zone("America/New_York").beginning_of_week(:sunday) + period = PeriodHighlights::Period.new(@user.collections, starts_at: period_start, duration: 1.week) + + # The Saturday event should NOT be included (it's in the previous week in NYC time) + assert_not_includes period.events, saturday_event + + # Should include 7 events from the current week + assert_equal 7, period.events.count + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index a71063f10..fbb2b1149 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -65,6 +65,6 @@ RubyLLM.configure do |config| config.openai_api_key ||= "DUMMY-TEST-KEY" # Run tests with VCR without having to configure OpenAI API key locally. end -unless Rails.application.config.x.local_authentication +unless Rails.application.config.x.oss_config load File.expand_path("../gems/fizzy-saas/test/test_helper.rb", __dir__) end