2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Notification do
|
2013-05-14 09:59:55 +08:00
|
|
|
before do
|
|
|
|
ActiveRecord::Base.observers.enable :all
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it { should validate_presence_of :notification_type }
|
|
|
|
it { should validate_presence_of :data }
|
|
|
|
|
|
|
|
it { should belong_to :user }
|
|
|
|
it { should belong_to :topic }
|
|
|
|
|
2013-05-14 09:59:55 +08:00
|
|
|
describe 'post' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:post_args) do
|
|
|
|
{user: topic.user, topic: topic}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
|
|
|
|
describe 'replies' do
|
2014-03-18 12:22:39 +08:00
|
|
|
def process_alerts(post)
|
|
|
|
PostAlerter.post_created(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:post) {
|
|
|
|
process_alerts(Fabricate(:post, post_args.merge(raw: "Hello @CodingHorror")))
|
|
|
|
}
|
2013-05-14 09:59:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
it 'notifies the poster on reply' do
|
|
|
|
lambda {
|
2014-03-18 12:22:39 +08:00
|
|
|
reply = Fabricate(:basic_reply, user: coding_horror, topic: post.topic)
|
|
|
|
process_alerts(reply)
|
2013-05-14 09:59:55 +08:00
|
|
|
}.should change(post.user.notifications, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't notify the poster when they reply to their own post" do
|
|
|
|
lambda {
|
2014-03-18 12:22:39 +08:00
|
|
|
reply = Fabricate(:basic_reply, user: post.user, topic: post.topic)
|
|
|
|
process_alerts(reply)
|
2013-05-14 09:59:55 +08:00
|
|
|
}.should_not change(post.user.notifications, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'watching' do
|
|
|
|
it "does notify watching users of new posts" do
|
2014-03-18 12:22:39 +08:00
|
|
|
post = PostAlerter.post_created(Fabricate(:post, post_args))
|
2013-05-14 09:59:55 +08:00
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
post_args[:topic].notify_watch!(user2)
|
|
|
|
lambda {
|
2014-03-18 12:22:39 +08:00
|
|
|
PostAlerter.post_created(Fabricate(:post, user: post.user, topic: post.topic))
|
2013-05-14 09:59:55 +08:00
|
|
|
}.should change(user2.notifications, :count).by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'muting' do
|
|
|
|
it "does not notify users of new posts" do
|
|
|
|
post = Fabricate(:post, post_args)
|
|
|
|
user = post_args[:user]
|
|
|
|
user2 = Fabricate(:coding_horror)
|
|
|
|
|
|
|
|
post_args[:topic].notify_muted!(user)
|
|
|
|
lambda {
|
|
|
|
Fabricate(:post, user: user2, topic: post.topic, raw: 'hello @' + user.username)
|
|
|
|
}.should change(user.notifications, :count).by(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'unread counts' do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'a regular notification' do
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'increases unread_notifications' do
|
|
|
|
lambda { Fabricate(:notification, user: user); user.reload }.should change(user, :unread_notifications)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't increase unread_private_messages" do
|
|
|
|
lambda { Fabricate(:notification, user: user); user.reload }.should_not change(user, :unread_private_messages)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'a private message' do
|
|
|
|
it "doesn't increase unread_notifications" do
|
|
|
|
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should_not change(user, :unread_notifications)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "increases unread_private_messages" do
|
|
|
|
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should change(user, :unread_private_messages)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'message bus' do
|
|
|
|
|
|
|
|
it 'updates the notification count on create' do
|
2013-05-16 13:03:03 +08:00
|
|
|
Notification.any_instance.expects(:refresh_notification_count).returns(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
Fabricate(:notification)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'destroy' do
|
|
|
|
|
|
|
|
let!(:notification) { Fabricate(:notification) }
|
|
|
|
|
|
|
|
it 'updates the notification count on destroy' do
|
2013-05-16 13:03:03 +08:00
|
|
|
Notification.any_instance.expects(:refresh_notification_count).returns(nil)
|
2013-02-26 00:42:20 +08:00
|
|
|
notification.destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '@mention' do
|
|
|
|
|
|
|
|
it "calls email_user_mentioned on creating a notification" do
|
2013-08-24 19:21:39 +08:00
|
|
|
UserEmailObserver.any_instance.expects(:after_commit).with(instance_of(Notification))
|
2013-02-06 03:16:51 +08:00
|
|
|
Fabricate(:notification)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '@mention' do
|
|
|
|
it "calls email_user_quoted on creating a quote notification" do
|
2013-08-24 19:21:39 +08:00
|
|
|
UserEmailObserver.any_instance.expects(:after_commit).with(instance_of(Notification))
|
2013-02-06 03:16:51 +08:00
|
|
|
Fabricate(:quote_notification)
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'private message' do
|
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic = Fabricate(:private_message_topic)
|
2013-03-23 23:02:59 +08:00
|
|
|
@post = Fabricate(:post, topic: @topic, user: @topic.user)
|
2014-03-18 12:22:39 +08:00
|
|
|
PostAlerter.post_created(@post)
|
2013-02-06 03:16:51 +08:00
|
|
|
@target = @post.topic.topic_allowed_users.reject{|a| a.user_id == @post.user_id}[0].user
|
|
|
|
end
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
it 'should create and rollup private message notifications' do
|
2013-03-01 20:07:44 +08:00
|
|
|
@target.notifications.first.notification_type.should == Notification.types[:private_message]
|
2013-02-06 03:16:51 +08:00
|
|
|
@post.user.unread_notifications.should == 0
|
2014-01-20 13:18:43 +08:00
|
|
|
@target.unread_private_messages.should == 1
|
|
|
|
|
|
|
|
Fabricate(:post, topic: @topic, user: @topic.user)
|
|
|
|
@target.reload
|
|
|
|
@target.unread_private_messages.should == 1
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-01-20 13:18:43 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.post' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let!(:notification) { Fabricate(:notification, user: post.user, topic: post.topic, post_number: post.post_number) }
|
|
|
|
|
|
|
|
it 'returns the post' do
|
|
|
|
notification.post.should == post
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'data' do
|
|
|
|
let(:notification) { Fabricate.build(:notification) }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should have a data hash' do
|
|
|
|
notification.data_hash.should be_present
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should have the data within the json' do
|
|
|
|
notification.data_hash[:poison].should == 'ivy'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-16 14:37:47 +08:00
|
|
|
describe 'saw_regular_notification_id' do
|
|
|
|
it 'correctly updates the read state' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
Notification.create!(read: false,
|
2013-05-16 14:37:47 +08:00
|
|
|
user_id: user.id,
|
|
|
|
topic_id: 2,
|
|
|
|
post_number: 1,
|
2014-02-04 09:57:52 +08:00
|
|
|
data: '{}',
|
2013-05-16 14:37:47 +08:00
|
|
|
notification_type: Notification.types[:private_message])
|
|
|
|
|
|
|
|
other = Notification.create!(read: false,
|
|
|
|
user_id: user.id,
|
|
|
|
topic_id: 2,
|
|
|
|
post_number: 1,
|
2014-02-04 09:57:52 +08:00
|
|
|
data: '{}',
|
2013-05-16 14:37:47 +08:00
|
|
|
notification_type: Notification.types[:mentioned])
|
|
|
|
|
|
|
|
|
|
|
|
user.saw_notification_id(other.id)
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
user.unread_notifications.should == 0
|
|
|
|
user.unread_private_messages.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
describe 'mark_posts_read' do
|
|
|
|
it "marks multiple posts as read if needed" do
|
2013-02-25 15:42:42 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2014-01-20 13:18:43 +08:00
|
|
|
(1..3).map do |i|
|
2014-02-04 09:56:28 +08:00
|
|
|
Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '{}', notification_type: 1)
|
2013-02-25 15:42:42 +08:00
|
|
|
end
|
2014-02-04 09:56:28 +08:00
|
|
|
Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '{}', notification_type: 1)
|
2013-02-25 15:42:42 +08:00
|
|
|
|
|
|
|
Notification.mark_posts_read(user,2,[1,2,3,4]).should == 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 14:12:17 +08:00
|
|
|
|
2013-05-16 15:50:14 +08:00
|
|
|
describe 'ensure consistency' do
|
|
|
|
it 'deletes notifications if post is missing or deleted' do
|
|
|
|
|
|
|
|
ActiveRecord::Base.observers.disable :all
|
|
|
|
p = Fabricate(:post)
|
|
|
|
p2 = Fabricate(:post)
|
|
|
|
|
|
|
|
Notification.create!(read: false, user_id: p.user_id, topic_id: p.topic_id, post_number: p.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:private_message])
|
|
|
|
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:private_message])
|
|
|
|
|
|
|
|
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
|
|
|
|
notification_type: Notification.types[:liked])
|
2013-07-10 03:20:18 +08:00
|
|
|
p2.trash!(p.user)
|
2013-05-16 15:50:14 +08:00
|
|
|
|
|
|
|
# we may want to make notification "trashable" but for now we nuke pm notifications from deleted topics/posts
|
|
|
|
Notification.ensure_consistency!
|
|
|
|
|
|
|
|
Notification.count.should == 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-02-13 14:12:17 +08:00
|
|
|
|
|
|
|
# pulling this out cause I don't want an observer
|
|
|
|
describe Notification do
|
|
|
|
describe '#recent_report' do
|
|
|
|
let(:user){ Fabricate(:user) }
|
|
|
|
let(:post){ Fabricate(:post) }
|
|
|
|
|
|
|
|
def fab(type, read)
|
|
|
|
@i ||= 0
|
|
|
|
@i += 1
|
|
|
|
Notification.create!(read: read, user_id: user.id, topic_id: post.topic_id, post_number: post.post_number, data: '[]',
|
|
|
|
notification_type: type, created_at: @i.days.from_now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_pm
|
|
|
|
fab(Notification.types[:private_message], false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pm
|
|
|
|
fab(Notification.types[:private_message], true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def regular
|
|
|
|
fab(Notification.types[:liked], true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'orders stuff correctly' do
|
|
|
|
a = unread_pm
|
|
|
|
regular
|
|
|
|
c = pm
|
|
|
|
d = regular
|
|
|
|
|
|
|
|
# bumps unread pms to front of list
|
|
|
|
|
|
|
|
notifications = Notification.recent_report(user, 3)
|
|
|
|
notifications.map{|n| n.id}.should == [a.id, d.id, c.id]
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|