FIX: Don’t run validations when invalidating invites

This patch is a followup of
https://github.com/discourse/discourse/pull/21504 where limits on custom
message for an invite were introduced.

This had a side effect of making some existing invites invalid and with
the current code, they can’t be invalidated anymore.

This patch takes the approach of skipping the validations when invites
are invalidated since the important thing here is to mark the invite as
invalidated regardless of its actual state in the DB. (no other
attributes are updated at the same time anyway)
This commit is contained in:
Loïc Guitaut 2023-05-24 11:08:56 +02:00 committed by Loïc Guitaut
parent 4de1d3952b
commit 37b71c5903
2 changed files with 31 additions and 4 deletions

View File

@ -274,10 +274,12 @@ class Invite < ActiveRecord::Base
end end
def self.invalidate_for_email(email) def self.invalidate_for_email(email)
invite = Invite.find_by(email: Email.downcase(email)) Invite.find_by(email: Email.downcase(email))&.invalidate!
invite.update!(invalidated_at: Time.zone.now) if invite end
invite def invalidate!
update_attribute(:invalidated_at, Time.current)
self
end end
def resend_invite def resend_invite

View File

@ -478,7 +478,7 @@ RSpec.describe Invite do
end end
end end
describe "#invalidate_for_email" do describe ".invalidate_for_email" do
it "returns nil if there is no invite for the given email" do it "returns nil if there is no invite for the given email" do
invite = Invite.invalidate_for_email("test@example.com") invite = Invite.invalidate_for_email("test@example.com")
expect(invite).to eq(nil) expect(invite).to eq(nil)
@ -600,4 +600,29 @@ RSpec.describe Invite do
end end
end end
end end
describe "#invalidate!" do
subject(:invalidate) { invite.invalidate! }
fab!(:invite) { Fabricate(:invite) }
before { freeze_time }
it "invalidates the invite" do
expect { invalidate }.to change { invite.invalidated_at }.to Time.current
end
it "returns the invite" do
expect(invalidate).to eq invite
end
context "when the invite is in an invalid state" do
before { invite.update_attribute(:custom_message, "a" * 2000) }
it "still invalidates the invite" do
expect(invite).to be_invalid
expect { invalidate }.to change { invite.invalidated_at }.to Time.current
end
end
end
end end