2013-09-12 17:46:43 -04:00
# encoding: utf-8
2015-10-11 10:41:23 +01:00
require 'rails_helper'
2013-09-12 17:46:43 -04:00
require 'composer_messages_finder'
describe ComposerMessagesFinder do
context " delegates work " do
let ( :user ) { Fabricate . build ( :user ) }
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'createTopic' ) }
2013-09-12 17:46:43 -04:00
it " calls all the message finders " do
finder . expects ( :check_education_message ) . once
2013-12-19 13:45:55 -05:00
finder . expects ( :check_new_user_many_replies ) . once
2013-09-12 17:46:43 -04:00
finder . expects ( :check_avatar_notification ) . once
2013-09-13 13:49:34 -04:00
finder . expects ( :check_sequential_replies ) . once
2013-09-17 14:38:39 -04:00
finder . expects ( :check_dominating_topic ) . once
2014-03-12 10:44:08 -04:00
finder . expects ( :check_reviving_old_topic ) . once
2013-09-12 17:46:43 -04:00
finder . find
end
end
context '.check_education_message' do
let ( :user ) { Fabricate . build ( :user ) }
context 'creating topic' do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'createTopic' ) }
2013-09-12 17:46:43 -04:00
before do
SiteSetting . stubs ( :educate_until_posts ) . returns ( 10 )
end
it " returns a message for a user who has not posted any topics " do
2013-09-17 12:11:17 -04:00
user . expects ( :created_topic_count ) . returns ( 9 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_education_message ) . to be_present
2013-09-12 17:46:43 -04:00
end
it " returns no message when the user has posted enough topics " do
2013-09-17 12:11:17 -04:00
user . expects ( :created_topic_count ) . returns ( 10 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_education_message ) . to be_blank
2013-09-12 17:46:43 -04:00
end
end
context 'creating reply' do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'reply' ) }
2013-09-12 17:46:43 -04:00
before do
SiteSetting . stubs ( :educate_until_posts ) . returns ( 10 )
end
it " returns a message for a user who has not posted any topics " do
2013-09-17 12:11:17 -04:00
user . expects ( :post_count ) . returns ( 9 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_education_message ) . to be_present
2013-09-12 17:46:43 -04:00
end
it " returns no message when the user has posted enough topics " do
2013-09-17 12:11:17 -04:00
user . expects ( :post_count ) . returns ( 10 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_education_message ) . to be_blank
2013-09-12 17:46:43 -04:00
end
end
2013-12-19 13:45:55 -05:00
end
context '.check_new_user_many_replies' do
let ( :user ) { Fabricate . build ( :user ) }
context 'replying' do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'reply' ) }
2013-12-19 13:45:55 -05:00
it " has no message when `posted_too_much_in_topic?` is false " do
user . expects ( :posted_too_much_in_topic? ) . returns ( false )
2015-01-09 13:34:37 -03:00
expect ( finder . check_new_user_many_replies ) . to be_blank
2013-12-19 13:45:55 -05:00
end
it " has a message when a user has posted too much " do
user . expects ( :posted_too_much_in_topic? ) . returns ( true )
2015-01-09 13:34:37 -03:00
expect ( finder . check_new_user_many_replies ) . to be_present
2013-12-19 13:45:55 -05:00
end
end
2013-09-12 17:46:43 -04:00
end
context '.check_avatar_notification' do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'createTopic' ) }
2013-09-12 17:46:43 -04:00
let ( :user ) { Fabricate ( :user ) }
2015-01-02 12:37:17 +01:00
context " success " do
let! ( :message ) { finder . check_avatar_notification }
2013-09-12 17:46:43 -04:00
2015-01-02 12:37:17 +01:00
it " returns an avatar upgrade message " do
2015-01-09 13:34:37 -03:00
expect ( message ) . to be_present
2013-09-12 17:46:43 -04:00
end
2015-01-02 12:37:17 +01:00
it " creates a notified_about_avatar log " do
2015-01-09 13:34:37 -03:00
expect ( UserHistory . exists_for_user? ( user , :notified_about_avatar ) ) . to eq ( true )
2013-09-12 17:46:43 -04:00
end
2015-01-02 12:37:17 +01:00
end
2013-09-12 17:46:43 -04:00
2015-01-02 12:37:17 +01:00
it " doesn't return notifications for new users " do
user . trust_level = TrustLevel [ 0 ]
2015-01-09 13:34:37 -03:00
expect ( finder . check_avatar_notification ) . to be_blank
2015-01-02 12:37:17 +01:00
end
2013-09-12 17:46:43 -04:00
2015-01-02 12:37:17 +01:00
it " doesn't return notifications for users who have custom avatars " do
user . uploaded_avatar_id = 1
2015-01-09 13:34:37 -03:00
expect ( finder . check_avatar_notification ) . to be_blank
2015-01-02 12:37:17 +01:00
end
2013-09-12 17:46:43 -04:00
2015-01-02 12:37:17 +01:00
it " doesn't notify users who have been notified already " do
UserHistory . create! ( action : UserHistory . actions [ :notified_about_avatar ] , target_user_id : user . id )
2015-01-09 13:34:37 -03:00
expect ( finder . check_avatar_notification ) . to be_blank
2013-09-12 17:46:43 -04:00
end
2015-12-14 22:32:40 +05:30
it " doesn't notify users if 'disable_avatar_education_message' setting is enabled " do
SiteSetting . disable_avatar_education_message = true
expect ( finder . check_avatar_notification ) . to be_blank
end
it " doesn't notify users if 'sso_overrides_avatar' setting is enabled " do
SiteSetting . sso_overrides_avatar = true
expect ( finder . check_avatar_notification ) . to be_blank
end
it " doesn't notify users if 'allow_uploaded_avatars' setting is disabled " do
SiteSetting . allow_uploaded_avatars = false
expect ( finder . check_avatar_notification ) . to be_blank
end
2013-09-13 13:49:34 -04:00
end
context '.check_sequential_replies' do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic ) }
before do
SiteSetting . stubs ( :educate_until_posts ) . returns ( 10 )
2013-09-14 00:32:18 -04:00
user . stubs ( :post_count ) . returns ( 11 )
2013-09-13 13:49:34 -04:00
Fabricate ( :post , topic : topic , user : user )
Fabricate ( :post , topic : topic , user : user )
SiteSetting . stubs ( :sequential_replies_threshold ) . returns ( 2 )
end
it " does not give a message for new topics " do
2016-06-06 16:58:35 -04:00
finder = ComposerMessagesFinder . new ( user , composer_action : 'createTopic' )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
it " does not give a message without a topic id " do
2016-06-06 16:58:35 -04:00
expect ( ComposerMessagesFinder . new ( user , composer_action : 'reply' ) . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
context " reply " do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'reply' , topic_id : topic . id ) }
2013-09-13 13:49:34 -04:00
it " does not give a message to users who are still in the 'education' phase " do
2013-09-17 12:11:17 -04:00
user . stubs ( :post_count ) . returns ( 9 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
it " doesn't notify a user it has already notified about sequential replies " do
2013-09-17 14:38:39 -04:00
UserHistory . create! ( action : UserHistory . actions [ :notified_about_sequential_replies ] , target_user_id : user . id , topic_id : topic . id )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
2013-09-17 14:38:39 -04:00
it " will notify you if it hasn't in the current topic " do
UserHistory . create! ( action : UserHistory . actions [ :notified_about_sequential_replies ] , target_user_id : user . id , topic_id : topic . id + 1 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_present
2013-09-17 14:38:39 -04:00
end
2013-09-13 13:49:34 -04:00
it " doesn't notify a user who has less than the `sequential_replies_threshold` threshold posts " do
SiteSetting . stubs ( :sequential_replies_threshold ) . returns ( 5 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
it " doesn't notify a user if another user posted " do
Fabricate ( :post , topic : topic , user : Fabricate ( :user ) )
2015-01-09 13:34:37 -03:00
expect ( finder . check_sequential_replies ) . to be_blank
2013-09-13 13:49:34 -04:00
end
2015-11-27 19:29:44 +01:00
it " doesn't notify in message " do
Topic . any_instance . expects ( :private_message? ) . returns ( true )
expect ( finder . check_sequential_replies ) . to be_blank
end
2013-09-13 13:49:34 -04:00
context " success " do
let! ( :message ) { finder . check_sequential_replies }
it " returns a message " do
2015-01-09 13:34:37 -03:00
expect ( message ) . to be_present
2013-09-13 13:49:34 -04:00
end
it " creates a notified_about_sequential_replies log " do
2015-01-09 13:34:37 -03:00
expect ( UserHistory . exists_for_user? ( user , :notified_about_sequential_replies ) ) . to eq ( true )
2013-09-13 13:49:34 -04:00
end
end
end
2013-09-12 17:46:43 -04:00
end
2013-09-17 14:38:39 -04:00
context '.check_dominating_topic' do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic ) }
before do
SiteSetting . stubs ( :educate_until_posts ) . returns ( 10 )
user . stubs ( :post_count ) . returns ( 11 )
2013-11-18 12:48:26 -05:00
SiteSetting . stubs ( :summary_posts_required ) . returns ( 1 )
2013-09-17 14:38:39 -04:00
Fabricate ( :post , topic : topic , user : user )
Fabricate ( :post , topic : topic , user : user )
Fabricate ( :post , topic : topic , user : Fabricate ( :user ) )
SiteSetting . stubs ( :sequential_replies_threshold ) . returns ( 2 )
end
it " does not give a message for new topics " do
2016-06-06 16:58:35 -04:00
finder = ComposerMessagesFinder . new ( user , composer_action : 'createTopic' )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
it " does not give a message without a topic id " do
2016-06-06 16:58:35 -04:00
expect ( ComposerMessagesFinder . new ( user , composer_action : 'reply' ) . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
context " reply " do
2016-06-06 16:58:35 -04:00
let ( :finder ) { ComposerMessagesFinder . new ( user , composer_action : 'reply' , topic_id : topic . id ) }
2013-09-17 14:38:39 -04:00
it " does not give a message to users who are still in the 'education' phase " do
user . stubs ( :post_count ) . returns ( 9 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
2013-11-18 12:48:26 -05:00
it " does not notify if the `summary_posts_required` has not been reached " do
SiteSetting . stubs ( :summary_posts_required ) . returns ( 100 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
it " doesn't notify a user it has already notified in this topic " do
2014-02-07 05:49:45 +05:30
UserHistory . create! ( action : UserHistory . actions [ :notified_about_dominating_topic ] , topic_id : topic . id , target_user_id : user . id )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
it " notifies a user if the topic is different " do
2014-02-07 05:49:45 +05:30
UserHistory . create! ( action : UserHistory . actions [ :notified_about_dominating_topic ] , topic_id : topic . id + 1 , target_user_id : user . id )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_present
2013-09-17 14:38:39 -04:00
end
2013-11-18 12:48:26 -05:00
it " doesn't notify a user if the topic has less than `summary_posts_required` posts " do
SiteSetting . stubs ( :summary_posts_required ) . returns ( 5 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
it " doesn't notify a user if they've posted less than the percentage " do
SiteSetting . stubs ( :dominating_topic_minimum_percent ) . returns ( 100 )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
it " doesn't notify you if it's your own topic " do
topic . update_column ( :user_id , user . id )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2013-09-17 14:38:39 -04:00
end
2014-02-07 05:49:45 +05:30
it " doesn't notify you in a private message " do
2014-09-11 17:39:20 +10:00
topic . update_columns ( category_id : nil , archetype : Archetype . private_message )
2015-01-09 13:34:37 -03:00
expect ( finder . check_dominating_topic ) . to be_blank
2014-02-07 05:49:45 +05:30
end
2013-09-17 14:38:39 -04:00
context " success " do
let! ( :message ) { finder . check_dominating_topic }
it " returns a message " do
2015-01-09 13:34:37 -03:00
expect ( message ) . to be_present
2013-09-17 14:38:39 -04:00
end
2014-02-07 05:49:45 +05:30
it " creates a notified_about_dominating_topic log " do
2015-01-09 13:34:37 -03:00
expect ( UserHistory . exists_for_user? ( user , :notified_about_dominating_topic ) ) . to eq ( true )
2013-09-17 14:38:39 -04:00
end
end
end
end
2014-03-12 10:44:08 -04:00
context '.check_reviving_old_topic' do
let ( :user ) { Fabricate ( :user ) }
let ( :topic ) { Fabricate ( :topic ) }
it " does not give a message without a topic id " do
2016-06-06 16:58:35 -04:00
expect ( described_class . new ( user , composer_action : 'createTopic' ) . check_reviving_old_topic ) . to be_blank
expect ( described_class . new ( user , composer_action : 'reply' ) . check_reviving_old_topic ) . to be_blank
2014-03-12 10:44:08 -04:00
end
context " a reply " do
context " warn_reviving_old_topic_age is 180 days " do
before do
SiteSetting . stubs ( :warn_reviving_old_topic_age ) . returns ( 180 )
end
it " does not notify if last post is recent " do
topic = Fabricate ( :topic , last_posted_at : 1 . hour . ago )
2016-06-06 16:58:35 -04:00
expect ( described_class . new ( user , composer_action : 'reply' , topic_id : topic . id ) . check_reviving_old_topic ) . to be_blank
2014-03-12 10:44:08 -04:00
end
it " notifies if last post is old " do
topic = Fabricate ( :topic , last_posted_at : 181 . days . ago )
2016-06-06 16:58:35 -04:00
expect ( described_class . new ( user , composer_action : 'reply' , topic_id : topic . id ) . check_reviving_old_topic ) . not_to be_blank
2014-03-12 10:44:08 -04:00
end
end
context " warn_reviving_old_topic_age is 0 " do
before do
SiteSetting . stubs ( :warn_reviving_old_topic_age ) . returns ( 0 )
end
it " does not notify if last post is new " do
topic = Fabricate ( :topic , last_posted_at : 1 . hour . ago )
2016-06-06 16:58:35 -04:00
expect ( described_class . new ( user , composer_action : 'reply' , topic_id : topic . id ) . check_reviving_old_topic ) . to be_blank
2014-03-12 10:44:08 -04:00
end
it " does not notify if last post is old " do
topic = Fabricate ( :topic , last_posted_at : 365 . days . ago )
2016-06-06 16:58:35 -04:00
expect ( described_class . new ( user , composer_action : 'reply' , topic_id : topic . id ) . check_reviving_old_topic ) . to be_blank
2014-03-12 10:44:08 -04:00
end
end
end
end
2013-09-12 17:46:43 -04:00
end