]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Major overhaul of the test suite
authorAidan Cornelius-Bell <[email protected]>
Sat, 11 Jan 2025 06:09:34 +0000 (16:39 +1030)
committerAidan Cornelius-Bell <[email protected]>
Sat, 11 Jan 2025 06:09:34 +0000 (16:39 +1030)
12 files changed:
.env.test
app/mailers/application_mailer.rb
app/views/admin_mailer/new_non_financial_member.html.erb
config/application.rb
config/database.yml
config/environments/test.rb
test/controllers/pages_controller_test.rb
test/mailers/admin_mailer_test.rb
test/mailers/digest_mailer_test.rb
test/mailers/subscription_mailer_test.rb
test/mailers/welcome_mailer_test.rb
test/test_helper.rb

index 1146342f42a73bd15367c3b0cb477e6c8d171ef4..2a97b16e3680ea52f3e8208519243009f47f08be 100644 (file)
--- a/.env.test
+++ b/.env.test
@@ -10,4 +10,8 @@ STRIPE_SECRET_KEY=sk_test_WillFailOnTestDueToKey
 ADMIN_EMAIL="mind reader <[email protected]>"
 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"
index dada9b008bf4c839b5c10047d8a36830e7057c3f..09bf6da92d242f7fe80a698f5441dbb4f00071d6 100644 (file)
@@ -1,4 +1,11 @@
 class ApplicationMailer < ActionMailer::Base
-  default from: "mr <[email protected]>"
+  default from: "mind reader <[email protected]>"
   layout "mailer"
+
+  private
+
+  def format_date(date)
+    date&.strftime("%B %d, %Y")
+  end
+  helper_method :format_date
 end
index 26212f10977e7b30c0c76fc301eff300240952bc..946ebe7aafc75cb1b088bfb166752184b29478c3 100644 (file)
@@ -7,7 +7,7 @@
   <li><strong>Email:</strong> <%= @user.email %></li>
   <li><strong>Subscription Type:</strong> <%= @user.subscription_type %></li>
   <li><strong>Payment Amount:</strong> $<%= @user.payment_amount %></li>
-  <li><strong>Payment Date:</strong> <%= @user.last_payment_at.strftime("%B %d, %Y") %></li>
+  <li><strong>Payment Date:</strong> <%= format_date(@user.last_payment_at) %></li>
 </ul>
 
-<p>Please ensure that their account is properly set up and they have access to all paid member benefits.</p>
\ No newline at end of file
+<p>Please ensure that their account is properly set up and they have access to all paid member benefits.</p>
index 4f18747ca1ec4f4599869cf986420080f9664834..72952459383ab2c44ae476332971bd1a4d6d185d 100644 (file)
@@ -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
index ead71ef8b46d69514febff6df80a655b3d92eb53..08dbdd481a54375eb6195f62226633d82c69c22f 100644 (file)
@@ -35,7 +35,6 @@ development:
 test:
   <<: *default
   database: arelpe_test
-  host: 127.0.0.1
 
 production:
   <<: *default
index a1589e5e0e1762efe84c31a10c707bbc41292cdb..e0132d256ce1c221a19a6b2242b6b24dce47fdf3 100644 (file)
@@ -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: "[email protected]"
+  }
 
   # Print deprecation notices to the stderr.
   config.active_support.deprecation = :stderr
index c087aeeb5977ef7cb6e12b84bd842c19bbe623e7..1518a8c1d7652de34e012b945196879ad54548ab 100644 (file)
@@ -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
index e454cfe250a6dbbfcea719001d678f17d92a121a..55bbb31c464d9f7863fdabf36e2c5bfb49feeeaf 100644 (file)
@@ -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', "[email protected]")
     @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 ["[email protected]"], 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 ["[email protected]"], mail.from
     assert_match user.email, mail.body.encoded
+    assert_match user.created_at.strftime("%B %d, %Y"), mail.body.encoded
   end
 end
index 04bd9f661e65e757525f2ffed87c84fc7d377d37..ac6a64667a01e2adcb0f22c3032c42607487489f 100644 (file)
@@ -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 ["[email protected]"], 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
index 76f0e56bde2d82a181c5645d6b887e3db41ac282..9cabbb4b91e5ab749d62121d8b13b738c15c9b4e 100644 (file)
@@ -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 ["[email protected]"], mail.from
index 58d2beecab356cec0171a514213b242fd71686cf..d6daca88473488cd4c5c9b718aeced80bbad78af 100644 (file)
@@ -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 ["[email protected]"], 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 ["[email protected]"], mail.from
index b0bdedb27c72b91b306cd217617bb9bc07f4d340..2c0fd7e24325e4a24b19e3ef2d5f5d721701f87b 100644 (file)
@@ -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