2015-03-03 03:25:25 +08:00
|
|
|
# encoding: utf-8
|
2013-02-07 04:47:36 +08:00
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-03-19 05:52:29 +08:00
|
|
|
require_dependency 'post_destroyer'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Topic do
|
|
|
|
|
2014-03-07 18:38:24 +08:00
|
|
|
let(:now) { Time.zone.local(2013,11,20,8,0) }
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
it { is_expected.to validate_presence_of :title }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
it { is_expected.to rate_limit }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-09-11 04:01:23 +08:00
|
|
|
context '#visible_post_types' do
|
|
|
|
let(:types) { Post.types }
|
|
|
|
|
|
|
|
it "returns the appropriate types for anonymous users" do
|
2015-09-22 06:50:52 +08:00
|
|
|
post_types = Topic.visible_post_types
|
2015-09-11 04:01:23 +08:00
|
|
|
|
|
|
|
expect(post_types).to include(types[:regular])
|
|
|
|
expect(post_types).to include(types[:moderator_action])
|
|
|
|
expect(post_types).to include(types[:small_action])
|
|
|
|
expect(post_types).to_not include(types[:whisper])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the appropriate types for regular users" do
|
2015-09-22 06:50:52 +08:00
|
|
|
post_types = Topic.visible_post_types(Fabricate.build(:user))
|
2015-09-11 04:01:23 +08:00
|
|
|
|
|
|
|
expect(post_types).to include(types[:regular])
|
|
|
|
expect(post_types).to include(types[:moderator_action])
|
|
|
|
expect(post_types).to include(types[:small_action])
|
|
|
|
expect(post_types).to_not include(types[:whisper])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the appropriate types for staff users" do
|
2015-09-22 06:50:52 +08:00
|
|
|
post_types = Topic.visible_post_types(Fabricate.build(:moderator))
|
2015-09-11 04:01:23 +08:00
|
|
|
|
|
|
|
expect(post_types).to include(types[:regular])
|
|
|
|
expect(post_types).to include(types[:moderator_action])
|
|
|
|
expect(post_types).to include(types[:small_action])
|
|
|
|
expect(post_types).to include(types[:whisper])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-15 06:13:03 +08:00
|
|
|
context 'slug' do
|
|
|
|
let(:title) { "hello world topic" }
|
2015-04-13 22:50:41 +08:00
|
|
|
let(:slug) { "hello-world-topic" }
|
|
|
|
context 'encoded generator' do
|
|
|
|
before { SiteSetting.slug_generation_method = 'encoded' }
|
|
|
|
after { SiteSetting.slug_generation_method = 'ascii' }
|
|
|
|
|
|
|
|
it "returns a Slug for a title" do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns(slug)
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for cjk characters' do
|
|
|
|
let(:title) { "熱帶風暴畫眉" }
|
2015-05-04 19:48:37 +08:00
|
|
|
let(:slug) { "熱帶風暴畫眉" }
|
2015-04-13 22:50:41 +08:00
|
|
|
it "returns encoded Slug for a title" do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns(slug)
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for numbers' do
|
|
|
|
let(:title) { "123456789" }
|
|
|
|
let(:slug) { "topic" }
|
|
|
|
it 'generates default slug' do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns("topic")
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
|
|
|
|
end
|
|
|
|
end
|
2013-02-15 06:13:03 +08:00
|
|
|
end
|
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
context 'none generator' do
|
|
|
|
before { SiteSetting.slug_generation_method = 'none' }
|
|
|
|
after { SiteSetting.slug_generation_method = 'ascii' }
|
|
|
|
let(:title) { "熱帶風暴畫眉" }
|
|
|
|
let(:slug) { "topic" }
|
2014-09-16 19:15:05 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
it "returns a Slug for a title" do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns('topic')
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
|
|
|
end
|
2014-09-16 19:15:05 +08:00
|
|
|
end
|
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
context '#ascii_generator' do
|
|
|
|
before { SiteSetting.slug_generation_method = 'ascii' }
|
|
|
|
it "returns a Slug for a title" do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns(slug)
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for cjk characters' do
|
|
|
|
let(:title) { "熱帶風暴畫眉" }
|
|
|
|
let(:slug) { 'topic' }
|
|
|
|
it "returns 'topic' when the slug is empty (say, non-latin characters)" do
|
2015-05-04 19:48:37 +08:00
|
|
|
Slug.expects(:for).with(title).returns("topic")
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")
|
|
|
|
end
|
|
|
|
end
|
2013-02-15 06:13:03 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-01 03:22:34 +08:00
|
|
|
context "updating a title to be shorter" do
|
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "doesn't update it to be shorter due to cleaning using TextCleaner" do
|
|
|
|
topic.title = 'unread glitch'
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(false)
|
2013-06-01 03:22:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-05 05:58:25 +08:00
|
|
|
context 'private message title' do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:min_topic_title_length).returns(15)
|
|
|
|
SiteSetting.stubs(:min_private_message_title_length).returns(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows shorter titles' do
|
|
|
|
pm = Fabricate.build(:private_message_topic, title: 'a' * SiteSetting.min_private_message_title_length)
|
|
|
|
expect(pm).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'but not too short' do
|
|
|
|
pm = Fabricate.build(:private_message_topic, title: 'a')
|
|
|
|
expect(pm).to_not be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-30 05:57:10 +08:00
|
|
|
context 'admin topic title' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it 'allows really short titles' do
|
|
|
|
pm = Fabricate.build(:private_message_topic, user: admin, title: 'a')
|
|
|
|
expect(pm).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'but not blank' do
|
|
|
|
pm = Fabricate.build(:private_message_topic, title: '')
|
|
|
|
expect(pm).to_not be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'topic title uniqueness' do
|
|
|
|
|
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
let(:new_topic) { Fabricate.build(:topic, title: topic.title) }
|
|
|
|
|
|
|
|
context "when duplicates aren't allowed" do
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:allow_duplicate_topic_titles?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "won't allow another topic to be created with the same name" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(new_topic).not_to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't allow another topic with an upper case title to be created" do
|
|
|
|
new_topic.title = new_topic.title.upcase
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(new_topic).not_to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows it when the topic is deleted" do
|
|
|
|
topic.destroy
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(new_topic).to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows a private message to be created with the same topic" do
|
|
|
|
new_topic.archetype = Archetype.private_message
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(new_topic).to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when duplicates are allowed" do
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:allow_duplicate_topic_titles?).returns(true)
|
|
|
|
end
|
|
|
|
|
2013-05-23 12:52:12 +08:00
|
|
|
it "will allow another topic to be created with the same name" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(new_topic).to be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-20 05:08:23 +08:00
|
|
|
context 'html in title' do
|
2013-04-22 11:48:05 +08:00
|
|
|
|
|
|
|
def build_topic_with_title(title)
|
2013-05-23 12:52:12 +08:00
|
|
|
build(:topic, title: title).tap{ |t| t.valid? }
|
2013-04-22 11:48:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:topic_bold) { build_topic_with_title("Topic with <b>bold</b> text in its title" ) }
|
|
|
|
let(:topic_image) { build_topic_with_title("Topic with <img src='something'> image in its title" ) }
|
|
|
|
let(:topic_script) { build_topic_with_title("Topic with <script>alert('title')</script> script in its title" ) }
|
2013-02-20 05:08:23 +08:00
|
|
|
|
2014-07-14 15:18:02 +08:00
|
|
|
it "escapes script contents" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic_script.fancy_title).to eq("Topic with <script>alert(‘title’)</script> script in its title")
|
2013-03-07 00:36:42 +08:00
|
|
|
end
|
|
|
|
|
2014-07-14 15:18:02 +08:00
|
|
|
it "escapes bold contents" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic_bold.fancy_title).to eq("Topic with <b>bold</b> text in its title")
|
2014-07-14 15:18:02 +08:00
|
|
|
end
|
2014-04-19 03:01:21 +08:00
|
|
|
|
2014-07-14 15:18:02 +08:00
|
|
|
it "escapes image contents" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic_image.fancy_title).to eq("Topic with <img src=‘something’> image in its title")
|
2013-02-20 05:08:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'fancy title' do
|
2013-04-22 11:48:05 +08:00
|
|
|
let(:topic) { Fabricate.build(:topic, title: "\"this topic\" -- has ``fancy stuff''" ) }
|
2013-02-20 05:08:23 +08:00
|
|
|
|
|
|
|
context 'title_fancy_entities disabled' do
|
|
|
|
before do
|
2015-09-24 11:37:53 +08:00
|
|
|
SiteSetting.title_fancy_entities = false
|
2013-02-20 05:08:23 +08:00
|
|
|
end
|
|
|
|
|
2014-04-18 12:48:38 +08:00
|
|
|
it "doesn't add entities to the title" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.fancy_title).to eq(""this topic" -- has ``fancy stuff''")
|
2013-02-20 05:08:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'title_fancy_entities enabled' do
|
|
|
|
before do
|
2015-09-24 11:37:53 +08:00
|
|
|
SiteSetting.title_fancy_entities = true
|
2013-02-20 05:08:23 +08:00
|
|
|
end
|
|
|
|
|
2015-09-24 11:37:53 +08:00
|
|
|
it "converts the title to have fancy entities and updates" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.fancy_title).to eq("“this topic” – has “fancy stuff”")
|
2015-09-24 11:37:53 +08:00
|
|
|
topic.title = "this is my test hello world... yay"
|
|
|
|
topic.user.save!
|
|
|
|
topic.save!
|
|
|
|
topic.reload
|
|
|
|
expect(topic.fancy_title).to eq("This is my test hello world… yay")
|
|
|
|
|
|
|
|
topic.title = "I made a change to the title"
|
|
|
|
topic.save!
|
|
|
|
|
|
|
|
topic.reload
|
|
|
|
expect(topic.fancy_title).to eq("I made a change to the title")
|
|
|
|
|
|
|
|
# another edge case
|
|
|
|
topic.title = "this is another edge case"
|
|
|
|
expect(topic.fancy_title).to eq("this is another edge case")
|
2013-02-20 05:08:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-09 02:40:31 +08:00
|
|
|
context 'category validation' do
|
|
|
|
context 'allow_uncategorized_topics is false' do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:allow_uncategorized_topics).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow nil category" do
|
2013-10-24 07:05:51 +08:00
|
|
|
topic = Fabricate.build(:topic, category: nil)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic).not_to be_valid
|
|
|
|
expect(topic.errors[:category_id]).to be_present
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
|
|
|
|
2013-10-25 06:33:18 +08:00
|
|
|
it "allows PMs" do
|
|
|
|
topic = Fabricate.build(:topic, category: nil, archetype: Archetype.private_message)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic).to be_valid
|
2013-10-25 06:33:18 +08:00
|
|
|
end
|
|
|
|
|
2013-10-09 02:40:31 +08:00
|
|
|
it 'passes for topics with a category' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Fabricate.build(:topic, category: Fabricate(:category))).to be_valid
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'allow_uncategorized_topics is true' do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:allow_uncategorized_topics).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes for topics with nil category" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Fabricate.build(:topic, category: nil)).to be_valid
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes for topics with a category' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Fabricate.build(:topic, category: Fabricate(:category))).to be_valid
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-15 02:45:29 +08:00
|
|
|
context 'similar_to' do
|
|
|
|
|
|
|
|
it 'returns blank with nil params' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.similar_to(nil, nil)).to be_blank
|
2013-03-15 02:45:29 +08:00
|
|
|
end
|
|
|
|
|
2014-04-15 03:20:41 +08:00
|
|
|
context "with a category definition" do
|
|
|
|
let!(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it "excludes the category definition topic from similar_to" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.similar_to('category definition for', "no body")).to be_blank
|
2014-04-15 03:20:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-15 02:45:29 +08:00
|
|
|
context 'with a similar topic' do
|
2014-08-08 10:12:53 +08:00
|
|
|
let!(:topic) {
|
|
|
|
ActiveRecord::Base.observers.enable :search_observer
|
|
|
|
post = create_post(title: "Evil trout is the dude who posted this topic")
|
|
|
|
post.topic
|
|
|
|
}
|
2013-03-15 02:45:29 +08:00
|
|
|
|
|
|
|
it 'returns the similar topic if the title is similar' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?")).to eq([topic])
|
2013-03-15 02:45:29 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 01:43:59 +08:00
|
|
|
context "secure categories" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2013-07-14 09:24:16 +08:00
|
|
|
let(:category) { Fabricate(:category, read_restricted: true) }
|
2013-06-13 01:43:59 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
topic.category = category
|
|
|
|
topic.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return topics from private categories" do
|
|
|
|
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the cat since the user can see it" do
|
|
|
|
Guardian.any_instance.expects(:secure_category_ids).returns([category.id])
|
|
|
|
expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?", user)).to include(topic)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-15 02:45:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'post_numbers' do
|
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, user: topic.user) }
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, user: topic.user) }
|
|
|
|
let!(:p3) { Fabricate(:post, topic: topic, user: topic.user) }
|
|
|
|
|
|
|
|
it "returns the post numbers of the topic" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.post_numbers).to eq([1, 2, 3])
|
2013-02-06 03:16:51 +08:00
|
|
|
p2.destroy
|
2013-04-22 11:48:05 +08:00
|
|
|
topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.post_numbers).to eq([1, 3])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'private message' do
|
2014-05-06 21:41:59 +08:00
|
|
|
let(:coding_horror) { User.find_by(username: "CodingHorror") }
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:evil_trout) { Fabricate(:evil_trout) }
|
2013-05-14 09:59:55 +08:00
|
|
|
let(:topic) { Fabricate(:private_message_topic) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it "should integrate correctly" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Guardian.new(topic.user).can_see?(topic)).to eq(true)
|
|
|
|
expect(Guardian.new.can_see?(topic)).to eq(false)
|
|
|
|
expect(Guardian.new(evil_trout).can_see?(topic)).to eq(false)
|
|
|
|
expect(Guardian.new(coding_horror).can_see?(topic)).to eq(true)
|
|
|
|
expect(TopicQuery.new(evil_trout).list_latest.topics).not_to include(topic)
|
2013-04-22 11:48:05 +08:00
|
|
|
|
|
|
|
# invites
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.invite(topic.user, 'duhhhhh')).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'invite' do
|
|
|
|
|
|
|
|
context 'existing user' do
|
|
|
|
let(:walter) { Fabricate(:walter_white) }
|
|
|
|
|
|
|
|
context 'by username' do
|
|
|
|
|
2013-06-18 15:17:01 +08:00
|
|
|
it 'adds and removes walter to the allowed users' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.invite(topic.user, walter.username)).to eq(true)
|
|
|
|
expect(topic.allowed_users.include?(walter)).to eq(true)
|
2013-06-18 15:17:01 +08:00
|
|
|
|
2016-01-11 19:42:06 +08:00
|
|
|
expect(topic.remove_allowed_user(topic.user, walter.username)).to eq(true)
|
2013-06-18 15:17:01 +08:00
|
|
|
topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.allowed_users.include?(walter)).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a notification' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { topic.invite(topic.user, walter.username) }.to change(Notification, :count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2016-01-11 19:42:06 +08:00
|
|
|
|
|
|
|
it 'creates a small action post' do
|
|
|
|
expect { topic.invite(topic.user, walter.username) }.to change(Post, :count)
|
|
|
|
expect { topic.remove_allowed_user(topic.user, walter.username) }.to change(Post, :count)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'by email' do
|
|
|
|
|
2014-05-09 09:45:18 +08:00
|
|
|
it 'adds user correctly' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
|
|
|
expect(topic.invite(topic.user, walter.email)).to eq(true)
|
|
|
|
}.to change(Notification, :count)
|
|
|
|
expect(topic.allowed_users.include?(walter)).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context "user actions" do
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:actions) { topic.user.user_actions }
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it "should set up actions correctly" do
|
2013-05-14 09:59:55 +08:00
|
|
|
ActiveRecord::Base.observers.enable :all
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(actions.map{|a| a.action_type}).not_to include(UserAction::NEW_TOPIC)
|
|
|
|
expect(actions.map{|a| a.action_type}).to include(UserAction::NEW_PRIVATE_MESSAGE)
|
|
|
|
expect(coding_horror.user_actions.map{|a| a.action_type}).to include(UserAction::GOT_PRIVATE_MESSAGE)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-04-22 11:48:05 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-13 15:04:05 +08:00
|
|
|
it "rate limits topic invitations" do
|
|
|
|
SiteSetting.stubs(:max_topic_invitations_per_day).returns(2)
|
|
|
|
RateLimiter.stubs(:disabled?).returns(false)
|
|
|
|
RateLimiter.clear_all!
|
|
|
|
|
|
|
|
start = Time.now.tomorrow.beginning_of_day
|
|
|
|
freeze_time(start)
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
freeze_time(start + 10.minutes)
|
|
|
|
topic.invite(topic.user, user.username)
|
|
|
|
|
|
|
|
freeze_time(start + 20.minutes)
|
|
|
|
topic.invite(topic.user, "walter@white.com")
|
|
|
|
|
|
|
|
freeze_time(start + 30.minutes)
|
|
|
|
|
|
|
|
expect {
|
|
|
|
topic.invite(topic.user, "user@example.com")
|
|
|
|
}.to raise_exception
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context 'bumping topics' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@topic = Fabricate(:topic, bumped_at: 1.year.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the bumped_at field when a new post is made' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.bumped_at).to be_present
|
|
|
|
expect {
|
2013-07-22 13:06:53 +08:00
|
|
|
create_post(topic: @topic, user: @topic.user)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to change(@topic, :bumped_at)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'editing posts' do
|
|
|
|
before do
|
|
|
|
@earlier_post = Fabricate(:post, topic: @topic, user: @topic.user)
|
|
|
|
@last_post = Fabricate(:post, topic: @topic, user: @topic.user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't bump the topic on an edit to the last post that doesn't result in a new version" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2015-11-25 03:28:42 +08:00
|
|
|
SiteSetting.expects(:editing_grace_period).returns(5.minutes)
|
2014-10-28 05:06:43 +08:00
|
|
|
@last_post.revise(@last_post.user, { raw: 'updated contents' }, revised_at: @last_post.created_at + 10.seconds)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
}.not_to change(@topic, :bumped_at)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "bumps the topic when a new version is made of the last post" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2014-10-28 05:06:43 +08:00
|
|
|
@last_post.revise(Fabricate(:moderator), { raw: 'updated contents' })
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to change(@topic, :bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't bump the topic when a post that isn't the last post receives a new version" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2014-10-28 05:06:43 +08:00
|
|
|
@earlier_post.revise(Fabricate(:moderator), { raw: 'updated contents' })
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
}.not_to change(@topic, :bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'moderator posts' do
|
|
|
|
before do
|
|
|
|
@moderator = Fabricate(:moderator)
|
|
|
|
@topic = Fabricate(:topic)
|
|
|
|
@mod_post = @topic.add_moderator_post(@moderator, "Moderator did something. http://discourse.org", post_number: 999)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a moderator post' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@mod_post).to be_present
|
|
|
|
expect(@mod_post.post_type).to eq(Post.types[:moderator_action])
|
|
|
|
expect(@mod_post.post_number).to eq(999)
|
|
|
|
expect(@mod_post.sort_order).to eq(999)
|
|
|
|
expect(@topic.topic_links.count).to eq(1)
|
2013-04-22 11:48:05 +08:00
|
|
|
@topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
context 'update_status' do
|
|
|
|
before do
|
|
|
|
@topic = Fabricate(:topic, bumped_at: 1.hour.ago)
|
|
|
|
@topic.reload
|
|
|
|
@original_bumped_at = @topic.bumped_at.to_f
|
|
|
|
@user = @topic.user
|
2013-03-29 14:38:54 +08:00
|
|
|
@user.admin = true
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'visibility' do
|
|
|
|
context 'disable' do
|
2013-02-26 00:42:20 +08:00
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.update_status('visible', false, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it 'should not be visible and have correct counts' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).not_to be_visible
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'enable' do
|
|
|
|
before do
|
|
|
|
@topic.update_attribute :visible, false
|
|
|
|
@topic.update_status('visible', true, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it 'should be visible with correct counts' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).to be_visible
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'pinned' do
|
|
|
|
context 'disable' do
|
|
|
|
before do
|
|
|
|
@topic.update_status('pinned', false, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it "doesn't have a pinned_at but has correct dates" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.pinned_at).to be_blank
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'enable' do
|
|
|
|
before do
|
2013-03-07 04:17:07 +08:00
|
|
|
@topic.update_attribute :pinned_at, nil
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.update_status('pinned', true, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it 'should enable correctly' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.pinned_at).to be_present
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'archived' do
|
|
|
|
context 'disable' do
|
|
|
|
before do
|
|
|
|
@topic.update_status('archived', false, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
it 'should archive correctly' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).not_to be_archived
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'enable' do
|
|
|
|
before do
|
|
|
|
@topic.update_attribute :archived, false
|
|
|
|
@topic.update_status('archived', true, @user)
|
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be archived' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).to be_archived
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-04-22 11:48:05 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 02:25:41 +08:00
|
|
|
shared_examples_for 'a status that closes a topic' do
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'disable' do
|
|
|
|
before do
|
2013-05-08 02:25:41 +08:00
|
|
|
@topic.update_status(status, false, @user)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be pinned' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).not_to be_closed
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
|
|
|
expect(@topic.bumped_at.to_f).not_to eq(@original_bumped_at)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'enable' do
|
|
|
|
before do
|
|
|
|
@topic.update_attribute :closed, false
|
2013-05-08 02:25:41 +08:00
|
|
|
@topic.update_status(status, true, @user)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be closed' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic).to be_closed
|
|
|
|
expect(@topic.bumped_at.to_f).to eq(@original_bumped_at)
|
|
|
|
expect(@topic.moderator_posts_count).to eq(1)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 02:25:41 +08:00
|
|
|
context 'closed' do
|
|
|
|
let(:status) { 'closed' }
|
|
|
|
it_should_behave_like 'a status that closes a topic'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'autoclosed' do
|
|
|
|
let(:status) { 'autoclosed' }
|
|
|
|
it_should_behave_like 'a status that closes a topic'
|
2013-05-28 08:59:43 +08:00
|
|
|
|
2013-06-07 05:04:10 +08:00
|
|
|
context 'topic was set to close when it was created' do
|
|
|
|
it 'puts the autoclose duration in the moderator post' do
|
2014-03-10 07:11:54 +08:00
|
|
|
freeze_time(Time.new(2000,1,1)) do
|
|
|
|
@topic.created_at = 3.days.ago
|
|
|
|
@topic.update_status(status, true, @user)
|
|
|
|
expect(@topic.posts.last.raw).to include "closed after 3 days"
|
|
|
|
end
|
2013-06-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-28 08:59:43 +08:00
|
|
|
|
2013-06-07 05:04:10 +08:00
|
|
|
context 'topic was set to close after it was created' do
|
|
|
|
it 'puts the autoclose duration in the moderator post' do
|
2014-03-10 07:11:54 +08:00
|
|
|
freeze_time(Time.new(2000,1,1)) do
|
|
|
|
@topic.created_at = 7.days.ago
|
|
|
|
freeze_time(2.days.ago) do
|
|
|
|
@topic.set_auto_close(48)
|
|
|
|
end
|
|
|
|
@topic.update_status(status, true, @user)
|
|
|
|
expect(@topic.posts.last.raw).to include "closed after 2 days"
|
2013-06-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-28 08:59:43 +08:00
|
|
|
end
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
describe "banner" do
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:user) { topic.user }
|
|
|
|
let(:banner) { { html: "<p>BANNER</p>", url: topic.url, key: topic.id } }
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
before { topic.stubs(:banner).returns(banner) }
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
describe "make_banner!" do
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
it "changes the topic archetype to 'banner'" do
|
2015-05-04 10:21:00 +08:00
|
|
|
messages = MessageBus.track_publish do
|
2014-11-14 15:10:52 +08:00
|
|
|
topic.make_banner!(user)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.archetype).to eq(Archetype.banner)
|
2014-11-14 15:10:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
channels = messages.map(&:channel)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(channels).to include('/site/banner')
|
|
|
|
expect(channels).to include('/distributed_hash')
|
2014-06-19 02:04:10 +08:00
|
|
|
end
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
it "ensures only one banner topic at all time" do
|
2014-11-14 15:10:52 +08:00
|
|
|
_banner_topic = Fabricate(:banner_topic)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.where(archetype: Archetype.banner).count).to eq(1)
|
2014-06-17 01:21:21 +08:00
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
topic.make_banner!(user)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.where(archetype: Archetype.banner).count).to eq(1)
|
2014-06-19 02:04:10 +08:00
|
|
|
end
|
2014-06-17 01:21:21 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
describe "remove_banner!" do
|
|
|
|
|
|
|
|
it "resets the topic archetype" do
|
|
|
|
topic.expects(:add_moderator_post)
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.expects(:publish).with("/site/banner", nil)
|
2014-06-19 02:04:10 +08:00
|
|
|
topic.remove_banner!(user)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.archetype).to eq(Archetype.default)
|
2014-06-19 02:04:10 +08:00
|
|
|
end
|
|
|
|
|
2014-06-17 01:21:21 +08:00
|
|
|
end
|
|
|
|
|
2014-06-19 02:04:10 +08:00
|
|
|
|
2014-06-17 01:21:21 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'last_poster info' do
|
|
|
|
|
|
|
|
before do
|
2013-07-22 13:06:53 +08:00
|
|
|
@post = create_post
|
|
|
|
@user = @post.user
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic = @post.topic
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'initially has the last_post_user_id of the OP' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.last_post_user_id).to eq(@user.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'after a second post' do
|
|
|
|
before do
|
|
|
|
@second_user = Fabricate(:coding_horror)
|
2013-07-22 13:06:53 +08:00
|
|
|
@new_post = create_post(topic: @topic, user: @second_user)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the last_post_user_id to the second_user' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.last_post_user_id).to eq(@second_user.id)
|
|
|
|
expect(@topic.last_posted_at.to_i).to eq(@new_post.created_at.to_i)
|
2014-05-06 21:41:59 +08:00
|
|
|
topic_user = @second_user.topic_users.find_by(topic_id: @topic.id)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic_user.posted?).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-22 11:48:05 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with category' do
|
2014-10-28 05:06:43 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
|
|
|
@category = Fabricate(:category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not increase the topic_count with no category" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:topic, user: @category.user); @category.reload }.not_to change(@category, :topic_count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should increase the category's topic_count" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { Fabricate(:topic, user: @category.user, category_id: @category.id); @category.reload }.to change(@category, :topic_count).by(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'meta data' do
|
2013-07-24 02:42:52 +08:00
|
|
|
let(:topic) { Fabricate(:topic, meta_data: {'hello' => 'world'}) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'allows us to create a topic with meta data' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.meta_data['hello']).to eq('world')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'updating' do
|
|
|
|
|
|
|
|
context 'existing key' do
|
|
|
|
before do
|
2013-07-24 02:42:52 +08:00
|
|
|
topic.update_meta_data('hello' => 'bane')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the key' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.meta_data['hello']).to eq('bane')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'new key' do
|
|
|
|
before do
|
2013-07-24 02:42:52 +08:00
|
|
|
topic.update_meta_data('city' => 'gotham')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds the new key' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.meta_data['city']).to eq('gotham')
|
|
|
|
expect(topic.meta_data['hello']).to eq('world')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-26 00:24:22 +08:00
|
|
|
context 'new key' do
|
|
|
|
before do
|
|
|
|
topic.update_meta_data('other' => 'key')
|
|
|
|
topic.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be loaded" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.find(topic.id).meta_data["other"]).to eq("key")
|
2014-04-26 00:24:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is in sync with custom_fields" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.find(topic.id).custom_fields["other"]).to eq("key")
|
2014-04-26 00:24:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'after create' do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it 'is a regular topic by default' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.archetype).to eq(Archetype.default)
|
|
|
|
expect(topic.has_summary).to eq(false)
|
|
|
|
expect(topic.percent_rank).to eq(1.0)
|
|
|
|
expect(topic).to be_visible
|
|
|
|
expect(topic.pinned_at).to be_blank
|
|
|
|
expect(topic).not_to be_closed
|
|
|
|
expect(topic).not_to be_archived
|
|
|
|
expect(topic.moderator_posts_count).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'post' do
|
|
|
|
let(:post) { Fabricate(:post, topic: topic, user: topic.user) }
|
|
|
|
|
|
|
|
it 'has the same archetype as the topic' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(post.archetype).to eq(topic.archetype)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'change_category' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
@topic = Fabricate(:topic)
|
2013-02-26 00:42:20 +08:00
|
|
|
@category = Fabricate(:category, user: @topic.user)
|
|
|
|
@user = @topic.user
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'without a previous category' do
|
|
|
|
|
|
|
|
it 'should not change the topic_count when not changed' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { @topic.change_category_to_id(@topic.category.id); @category.reload }.not_to change(@category, :topic_count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'changed category' do
|
|
|
|
before do
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(@category.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'changes the category' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.category).to eq(@category)
|
|
|
|
expect(@category.topic_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change the category when it can't be found" do
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(12312312)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.category_id).to eq(SiteSetting.uncategorized_category_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a previous category' do
|
|
|
|
before do
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(@category.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increases the topic_count' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@category.topic_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change the topic_count when the value doesn't change" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect { @topic.change_category_to_id(@category.id); @category.reload }.not_to change(@category, :topic_count)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't reset the category when given a name that doesn't exist" do
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(55556)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.category_id).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'to a different category' do
|
|
|
|
before do
|
|
|
|
@new_category = Fabricate(:category, user: @user, name: '2nd category')
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(@new_category.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
@new_category.reload
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should increase the new category's topic count" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@new_category.topic_count).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should lower the original category's topic count" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@category.topic_count).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when allow_uncategorized_topics is false' do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:allow_uncategorized_topics).returns(false)
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-10-09 02:40:31 +08:00
|
|
|
let!(:topic) { Fabricate(:topic, category: Fabricate(:category)) }
|
|
|
|
|
|
|
|
it 'returns false' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.change_category_to_id(nil)).to eq(false) # don't use "== false" here because it would also match nil
|
2013-10-09 02:40:31 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when the category exists' do
|
|
|
|
before do
|
2014-07-17 03:39:39 +08:00
|
|
|
@topic.change_category_to_id(nil)
|
2013-02-26 00:42:20 +08:00
|
|
|
@category.reload
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "resets the category" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(@topic.category_id).to eq(SiteSetting.uncategorized_category_id)
|
|
|
|
expect(@category.topic_count).to eq(0)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-28 11:36:12 +08:00
|
|
|
describe 'scopes' do
|
|
|
|
describe '#by_most_recently_created' do
|
|
|
|
it 'returns topics ordered by created_at desc, id desc' do
|
|
|
|
now = Time.now
|
|
|
|
a = Fabricate(:topic, created_at: now - 2.minutes)
|
|
|
|
b = Fabricate(:topic, created_at: now)
|
|
|
|
c = Fabricate(:topic, created_at: now)
|
|
|
|
d = Fabricate(:topic, created_at: now - 2.minutes)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.by_newest).to eq([c,b,d,a])
|
2013-02-28 11:36:12 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-30 19:23:40 +08:00
|
|
|
|
|
|
|
describe '#created_since' do
|
|
|
|
it 'returns topics created after some date' do
|
|
|
|
now = Time.now
|
|
|
|
a = Fabricate(:topic, created_at: now - 2.minutes)
|
|
|
|
b = Fabricate(:topic, created_at: now - 1.minute)
|
|
|
|
c = Fabricate(:topic, created_at: now)
|
|
|
|
d = Fabricate(:topic, created_at: now + 1.minute)
|
|
|
|
e = Fabricate(:topic, created_at: now + 2.minutes)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.created_since(now)).not_to include a
|
|
|
|
expect(Topic.created_since(now)).not_to include b
|
|
|
|
expect(Topic.created_since(now)).not_to include c
|
|
|
|
expect(Topic.created_since(now)).to include d
|
|
|
|
expect(Topic.created_since(now)).to include e
|
2013-05-30 19:23:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#visible' do
|
|
|
|
it 'returns topics set as visible' do
|
|
|
|
a = Fabricate(:topic, visible: false)
|
|
|
|
b = Fabricate(:topic, visible: true)
|
|
|
|
c = Fabricate(:topic, visible: true)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.visible).not_to include a
|
|
|
|
expect(Topic.visible).to include b
|
|
|
|
expect(Topic.visible).to include c
|
2013-05-30 19:23:40 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-28 11:36:12 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 02:25:41 +08:00
|
|
|
describe 'auto-close' do
|
|
|
|
context 'a new topic' do
|
|
|
|
context 'auto_close_at is set' do
|
|
|
|
it 'queues a job to close the topic' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-12-07 05:39:35 +08:00
|
|
|
Jobs.expects(:enqueue_at).with(7.hours.from_now, :close_topic, all_of( has_key(:topic_id), has_key(:user_id) ))
|
2014-10-11 00:21:44 +08:00
|
|
|
topic = Fabricate(:topic, user: Fabricate(:admin))
|
|
|
|
topic.set_auto_close(7).save
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_user_id is nil, it will use the topic creator as the topic closer' do
|
|
|
|
topic_creator = Fabricate(:admin)
|
|
|
|
Jobs.expects(:enqueue_at).with do |datetime, job_name, job_args|
|
|
|
|
job_args[:user_id] == topic_creator.id
|
|
|
|
end
|
2014-10-11 00:21:44 +08:00
|
|
|
topic = Fabricate(:topic, user: topic_creator)
|
|
|
|
topic.set_auto_close(7).save
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_user_id is set, it will use it as the topic closer' do
|
|
|
|
topic_creator = Fabricate(:admin)
|
|
|
|
topic_closer = Fabricate(:user, admin: true)
|
|
|
|
Jobs.expects(:enqueue_at).with do |datetime, job_name, job_args|
|
|
|
|
job_args[:user_id] == topic_closer.id
|
|
|
|
end
|
2014-10-11 00:21:44 +08:00
|
|
|
topic = Fabricate(:topic, user: topic_creator)
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(7, {by_user: topic_closer}).save
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
2013-05-16 03:19:41 +08:00
|
|
|
|
|
|
|
it "ignores the category's default auto-close" do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-12-07 05:39:35 +08:00
|
|
|
Jobs.expects(:enqueue_at).with(7.hours.from_now, :close_topic, all_of( has_key(:topic_id), has_key(:user_id) ))
|
2014-10-11 00:21:44 +08:00
|
|
|
topic = Fabricate(:topic, user: Fabricate(:admin), ignore_category_auto_close: true, category_id: Fabricate(:category, auto_close_hours: 2).id)
|
|
|
|
topic.set_auto_close(7).save
|
2013-06-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the time when auto_close timer starts' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2014-10-11 00:21:44 +08:00
|
|
|
topic = Fabricate(:topic, user: Fabricate(:admin))
|
|
|
|
topic.set_auto_close(7).save
|
2014-03-07 18:38:24 +08:00
|
|
|
expect(topic.auto_close_started_at).to eq(now)
|
2013-05-16 03:19:41 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an existing topic' do
|
|
|
|
it 'when auto_close_at is set, it queues a job to close the topic' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id))
|
|
|
|
topic.auto_close_at = 12.hours.from_now
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_at and auto_closer_user_id are set, it queues a job to close the topic' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
topic = Fabricate(:topic)
|
|
|
|
closer = Fabricate(:admin)
|
|
|
|
Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: closer.id))
|
|
|
|
topic.auto_close_at = 12.hours.from_now
|
|
|
|
topic.auto_close_user = closer
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_at is removed, it cancels the job to close the topic' do
|
|
|
|
Jobs.stubs(:enqueue_at).returns(true)
|
|
|
|
topic = Fabricate(:topic, auto_close_at: 1.day.from_now)
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(:close_topic, {topic_id: topic.id})
|
|
|
|
topic.auto_close_at = nil
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
|
|
|
expect(topic.auto_close_user).to eq(nil)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_user is removed, it updates the job' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
Jobs.stubs(:enqueue_at).with(1.day.from_now, :close_topic, anything).returns(true)
|
|
|
|
topic = Fabricate(:topic, auto_close_at: 1.day.from_now, auto_close_user: Fabricate(:admin))
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(:close_topic, {topic_id: topic.id})
|
|
|
|
Jobs.expects(:enqueue_at).with(1.day.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id))
|
|
|
|
topic.auto_close_user = nil
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_at value is changed, it reschedules the job' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
Jobs.stubs(:enqueue_at).returns(true)
|
|
|
|
topic = Fabricate(:topic, auto_close_at: 1.day.from_now)
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(:close_topic, {topic_id: topic.id})
|
|
|
|
Jobs.expects(:enqueue_at).with(3.days.from_now, :close_topic, has_entry(topic_id: topic.id))
|
|
|
|
topic.auto_close_at = 3.days.from_now
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_user_id is changed, it updates the job' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
admin = Fabricate(:admin)
|
|
|
|
Jobs.stubs(:enqueue_at).returns(true)
|
|
|
|
topic = Fabricate(:topic, auto_close_at: 1.day.from_now)
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(:close_topic, {topic_id: topic.id})
|
|
|
|
Jobs.expects(:enqueue_at).with(1.day.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: admin.id))
|
|
|
|
topic.auto_close_user = admin
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when auto_close_at and auto_close_user_id are not changed, it should not schedule another CloseTopic job' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-05-08 02:25:41 +08:00
|
|
|
Jobs.expects(:enqueue_at).with(1.day.from_now, :close_topic, has_key(:topic_id)).once.returns(true)
|
|
|
|
Jobs.expects(:cancel_scheduled_job).never
|
|
|
|
topic = Fabricate(:topic, auto_close_at: 1.day.from_now)
|
|
|
|
topic.title = 'A new title that is long enough'
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-16 03:19:41 +08:00
|
|
|
|
|
|
|
it "ignores the category's default auto-close" do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-09-06 15:28:37 +08:00
|
|
|
mod = Fabricate(:moderator)
|
|
|
|
# NOTE, only moderators can auto-close, if missing system user is used
|
2013-12-07 05:39:35 +08:00
|
|
|
topic = Fabricate(:topic, category: Fabricate(:category, auto_close_hours: 14), user: mod)
|
2013-05-16 03:19:41 +08:00
|
|
|
Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id))
|
|
|
|
topic.auto_close_at = 12.hours.from_now
|
2013-11-11 07:52:44 +08:00
|
|
|
topic.save
|
|
|
|
|
|
|
|
topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.closed).to eq(false)
|
2013-11-11 07:52:44 +08:00
|
|
|
|
|
|
|
Timecop.freeze(24.hours.from_now) do
|
|
|
|
Topic.auto_close
|
|
|
|
topic.reload
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.closed).to eq(true)
|
2013-11-11 07:52:44 +08:00
|
|
|
end
|
|
|
|
|
2013-05-16 03:19:41 +08:00
|
|
|
end
|
|
|
|
end
|
2013-05-08 02:25:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-07 05:04:10 +08:00
|
|
|
describe 'set_auto_close' do
|
|
|
|
let(:topic) { Fabricate.build(:topic) }
|
2014-10-11 00:21:44 +08:00
|
|
|
let(:closing_topic) { Fabricate.build(:topic, auto_close_hours: 5, auto_close_at: 5.hours.from_now, auto_close_started_at: 5.hours.from_now) }
|
2013-06-07 05:04:10 +08:00
|
|
|
let(:admin) { Fabricate.build(:user, id: 123) }
|
2014-11-27 02:51:07 +08:00
|
|
|
let(:trust_level_4) { Fabricate.build(:trust_level_4) }
|
2013-06-07 05:04:10 +08:00
|
|
|
|
|
|
|
before { Discourse.stubs(:system_user).returns(admin) }
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it 'can take a number of hours as an integer' do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(72, {by_user: admin})
|
|
|
|
expect(topic.auto_close_at).to eq(3.days.from_now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can take a number of hours as an integer, with timezone offset' do
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
topic.set_auto_close(72, {by_user: admin, timezone_offset: 240})
|
2013-06-07 05:04:10 +08:00
|
|
|
expect(topic.auto_close_at).to eq(3.days.from_now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it 'can take a number of hours as a string' do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('18', {by_user: admin})
|
|
|
|
expect(topic.auto_close_at).to eq(18.hours.from_now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can take a number of hours as a string, with timezone offset' do
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
topic.set_auto_close('18', {by_user: admin, timezone_offset: 240})
|
2013-11-27 08:06:20 +08:00
|
|
|
expect(topic.auto_close_at).to eq(18.hours.from_now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can take a time later in the day" do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('13:00', {by_user: admin})
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,20,13,0))
|
2013-11-27 08:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-28 00:22:34 +08:00
|
|
|
it "can take a time later in the day, with timezone offset" do
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
topic.set_auto_close('13:00', {by_user: admin, timezone_offset: 240})
|
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,20,17,0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it "can take a time for the next day" do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('5:00', {by_user: admin})
|
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,21,5,0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can take a time for the next day, with timezone offset" do
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
topic.set_auto_close('1:00', {by_user: admin, timezone_offset: 240})
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,21,5,0))
|
2013-11-27 08:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can take a timestamp for a future time" do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('2013-11-22 5:00', {by_user: admin})
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,22,5,0))
|
2013-11-27 08:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-28 00:22:34 +08:00
|
|
|
it "can take a timestamp for a future time, with timezone offset" do
|
|
|
|
Timecop.freeze(now) do
|
|
|
|
topic.set_auto_close('2013-11-22 5:00', {by_user: admin, timezone_offset: 240})
|
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,22,9,0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it "sets a validation error when given a timestamp in the past" do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('2013-11-19 5:00', {by_user: admin})
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.auto_close_at).to eq(Time.zone.local(2013,11,19,5,0))
|
|
|
|
expect(topic.errors[:auto_close_at]).to be_present
|
2013-11-27 08:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can take a timestamp with timezone" do
|
2014-03-06 19:44:52 +08:00
|
|
|
Timecop.freeze(now) do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close('2013-11-25T01:35:00-08:00', {by_user: admin})
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.auto_close_at).to eq(Time.utc(2013,11,25,9,35))
|
2013-11-27 08:06:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-27 02:51:07 +08:00
|
|
|
it 'sets auto_close_user to given user if it is a staff or TL4 user' do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(3, {by_user: admin})
|
2013-06-07 05:04:10 +08:00
|
|
|
expect(topic.auto_close_user_id).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
2014-11-27 02:51:07 +08:00
|
|
|
it 'sets auto_close_user to given user if it is a TL4 user' do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(3, {by_user: trust_level_4})
|
2014-11-27 02:51:07 +08:00
|
|
|
expect(topic.auto_close_user_id).to eq(trust_level_4.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets auto_close_user to system user if given user is not staff or a TL4 user' do
|
2015-05-28 00:22:34 +08:00
|
|
|
topic.set_auto_close(3, {by_user: Fabricate.build(:user, id: 444)})
|
2013-06-07 05:04:10 +08:00
|
|
|
expect(topic.auto_close_user_id).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
2014-11-27 02:51:07 +08:00
|
|
|
it 'sets auto_close_user to system user if user is not given and topic creator is not staff nor TL4 user' do
|
2013-06-07 05:04:10 +08:00
|
|
|
topic.set_auto_close(3)
|
|
|
|
expect(topic.auto_close_user_id).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets auto_close_user to topic creator if it is a staff user' do
|
|
|
|
staff_topic = Fabricate.build(:topic, user: Fabricate.build(:admin, id: 999))
|
|
|
|
staff_topic.set_auto_close(3)
|
|
|
|
expect(staff_topic.auto_close_user_id).to eq(999)
|
|
|
|
end
|
|
|
|
|
2014-11-27 02:51:07 +08:00
|
|
|
it 'sets auto_close_user to topic creator if it is a TL4 user' do
|
|
|
|
tl4_topic = Fabricate.build(:topic, user: Fabricate.build(:trust_level_4, id: 998))
|
|
|
|
tl4_topic.set_auto_close(3)
|
|
|
|
expect(tl4_topic.auto_close_user_id).to eq(998)
|
|
|
|
end
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it 'clears auto_close_at if arg is nil' do
|
2013-06-07 05:04:10 +08:00
|
|
|
closing_topic.set_auto_close(nil)
|
|
|
|
expect(closing_topic.auto_close_at).to be_nil
|
|
|
|
end
|
|
|
|
|
2013-11-27 08:06:20 +08:00
|
|
|
it 'clears auto_close_started_at if arg is nil' do
|
2013-06-07 05:04:10 +08:00
|
|
|
closing_topic.set_auto_close(nil)
|
|
|
|
expect(closing_topic.auto_close_started_at).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates auto_close_at if it was already set to close' do
|
2014-03-07 18:38:24 +08:00
|
|
|
Timecop.freeze(now) do
|
2013-11-27 08:06:20 +08:00
|
|
|
closing_topic.set_auto_close(48)
|
|
|
|
expect(closing_topic.auto_close_at).to eq(2.days.from_now)
|
2013-06-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update auto_close_started_at if it was already set to close' do
|
|
|
|
expect{
|
|
|
|
closing_topic.set_auto_close(14)
|
|
|
|
}.to_not change(closing_topic, :auto_close_started_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 04:05:06 +08:00
|
|
|
describe 'for_digest' do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
|
|
|
|
it "returns none when there are no topics" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
2013-11-07 04:05:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return category topics" do
|
|
|
|
Fabricate(:category)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
2013-11-07 04:05:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns regular topics" do
|
|
|
|
topic = Fabricate(:topic)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to eq([topic])
|
2013-11-07 04:05:06 +08:00
|
|
|
end
|
|
|
|
|
2014-11-03 23:57:50 +08:00
|
|
|
it "doesn't return topics from muted categories" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
category = Fabricate(:category)
|
2015-08-19 05:15:46 +08:00
|
|
|
Fabricate(:topic, category: category)
|
2014-11-03 23:57:50 +08:00
|
|
|
|
|
|
|
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:muted], category.id)
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
2014-11-03 23:57:50 +08:00
|
|
|
end
|
|
|
|
|
2015-01-29 23:40:26 +08:00
|
|
|
it "doesn't return topics from TL0 users" do
|
|
|
|
new_user = Fabricate(:user, trust_level: 0)
|
2015-08-19 05:15:46 +08:00
|
|
|
Fabricate(:topic, user_id: new_user.id)
|
2015-01-29 23:40:26 +08:00
|
|
|
|
|
|
|
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank
|
|
|
|
end
|
|
|
|
|
2013-11-07 04:05:06 +08:00
|
|
|
end
|
|
|
|
|
2013-06-08 21:52:06 +08:00
|
|
|
describe 'secured' do
|
|
|
|
it 'can remove secure groups' do
|
2013-07-14 09:24:16 +08:00
|
|
|
category = Fabricate(:category, read_restricted: true)
|
2014-03-07 15:59:47 +08:00
|
|
|
Fabricate(:topic, category: category)
|
2013-06-08 21:52:06 +08:00
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.secured(Guardian.new(nil)).count).to eq(0)
|
|
|
|
expect(Topic.secured(Guardian.new(Fabricate(:admin))).count).to eq(2)
|
2013-06-08 21:52:06 +08:00
|
|
|
|
|
|
|
# for_digest
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.for_digest(Fabricate(:user), 1.year.ago).count).to eq(0)
|
|
|
|
expect(Topic.for_digest(Fabricate(:admin), 1.year.ago).count).to eq(1)
|
2013-06-08 21:52:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-30 01:39:30 +08:00
|
|
|
describe 'all_allowed_users' do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
let(:topic) { Fabricate(:topic, allowed_groups: [group]) }
|
|
|
|
let!(:allowed_user) { Fabricate(:user) }
|
|
|
|
let!(:allowed_group_user) { Fabricate(:user) }
|
|
|
|
let!(:moderator) { Fabricate(:user, moderator: true) }
|
|
|
|
let!(:rando) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
topic.allowed_users << allowed_user
|
|
|
|
group.users << allowed_group_user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes allowed_users' do
|
|
|
|
expect(topic.all_allowed_users).to include allowed_user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes allowed_group_users' do
|
|
|
|
expect(topic.all_allowed_users).to include allowed_group_user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes moderators if flagged and a pm' do
|
|
|
|
topic.stubs(:has_flags?).returns(true)
|
|
|
|
topic.stubs(:private_message?).returns(true)
|
|
|
|
expect(topic.all_allowed_users).to include moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include moderators if pm without flags' do
|
|
|
|
topic.stubs(:private_message?).returns(true)
|
|
|
|
expect(topic.all_allowed_users).not_to include moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include moderators for regular topic' do
|
|
|
|
expect(topic.all_allowed_users).not_to include moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not include randos' do
|
|
|
|
expect(topic.all_allowed_users).not_to include rando
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-30 22:06:15 +08:00
|
|
|
describe '#listable_count_per_day' do
|
|
|
|
before(:each) do
|
|
|
|
Timecop.freeze
|
|
|
|
Fabricate(:topic)
|
|
|
|
Fabricate(:topic, created_at: 1.day.ago)
|
|
|
|
Fabricate(:topic, created_at: 1.day.ago)
|
|
|
|
Fabricate(:topic, created_at: 2.days.ago)
|
|
|
|
Fabricate(:topic, created_at: 4.days.ago)
|
|
|
|
end
|
|
|
|
after(:each) do
|
|
|
|
Timecop.return
|
|
|
|
end
|
|
|
|
let(:listable_topics_count_per_day) { {1.day.ago.to_date => 2, 2.days.ago.to_date => 1, Time.now.utc.to_date => 1 } }
|
|
|
|
|
|
|
|
it 'collect closed interval listable topics count' do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.listable_count_per_day(2.days.ago, Time.now)).to include(listable_topics_count_per_day)
|
|
|
|
expect(Topic.listable_count_per_day(2.days.ago, Time.now)).not_to include({4.days.ago.to_date => 1})
|
2014-12-30 22:06:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-20 14:04:53 +08:00
|
|
|
describe '#secure_category?' do
|
|
|
|
let(:category){ Category.new }
|
|
|
|
|
|
|
|
it "is true if the category is secure" do
|
2013-07-14 09:24:16 +08:00
|
|
|
category.stubs(:read_restricted).returns(true)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.new(:category => category)).to be_read_restricted_category
|
2013-05-20 14:04:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if the category is not secure" do
|
2013-07-14 09:24:16 +08:00
|
|
|
category.stubs(:read_restricted).returns(false)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.new(:category => category)).not_to be_read_restricted_category
|
2013-05-20 14:04:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if there is no category" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(Topic.new(:category => nil)).not_to be_read_restricted_category
|
2013-05-20 14:04:53 +08:00
|
|
|
end
|
|
|
|
end
|
2013-07-09 03:23:20 +08:00
|
|
|
|
|
|
|
describe 'trash!' do
|
|
|
|
context "its category's topic count" do
|
2013-07-10 03:20:18 +08:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2013-07-09 03:23:20 +08:00
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it "subtracts 1 if topic is being deleted" do
|
|
|
|
topic = Fabricate(:topic, category: category)
|
2013-07-10 03:20:18 +08:00
|
|
|
expect { topic.trash!(moderator) }.to change { category.reload.topic_count }.by(-1)
|
2013-07-09 03:23:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't subtract 1 if topic is already deleted" do
|
|
|
|
topic = Fabricate(:topic, category: category, deleted_at: 1.day.ago)
|
2013-07-10 03:20:18 +08:00
|
|
|
expect { topic.trash!(moderator) }.to_not change { category.reload.topic_count }
|
2013-07-09 03:23:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'recover!' do
|
|
|
|
context "its category's topic count" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
it "adds 1 if topic is deleted" do
|
|
|
|
topic = Fabricate(:topic, category: category, deleted_at: 1.day.ago)
|
|
|
|
expect { topic.recover! }.to change { category.reload.topic_count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add 1 if topic is not deleted" do
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
expect { topic.recover! }.to_not change { category.reload.topic_count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-10 07:32:03 +08:00
|
|
|
|
|
|
|
it "limits new users to max_topics_in_first_day and max_posts_in_first_day" do
|
|
|
|
SiteSetting.stubs(:max_topics_in_first_day).returns(1)
|
|
|
|
SiteSetting.stubs(:max_replies_in_first_day).returns(1)
|
|
|
|
SiteSetting.stubs(:client_settings_json).returns(SiteSetting.client_settings_json_uncached)
|
2014-09-25 23:44:48 +08:00
|
|
|
RateLimiter.stubs(:rate_limit_create_topic).returns(100)
|
|
|
|
RateLimiter.stubs(:disabled?).returns(false)
|
2015-01-30 00:44:51 +08:00
|
|
|
RateLimiter.clear_all!
|
2013-10-10 07:32:03 +08:00
|
|
|
|
|
|
|
start = Time.now.tomorrow.beginning_of_day
|
|
|
|
|
|
|
|
freeze_time(start)
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
topic_id = create_post(user: user).topic_id
|
|
|
|
|
|
|
|
freeze_time(start + 10.minutes)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2013-10-10 07:32:03 +08:00
|
|
|
create_post(user: user)
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to raise_exception
|
2013-10-10 07:32:03 +08:00
|
|
|
|
|
|
|
freeze_time(start + 20.minutes)
|
|
|
|
create_post(user: user, topic_id: topic_id)
|
|
|
|
|
|
|
|
freeze_time(start + 30.minutes)
|
|
|
|
|
2015-01-06 00:04:23 +08:00
|
|
|
expect {
|
2013-10-10 07:32:03 +08:00
|
|
|
create_post(user: user, topic_id: topic_id)
|
2015-01-06 00:04:23 +08:00
|
|
|
}.to raise_exception
|
2013-10-10 07:32:03 +08:00
|
|
|
end
|
2013-10-28 14:12:07 +08:00
|
|
|
|
|
|
|
describe ".count_exceeds_minimun?" do
|
|
|
|
before { SiteSetting.stubs(:minimum_topics_similar).returns(20) }
|
|
|
|
|
|
|
|
context "when Topic count is geater than minimum_topics_similar" do
|
|
|
|
it "should be true" do
|
|
|
|
Topic.stubs(:count).returns(30)
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(Topic.count_exceeds_minimum?).to be_truthy
|
2013-10-28 14:12:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when topic's count is less than minimum_topics_similar" do
|
|
|
|
it "should be false" do
|
|
|
|
Topic.stubs(:count).returns(10)
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(Topic.count_exceeds_minimum?).to_not be_truthy
|
2013-10-28 14:12:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-02-27 08:45:20 +08:00
|
|
|
|
|
|
|
describe "calculate_avg_time" do
|
|
|
|
it "does not explode" do
|
|
|
|
Topic.calculate_avg_time
|
|
|
|
Topic.calculate_avg_time(1.day.ago)
|
|
|
|
end
|
|
|
|
end
|
2014-04-02 03:29:15 +08:00
|
|
|
|
|
|
|
describe "expandable_first_post?" do
|
|
|
|
|
2015-08-19 05:15:46 +08:00
|
|
|
let(:topic) { Fabricate.build(:topic) }
|
2014-04-02 03:29:15 +08:00
|
|
|
|
|
|
|
it "is false if embeddable_host is blank" do
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.expandable_first_post?).to eq(false)
|
2014-04-02 03:29:15 +08:00
|
|
|
end
|
|
|
|
|
2015-08-19 05:15:46 +08:00
|
|
|
describe 'with an emeddable host' do
|
|
|
|
before do
|
|
|
|
Fabricate(:embeddable_host)
|
|
|
|
SiteSetting.embed_truncate = true
|
|
|
|
topic.stubs(:has_topic_embed?).returns(true)
|
|
|
|
end
|
2014-04-02 03:29:15 +08:00
|
|
|
|
2015-08-19 05:15:46 +08:00
|
|
|
it "is true with the correct settings and topic_embed" do
|
|
|
|
expect(topic.expandable_first_post?).to eq(true)
|
|
|
|
end
|
|
|
|
it "is false if embed_truncate? is false" do
|
|
|
|
SiteSetting.embed_truncate = false
|
|
|
|
expect(topic.expandable_first_post?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if has_topic_embed? is false" do
|
|
|
|
topic.stubs(:has_topic_embed?).returns(false)
|
|
|
|
expect(topic.expandable_first_post?).to eq(false)
|
|
|
|
end
|
2014-04-02 03:29:15 +08:00
|
|
|
end
|
2015-08-19 05:15:46 +08:00
|
|
|
|
2014-04-26 00:24:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has custom fields" do
|
|
|
|
topic = Fabricate(:topic)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.custom_fields["a"]).to eq(nil)
|
2014-04-02 03:29:15 +08:00
|
|
|
|
2014-04-26 00:24:22 +08:00
|
|
|
topic.custom_fields["bob"] = "marley"
|
|
|
|
topic.custom_fields["jack"] = "black"
|
|
|
|
topic.save
|
2014-04-02 03:29:15 +08:00
|
|
|
|
2014-04-26 00:24:22 +08:00
|
|
|
topic = Topic.find(topic.id)
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.custom_fields).to eq({"bob" => "marley", "jack" => "black"})
|
2014-04-02 03:29:15 +08:00
|
|
|
end
|
2014-08-02 05:28:00 +08:00
|
|
|
|
|
|
|
it "doesn't validate the title again if it isn't changing" do
|
|
|
|
SiteSetting.stubs(:min_topic_title_length).returns(5)
|
|
|
|
topic = Fabricate(:topic, title: "Short")
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic).to be_valid
|
2014-08-02 05:28:00 +08:00
|
|
|
|
|
|
|
SiteSetting.stubs(:min_topic_title_length).returns(15)
|
|
|
|
topic.last_posted_at = 1.minute.ago
|
2015-01-06 00:04:23 +08:00
|
|
|
expect(topic.save).to eq(true)
|
2014-08-02 05:28:00 +08:00
|
|
|
end
|
2015-03-03 03:25:25 +08:00
|
|
|
|
|
|
|
context 'invite by group manager' do
|
|
|
|
let(:group_manager) { Fabricate(:user) }
|
2015-11-09 21:52:04 +08:00
|
|
|
let(:group) { Fabricate(:group).tap { |g| g.add_owner(group_manager) } }
|
2015-03-03 03:25:25 +08:00
|
|
|
let(:private_category) { Fabricate(:private_category, group: group) }
|
|
|
|
let(:group_private_topic) { Fabricate(:topic, category: private_category, user: group_manager) }
|
|
|
|
|
|
|
|
context 'to an email' do
|
|
|
|
let(:randolph) { 'randolph@duke.ooo' }
|
|
|
|
|
|
|
|
it "should attach group to the invite" do
|
|
|
|
invite = group_private_topic.invite(group_manager, randolph)
|
|
|
|
expect(invite.groups).to eq([group])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# should work for an existing user - give access, send notification
|
|
|
|
context 'to an existing user' do
|
|
|
|
let(:walter) { Fabricate(:walter_white) }
|
|
|
|
|
|
|
|
it "should add user to the group" do
|
|
|
|
expect(Guardian.new(walter).can_see?(group_private_topic)).to be_falsey
|
|
|
|
invite = group_private_topic.invite(group_manager, walter.email)
|
|
|
|
expect(invite).to be_nil
|
|
|
|
expect(walter.groups).to include(group)
|
|
|
|
expect(Guardian.new(walter).can_see?(group_private_topic)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-01-19 15:35:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "Correctly sets #message_archived?" do
|
|
|
|
topic = Fabricate(:private_message_topic)
|
|
|
|
user = topic.user
|
|
|
|
|
|
|
|
expect(topic.message_archived?(user)).to eq(false)
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
TopicAllowedGroup.create!(topic_id: topic.id, group_id: group.id)
|
|
|
|
GroupArchivedMessage.create!(topic_id: topic.id, group_id: group.id)
|
2015-03-03 03:25:25 +08:00
|
|
|
|
2016-01-19 15:35:46 +08:00
|
|
|
expect(topic.message_archived?(user)).to eq(true)
|
2015-03-03 03:25:25 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|