2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-11-29 00:06:04 +08:00
|
|
|
|
|
|
|
describe TopicCreator do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
|
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2013-11-29 00:06:04 +08:00
|
|
|
|
|
|
|
let(:valid_attrs) { Fabricate.attributes_for(:topic) }
|
2017-10-24 05:19:30 +08:00
|
|
|
let(:pm_valid_attrs) { { raw: 'this is a new post', title: 'this is a new title', archetype: Archetype.private_message, target_usernames: moderator.username } }
|
2017-09-28 12:25:42 +08:00
|
|
|
|
|
|
|
let(:pm_to_email_valid_attrs) do
|
|
|
|
{
|
|
|
|
raw: 'this is a new email',
|
|
|
|
title: 'this is a new subject',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_emails: 'moderator@example.com'
|
|
|
|
}
|
|
|
|
end
|
2013-11-29 00:06:04 +08:00
|
|
|
|
|
|
|
describe '#create' do
|
2015-10-17 15:48:56 +08:00
|
|
|
context 'topic success cases' do
|
2013-11-29 00:06:04 +08:00
|
|
|
before do
|
|
|
|
TopicCreator.any_instance.expects(:save_topic).returns(true)
|
|
|
|
TopicCreator.any_instance.expects(:watch_topic).returns(true)
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.allow_duplicate_topic_titles = true
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for an admin to create a topic" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicCreator.create(admin, Guardian.new(admin), valid_attrs)).to be_valid
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for a moderator to create a topic" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicCreator.create(moderator, Guardian.new(moderator), valid_attrs)).to be_valid
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
|
2020-02-05 17:32:03 +08:00
|
|
|
it "supports both meta_data and custom_fields" do
|
|
|
|
opts = valid_attrs.merge(
|
|
|
|
meta_data: { import_topic_id: "foo" },
|
|
|
|
custom_fields: { import_id: "bar" }
|
|
|
|
)
|
|
|
|
|
|
|
|
topic = TopicCreator.create(admin, Guardian.new(admin), opts)
|
|
|
|
|
|
|
|
expect(topic.custom_fields["import_topic_id"]).to eq("foo")
|
|
|
|
expect(topic.custom_fields["import_id"]).to eq("bar")
|
|
|
|
end
|
|
|
|
|
2013-11-29 00:06:04 +08:00
|
|
|
context 'regular user' do
|
2017-07-07 14:09:14 +08:00
|
|
|
before { SiteSetting.min_trust_to_create_topic = TrustLevel[0] }
|
2013-11-29 00:06:04 +08:00
|
|
|
|
|
|
|
it "should be possible for a regular user to create a topic" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), valid_attrs)).to be_valid
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for a regular user to create a topic with blank auto_close_time" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(auto_close_time: ''))).to be_valid
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores auto_close_time without raising an error" do
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(auto_close_time: '24'))
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).to be_valid
|
2017-05-17 02:49:42 +08:00
|
|
|
expect(topic.public_topic_timer).to eq(nil)
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
2014-08-18 23:07:32 +08:00
|
|
|
|
2019-05-25 01:16:19 +08:00
|
|
|
it "can create a topic in a category" do
|
2014-08-18 23:07:32 +08:00
|
|
|
category = Fabricate(:category, name: "Neil's Blog")
|
2019-05-25 01:16:19 +08:00
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: category.id))
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.category).to eq(category)
|
2014-08-18 23:07:32 +08:00
|
|
|
end
|
2020-09-10 06:16:57 +08:00
|
|
|
|
|
|
|
it "ignores participant_count without raising an error" do
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(participant_count: 3))
|
|
|
|
expect(topic.participant_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts participant_count in import mode" do
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(import_mode: true, participant_count: 3))
|
|
|
|
expect(topic.participant_count).to eq(3)
|
|
|
|
end
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-29 02:40:26 +08:00
|
|
|
context 'tags' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:tag1) { Fabricate(:tag, name: "fun") }
|
|
|
|
fab!(:tag2) { Fabricate(:tag, name: "fun2") }
|
2018-03-29 02:40:26 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'regular tags' do
|
|
|
|
it "user can add tags to topic" do
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name]))
|
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'staff-only tags' do
|
|
|
|
before do
|
2020-10-15 01:15:54 +08:00
|
|
|
create_staff_only_tags(['alpha'])
|
2018-03-29 02:40:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "regular users can't add staff-only tags" do
|
|
|
|
expect do
|
|
|
|
TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: ['alpha']))
|
|
|
|
end.to raise_error(ActiveRecord::Rollback)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'staff can add staff-only tags' do
|
|
|
|
topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: ['alpha']))
|
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'minimum_required_tags is present' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category, name: "beta", minimum_required_tags: 2) }
|
2018-03-29 02:40:26 +08:00
|
|
|
|
|
|
|
it "fails for regular user if minimum_required_tags is not satisfied" do
|
2021-04-19 07:43:50 +08:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(user, Guardian.new(user), valid_attrs.merge(category: category.id)).valid?
|
|
|
|
).to be_falsy
|
2018-03-29 02:40:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets admin create a topic regardless of minimum_required_tags" do
|
2019-05-25 01:16:19 +08:00
|
|
|
topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: [tag1.name], category: category.id))
|
2018-03-29 02:40:26 +08:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for regular user if minimum_required_tags is satisfied" do
|
2019-05-25 01:16:19 +08:00
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name, tag2.name], category: category.id))
|
2018-03-29 02:40:26 +08:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(2)
|
|
|
|
end
|
2018-04-07 01:45:33 +08:00
|
|
|
|
2021-10-28 14:29:46 +08:00
|
|
|
it "minimum_required_tags is satisfying for new tags if user can create" do
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: ["new tag", "another tag"], category: category.id))
|
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(2)
|
|
|
|
end
|
|
|
|
|
2018-04-07 01:45:33 +08:00
|
|
|
it "lets new user create a topic if they don't have sufficient trust level to tag topics" do
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 1
|
|
|
|
new_user = Fabricate(:newuser)
|
2019-05-25 01:16:19 +08:00
|
|
|
topic = TopicCreator.create(new_user, Guardian.new(new_user), valid_attrs.merge(category: category.id))
|
2018-04-07 01:45:33 +08:00
|
|
|
expect(topic).to be_valid
|
|
|
|
end
|
2018-03-29 02:40:26 +08:00
|
|
|
end
|
2019-10-31 02:49:00 +08:00
|
|
|
|
|
|
|
context 'required tag group' do
|
|
|
|
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1]) }
|
|
|
|
fab!(:category) { Fabricate(:category, name: "beta", required_tag_group: tag_group, min_tags_from_required_group: 1) }
|
|
|
|
|
|
|
|
it "when no tags are not present" do
|
2021-04-19 07:43:50 +08:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(user, Guardian.new(user), valid_attrs.merge(category: category.id)).valid?
|
|
|
|
).to be_falsy
|
2019-10-31 02:49:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "when tags are not part of the tag group" do
|
2021-04-19 07:43:50 +08:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(user, Guardian.new(user), valid_attrs.merge(category: category.id, tags: ['nope'])).valid?
|
|
|
|
).to be_falsy
|
2019-10-31 02:49:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "when requirement is met" do
|
2021-04-19 07:43:50 +08:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(user, Guardian.new(user), valid_attrs.merge(category: category.id, tags: [tag1.name, tag2.name])).valid?
|
|
|
|
).to be_truthy
|
2019-10-31 02:49:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets staff ignore the restriction" do
|
2021-04-19 07:43:50 +08:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(user, Guardian.new(admin), valid_attrs.merge(category: category.id)).valid?
|
|
|
|
).to be_truthy
|
2019-10-31 02:49:00 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-29 02:40:26 +08:00
|
|
|
end
|
|
|
|
|
2018-08-27 09:38:11 +08:00
|
|
|
context 'personal message' do
|
2015-10-17 15:48:56 +08:00
|
|
|
|
|
|
|
context 'success cases' do
|
|
|
|
before do
|
|
|
|
TopicCreator.any_instance.expects(:save_topic).returns(true)
|
|
|
|
TopicCreator.any_instance.expects(:watch_topic).returns(true)
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.allow_duplicate_topic_titles = true
|
2017-08-29 00:07:30 +08:00
|
|
|
SiteSetting.enable_staged_users = true
|
2015-10-17 15:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for a regular user to send private message" do
|
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), pm_valid_attrs)).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "min_trust_to_create_topic setting should not be checked when sending private message" do
|
|
|
|
SiteSetting.min_trust_to_create_topic = TrustLevel[4]
|
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), pm_valid_attrs)).to be_valid
|
|
|
|
end
|
2017-08-29 00:07:30 +08:00
|
|
|
|
2019-01-24 19:26:59 +08:00
|
|
|
it "enable_personal_messages setting should not be checked when sending private message to staff via flag" do
|
|
|
|
SiteSetting.enable_personal_messages = false
|
|
|
|
SiteSetting.min_trust_to_send_messages = TrustLevel[4]
|
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), pm_valid_attrs.merge(subtype: TopicSubtype.notify_moderators))).to be_valid
|
|
|
|
end
|
2015-10-17 15:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'failure cases' do
|
2017-09-28 12:25:42 +08:00
|
|
|
it "should be rollback the changes when email is invalid" do
|
2018-08-27 12:50:56 +08:00
|
|
|
SiteSetting.manual_polling_enabled = true
|
|
|
|
SiteSetting.reply_by_email_address = "sam+%{reply_key}@sam.com"
|
|
|
|
SiteSetting.reply_by_email_enabled = true
|
2017-09-28 12:25:42 +08:00
|
|
|
SiteSetting.min_trust_to_send_email_messages = TrustLevel[1]
|
|
|
|
attrs = pm_to_email_valid_attrs.dup
|
|
|
|
attrs[:target_emails] = "t" * 256
|
|
|
|
|
|
|
|
expect do
|
|
|
|
TopicCreator.create(user, Guardian.new(user), attrs)
|
|
|
|
end.to raise_error(ActiveRecord::Rollback)
|
|
|
|
end
|
|
|
|
|
2015-10-17 15:48:56 +08:00
|
|
|
it "min_trust_to_send_messages setting should be checked when sending private message" do
|
|
|
|
SiteSetting.min_trust_to_send_messages = TrustLevel[4]
|
2017-08-29 08:26:48 +08:00
|
|
|
|
|
|
|
expect do
|
|
|
|
TopicCreator.create(user, Guardian.new(user), pm_valid_attrs)
|
|
|
|
end.to raise_error(ActiveRecord::Rollback)
|
2015-10-17 15:48:56 +08:00
|
|
|
end
|
|
|
|
end
|
2021-03-30 17:18:57 +08:00
|
|
|
|
|
|
|
context 'to emails' do
|
|
|
|
it 'works for staff' do
|
2021-04-06 20:39:27 +08:00
|
|
|
SiteSetting.min_trust_to_send_email_messages = 'staff'
|
|
|
|
expect(TopicCreator.create(admin, Guardian.new(admin), pm_to_email_valid_attrs)).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'work for trusted users' do
|
|
|
|
SiteSetting.min_trust_to_send_email_messages = 3
|
|
|
|
user.update!(trust_level: 3)
|
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), pm_to_email_valid_attrs)).to be_valid
|
2021-03-30 17:18:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not work for non-staff' do
|
2021-04-06 20:39:27 +08:00
|
|
|
SiteSetting.min_trust_to_send_email_messages = 'staff'
|
|
|
|
expect { TopicCreator.create(user, Guardian.new(user), pm_to_email_valid_attrs) }.to raise_error(ActiveRecord::Rollback)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not work for untrusted users' do
|
|
|
|
SiteSetting.min_trust_to_send_email_messages = 3
|
|
|
|
user.update!(trust_level: 2)
|
|
|
|
expect { TopicCreator.create(user, Guardian.new(user), pm_to_email_valid_attrs) }.to raise_error(ActiveRecord::Rollback)
|
2021-03-30 17:18:57 +08:00
|
|
|
end
|
|
|
|
end
|
2015-10-17 15:48:56 +08:00
|
|
|
end
|
2020-03-10 22:35:40 +08:00
|
|
|
|
|
|
|
context 'setting timestamps' do
|
|
|
|
it 'supports Time instances' do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(
|
|
|
|
created_at: 1.week.ago,
|
|
|
|
pinned_at: 3.days.ago
|
|
|
|
))
|
|
|
|
|
2020-03-11 05:13:17 +08:00
|
|
|
expect(topic.created_at).to eq_time(1.week.ago)
|
|
|
|
expect(topic.pinned_at).to eq_time(3.days.ago)
|
2020-03-10 22:35:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports strings' do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
time1 = Time.zone.parse('2019-09-02')
|
|
|
|
time2 = Time.zone.parse('2020-03-10 15:17')
|
|
|
|
|
|
|
|
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(
|
|
|
|
created_at: '2019-09-02',
|
|
|
|
pinned_at: '2020-03-10 15:17'
|
|
|
|
))
|
|
|
|
|
2020-03-11 05:13:17 +08:00
|
|
|
expect(topic.created_at).to eq_time(time1)
|
|
|
|
expect(topic.pinned_at).to eq_time(time2)
|
2020-03-10 22:35:40 +08:00
|
|
|
end
|
|
|
|
end
|
2015-10-17 15:48:56 +08:00
|
|
|
end
|
2013-11-29 00:06:04 +08:00
|
|
|
end
|