From 2d95b11a34780f65ed2b062fdb830d8bedc7ce0e Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 19 Apr 2023 00:31:52 +0200 Subject: [PATCH] Test registration e-mail --- config/application.rb.dist | 2 +- config/environments/test.rb | 1 + test/system/users_test.rb | 13 ++++++++++++- test/test_helper.rb | 5 +++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/config/application.rb.dist b/config/application.rb.dist index 3137f6f..1494571 100644 --- a/config/application.rb.dist +++ b/config/application.rb.dist @@ -34,7 +34,7 @@ module FixinMe # SETUP: Below settings need to be updated on a per-installation basis. # # URL to use in sent e-mails. - config.action_mailer.default_url_options = {host: 'localhost'} + config.action_mailer.default_url_options = {host: 'localhost', :protocol => 'https'} # https://guides.rubyonrails.org/configuring.html#config-action-mailer-delivery-method config.action_mailer.delivery_method = :sendmail diff --git a/config/environments/test.rb b/config/environments/test.rb index e57c036..0702df7 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,6 +39,7 @@ Rails.application.configure do # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.action_mailer.default_url_options = {host: '127.0.0.1', :protocol => 'http'} # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr diff --git a/test/system/users_test.rb b/test/system/users_test.rb index ff4172a..477e1de 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -28,15 +28,26 @@ class UsersTest < ApplicationSystemTestCase test "register" do visit new_user_session_url click_link t(:register) + fill_in User.human_attribute_name(:email).capitalize, with: random_email password = random_password fill_in User.human_attribute_name(:password).capitalize, with: password fill_in t('users.registrations.new.password_confirmation'), with: password assert_difference ->{User.count}, 1 do - click_on t(:register) + assert_emails 1 do + click_on t(:register) + end end + assert_no_current_path new_user_registration_path assert_text t('devise.registrations.signed_up_but_unconfirmed') + + with_last_email do |mail| + visit Capybara.string(mail.body.to_s).find_link("Confirm my account")[:href] + end + assert_current_path new_user_session_path + assert_text t('devise.confirmations.confirmed') + assert User.last.confirmed? end #test "visiting the index" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 4dbffc4..dfeff23 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,7 @@ class ActiveSupport::TestCase fixtures :all include AbstractController::Translation + include ActionMailer::TestHelper def randomize_user_password!(user) random_password.tap { |p| user.update!(password: p) } @@ -22,4 +23,8 @@ class ActiveSupport::TestCase def random_email "%s@%s.%s" % (1..3).map { SecureRandom.alphanumeric(rand(1..20)) } end + + def with_last_email + yield(ActionMailer::Base.deliveries.last) + end end