diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb index 8a5d248548d..6baa2232d09 100644 --- a/app/jobs/regular/bulk_invite.rb +++ b/app/jobs/regular/bulk_invite.rb @@ -93,7 +93,7 @@ module Jobs topic = get_topic(invite[:topic_id]) begin - if user = User.find_by_email(email) + if user = Invite.find_user_by_email(email) if groups.present? Group.transaction do groups.each do |group| diff --git a/app/models/invite.rb b/app/models/invite.rb index 34ff75b2822..597ef010d9c 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -165,7 +165,7 @@ class Invite < ActiveRecord::Base end def self.find_user_by_email(email) - User.with_email(email).where(staged: false).first + User.with_email(Email.downcase(email)).where(staged: false).first end def self.get_group_ids(group_names) diff --git a/spec/jobs/bulk_invite_spec.rb b/spec/jobs/bulk_invite_spec.rb index f25bf7812e0..aad9f5ffe47 100644 --- a/spec/jobs/bulk_invite_spec.rb +++ b/spec/jobs/bulk_invite_spec.rb @@ -9,8 +9,9 @@ describe Jobs::BulkInvite do fab!(:group1) { Fabricate(:group, name: 'group1') } fab!(:group2) { Fabricate(:group, name: 'group2') } fab!(:topic) { Fabricate(:topic) } + let(:staged_user) { Fabricate(:user, staged: true, active: false) } let(:email) { "test@discourse.org" } - let(:invites) { [{ email: 'test2@discourse.org' }, { email: 'test@discourse.org', groups: 'GROUP1;group2', topic_id: topic.id }] } + let(:invites) { [{ email: staged_user.email }, { email: 'test2@discourse.org' }, { email: 'test@discourse.org', groups: 'GROUP1;group2', topic_id: topic.id }] } it 'raises an error when the invites array is missing' do expect { Jobs::BulkInvite.new.execute(current_user_id: user.id) } @@ -28,15 +29,14 @@ describe Jobs::BulkInvite do invites: invites ) - invite = Invite.last - - expect(invite.email).to eq(email) + expect(Invite.exists?(email: staged_user.email)).to eq(true) expect(Invite.exists?(email: "test2@discourse.org")).to eq(true) + invite = Invite.last + expect(invite.email).to eq(email) expect(invite.invited_groups.pluck(:group_id)).to contain_exactly( group1.id, group2.id ) - expect(invite.topic_invites.pluck(:topic_id)).to contain_exactly(topic.id) end @@ -84,8 +84,9 @@ describe Jobs::BulkInvite do current_user_id: admin.id, invites: invites ) - end.to change { Invite.count }.by(1) + end.to change { Invite.count }.by(2) + expect(Invite.exists?(email: staged_user.email)).to eq(true) expect(Invite.exists?(email: "test2@discourse.org")).to eq(true) expect(existing_user.reload.groups).to eq([group1]) end