A platform for community discussion. Free, open, simple.
FIX: validate Invite email against `EmailValidator.email_regex` (#6975)
Arpit Jalanab2c2ea60543
diff --git a/app/models/invite.rb b/app/models/invite.rbindex 2a7f96876aa..b2012405365 100644--- a/app/models/invite.rb+++ b/app/models/invite.rb@@ -16,7 +16,7 @@ class Invite < ActiveRecord::Basehas_many :topic_inviteshas_many :topics, through: :topic_invites, source: :topicvalidates_presence_of :invited_by_id- validates :email, email: true+ validates :email, email: true, format: { with: EmailValidator.email_regex }before_create doself.invite_key ||= SecureRandom.hexdiff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rbindex 6ed73ee5383..15ebea3f32d 100644--- a/spec/models/invite_spec.rb+++ b/spec/models/invite_spec.rb@@ -25,9 +25,15 @@ describe Invite docontext 'email validators' dolet(:coding_horror) { Fabricate(:coding_horror) }- let(:invite) { Invite.create(email: "[email protected]", invited_by: coding_horror) }++ it "should not allow an invite with unformatted email address" do+ expect {+ Fabricate(:invite, email: "John Doe <[email protected]>")+ }.to raise_error(ActiveRecord::RecordInvalid)+ endit "should not allow an invite with blacklisted email" do+ invite = Invite.create(email: "[email protected]", invited_by: coding_horror)expect(invite).not_to be_validenddiff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rbindex b2b7f4152ca..9da47143384 100644--- a/spec/requests/invites_controller_spec.rb+++ b/spec/requests/invites_controller_spec.rb@@ -214,8 +214,9 @@ describe InvitesController doendcontext 'with an invalid invite record' do- let(:invite) { Fabricate(:invite, email: "John Doe <[email protected]>") }+ let(:invite) { Fabricate(:invite) }it "responds with error message" do+ invite.update_attribute(:email, "John Doe <[email protected]>")put "/invites/show/#{invite.invite_key}.json"expect(response.status).to eq(200)json = JSON.parse(response.body)