2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-06-26 02:35:11 +08:00
|
|
|
|
|
|
|
describe Jobs::BulkInvite do
|
2019-04-15 21:13:53 +08:00
|
|
|
describe '#execute' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:group1) { Fabricate(:group, name: 'group1') }
|
|
|
|
fab!(:group2) { Fabricate(:group, name: 'group2') }
|
2019-10-08 06:37:07 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2019-10-30 14:08:47 +08:00
|
|
|
let(:staged_user) { Fabricate(:user, staged: true, active: false) }
|
2019-04-15 21:13:53 +08:00
|
|
|
let(:email) { "test@discourse.org" }
|
2019-10-30 14:08:47 +08:00
|
|
|
let(:invites) { [{ email: staged_user.email }, { email: 'test2@discourse.org' }, { email: 'test@discourse.org', groups: 'GROUP1;group2', topic_id: topic.id }] }
|
2019-04-15 21:13:53 +08:00
|
|
|
|
2019-06-04 22:49:46 +08:00
|
|
|
it 'raises an error when the invites array is missing' do
|
2019-04-15 21:13:53 +08:00
|
|
|
expect { Jobs::BulkInvite.new.execute(current_user_id: user.id) }
|
2019-06-04 22:49:46 +08:00
|
|
|
.to raise_error(Discourse::InvalidParameters, /invites/)
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2019-04-15 21:13:53 +08:00
|
|
|
it 'raises an error when current_user_id is not valid' do
|
2019-06-04 22:49:46 +08:00
|
|
|
expect { Jobs::BulkInvite.new.execute(invites: invites) }
|
2019-04-15 21:13:53 +08:00
|
|
|
.to raise_error(Discourse::InvalidParameters, /current_user_id/)
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2019-04-15 21:13:53 +08:00
|
|
|
it 'creates the right invites' do
|
|
|
|
described_class.new.execute(
|
2019-05-06 17:09:47 +08:00
|
|
|
current_user_id: admin.id,
|
2019-06-04 22:49:46 +08:00
|
|
|
invites: invites
|
2019-04-15 21:13:53 +08:00
|
|
|
)
|
2014-06-26 23:16:53 +08:00
|
|
|
|
2019-10-30 14:08:47 +08:00
|
|
|
expect(Invite.exists?(email: staged_user.email)).to eq(true)
|
2019-04-15 21:13:53 +08:00
|
|
|
expect(Invite.exists?(email: "test2@discourse.org")).to eq(true)
|
|
|
|
|
2019-10-30 14:08:47 +08:00
|
|
|
invite = Invite.last
|
|
|
|
expect(invite.email).to eq(email)
|
2019-04-15 21:13:53 +08:00
|
|
|
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)
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2019-04-15 21:13:53 +08:00
|
|
|
it 'does not create invited groups for automatic groups' do
|
|
|
|
group2.update!(automatic: true)
|
|
|
|
|
|
|
|
described_class.new.execute(
|
2019-05-06 17:09:47 +08:00
|
|
|
current_user_id: admin.id,
|
2019-06-04 22:49:46 +08:00
|
|
|
invites: invites
|
2019-04-15 21:13:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
invite = Invite.last
|
|
|
|
|
|
|
|
expect(invite.email).to eq(email)
|
|
|
|
|
|
|
|
expect(invite.invited_groups.pluck(:group_id)).to contain_exactly(
|
|
|
|
group1.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create invited groups record if the user can not manage the group' do
|
|
|
|
group1.add_owner(user)
|
|
|
|
|
|
|
|
described_class.new.execute(
|
|
|
|
current_user_id: user.id,
|
2019-06-04 22:49:46 +08:00
|
|
|
invites: invites
|
2019-04-15 21:13:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
invite = Invite.last
|
|
|
|
|
|
|
|
expect(invite.email).to eq(email)
|
|
|
|
|
|
|
|
expect(invite.invited_groups.pluck(:group_id)).to contain_exactly(
|
|
|
|
group1.id
|
|
|
|
)
|
|
|
|
end
|
2019-04-15 21:26:03 +08:00
|
|
|
|
|
|
|
it 'adds existing users to valid groups' do
|
|
|
|
existing_user = Fabricate(:user, email: "test@discourse.org")
|
|
|
|
|
|
|
|
group2.update!(automatic: true)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
described_class.new.execute(
|
|
|
|
current_user_id: admin.id,
|
2019-06-04 22:49:46 +08:00
|
|
|
invites: invites
|
2019-04-15 21:26:03 +08:00
|
|
|
)
|
2019-10-30 14:08:47 +08:00
|
|
|
end.to change { Invite.count }.by(2)
|
2019-04-15 21:26:03 +08:00
|
|
|
|
2019-10-30 14:08:47 +08:00
|
|
|
expect(Invite.exists?(email: staged_user.email)).to eq(true)
|
2019-04-15 21:26:03 +08:00
|
|
|
expect(Invite.exists?(email: "test2@discourse.org")).to eq(true)
|
|
|
|
expect(existing_user.reload.groups).to eq([group1])
|
|
|
|
end
|
2014-06-26 02:35:11 +08:00
|
|
|
|
2019-07-19 13:59:12 +08:00
|
|
|
context 'invites are more than 200' do
|
|
|
|
let(:bulk_invites) { [] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
202.times do |i|
|
|
|
|
bulk_invites << { "email": "test_#{i}@discourse.org" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rate limits email sending' do
|
|
|
|
described_class.new.execute(
|
|
|
|
current_user_id: admin.id,
|
|
|
|
invites: bulk_invites
|
|
|
|
)
|
|
|
|
|
|
|
|
invite = Invite.last
|
|
|
|
expect(invite.email).to eq("test_201@discourse.org")
|
|
|
|
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:bulk_pending])
|
|
|
|
expect(Jobs::ProcessBulkInviteEmails.jobs.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|