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
|
|
|
|
let(:user) { Fabricate(:user) }
|
2019-04-15 21:26:03 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2019-04-15 21:13:53 +08:00
|
|
|
let!(:group1) { Fabricate(:group, name: 'group1') }
|
|
|
|
let!(:group2) { Fabricate(:group, name: 'group2') }
|
|
|
|
let!(:topic) { Fabricate(:topic, id: 999) }
|
|
|
|
let(:email) { "test@discourse.org" }
|
|
|
|
let(:basename) { "bulk_invite.csv" }
|
|
|
|
let(:filename) { "#{Invite.base_directory}/#{basename}" }
|
|
|
|
|
|
|
|
before do
|
2019-04-16 09:59:35 +08:00
|
|
|
Invite.create_csv(
|
|
|
|
fixture_file_upload("#{Rails.root}/spec/fixtures/csv/#{basename}"),
|
|
|
|
"bulk_invite"
|
2019-04-15 21:13:53 +08:00
|
|
|
)
|
|
|
|
end
|
2014-06-26 02:35:11 +08:00
|
|
|
|
|
|
|
it 'raises an error when the filename is missing' do
|
2016-12-05 00:06:35 +08:00
|
|
|
user = Fabricate(:user)
|
2019-04-15 21:13:53 +08:00
|
|
|
|
|
|
|
expect { Jobs::BulkInvite.new.execute(current_user_id: user.id) }
|
|
|
|
.to raise_error(Discourse::InvalidParameters, /filename/)
|
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
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
expect { Jobs::BulkInvite.new.execute(filename: filename) }
|
|
|
|
.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(
|
|
|
|
current_user_id: Fabricate(:admin).id,
|
|
|
|
filename: basename,
|
|
|
|
)
|
2014-06-26 23:16:53 +08:00
|
|
|
|
2019-04-15 21:13:53 +08:00
|
|
|
invite = Invite.last
|
|
|
|
|
|
|
|
expect(invite.email).to eq(email)
|
|
|
|
expect(Invite.exists?(email: "test2@discourse.org")).to eq(true)
|
|
|
|
|
|
|
|
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(
|
|
|
|
current_user_id: Fabricate(:admin).id,
|
|
|
|
filename: basename,
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
filename: basename
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
filename: basename
|
|
|
|
)
|
|
|
|
end.to change { Invite.count }.by(1)
|
|
|
|
|
|
|
|
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
|
|
|
end
|
|
|
|
|
|
|
|
end
|