2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-03-18 17:52:29 -04:00
|
|
|
require_dependency 'post_destroyer'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe PostAction do
|
|
|
|
|
2013-04-13 10:31:20 -04:00
|
|
|
before do
|
|
|
|
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it { should belong_to :user }
|
|
|
|
it { should belong_to :post }
|
|
|
|
it { should belong_to :post_action_type }
|
|
|
|
it { should rate_limit }
|
|
|
|
|
2013-03-18 17:52:29 -04:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:codinghorror) { Fabricate(:coding_horror) }
|
|
|
|
let(:post) { Fabricate(:post) }
|
2013-03-01 07:07:44 -05:00
|
|
|
let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-04-12 03:55:45 -04:00
|
|
|
describe "messaging" do
|
2013-04-22 03:45:03 -04:00
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
it "notify moderators integration test" do
|
2013-04-22 03:45:03 -04:00
|
|
|
mod = moderator
|
|
|
|
action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], "this is my special long message");
|
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
posts = Post.joins(:topic)
|
|
|
|
.select('posts.id, topics.subtype')
|
|
|
|
.where('topics.archetype' => Archetype.private_message)
|
|
|
|
.to_a
|
2013-04-22 03:45:03 -04:00
|
|
|
|
|
|
|
posts.count.should == 1
|
|
|
|
action.related_post_id.should == posts[0].id.to_i
|
|
|
|
posts[0].subtype.should == TopicSubtype.notify_moderators
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
describe 'notify_moderators' do
|
|
|
|
before do
|
|
|
|
PostAction.stubs(:create)
|
2013-05-09 03:37:34 -04:00
|
|
|
PostAction.expects(:target_moderators).returns("moderators")
|
2013-04-16 16:56:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sends an email to all moderators if selected" do
|
2013-04-22 03:45:03 -04:00
|
|
|
post = build(:post, id: 1000)
|
|
|
|
PostCreator.any_instance.expects(:create).returns(post)
|
2013-04-16 16:56:18 -04:00
|
|
|
PostAction.act(build(:user), build(:post), PostActionType.types[:notify_moderators], "this is my special message");
|
|
|
|
end
|
2013-04-12 03:55:45 -04:00
|
|
|
end
|
2013-04-12 07:09:41 -04:00
|
|
|
|
2013-04-16 16:56:18 -04:00
|
|
|
describe "notify_user" do
|
|
|
|
before do
|
|
|
|
PostAction.stubs(:create)
|
|
|
|
post = build(:post)
|
|
|
|
post.user = build(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends an email to user if selected" do
|
2013-04-22 03:45:03 -04:00
|
|
|
PostCreator.any_instance.expects(:create).returns(build(:post))
|
2013-04-16 16:56:18 -04:00
|
|
|
PostAction.act(build(:user), post, PostActionType.types[:notify_user], "this is my special message");
|
|
|
|
end
|
2013-04-12 04:14:36 -04:00
|
|
|
end
|
2013-04-12 03:55:45 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "flag counts" do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
|
|
|
PostAction.update_flagged_posts_count
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "increments the numbers correctly" do
|
2013-04-22 03:45:03 -04:00
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:off_topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
PostAction.flagged_posts_count.should == 1
|
|
|
|
|
|
|
|
PostAction.clear_flags!(post, -1)
|
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should reset counts when a topic is deleted" do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:off_topic])
|
2013-05-07 00:39:01 -04:00
|
|
|
post.topic.trash!
|
2013-02-05 20:13:41 -05:00
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
describe "when a user bookmarks something" do
|
|
|
|
it "increases the post's bookmark count when saved" do
|
|
|
|
lambda { bookmark.save; post.reload }.should change(post, :bookmark_count).by(1)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
it "increases the forum topic's bookmark count when saved" do
|
|
|
|
lambda { bookmark.save; post.topic.reload }.should change(post.topic, :bookmark_count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when deleted' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
bookmark.save
|
|
|
|
post.reload
|
|
|
|
@topic = post.topic
|
|
|
|
@topic.reload
|
|
|
|
bookmark.deleted_at = DateTime.now
|
|
|
|
bookmark.save
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
it 'reduces the bookmark count of the post' do
|
|
|
|
lambda { post.reload }.should change(post, :bookmark_count).by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reduces the bookmark count of the forum topic' do
|
|
|
|
lambda { @topic.reload }.should change(post.topic, :bookmark_count).by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
describe 'when a user likes something' do
|
|
|
|
it 'should increase the post counts when a user likes' do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:like])
|
2013-02-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
}.should change(post, :like_count).by(1)
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it 'should increase the forum topic like count when a user likes' do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:like])
|
2013-02-05 14:16:51 -05:00
|
|
|
post.topic.reload
|
|
|
|
}.should change(post.topic, :like_count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'when a user votes for something' do
|
2013-05-03 20:52:45 -04:00
|
|
|
it 'should increase the vote counts when a user votes' do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:vote])
|
2013-02-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
}.should change(post, :vote_count).by(1)
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
it 'should increase the forum topic vote count when a user votes' do
|
2013-02-05 14:16:51 -05:00
|
|
|
lambda {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:vote])
|
2013-02-05 14:16:51 -05:00
|
|
|
post.topic.reload
|
|
|
|
}.should change(post.topic, :vote_count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'flagging' do
|
|
|
|
|
2013-02-06 18:45:58 -05:00
|
|
|
it 'does not allow you to flag stuff with 2 reasons' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
2013-05-03 20:52:45 -04:00
|
|
|
lambda { PostAction.act(u1, post, PostActionType.types[:off_topic]) }.should raise_error(PostAction::AlreadyActed)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows you to flag stuff with another reason' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
PostAction.remove_act(u1, post, PostActionType.types[:spam])
|
|
|
|
lambda { PostAction.act(u1, post, PostActionType.types[:off_topic]) }.should_not raise_error(PostAction::AlreadyActed)
|
2013-02-06 18:45:58 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should update counts when you clear flags' do
|
2013-02-05 14:16:51 -05:00
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
post.spam_count.should == 1
|
|
|
|
|
|
|
|
PostAction.clear_flags!(post, -1)
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.spam_count.should == 0
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'should follow the rules for automatic hiding workflow' do
|
2013-02-05 14:16:51 -05:00
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
|
|
|
u2 = Fabricate(:walter_white)
|
2013-05-03 20:52:45 -04:00
|
|
|
admin = Fabricate(:admin) # we need an admin for the messages
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
SiteSetting.flags_required_to_hide_post = 2
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
PostAction.act(u2, post, PostActionType.types[:spam])
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should.should be_true
|
2013-03-18 14:59:34 -04:00
|
|
|
post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached]
|
2013-02-05 14:16:51 -05:00
|
|
|
post.topic.visible.should be_false
|
|
|
|
|
|
|
|
post.revise(post.user, post.raw + " ha I edited it ")
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should be_false
|
|
|
|
post.hidden_reason_id.should be_nil
|
|
|
|
post.topic.visible.should be_true
|
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
PostAction.act(u2, post, PostActionType.types[:off_topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should be_true
|
2013-03-18 14:59:34 -04:00
|
|
|
post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached_again]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
post.revise(post.user, post.raw + " ha I edited it again ")
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should be_true
|
2013-03-18 14:59:34 -04:00
|
|
|
post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached_again]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-03 20:52:45 -04:00
|
|
|
it "prevents user to act twice at the same time" do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user = Fabricate(:evil_trout)
|
|
|
|
|
|
|
|
# flags are already being tested
|
|
|
|
all_types_except_flags = PostActionType.types.except(PostActionType.flag_types)
|
|
|
|
all_types_except_flags.values.each do |action|
|
|
|
|
lambda do
|
|
|
|
PostAction.act(user, post, action)
|
|
|
|
PostAction.act(user, post, action)
|
|
|
|
end.should raise_error(PostAction::AlreadyActed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|