From 625ff37398c1933fb7ae6766757b8db22b01e2bb Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 11 May 2020 16:07:39 -0500 Subject: [PATCH] [deploy] Only Throttle Confirmation Emails for Existing Users (#7780) --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 73372c7f9..cc332913d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -611,7 +611,7 @@ class User < ApplicationRecord end def can_send_confirmation_email - return if changes[:email].blank? + return if changes[:email].blank? || id.blank? rate_limiter.track_limit_by_action(:send_email_confirmation) rate_limiter.check_limit!(:send_email_confirmation) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9f6ab3512..b80be8fbf 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -201,11 +201,12 @@ RSpec.describe User, type: :model do expect(user.errors[:username].to_s).to include("taken") end - it "validates can_send_confirmation_email" do - user = build(:user) + it "validates can_send_confirmation_email for existing user" do + user = create(:user) limiter = RateLimitChecker.new(user) allow(user).to receive(:rate_limiter).and_return(limiter) allow(limiter).to receive(:limit_by_action).and_return(true) + user.update(email: "new_email@yo.com") expect(user).not_to be_valid expect(user.errors[:email].to_s).to include("confirmation could not be sent. Rate limit reached") end