2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe TopicCreator do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:admin)
|
2013-11-28 10:06:04 -06:00
|
|
|
|
|
|
|
let(:valid_attrs) { Fabricate.attributes_for(:topic) }
|
2017-10-23 16:19:30 -05:00
|
|
|
let(:pm_valid_attrs) do
|
|
|
|
{
|
|
|
|
raw: "this is a new post",
|
|
|
|
title: "this is a new title",
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: moderator.username,
|
|
|
|
}
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2017-09-27 23:25:42 -05: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-28 10:06:04 -06:00
|
|
|
|
|
|
|
describe "#create" do
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with topic success cases" do
|
2013-11-28 10:06:04 -06:00
|
|
|
before do
|
|
|
|
TopicCreator.any_instance.expects(:save_topic).returns(true)
|
|
|
|
TopicCreator.any_instance.expects(:watch_topic).returns(true)
|
2017-07-07 01:09:14 -05:00
|
|
|
SiteSetting.allow_duplicate_topic_titles = true
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for an admin to create a topic" do
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(TopicCreator.create(admin, Guardian.new(admin), valid_attrs)).to be_valid
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for a moderator to create a topic" do
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(TopicCreator.create(moderator, Guardian.new(moderator), valid_attrs)).to be_valid
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|
|
|
|
|
2023-10-09 22:23:56 -05:00
|
|
|
it "supports custom_fields that has been registered to the DiscoursePluginRegistry" do
|
|
|
|
opts = valid_attrs.merge(custom_fields: { import_id: "bar" })
|
2020-02-05 03:32:03 -06:00
|
|
|
|
|
|
|
topic = TopicCreator.create(admin, Guardian.new(admin), opts)
|
|
|
|
|
|
|
|
expect(topic.custom_fields["import_id"]).to eq("bar")
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with regular user" do
|
2017-07-07 01:09:14 -05:00
|
|
|
before { SiteSetting.min_trust_to_create_topic = TrustLevel[0] }
|
2013-11-28 10:06:04 -06:00
|
|
|
|
|
|
|
it "should be possible for a regular user to create a topic" do
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(TopicCreator.create(user, Guardian.new(user), valid_attrs)).to be_valid
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be possible for a regular user to create a topic with blank auto_close_time" do
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(
|
|
|
|
TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(auto_close_time: "")),
|
|
|
|
).to be_valid
|
2013-11-28 10:06:04 -06: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-09 10:34:37 -06:00
|
|
|
expect(topic).to be_valid
|
2017-05-16 13:49:42 -05:00
|
|
|
expect(topic.public_topic_timer).to eq(nil)
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|
2014-08-18 10:07:32 -05:00
|
|
|
|
2019-05-24 12:16:19 -05:00
|
|
|
it "can create a topic in a category" do
|
2014-08-18 10:07:32 -05:00
|
|
|
category = Fabricate(:category, name: "Neil's Blog")
|
2019-05-24 12:16:19 -05:00
|
|
|
topic =
|
|
|
|
TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: category.id))
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.category).to eq(category)
|
2014-08-18 10:07:32 -05:00
|
|
|
end
|
2020-09-09 17:16:57 -05: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-28 10:06:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with tags" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:tag1) { Fabricate(:tag, name: "fun") }
|
|
|
|
fab!(:tag2) { Fabricate(:tag, name: "fun2") }
|
2022-03-28 13:25:26 -05:00
|
|
|
fab!(:tag3) { Fabricate(:tag, name: "fun3") }
|
|
|
|
fab!(:tag4) { Fabricate(:tag, name: "fun4") }
|
|
|
|
fab!(:tag5) { Fabricate(:tag, name: "fun5") }
|
|
|
|
fab!(:tag_group1) { Fabricate(:tag_group, tags: [tag1]) }
|
|
|
|
fab!(:tag_group2) { Fabricate(:tag_group, tags: [tag2]) }
|
2018-03-28 13:40:26 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with regular tags" do
|
2018-03-28 13:40:26 -05:00
|
|
|
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
|
|
|
|
|
2022-08-02 03:06:03 -05:00
|
|
|
context "when assigned via matched watched words" do
|
|
|
|
fab!(:word1) do
|
|
|
|
Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: tag1.name)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2022-08-02 03:06:03 -05:00
|
|
|
fab!(:word2) do
|
|
|
|
Fabricate(:watched_word, action: WatchedWord.actions[:tag], replacement: tag2.name)
|
|
|
|
end
|
|
|
|
fab!(:word3) do
|
|
|
|
Fabricate(
|
|
|
|
:watched_word,
|
|
|
|
action: WatchedWord.actions[:tag],
|
|
|
|
replacement: tag3.name,
|
|
|
|
case_sensitive: true,
|
|
|
|
)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
|
|
|
|
2022-08-02 03:06:03 -05:00
|
|
|
it "adds watched words as tags" do
|
2023-01-09 05:18:21 -06:00
|
|
|
topic =
|
2022-08-02 03:06:03 -05:00
|
|
|
TopicCreator.create(
|
2023-01-09 05:18:21 -06:00
|
|
|
user,
|
2021-04-18 18:43:50 -05:00
|
|
|
Guardian.new(user),
|
2022-08-02 03:06:03 -05:00
|
|
|
valid_attrs.merge(
|
|
|
|
title: "This is a #{word1.word} title",
|
|
|
|
raw: "#{word2.word.upcase} is not the same as #{word3.word.upcase}",
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
|
|
|
)
|
2022-08-02 03:06:03 -05:00
|
|
|
|
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags).to contain_exactly(tag1, tag2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with staff-only tags" do
|
2020-10-14 12:15:54 -05:00
|
|
|
before { create_staff_only_tags(["alpha"]) }
|
2018-03-28 13:40:26 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when minimum_required_tags is present" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:category) { Fabricate(:category, name: "beta", minimum_required_tags: 2) }
|
2018-03-28 13:40:26 -05:00
|
|
|
|
|
|
|
it "fails for regular user if minimum_required_tags is not satisfied" do
|
2021-04-18 18:43:50 -05:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
valid_attrs.merge(category: category.id),
|
|
|
|
).valid?,
|
|
|
|
).to be_falsy
|
2018-03-28 13:40:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets admin create a topic regardless of minimum_required_tags" do
|
2019-05-24 12:16:19 -05:00
|
|
|
topic =
|
|
|
|
TopicCreator.create(
|
|
|
|
admin,
|
|
|
|
Guardian.new(admin),
|
|
|
|
valid_attrs.merge(tags: [tag1.name], category: category.id),
|
|
|
|
)
|
2018-03-28 13:40:26 -05: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-24 12:16:19 -05:00
|
|
|
topic =
|
|
|
|
TopicCreator.create(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
valid_attrs.merge(tags: [tag1.name, tag2.name], category: category.id),
|
|
|
|
)
|
2018-03-28 13:40:26 -05:00
|
|
|
expect(topic).to be_valid
|
|
|
|
expect(topic.tags.length).to eq(2)
|
|
|
|
end
|
2018-04-06 12:45:33 -05:00
|
|
|
|
2021-10-28 01:29:46 -05: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-06 12:45:33 -05: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-24 12:16:19 -05:00
|
|
|
topic =
|
|
|
|
TopicCreator.create(
|
|
|
|
new_user,
|
|
|
|
Guardian.new(new_user),
|
|
|
|
valid_attrs.merge(category: category.id),
|
|
|
|
)
|
2018-04-06 12:45:33 -05:00
|
|
|
expect(topic).to be_valid
|
|
|
|
end
|
2018-03-28 13:40:26 -05:00
|
|
|
end
|
2019-10-30 13:49:00 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with required tag group" do
|
2019-10-30 13:49:00 -05:00
|
|
|
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1]) }
|
2022-04-06 08:08:06 -05:00
|
|
|
fab!(:category) do
|
|
|
|
Fabricate(
|
|
|
|
:category,
|
|
|
|
name: "beta",
|
|
|
|
category_required_tag_groups: [
|
|
|
|
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
|
2023-01-09 05:18:21 -06:00
|
|
|
],
|
2022-04-06 08:08:06 -05:00
|
|
|
)
|
2023-01-09 05:18:21 -06:00
|
|
|
end
|
2019-10-30 13:49:00 -05:00
|
|
|
|
|
|
|
it "when no tags are not present" do
|
2021-04-18 18:43:50 -05:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
valid_attrs.merge(category: category.id),
|
|
|
|
).valid?,
|
|
|
|
).to be_falsy
|
2019-10-30 13:49:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "when tags are not part of the tag group" do
|
2021-04-18 18:43:50 -05:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
valid_attrs.merge(category: category.id, tags: ["nope"]),
|
|
|
|
).valid?,
|
|
|
|
).to be_falsy
|
2019-10-30 13:49:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "when requirement is met" do
|
2021-04-18 18:43:50 -05: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-30 13:49:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets staff ignore the restriction" do
|
2021-04-18 18:43:50 -05:00
|
|
|
expect(
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(admin),
|
|
|
|
valid_attrs.merge(category: category.id),
|
|
|
|
).valid?,
|
|
|
|
).to be_truthy
|
2019-10-30 13:49:00 -05:00
|
|
|
end
|
|
|
|
end
|
2022-03-28 13:25:26 -05:00
|
|
|
|
|
|
|
context "when category has restricted tags or tag groups" do
|
|
|
|
fab!(:category) { Fabricate(:category, tags: [tag3], tag_groups: [tag_group1]) }
|
|
|
|
|
|
|
|
it "allows topics without any tags" do
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic without tags",
|
|
|
|
raw: "hello this is a test topic without tags",
|
|
|
|
category: category.id,
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(true)
|
|
|
|
expect(tc.errors).to be_empty
|
|
|
|
topic = tc.create
|
|
|
|
expect(topic.tags).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows topics if they use tags only from the tags set that the category restricts" do
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag3.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(true)
|
|
|
|
expect(tc.errors).to be_empty
|
|
|
|
topic = tc.create
|
|
|
|
expect(topic.tags).to contain_exactly(tag1, tag3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows topics to use tags that are restricted in multiple categories" do
|
|
|
|
category2 = Fabricate(:category, tags: [tag5], tag_groups: [tag_group1])
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category2.id,
|
|
|
|
tags: [tag1.name, tag5.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(true)
|
|
|
|
expect(tc.errors).to be_empty
|
|
|
|
topic = tc.create
|
|
|
|
expect(topic.tags).to contain_exactly(tag1, tag5)
|
|
|
|
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags 1",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag3.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(true)
|
|
|
|
expect(tc.errors).to be_empty
|
|
|
|
topic = tc.create
|
|
|
|
expect(topic.tags).to contain_exactly(tag1, tag3)
|
|
|
|
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags 2",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag5.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
|
|
|
|
count: 1,
|
|
|
|
tags: tag5.name,
|
|
|
|
category: category.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects topics if they use a tag outside the set of tags that the category restricts" do
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag2.name, tag1.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.category_does_not_allow_tags",
|
|
|
|
count: 1,
|
|
|
|
tags: tag2.name,
|
|
|
|
category: category.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag2.name, tag5.name, tag3.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.category_does_not_allow_tags",
|
|
|
|
count: 2,
|
|
|
|
tags: [tag2, tag5].map(&:name).sort.join(", "),
|
|
|
|
category: category.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects topics in other categories if a restricted tag of a category are used" do
|
|
|
|
category2 = Fabricate(:category)
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category2.id,
|
|
|
|
tags: [tag1.name, tag2.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
|
|
|
|
count: 1,
|
|
|
|
tags: tag1.name,
|
|
|
|
category: category2.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when allowing other tags" do
|
2022-03-28 13:25:26 -05:00
|
|
|
before { category.update!(allow_global_tags: true) }
|
|
|
|
|
|
|
|
it "allows topics to use tags that aren't restricted by any category" do
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag2.name, tag3.name, tag5.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(true)
|
|
|
|
expect(tc.errors).to be_empty
|
|
|
|
topic = tc.create
|
|
|
|
expect(topic.tags).to contain_exactly(tag1, tag2, tag3, tag5)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rejects topics if they use restricted tags of another category" do
|
|
|
|
Fabricate(:category, tags: [tag5], tag_groups: [tag_group2])
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag5.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
|
|
|
|
count: 1,
|
|
|
|
tags: tag5.name,
|
|
|
|
category: category.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
tc =
|
|
|
|
TopicCreator.new(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
title: "hello this is a test topic with tags",
|
|
|
|
raw: "hello this is a test topic with tags",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag2.name, tag5.name],
|
|
|
|
)
|
|
|
|
expect(tc.valid?).to eq(false)
|
|
|
|
expect(tc.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
|
|
|
|
count: 2,
|
|
|
|
tags: [tag2, tag5].map(&:name).sort.join(", "),
|
|
|
|
category: category.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-28 13:40:26 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with personal message" do
|
|
|
|
context "with success cases" do
|
2015-10-17 02:48:56 -05:00
|
|
|
before do
|
|
|
|
TopicCreator.any_instance.expects(:save_topic).returns(true)
|
|
|
|
TopicCreator.any_instance.expects(:watch_topic).returns(true)
|
2017-07-07 01:09:14 -05:00
|
|
|
SiteSetting.allow_duplicate_topic_titles = true
|
2017-08-28 11:07:30 -05:00
|
|
|
SiteSetting.enable_staged_users = true
|
2022-10-04 19:50:20 -05:00
|
|
|
Group.refresh_automatic_groups!
|
2015-10-17 02:48:56 -05: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-28 11:07:30 -05:00
|
|
|
|
2022-09-25 22:58:40 -05:00
|
|
|
it "personal_message_enabled_groups setting should not be checked when sending private messages to staff via flag" do
|
|
|
|
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:staff]
|
2019-01-24 05:26:59 -06:00
|
|
|
expect(
|
|
|
|
TopicCreator.create(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
pm_valid_attrs.merge(subtype: TopicSubtype.notify_moderators),
|
2023-01-09 05:18:21 -06:00
|
|
|
),
|
2019-01-24 05:26:59 -06:00
|
|
|
).to be_valid
|
|
|
|
end
|
2015-10-17 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with failure cases" do
|
2017-09-27 23:25:42 -05:00
|
|
|
it "should be rollback the changes when email is invalid" do
|
2018-08-26 23:50:56 -05:00
|
|
|
SiteSetting.manual_polling_enabled = true
|
|
|
|
SiteSetting.reply_by_email_address = "sam+%{reply_key}@sam.com"
|
|
|
|
SiteSetting.reply_by_email_enabled = true
|
2017-09-27 23:25:42 -05: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
|
|
|
|
|
2022-09-25 22:58:40 -05:00
|
|
|
it "personal_message_enabled_groups setting should be checked when sending private message" do
|
|
|
|
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_4]
|
2017-08-28 19:26:48 -05:00
|
|
|
|
|
|
|
expect do
|
|
|
|
TopicCreator.create(user, Guardian.new(user), pm_valid_attrs)
|
|
|
|
end.to raise_error(ActiveRecord::Rollback)
|
2015-10-17 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2021-03-30 04:18:57 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with to emails" do
|
2022-10-04 19:50:20 -05:00
|
|
|
before { Group.refresh_automatic_groups! }
|
|
|
|
|
2021-03-30 04:18:57 -05:00
|
|
|
it "works for staff" do
|
2021-04-06 07:39:27 -05: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 04:18:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not work for non-staff" do
|
2021-04-06 07:39:27 -05: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 04:18:57 -05:00
|
|
|
end
|
|
|
|
end
|
2015-10-17 02:48:56 -05:00
|
|
|
end
|
2020-03-10 09:35:40 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "when setting timestamps" do
|
2020-03-10 09:35:40 -05:00
|
|
|
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-10 16:13:17 -05:00
|
|
|
expect(topic.created_at).to eq_time(1.week.ago)
|
|
|
|
expect(topic.pinned_at).to eq_time(3.days.ago)
|
2020-03-10 09:35:40 -05: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-10 16:13:17 -05:00
|
|
|
expect(topic.created_at).to eq_time(time1)
|
|
|
|
expect(topic.pinned_at).to eq_time(time2)
|
2020-03-10 09:35:40 -05:00
|
|
|
end
|
|
|
|
end
|
2022-02-08 21:55:32 -06:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context "with external_id" do
|
2022-02-08 21:55:32 -06:00
|
|
|
it "adds external_id" do
|
|
|
|
topic =
|
|
|
|
TopicCreator.create(
|
|
|
|
user,
|
|
|
|
Guardian.new(user),
|
|
|
|
valid_attrs.merge(external_id: "external_id"),
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic.external_id).to eq("external_id")
|
|
|
|
end
|
|
|
|
end
|
2022-12-01 04:26:35 -06:00
|
|
|
|
|
|
|
context "when invisible/unlisted" do
|
|
|
|
let(:unlisted_attrs) { valid_attrs.merge(visible: false) }
|
|
|
|
|
|
|
|
it "throws an exception for a non-staff user" do
|
|
|
|
expect do TopicCreator.create(user, Guardian.new(user), unlisted_attrs) end.to raise_error(
|
|
|
|
ActiveRecord::Rollback,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is invalid for a non-staff user" do
|
|
|
|
expect(TopicCreator.new(user, Guardian.new(user), unlisted_attrs).valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates unlisted topic for an admin" do
|
|
|
|
expect(TopicCreator.create(admin, Guardian.new(admin), unlisted_attrs)).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid for an admin" do
|
|
|
|
expect(TopicCreator.new(admin, Guardian.new(admin), unlisted_attrs).valid?).to eq(true)
|
|
|
|
end
|
2023-12-12 08:35:26 -06:00
|
|
|
|
|
|
|
context "when embedded" do
|
|
|
|
let(:embedded_unlisted_attrs) do
|
|
|
|
unlisted_attrs.merge(embed_url: "http://eviltrout.com/stupid-url")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is valid for a non-staff user" do
|
|
|
|
expect(TopicCreator.new(user, Guardian.new(user), embedded_unlisted_attrs).valid?).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-12-01 04:26:35 -06:00
|
|
|
end
|
2015-10-17 02:48:56 -05:00
|
|
|
end
|
2013-11-28 10:06:04 -06:00
|
|
|
end
|