From 2fac5ed1452a7d62aab94db19d01b13280cd9998 Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Sat, 11 Jan 2025 16:39:34 +1030 Subject: [PATCH] Major overhaul of the test suite --- .env.test | 6 ++- app/mailers/application_mailer.rb | 9 ++++- .../new_non_financial_member.html.erb | 4 +- config/application.rb | 1 + config/database.yml | 1 - config/environments/test.rb | 6 ++- test/controllers/pages_controller_test.rb | 28 ++++++++----- test/mailers/admin_mailer_test.rb | 18 ++++++--- test/mailers/digest_mailer_test.rb | 7 +++- test/mailers/subscription_mailer_test.rb | 4 +- test/mailers/welcome_mailer_test.rb | 7 +++- test/test_helper.rb | 39 +++++++++++++++++-- 12 files changed, 102 insertions(+), 28 deletions(-) diff --git a/.env.test b/.env.test index 1146342..2a97b16 100644 --- a/.env.test +++ b/.env.test @@ -10,4 +10,8 @@ STRIPE_SECRET_KEY=sk_test_WillFailOnTestDueToKey ADMIN_EMAIL="mind reader " OTP_SECRET_KEY=12345678901234567890123456789012345678901e61dbd01685e96b65227a5d6c43862c477ce947bff0185ee126cd93665df878d2518d0f153b7a57e95d6a5b RAILS_MASTER_KEY=7cf6b8faada5332a399b0beecb626565 - +MYSQL_SOCKET=/var/lib/mysql/mysql.sock +MYSQL_PASSWORD="" # Add your test database password if any +MYSQL_USERNAME="arelpe_test" # Use a different username for test +MYSQL_HOST="localhost" +RAILS_ENV="test" diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index dada9b0..09bf6da 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,11 @@ class ApplicationMailer < ActionMailer::Base - default from: "mr " + default from: "mind reader " layout "mailer" + + private + + def format_date(date) + date&.strftime("%B %d, %Y") + end + helper_method :format_date end diff --git a/app/views/admin_mailer/new_non_financial_member.html.erb b/app/views/admin_mailer/new_non_financial_member.html.erb index 26212f1..946ebe7 100644 --- a/app/views/admin_mailer/new_non_financial_member.html.erb +++ b/app/views/admin_mailer/new_non_financial_member.html.erb @@ -7,7 +7,7 @@
  • Email: <%= @user.email %>
  • Subscription Type: <%= @user.subscription_type %>
  • Payment Amount: $<%= @user.payment_amount %>
  • -
  • Payment Date: <%= @user.last_payment_at.strftime("%B %d, %Y") %>
  • +
  • Payment Date: <%= format_date(@user.last_payment_at) %>
  • -

    Please ensure that their account is properly set up and they have access to all paid member benefits.

    \ No newline at end of file +

    Please ensure that their account is properly set up and they have access to all paid member benefits.

    diff --git a/config/application.rb b/config/application.rb index 4f18747..7295245 100644 --- a/config/application.rb +++ b/config/application.rb @@ -36,5 +36,6 @@ module Arelpe # config.time_zone = "Central Time (US & Canada)" config.time_zone = "Australia/Adelaide" # config.eager_load_paths << Rails.root.join("extras") + config.active_support.to_time_preserves_timezone = :zone end end diff --git a/config/database.yml b/config/database.yml index ead71ef..08dbdd4 100644 --- a/config/database.yml +++ b/config/database.yml @@ -35,7 +35,6 @@ development: test: <<: *default database: arelpe_test - host: 127.0.0.1 production: <<: *default diff --git a/config/environments/test.rb b/config/environments/test.rb index a1589e5..e0132d2 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -45,7 +45,11 @@ Rails.application.configure do # Unlike controllers, the mailer instance doesn't have any context about the # incoming request so you'll need to provide the :host parameter yourself. - config.action_mailer.default_url_options = { host: "www.example.com" } + config.action_mailer.default_url_options = { host: "mndrdr.org" } + + config.action_mailer.default_options = { + from: "mr@mndrdr.org" + } # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb index c087aee..1518a8c 100644 --- a/test/controllers/pages_controller_test.rb +++ b/test/controllers/pages_controller_test.rb @@ -3,7 +3,7 @@ require "test_helper" class PagesControllerTest < ActionDispatch::IntegrationTest setup do @page = pages(:about) - sign_in users(:admin) + sign_in_admin # Use our new helper method end test "should get index" do @@ -20,15 +20,15 @@ class PagesControllerTest < ActionDispatch::IntegrationTest assert_difference("Page.count") do post pages_url, params: { page: { - content: @page.content, - slug: "#{@page.slug}-new", - title: "#{@page.title} New", - visibility: @page.visibility + title: "New Test Page", + content: "Test content", + visibility: :visible } } end assert_redirected_to page_url(Page.last) + assert_equal "Page was successfully created.", flash[:notice] end test "should show page" do @@ -44,13 +44,13 @@ class PagesControllerTest < ActionDispatch::IntegrationTest test "should update page" do patch page_url(@page), params: { page: { - content: @page.content, - slug: @page.slug, - title: @page.title, - visibility: @page.visibility + title: "Updated Title", + content: "Updated content", + visibility: :visible } } assert_redirected_to page_url(@page) + assert_equal "Page was successfully updated.", flash[:notice] end test "should destroy page" do @@ -59,5 +59,15 @@ class PagesControllerTest < ActionDispatch::IntegrationTest end assert_redirected_to pages_url + assert_equal "Page was successfully destroyed.", flash[:notice] + end + + test "non-admin cannot access pages" do + sign_out :user + sign_in_regular_user + + get pages_url + assert_redirected_to root_path + assert_equal "You are not authorised to access this page. If you have an account please log in first.", flash[:alert] end end diff --git a/test/mailers/admin_mailer_test.rb b/test/mailers/admin_mailer_test.rb index e454cfe..55bbb31 100644 --- a/test/mailers/admin_mailer_test.rb +++ b/test/mailers/admin_mailer_test.rb @@ -1,25 +1,33 @@ +# test/mailers/admin_mailer_test.rb require "test_helper" class AdminMailerTest < ActionMailer::TestCase def setup + @admin_email = ENV.fetch('ADMIN_EMAIL', "admin@example.com") @user = users(:paid_user) - @user.update(last_payment_at: Time.current) # Ensure we have a payment date + @user.update(last_payment_at: Time.current) end - test "new_paid_member" do + test "should send new paid member notification" do mail = AdminMailer.new_paid_member(@user) + assert_equal "mind reader :: New Paid Member", mail.subject - assert_equal [ENV["ADMIN_EMAIL"]], mail.to + assert_equal [@admin_email], mail.to assert_equal ["mr@mndrdr.org"], mail.from assert_match @user.email, mail.body.encoded + assert_match @user.last_payment_at.strftime("%B %d, %Y"), mail.body.encoded end - test "new_non_financial_member" do + test "should send new non_financial member notification" do user = users(:regular_user) + user.update!(created_at: Time.current) # Ensure we have a timestamp + mail = AdminMailer.new_non_financial_member(user) + assert_equal "mind reader :: New Non-Financial Member", mail.subject - assert_equal [ENV["ADMIN_EMAIL"]], mail.to + assert_equal [@admin_email], mail.to assert_equal ["mr@mndrdr.org"], mail.from assert_match user.email, mail.body.encoded + assert_match user.created_at.strftime("%B %d, %Y"), mail.body.encoded end end diff --git a/test/mailers/digest_mailer_test.rb b/test/mailers/digest_mailer_test.rb index 04bd9f6..ac6a646 100644 --- a/test/mailers/digest_mailer_test.rb +++ b/test/mailers/digest_mailer_test.rb @@ -1,15 +1,18 @@ +# test/mailers/digest_mailer_test.rb require "test_helper" class DigestMailerTest < ActionMailer::TestCase - test "weekly_bookmarks_digest" do + test "should send weekly bookmarks digest" do user = users(:paid_user) bookmarks = Post.bookmarks.limit(5) mail = DigestMailer.weekly_bookmarks_digest(user, bookmarks) + assert_equal "mind reader :: weekly digest", mail.subject assert_equal [user.email], mail.to assert_equal ["mr@mndrdr.org"], mail.from - assert_match user.first_name, mail.body.encoded # Changed from "weekly digest" + assert_match user.first_name, mail.body.encoded + bookmarks.each do |bookmark| assert_match bookmark.title, mail.body.encoded end diff --git a/test/mailers/subscription_mailer_test.rb b/test/mailers/subscription_mailer_test.rb index 76f0e56..9cabbb4 100644 --- a/test/mailers/subscription_mailer_test.rb +++ b/test/mailers/subscription_mailer_test.rb @@ -1,12 +1,14 @@ +# test/mailers/subscription_mailer_test.rb require "test_helper" class SubscriptionMailerTest < ActionMailer::TestCase - test "confirmation_email" do + test "should send subscription confirmation email" do user = users(:paid_user) amount = 10.00 support_type = "ongoing" mail = SubscriptionMailer.confirmation_email(user, amount, support_type) + assert_equal "Thank you for supporting mind reader!", mail.subject assert_equal [user.email], mail.to assert_equal ["mr@mndrdr.org"], mail.from diff --git a/test/mailers/welcome_mailer_test.rb b/test/mailers/welcome_mailer_test.rb index 58d2bee..d6daca8 100644 --- a/test/mailers/welcome_mailer_test.rb +++ b/test/mailers/welcome_mailer_test.rb @@ -1,9 +1,11 @@ +# test/mailers/welcome_mailer_test.rb require "test_helper" class WelcomeMailerTest < ActionMailer::TestCase - test "welcome_email" do + test "should send basic welcome email" do user = users(:regular_user) mail = WelcomeMailer.welcome_email(user) + assert_equal "Welcome to mind reader", mail.subject assert_equal [user.email], mail.to assert_equal ["mr@mndrdr.org"], mail.from @@ -11,9 +13,10 @@ class WelcomeMailerTest < ActionMailer::TestCase assert_match user.first_name, mail.body.encoded end - test "welcome_email_for_paid_user" do + test "should send paid user welcome email" do user = users(:paid_user) mail = WelcomeMailer.welcome_email(user) + assert_equal "Welcome to mind reader", mail.subject assert_equal [user.email], mail.to assert_equal ["mr@mndrdr.org"], mail.from diff --git a/test/test_helper.rb b/test/test_helper.rb index b0bdedb..2c0fd7e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,12 +1,45 @@ ENV['RAILS_ENV'] ||= 'test' require_relative "../config/environment" require "rails/test_help" -require 'devise' +require "devise" + +module ActionDispatch + class IntegrationTest + include Devise::Test::IntegrationHelpers + include Warden::Test::Helpers + + def setup + Warden.test_mode! + end + + def teardown + Warden.test_reset! + end + end +end class ActiveSupport::TestCase - include Devise::Test::IntegrationHelpers # For controller tests - include Devise::Test::ControllerHelpers # For functional tests + include Devise::Test::IntegrationHelpers + # Run tests in parallel with specified workers parallelize(workers: :number_of_processors) + + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order fixtures :all + + # Add more helper methods to be used by all tests here... + def sign_in_admin + @admin = users(:admin) + sign_in @admin + end + + def sign_in_regular_user + @user = users(:regular_user) + sign_in @user + end + + def sign_in_paid_user + @paid_user = users(:paid_user) + sign_in @paid_user + end end -- 2.39.5