FIX: delete duplicate invites

This commit is contained in:
Arpit Jalan 2015-03-25 22:25:18 +05:30
parent e5c3266c30
commit 4862a93804
2 changed files with 21 additions and 7 deletions

View File

@ -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

View File

@ -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