From b4c2d49b0cfe3fbcc13f792f363c1556106c28e9 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 17 Jan 2019 11:08:03 +0800 Subject: [PATCH] DEV: Consolidate and remove duplicated liked notifications tests. --- spec/models/post_action_spec.rb | 120 ++++++++++++++++++++++------- spec/services/post_alerter_spec.rb | 96 ----------------------- 2 files changed, 93 insertions(+), 123 deletions(-) diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 5100f9768b3..296fdbfe29b 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -280,20 +280,103 @@ describe PostAction do end it 'should generate and remove notifications correctly' do - expect do - PostAction.act(codinghorror, post, PostActionType.types[:like]) - end.to change { Notification.count }.by(1) + PostAction.act(codinghorror, post, PostActionType.types[:like]) + + expect(Notification.count).to eq(1) notification = Notification.last expect(notification.user_id).to eq(post.user_id) expect(notification.notification_type).to eq(Notification.types[:liked]) - expect do - PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) - end.to change { Notification.count }.by(-1) + PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) - expect(Notification.exists?(id: notification.id)).to eq(false) + expect(Notification.count).to eq(0) + + PostAction.act(codinghorror, post, PostActionType.types[:like]) + + expect(Notification.count).to eq(1) + + notification = Notification.last + + expect(notification.user_id).to eq(post.user_id) + expect(notification.notification_type).to eq(Notification.types[:liked]) + end + + it 'should not notify when never is selected' do + post.user.user_option.update!( + like_notification_frequency: + UserOption.like_notification_frequency_type[:never] + ) + + expect do + PostAction.act(codinghorror, post, PostActionType.types[:like]) + end.to_not change { Notification.count } + end + + it 'notifies on likes correctly' do + PostAction.act(eviltrout, post, PostActionType.types[:like]) + PostAction.act(admin, post, PostActionType.types[:like]) + + # one like + expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) + .to eq(1) + + post.user.user_option.update!( + like_notification_frequency: UserOption.like_notification_frequency_type[:always] + ) + + admin2 = Fabricate(:admin) + + # Travel 1 hour in time to test that order post_actions by `created_at` + freeze_time 1.hour.from_now + + expect do + PostAction.act(admin2, post, PostActionType.types[:like]) + end.to_not change { Notification.count } + + # adds info to the notification + notification = Notification.find_by( + post_number: 1, + topic_id: post.topic_id + ) + + expect(notification.data_hash["count"].to_i).to eq(2) + expect(notification.data_hash["username2"]).to eq(eviltrout.username) + + # this is a tricky thing ... removing a like should fix up the notifications + PostAction.remove_act(eviltrout, post, PostActionType.types[:like]) + + # rebuilds the missing notification + expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) + .to eq(1) + + notification = Notification.find_by( + post_number: 1, + topic_id: post.topic_id + ) + + expect(notification.data_hash["count"]).to eq(2) + expect(notification.data_hash["username"]).to eq(admin2.username) + expect(notification.data_hash["username2"]).to eq(admin.username) + + post.user.user_option.update!( + like_notification_frequency: + UserOption.like_notification_frequency_type[:first_time_and_daily] + ) + + # this gets skipped + admin3 = Fabricate(:admin) + PostAction.act(admin3, post, PostActionType.types[:like]) + + freeze_time 2.days.from_now + + admin4 = Fabricate(:admin) + PostAction.act(admin4, post, PostActionType.types[:like]) + + # first happend within the same day, no need to notify + expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) + .to eq(2) end describe 'likes consolidation' do @@ -411,7 +494,6 @@ describe PostAction do it "should generate a notification if liker is an admin irregardles of \ muting" do - # you can not mute admin, sorry MutedUser.create!(user_id: post.user.id, muted_user_id: admin.id) expect do @@ -440,18 +522,21 @@ describe PostAction do post.reload expect(post.like_count).to eq(2) expect(post.like_score).to eq(4) + expect(post.topic.like_count).to eq(2) # Removing likes PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) post.reload expect(post.like_count).to eq(1) expect(post.like_score).to eq(3) + expect(post.topic.like_count).to eq(1) expect(value_for(codinghorror.id, Date.today)).to eq(0) PostAction.remove_act(moderator, post, PostActionType.types[:like]) post.reload expect(post.like_count).to eq(0) expect(post.like_score).to eq(0) + expect(post.topic.like_count).to eq(0) end it "shouldn't change given_likes unless likes are given or removed" do @@ -473,25 +558,6 @@ describe PostAction do expect(actual_count).to eq(1), "Expected likes_given to be 1 when removing '#{type_name}', but got #{actual_count}" end end - - it 'should increase the like counts when a user votes' do - expect { - PostAction.act(codinghorror, post, PostActionType.types[:like]) - post.reload - }.to change(post, :like_count).by(1) - end - - it 'should increase the forum topic vote count when a user votes' do - expect { - PostAction.act(codinghorror, post, PostActionType.types[:like]) - post.topic.reload - }.to change(post.topic, :like_count).by(1) - - expect { - PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) - post.topic.reload - }.to change(post.topic, :like_count).by(-1) - end end describe 'flagging' do diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index 85bd3ff29cb..f4888711b7d 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -160,102 +160,6 @@ describe PostAlerter do end end - context 'likes' do - - it 'notifies on likes after an undo' do - PostActionNotifier.enable - - post = Fabricate(:post, raw: 'I love waffles') - - PostAction.act(evil_trout, post, PostActionType.types[:like]) - PostAction.remove_act(evil_trout, post, PostActionType.types[:like]) - PostAction.act(evil_trout, post, PostActionType.types[:like]) - - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(1) - end - - it 'notifies on does not notify when never is selected' do - PostActionNotifier.enable - - post = Fabricate(:post, raw: 'I love waffles') - - post.user.user_option.update_columns(like_notification_frequency: - UserOption.like_notification_frequency_type[:never]) - - PostAction.act(evil_trout, post, PostActionType.types[:like]) - - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(0) - end - - it 'notifies on likes correctly' do - PostActionNotifier.enable - - post = Fabricate(:post, raw: 'I love waffles') - - PostAction.act(evil_trout, post, PostActionType.types[:like]) - admin = Fabricate(:admin) - PostAction.act(admin, post, PostActionType.types[:like]) - - # one like - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) - .to eq(1) - - post.user.user_option.update_columns( - like_notification_frequency: UserOption.like_notification_frequency_type[:always] - ) - - admin2 = Fabricate(:admin) - - # Travel 1 hour in time to test that order post_actions by `created_at` - freeze_time 1.hour.from_now - PostAction.act(admin2, post, PostActionType.types[:like]) - - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) - .to eq(1) - - # adds info to the notification - notification = Notification.find_by( - post_number: 1, - topic_id: post.topic_id - ) - - expect(notification.data_hash["count"].to_i).to eq(2) - expect(notification.data_hash["username2"]).to eq(evil_trout.username) - - # this is a tricky thing ... removing a like should fix up the notifications - PostAction.remove_act(evil_trout, post, PostActionType.types[:like]) - - # rebuilds the missing notification - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count) - .to eq(1) - - notification = Notification.find_by( - post_number: 1, - topic_id: post.topic_id - ) - - expect(notification.data_hash["count"]).to eq(2) - expect(notification.data_hash["username"]).to eq(admin2.username) - expect(notification.data_hash["username2"]).to eq(admin.username) - - post.user.user_option.update_columns(like_notification_frequency: - UserOption.like_notification_frequency_type[:first_time_and_daily]) - - # this gets skipped - admin3 = Fabricate(:admin) - PostAction.act(admin3, post, PostActionType.types[:like]) - - freeze_time 2.days.from_now - - admin4 = Fabricate(:admin) - PostAction.act(admin4, post, PostActionType.types[:like]) - - # first happend within the same day, no need to notify - expect(Notification.where(post_number: 1, topic_id: post.topic_id).count).to eq(2) - - end - end - context 'quotes' do it 'does not notify for muted users' do