From 4862a93804e9702f9286a55178f1f6aa28aa81d5 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 25 Mar 2015 22:25:18 +0530 Subject: [PATCH] FIX: delete duplicate invites --- app/models/invite_redeemer.rb | 5 +++++ spec/models/invite_spec.rb | 23 ++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index 1b93d5849f9..76361996bc5 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -49,6 +49,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do approve_account_if_needed notify_invitee send_password_instructions + delete_duplicate_invites end def invite_was_redeemed? @@ -114,4 +115,8 @@ InviteRedeemer = Struct.new(:invite, :username, :name) do invite.invited_by.notifications.create(notification_type: Notification.types[:invitee_accepted], data: {display_username: invited_user.username}.to_json) end + + def delete_duplicate_invites + Invite.where('invites.email = ? and invites.id != ?', invite.email, invite.id).delete_all + end end diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb index 9430203306e..ef57e635c87 100644 --- a/spec/models/invite_spec.rb +++ b/spec/models/invite_spec.rb @@ -188,6 +188,18 @@ describe Invite do expect(invite.redeem).to be_blank end + context "deletes duplicate invites" do + let(:another_user) { Fabricate(:user) } + + it 'delete duplicate invite' do + another_invite = Fabricate(:invite, email: invite.email, invited_by: another_user) + invite.redeem + duplicate_invite = Invite.find_by(id: another_invite.id) + expect(duplicate_invite).to be_nil + end + + end + context 'enqueues a job to email "set password" instructions' do it 'does not enqueue an email if sso is enabled' do @@ -329,17 +341,14 @@ describe Invite do it 'adds the user to the topic_users of the first topic' do expect(topic.allowed_users.include?(user)).to eq(true) expect(another_topic.allowed_users.include?(user)).to eq(true) - another_invite.reload - expect(another_invite).not_to be_redeemed + duplicate_invite = Invite.find_by(id: another_invite.id) + expect(duplicate_invite).to be_nil end context 'if they redeem the other invite afterwards' do - it 'returns the same user' do - result = another_invite.redeem - expect(result).to eq(user) - another_invite.reload - expect(another_invite).to be_redeemed + it 'wont redeem a duplicate invite' do + expect(another_invite.redeem).to be_blank end end