From cf45acd7ab5536eff4b457c16f3c0856d53b125d Mon Sep 17 00:00:00 2001 From: Chris McClaskey Date: Wed, 17 Feb 2016 09:47:00 -0700 Subject: [PATCH 1/2] Fix this test not actually testing anything --- spec/shared_examples/user_activation_shared_examples.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/shared_examples/user_activation_shared_examples.rb b/spec/shared_examples/user_activation_shared_examples.rb index f16f3db6..59b92fa5 100644 --- a/spec/shared_examples/user_activation_shared_examples.rb +++ b/spec/shared_examples/user_activation_shared_examples.rb @@ -149,6 +149,7 @@ it "does not send the user an activation email" do old_size = ActionMailer::Base.deliveries.size + create_new_user expect(ActionMailer::Base.deliveries.size).to eq old_size end From 12f712a3ae27e684bb403bba4a2c00b6e929e543 Mon Sep 17 00:00:00 2001 From: Chris McClaskey Date: Wed, 17 Feb 2016 10:16:51 -0700 Subject: [PATCH 2/2] Allow one-time skipping of activation-related emails for new users. This is useful for special cases where you want to manage the emails yourself (but still want the default config to send the emails) Activation Needed Example: special_user = User.new special_user.skip_activation_needed_email = true special_user.save! # activation needed email does not get sent Activation Success Example: special_user = User.find(special_user_id) special_user.skip_activation_success_email = true special_user.activate! # activation success email does not get sent --- .../model/submodules/user_activation.rb | 6 ++- .../user_activation_shared_examples.rb | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/sorcery/model/submodules/user_activation.rb b/lib/sorcery/model/submodules/user_activation.rb index 64baa22c..5c5bd5fa 100644 --- a/lib/sorcery/model/submodules/user_activation.rb +++ b/lib/sorcery/model/submodules/user_activation.rb @@ -112,6 +112,8 @@ def activate! sorcery_adapter.save(:validate => false, :raise_on_failure => true) end + attr_accessor :skip_activation_needed_email, :skip_activation_success_email + protected # called automatically after user initial creation. @@ -127,14 +129,14 @@ def send_activation_success_email? !external? && ( !(sorcery_config.activation_success_email_method_name.nil? || sorcery_config.activation_mailer_disabled == true) - ) + ) && !skip_activation_success_email end def send_activation_needed_email? !external? && ( !(sorcery_config.activation_needed_email_method_name.nil? || sorcery_config.activation_mailer_disabled == true) - ) + ) && !skip_activation_needed_email end def prevent_non_active_login diff --git a/spec/shared_examples/user_activation_shared_examples.rb b/spec/shared_examples/user_activation_shared_examples.rb index 59b92fa5..6eb82ced 100644 --- a/spec/shared_examples/user_activation_shared_examples.rb +++ b/spec/shared_examples/user_activation_shared_examples.rb @@ -140,6 +140,48 @@ expect(ActionMailer::Base.deliveries.size).to eq old_size end + + context "activation_needed_email is skipped" do + before(:each) do + @user = build_new_user + @user.skip_activation_needed_email = true + end + + it "does not send the user an activation email" do + old_size = ActionMailer::Base.deliveries.size + + @user.sorcery_adapter.save(:raise_on_failure => true) + + expect(ActionMailer::Base.deliveries.size).to eq old_size + end + + it "does not call send_activation_needed_email! method of user" do + expect(@user).to receive(:send_activation_needed_email!).never + + @user.sorcery_adapter.save(:raise_on_failure => true) + end + + it "calls send_activation_success_email! method of user on activation" do + expect(@user).to receive(:send_activation_success_email!).never + + @user.activate! + end + end + + context "activation_success_email is skipped" do + before(:each) do + @user = build_new_user + @user.skip_activation_success_email = true + end + + it "does not send the user an activation success email on successful activation" do + old_size = ActionMailer::Base.deliveries.size + + @user.activate! + + expect(ActionMailer::Base.deliveries.size).to eq old_size + end + end end context "mailer has been disabled" do