2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe PostAction do
|
|
|
|
|
|
|
|
it { should belong_to :user }
|
|
|
|
it { should belong_to :post }
|
|
|
|
it { should belong_to :post_action_type }
|
|
|
|
it { should rate_limit }
|
|
|
|
|
|
|
|
let(:codinghorror) { Fabricate(:coding_horror) }
|
|
|
|
let(:post) { Fabricate(:post) }
|
2013-03-01 20:07:44 +08:00
|
|
|
let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe "flag counts" do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
|
|
|
PostAction.update_flagged_posts_count
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
|
|
|
it "starts of with 0 flag counts" do
|
2013-02-06 03:16:51 +08:00
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "increments the numbers correctly" do
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:off_topic])
|
2013-02-06 03:16:51 +08:00
|
|
|
PostAction.flagged_posts_count.should == 1
|
|
|
|
|
|
|
|
PostAction.clear_flags!(post, -1)
|
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should reset counts when a topic is deleted" do
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:off_topic])
|
2013-02-06 09:13:41 +08:00
|
|
|
post.topic.destroy
|
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
|
|
|
it "should reset counts when a post is deleted" do
|
2013-02-06 09:13:41 +08:00
|
|
|
post2 = Fabricate(:post, topic_id: post.topic_id)
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post2, PostActionType.types[:off_topic])
|
2013-02-06 09:13:41 +08:00
|
|
|
post2.destroy
|
|
|
|
PostAction.flagged_posts_count.should == 0
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "increases the post's bookmark count when saved" do
|
|
|
|
lambda { bookmark.save; post.reload }.should change(post, :bookmark_count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
|
|
|
|
describe 'when a user likes something' do
|
|
|
|
it 'should increase the post counts when a user likes' do
|
2013-02-06 03:16:51 +08:00
|
|
|
lambda {
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:like])
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
}.should change(post, :like_count).by(1)
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
|
|
|
it 'should increase the forum topic like count when a user likes' do
|
2013-02-06 03:16:51 +08:00
|
|
|
lambda {
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:like])
|
2013-02-06 03:16:51 +08:00
|
|
|
post.topic.reload
|
|
|
|
}.should change(post.topic, :like_count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'when a user votes for something' do
|
|
|
|
it 'should increase the vote counts when a user likes' do
|
2013-02-06 03:16:51 +08:00
|
|
|
lambda {
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:vote])
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
}.should change(post, :vote_count).by(1)
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
|
|
|
it 'should increase the forum topic vote count when a user votes' do
|
2013-02-06 03:16:51 +08:00
|
|
|
lambda {
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(codinghorror, post, PostActionType.types[:vote])
|
2013-02-06 03:16:51 +08:00
|
|
|
post.topic.reload
|
|
|
|
}.should change(post.topic, :vote_count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
describe 'when deleted' do
|
|
|
|
before do
|
|
|
|
bookmark.save
|
|
|
|
post.reload
|
|
|
|
@topic = post.topic
|
|
|
|
@topic.reload
|
|
|
|
bookmark.deleted_at = DateTime.now
|
|
|
|
bookmark.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reduces the bookmark count of the post' do
|
2013-02-26 00:42:20 +08:00
|
|
|
lambda {
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
}.should change(post, :bookmark_count).by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reduces the bookmark count of the forum topic' do
|
2013-02-26 00:42:20 +08:00
|
|
|
lambda {
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic.reload
|
|
|
|
}.should change(post.topic, :bookmark_count).by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'flagging' do
|
|
|
|
|
2013-02-07 07:45:58 +08:00
|
|
|
it 'does not allow you to flag stuff with 2 reasons' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
lambda { PostAction.act(u1, post, PostActionType.types[:off_topic]) }.should raise_error(PostAction::AlreadyFlagged)
|
2013-02-07 07:45:58 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'should update counts when you clear flags' do
|
2013-02-06 03:16:51 +08:00
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
post.spam_count.should == 1
|
|
|
|
|
|
|
|
PostAction.clear_flags!(post, -1)
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.spam_count.should == 0
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'should follow the rules for automatic hiding workflow' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post)
|
|
|
|
u1 = Fabricate(:evil_trout)
|
|
|
|
u2 = Fabricate(:walter_white)
|
|
|
|
|
|
|
|
# we need an admin for the messages
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
SiteSetting.flags_required_to_hide_post = 2
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
PostAction.act(u2, post, PostActionType.types[:spam])
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should.should be_true
|
2013-02-26 00:42:20 +08:00
|
|
|
post.hidden_reason_id.should == Post::HiddenReason::FLAG_THRESHOLD_REACHED
|
2013-02-06 03:16:51 +08: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 20:07:44 +08:00
|
|
|
PostAction.act(u1, post, PostActionType.types[:spam])
|
|
|
|
PostAction.act(u2, post, PostActionType.types[:off_topic])
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should be_true
|
2013-02-26 00:42:20 +08:00
|
|
|
post.hidden_reason_id.should == Post::HiddenReason::FLAG_THRESHOLD_REACHED_AGAIN
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
post.revise(post.user, post.raw + " ha I edited it again ")
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
post.reload
|
|
|
|
|
|
|
|
post.hidden.should be_true
|
2013-02-26 00:42:20 +08:00
|
|
|
post.hidden_reason_id.should == Post::HiddenReason::FLAG_THRESHOLD_REACHED_AGAIN
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|