discourse/spec/models/post_alert_observer_spec.rb

123 lines
3.6 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require 'spec_helper'
require_dependency 'post_destroyer'
2013-02-05 14:16:51 -05:00
describe PostAlertObserver do
2013-04-13 10:31:20 -04:00
before do
ActiveRecord::Base.observers.enable :post_alert_observer
2013-04-13 10:31:20 -04:00
ImageSorcery.any_instance.stubs(:convert).returns(false)
end
2013-02-05 14:16:51 -05:00
let!(:evil_trout) { Fabricate(:evil_trout) }
let(:post) { Fabricate(:post) }
context 'liking' do
context 'when liking a post' do
it 'creates a notification' do
lambda {
2013-03-01 07:07:44 -05:00
PostAction.act(evil_trout, post, PostActionType.types[:like])
2013-02-05 14:16:51 -05:00
}.should change(Notification, :count).by(1)
end
end
context 'when removing a liked post' do
before do
2013-03-01 07:07:44 -05:00
PostAction.act(evil_trout, post, PostActionType.types[:like])
2013-02-05 14:16:51 -05:00
end
2013-02-25 11:42:20 -05:00
it 'removes a notification' do
2013-02-05 14:16:51 -05:00
lambda {
2013-03-01 07:07:44 -05:00
PostAction.remove_act(evil_trout, post, PostActionType.types[:like])
2013-02-05 14:16:51 -05:00
}.should change(Notification, :count).by(-1)
end
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
2013-02-25 11:42:20 -05:00
context 'when editing a post' do
2013-02-05 14:16:51 -05:00
it 'notifies a user of the revision' do
lambda {
post.revise(evil_trout, "world is the new body of the message")
2013-02-05 14:16:51 -05:00
}.should change(post.user.notifications, :count).by(1)
end
end
context 'quotes' do
it 'notifies a user by username' do
lambda {
Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]')
}.should change(evil_trout.notifications, :count).by(1)
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
it "won't notify the user a second time on revision" do
p1 = Fabricate(:post, raw: '[quote="Evil Trout, post:1"]whatup[/quote]')
2013-02-25 11:42:20 -05:00
lambda {
2013-02-05 14:16:51 -05:00
p1.revise(p1.user, '[quote="Evil Trout, post:1"]whatup now?[/quote]')
}.should_not change(evil_trout.notifications, :count)
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
it "doesn't notify the poster" do
topic = post.topic
lambda {
new_post = Fabricate(:post, topic: topic, user: topic.user, raw: '[quote="Bruce Wayne, post:1"]whatup[/quote]')
}.should_not change(topic.user.notifications, :count).by(1)
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
context '@mentions' do
let(:user) { Fabricate(:user) }
let(:mention_post) { Fabricate(:post, user: user, raw: 'Hello @eviltrout')}
let(:topic) { mention_post.topic }
it 'notifies a user' do
lambda {
mention_post
}.should change(evil_trout.notifications, :count).by(1)
end
it "won't notify the user a second time on revision" do
mention_post
2013-02-25 11:42:20 -05:00
lambda {
2013-02-05 14:16:51 -05:00
mention_post.revise(mention_post.user, "New raw content that still mentions @eviltrout")
}.should_not change(evil_trout.notifications, :count)
end
it "doesn't notify the user who created the topic in regular mode" do
topic.notify_regular!(user)
mention_post
2013-02-25 11:42:20 -05:00
lambda {
2013-02-05 14:16:51 -05:00
Fabricate(:post, user: user, raw: 'second post', topic: topic)
}.should_not change(user.notifications, :count).by(1)
end
end
context 'private message' do
let(:user) { Fabricate(:user) }
let(:mention_post) { Fabricate(:post, user: user, raw: 'Hello @eviltrout')}
2013-02-25 11:42:20 -05:00
let(:topic) { mention_post.topic }
it "won't notify someone who can't see the post" do
lambda {
Guardian.any_instance.expects(:can_see?).with(instance_of(Post)).returns(false)
mention_post
}.should_not change(evil_trout.notifications, :count)
end
2013-02-05 14:16:51 -05:00
end
context 'moderator action post' do
let(:user) { Fabricate(:user) }
let(:first_post) { Fabricate(:post, user: user, raw: 'A useless post for you.')}
let(:topic) { first_post.topic }
it 'should not notify anyone' do
expect {
Fabricate(:post, topic: topic, raw: 'This topic is CLOSED', post_type: Post.types[:moderator_action])
}.to_not change { Notification.count }
end
end
2013-02-05 14:16:51 -05:00
end