From 68f7f77658b2a840a1d053a68e51081151d10ecb Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Wed, 1 Jan 2025 08:19:04 +1030 Subject: [PATCH] Updated test suite, in its entirety --- .../controllers/job_runner_controller_test.rb | 18 +++++++-- .../mailing_lists_controller_test.rb | 28 ++++++++++---- test/controllers/pages_controller_test.rb | 21 ++++++++-- test/controllers/posts_controller_test.rb | 29 ++++++++++++-- test/controllers/pubview_controller_test.rb | 28 +++++++++++--- .../subscriptions_controller_test.rb | 38 +++++++++++++++---- .../controllers/two_factor_controller_test.rb | 21 +++++++--- test/mailers/admin_mailer_test.rb | 22 ++++++----- test/mailers/digest_mailer_test.rb | 16 +++++--- test/mailers/previews/admin_mailer_preview.rb | 7 +--- .../mailers/previews/digest_mailer_preview.rb | 4 +- .../previews/subscription_mailer_preview.rb | 4 +- .../previews/welcome_mailer_preview.rb | 4 +- test/mailers/subscription_mailer_test.rb | 15 +++++--- test/mailers/welcome_mailer_test.rb | 23 ++++++++--- 15 files changed, 204 insertions(+), 74 deletions(-) diff --git a/test/controllers/job_runner_controller_test.rb b/test/controllers/job_runner_controller_test.rb index c39d3d7..b89d2f8 100644 --- a/test/controllers/job_runner_controller_test.rb +++ b/test/controllers/job_runner_controller_test.rb @@ -1,13 +1,23 @@ require "test_helper" class JobRunnerControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in users(:admin) + end + test "should get index" do - get job_runner_index_url + get job_runner_url assert_response :success end - test "should get run" do - get job_runner_run_url - assert_response :success + test "should run job" do + post run_job_runner_url, params: { job: 'weekly_bookmarks_digest' } + assert_redirected_to job_runner_url + end + + test "should handle invalid job" do + post run_job_runner_url, params: { job: 'invalid_job' } + assert_redirected_to job_runner_url + assert_not_empty flash[:alert] end end diff --git a/test/controllers/mailing_lists_controller_test.rb b/test/controllers/mailing_lists_controller_test.rb index 6b69c78..08cf107 100644 --- a/test/controllers/mailing_lists_controller_test.rb +++ b/test/controllers/mailing_lists_controller_test.rb @@ -1,18 +1,32 @@ require "test_helper" class MailingListsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in users(:regular_user) + end + test "should get index" do - get mailing_lists_index_url + get mailing_lists_url assert_response :success end - test "should get edit" do - get mailing_lists_edit_url - assert_response :success + test "should subscribe" do + post subscribe_mailing_lists_url + assert_redirected_to mailing_lists_url end - test "should get update" do - get mailing_lists_update_url - assert_response :success + test "should unsubscribe" do + delete unsubscribe_mailing_lists_url + assert_redirected_to mailing_lists_url + end + + test "should sync status" do + post sync_status_mailing_lists_url + assert_redirected_to mailing_lists_url + end + + test "should resync from buttondown" do + post resync_from_buttondown_mailing_lists_url + assert_redirected_to mailing_lists_url end end diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb index 2380d9b..c087aee 100644 --- a/test/controllers/pages_controller_test.rb +++ b/test/controllers/pages_controller_test.rb @@ -2,7 +2,8 @@ require "test_helper" class PagesControllerTest < ActionDispatch::IntegrationTest setup do - @page = pages(:one) + @page = pages(:about) + sign_in users(:admin) end test "should get index" do @@ -17,7 +18,14 @@ class PagesControllerTest < ActionDispatch::IntegrationTest test "should create page" do assert_difference("Page.count") do - post pages_url, params: { page: { content: @page.content, public: @page.public, slug: @page.slug, title: @page.title } } + post pages_url, params: { + page: { + content: @page.content, + slug: "#{@page.slug}-new", + title: "#{@page.title} New", + visibility: @page.visibility + } + } end assert_redirected_to page_url(Page.last) @@ -34,7 +42,14 @@ class PagesControllerTest < ActionDispatch::IntegrationTest end test "should update page" do - patch page_url(@page), params: { page: { content: @page.content, public: @page.public, slug: @page.slug, title: @page.title } } + patch page_url(@page), params: { + page: { + content: @page.content, + slug: @page.slug, + title: @page.title, + visibility: @page.visibility + } + } assert_redirected_to page_url(@page) end diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb index 5ccdfbd..2c58316 100644 --- a/test/controllers/posts_controller_test.rb +++ b/test/controllers/posts_controller_test.rb @@ -2,7 +2,8 @@ require "test_helper" class PostsControllerTest < ActionDispatch::IntegrationTest setup do - @post = posts(:one) + @post = posts(:tech_dispatch) + sign_in users(:admin) end test "should get index" do @@ -17,7 +18,18 @@ class PostsControllerTest < ActionDispatch::IntegrationTest test "should create post" do assert_difference("Post.count") do - post posts_url, params: { post: { content: @post.content, excerpt: @post.excerpt, post_type: @post.post_type, published_at: @post.published_at, slug: @post.slug, tags: @post.tags, title: @post.title, url: @post.url } } + post posts_url, params: { + post: { + content: @post.content, + excerpt: @post.excerpt, + post_type: @post.post_type, + published_at: @post.published_at, + slug: "#{@post.slug}-new", + tags: @post.tags, + title: "#{@post.title} New", + url: @post.url + } + } end assert_redirected_to post_url(Post.last) @@ -34,7 +46,18 @@ class PostsControllerTest < ActionDispatch::IntegrationTest end test "should update post" do - patch post_url(@post), params: { post: { content: @post.content, excerpt: @post.excerpt, post_type: @post.post_type, published_at: @post.published_at, slug: @post.slug, tags: @post.tags, title: @post.title, url: @post.url } } + patch post_url(@post), params: { + post: { + content: @post.content, + excerpt: @post.excerpt, + post_type: @post.post_type, + published_at: @post.published_at, + slug: @post.slug, + tags: @post.tags, + title: @post.title, + url: @post.url + } + } assert_redirected_to post_url(@post) end diff --git a/test/controllers/pubview_controller_test.rb b/test/controllers/pubview_controller_test.rb index 0acbddf..d407ef0 100644 --- a/test/controllers/pubview_controller_test.rb +++ b/test/controllers/pubview_controller_test.rb @@ -2,17 +2,35 @@ require "test_helper" class PubviewControllerTest < ActionDispatch::IntegrationTest test "should get index" do - get pubview_index_url + get root_url assert_response :success end - test "should get post" do - get pubview_post_url + test "should get dispatch post" do + post = posts(:tech_dispatch) + get public_post_url(year: post.published_at.year, slug: post.slug) assert_response :success end - test "should get rss" do - get pubview_rss_url + test "should get bookmark post" do + post = posts(:tech_bookmark) + get public_post_url(year: post.published_at.year, slug: post.slug) + assert_response :success + end + + test "should get public page" do + page = pages(:about) + get public_page_url(slug: page.slug) + assert_response :success + end + + test "should get rss feed" do + get rss_url(format: :rss) + assert_response :success + end + + test "should get dispatches rss feed" do + get dispatches_rss_url(format: :rss) assert_response :success end end diff --git a/test/controllers/subscriptions_controller_test.rb b/test/controllers/subscriptions_controller_test.rb index 3958a5a..d5117e7 100644 --- a/test/controllers/subscriptions_controller_test.rb +++ b/test/controllers/subscriptions_controller_test.rb @@ -1,18 +1,42 @@ require "test_helper" class SubscriptionsControllerTest < ActionDispatch::IntegrationTest - test "should get new" do - get subscriptions_new_url - assert_response :success + setup do + sign_in users(:regular_user) end - test "should get create" do - get subscriptions_create_url + test "should get index" do + get subscriptions_url assert_response :success end - test "should get view" do - get subscriptions_view_url + test "should get new" do + get new_subscription_url assert_response :success end + + test "should create subscription" do + post subscriptions_url, params: { + support_type: 'non_financial' + } + assert_redirected_to subscriptions_url + end + + test "should handle one time payment" do + post subscriptions_url, params: { + support_type: 'one_time', + payment_amount: '10.00', + stripeToken: 'dummy_token' + } + assert_redirected_to subscriptions_url + end + + test "should handle ongoing payment" do + post subscriptions_url, params: { + support_type: 'ongoing', + payment_amount: '10.00', + stripeToken: 'dummy_token' + } + assert_redirected_to subscriptions_url + end end diff --git a/test/controllers/two_factor_controller_test.rb b/test/controllers/two_factor_controller_test.rb index 4ef2cf7..12b17a4 100644 --- a/test/controllers/two_factor_controller_test.rb +++ b/test/controllers/two_factor_controller_test.rb @@ -1,18 +1,27 @@ require "test_helper" class TwoFactorControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in users(:regular_user) + end + test "should get new" do - get two_factor_new_url + get new_two_factor_url assert_response :success end - test "should get create" do - get two_factor_create_url - assert_response :success + test "should create" do + post two_factor_url, params: { otp_attempt: "123456" } + assert_redirected_to backup_codes_two_factor_url end - test "should get destroy" do - get two_factor_destroy_url + test "should show backup codes" do + get backup_codes_two_factor_url assert_response :success end + + test "should destroy" do + delete two_factor_url + assert_redirected_to root_url + end end diff --git a/test/mailers/admin_mailer_test.rb b/test/mailers/admin_mailer_test.rb index 43f15c3..1291807 100644 --- a/test/mailers/admin_mailer_test.rb +++ b/test/mailers/admin_mailer_test.rb @@ -2,18 +2,20 @@ require "test_helper" class AdminMailerTest < ActionMailer::TestCase test "new_paid_member" do - mail = AdminMailer.new_paid_member - assert_equal "New paid member", mail.subject - assert_equal [ "to@example.org" ], mail.to - assert_equal [ "from@example.com" ], mail.from - assert_match "Hi", mail.body.encoded + user = users(:paid_user) + mail = AdminMailer.new_paid_member(user) + assert_equal "mind reader :: New Paid Member", mail.subject + assert_equal [ENV["ADMIN_EMAIL"]], mail.to + assert_equal ["mr@mndrdr.org"], mail.from + assert_match user.email, mail.body.encoded end test "new_non_financial_member" do - mail = AdminMailer.new_non_financial_member - assert_equal "New non financial member", mail.subject - assert_equal [ "to@example.org" ], mail.to - assert_equal [ "from@example.com" ], mail.from - assert_match "Hi", mail.body.encoded + user = users(:regular_user) + 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 ["mr@mndrdr.org"], mail.from + assert_match user.email, mail.body.encoded end end diff --git a/test/mailers/digest_mailer_test.rb b/test/mailers/digest_mailer_test.rb index b87ee99..ad12f37 100644 --- a/test/mailers/digest_mailer_test.rb +++ b/test/mailers/digest_mailer_test.rb @@ -2,10 +2,16 @@ require "test_helper" class DigestMailerTest < ActionMailer::TestCase test "weekly_bookmarks_digest" do - mail = DigestMailer.weekly_bookmarks_digest - assert_equal "Weekly bookmarks digest", mail.subject - assert_equal [ "to@example.org" ], mail.to - assert_equal [ "from@example.com" ], mail.from - assert_match "Hi", mail.body.encoded + 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 "weekly digest", mail.body.encoded + bookmarks.each do |bookmark| + assert_match bookmark.title, mail.body.encoded + end end end diff --git a/test/mailers/previews/admin_mailer_preview.rb b/test/mailers/previews/admin_mailer_preview.rb index a04596f..48505d1 100644 --- a/test/mailers/previews/admin_mailer_preview.rb +++ b/test/mailers/previews/admin_mailer_preview.rb @@ -1,12 +1,9 @@ -# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer class AdminMailerPreview < ActionMailer::Preview - # Preview this email at http://localhost:3000/rails/mailers/admin_mailer/new_paid_member def new_paid_member - AdminMailer.new_paid_member + AdminMailer.new_paid_member(User.first) end - # Preview this email at http://localhost:3000/rails/mailers/admin_mailer/new_non_financial_member def new_non_financial_member - AdminMailer.new_non_financial_member + AdminMailer.new_non_financial_member(User.first) end end diff --git a/test/mailers/previews/digest_mailer_preview.rb b/test/mailers/previews/digest_mailer_preview.rb index 7c29e21..8d0dc98 100644 --- a/test/mailers/previews/digest_mailer_preview.rb +++ b/test/mailers/previews/digest_mailer_preview.rb @@ -1,7 +1,5 @@ -# Preview all emails at http://localhost:3000/rails/mailers/digest_mailer class DigestMailerPreview < ActionMailer::Preview - # Preview this email at http://localhost:3000/rails/mailers/digest_mailer/weekly_bookmarks_digest def weekly_bookmarks_digest - DigestMailer.weekly_bookmarks_digest + DigestMailer.weekly_bookmarks_digest(User.first, Post.bookmarks.limit(5)) end end diff --git a/test/mailers/previews/subscription_mailer_preview.rb b/test/mailers/previews/subscription_mailer_preview.rb index 4bcb8aa..72d91b9 100644 --- a/test/mailers/previews/subscription_mailer_preview.rb +++ b/test/mailers/previews/subscription_mailer_preview.rb @@ -1,7 +1,5 @@ -# Preview all emails at http://localhost:3000/rails/mailers/subscription_mailer class SubscriptionMailerPreview < ActionMailer::Preview - # Preview this email at http://localhost:3000/rails/mailers/subscription_mailer/confirmation_email def confirmation_email - SubscriptionMailer.confirmation_email + SubscriptionMailer.confirmation_email(User.first, 10.00, "ongoing") end end diff --git a/test/mailers/previews/welcome_mailer_preview.rb b/test/mailers/previews/welcome_mailer_preview.rb index 1b5a5a6..aa0f89e 100644 --- a/test/mailers/previews/welcome_mailer_preview.rb +++ b/test/mailers/previews/welcome_mailer_preview.rb @@ -1,7 +1,5 @@ -# Preview all emails at http://localhost:3000/rails/mailers/welcome_mailer class WelcomeMailerPreview < ActionMailer::Preview - # Preview this email at http://localhost:3000/rails/mailers/welcome_mailer/welcome_email def welcome_email - WelcomeMailer.welcome_email + WelcomeMailer.welcome_email(User.first) end end diff --git a/test/mailers/subscription_mailer_test.rb b/test/mailers/subscription_mailer_test.rb index 83c53cf..76f0e56 100644 --- a/test/mailers/subscription_mailer_test.rb +++ b/test/mailers/subscription_mailer_test.rb @@ -2,10 +2,15 @@ require "test_helper" class SubscriptionMailerTest < ActionMailer::TestCase test "confirmation_email" do - mail = SubscriptionMailer.confirmation_email - assert_equal "Confirmation email", mail.subject - assert_equal [ "to@example.org" ], mail.to - assert_equal [ "from@example.com" ], mail.from - assert_match "Hi", mail.body.encoded + 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 + assert_match support_type, mail.body.encoded + assert_match amount.to_s, mail.body.encoded if amount end end diff --git a/test/mailers/welcome_mailer_test.rb b/test/mailers/welcome_mailer_test.rb index 0c5ce68..58d2bee 100644 --- a/test/mailers/welcome_mailer_test.rb +++ b/test/mailers/welcome_mailer_test.rb @@ -2,10 +2,23 @@ require "test_helper" class WelcomeMailerTest < ActionMailer::TestCase test "welcome_email" do - mail = WelcomeMailer.welcome_email - assert_equal "Welcome email", mail.subject - assert_equal [ "to@example.org" ], mail.to - assert_equal [ "from@example.com" ], mail.from - assert_match "Hi", mail.body.encoded + 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 + assert_match "Welcome", mail.body.encoded + assert_match user.first_name, mail.body.encoded + end + + test "welcome_email_for_paid_user" 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 + assert_match "Welcome", mail.body.encoded + assert_match user.first_name, mail.body.encoded + assert_match "Premium", mail.body.encoded end end -- 2.39.5