2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-19 23:33:02 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 03:49:53 +00:00
|
|
|
RSpec.describe Notification do
|
2017-11-09 13:37:10 +00:00
|
|
|
describe '#target_status' do
|
2018-03-24 11:51:28 +00:00
|
|
|
let(:notification) { Fabricate(:notification, activity: activity) }
|
2017-11-19 14:32:48 +00:00
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
let(:reblog) { Fabricate(:status, reblog: status) }
|
|
|
|
let(:favourite) { Fabricate(:favourite, status: status) }
|
|
|
|
let(:mention) { Fabricate(:mention, status: status) }
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when Activity is reblog' do
|
2017-11-19 14:32:48 +00:00
|
|
|
let(:activity) { reblog }
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2017-11-19 14:32:48 +00:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when Activity is favourite' do
|
2017-11-09 13:37:10 +00:00
|
|
|
let(:type) { :favourite }
|
|
|
|
let(:activity) { favourite }
|
|
|
|
|
2017-11-19 14:32:48 +00:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when Activity is mention' do
|
2017-11-09 13:37:10 +00:00
|
|
|
let(:activity) { mention }
|
|
|
|
|
2017-11-19 14:32:48 +00:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-19 23:33:02 +00:00
|
|
|
describe '#type' do
|
|
|
|
it 'returns :reblog for a Status' do
|
2023-06-06 11:58:33 +00:00
|
|
|
notification = described_class.new(activity: Status.new)
|
2016-11-19 23:33:02 +00:00
|
|
|
expect(notification.type).to eq :reblog
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns :mention for a Mention' do
|
2023-06-06 11:58:33 +00:00
|
|
|
notification = described_class.new(activity: Mention.new)
|
2016-11-19 23:33:02 +00:00
|
|
|
expect(notification.type).to eq :mention
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns :favourite for a Favourite' do
|
2023-06-06 11:58:33 +00:00
|
|
|
notification = described_class.new(activity: Favourite.new)
|
2016-11-19 23:33:02 +00:00
|
|
|
expect(notification.type).to eq :favourite
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns :follow for a Follow' do
|
2023-06-06 11:58:33 +00:00
|
|
|
notification = described_class.new(activity: Follow.new)
|
2016-11-19 23:33:02 +00:00
|
|
|
expect(notification.type).to eq :follow
|
|
|
|
end
|
|
|
|
end
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2024-01-09 09:40:08 +00:00
|
|
|
describe 'Setting account from activity_type' do
|
|
|
|
context 'when activity_type is a Status' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
status = Fabricate(:status)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Status', activity: status)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(status.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a Follow' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
follow = Fabricate(:follow)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Follow', activity: follow)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(follow.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a Favourite' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
favourite = Fabricate(:favourite)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Favourite', activity: favourite)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(favourite.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a FollowRequest' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
follow_request = Fabricate(:follow_request)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'FollowRequest', activity: follow_request)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(follow_request.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a Poll' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
poll = Fabricate(:poll)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Poll', activity: poll)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(poll.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a Report' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
report = Fabricate(:report)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Report', activity: report)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(report.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is a Mention' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
mention = Fabricate(:mention)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Mention', activity: mention)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(mention.status.account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when activity_type is an Account' do
|
|
|
|
it 'sets the notification from_account correctly' do
|
|
|
|
account = Fabricate(:account)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'Account', account: account)
|
|
|
|
|
|
|
|
expect(notification.account).to eq(account)
|
|
|
|
end
|
|
|
|
end
|
2024-04-26 12:42:06 +00:00
|
|
|
|
|
|
|
context 'when activity_type is an AccountWarning' do
|
|
|
|
it 'sets the notification from_account to the recipient of the notification' do
|
|
|
|
account = Fabricate(:account)
|
|
|
|
account_warning = Fabricate(:account_warning, target_account: account)
|
|
|
|
|
|
|
|
notification = Fabricate.build(:notification, activity_type: 'AccountWarning', activity: account_warning, account: account)
|
|
|
|
|
|
|
|
expect(notification.from_account).to eq(account)
|
|
|
|
end
|
|
|
|
end
|
2024-01-09 09:40:08 +00:00
|
|
|
end
|
|
|
|
|
2024-06-03 08:35:59 +00:00
|
|
|
describe '.paginate_groups_by_max_id' do
|
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
|
|
|
|
let!(:notifications) do
|
|
|
|
['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
|
|
|
|
.map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without since_id or max_id' do
|
|
|
|
it 'returns the most recent notifications, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_max_id(4).pluck(:id))
|
|
|
|
.to eq [notifications[7], notifications[6], notifications[4], notifications[2]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with since_id' do
|
|
|
|
it 'returns the most recent notifications, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_max_id(4, since_id: notifications[4].id).pluck(:id))
|
|
|
|
.to eq [notifications[7], notifications[6]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with max_id' do
|
|
|
|
it 'returns the most recent notifications after max_id, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_max_id(4, max_id: notifications[7].id).pluck(:id))
|
|
|
|
.to eq [notifications[6], notifications[5], notifications[4], notifications[2]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.paginate_groups_by_min_id' do
|
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
|
|
|
|
let!(:notifications) do
|
|
|
|
['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
|
|
|
|
.map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without min_id or max_id' do
|
|
|
|
it 'returns the oldest notifications, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_min_id(4).pluck(:id))
|
|
|
|
.to eq [notifications[0], notifications[2], notifications[3], notifications[4]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with max_id' do
|
|
|
|
it 'returns the oldest notifications, stopping at max_id, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_min_id(4, max_id: notifications[4].id).pluck(:id))
|
|
|
|
.to eq [notifications[0], notifications[2], notifications[3]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with min_id' do
|
|
|
|
it 'returns the most oldest notifications after min_id, only keeping one notification per group' do
|
|
|
|
expect(described_class.without_suspended.paginate_groups_by_min_id(4, min_id: notifications[0].id).pluck(:id))
|
|
|
|
.to eq [notifications[1], notifications[2], notifications[3], notifications[4]].pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-31 20:24:57 +00:00
|
|
|
describe '.preload_cache_collection_target_statuses' do
|
|
|
|
subject do
|
|
|
|
described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|
|
|
|
|
# preload account for testing instead of using cache_collection
|
|
|
|
Status.preload(:account).where(id: target_statuses.map(&:id))
|
|
|
|
end
|
|
|
|
end
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when notifications are empty' do
|
2021-01-31 20:24:57 +00:00
|
|
|
let(:notifications) { [] }
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2021-01-31 20:24:57 +00:00
|
|
|
it 'returns []' do
|
2023-02-20 04:00:48 +00:00
|
|
|
expect(subject).to eq []
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 03:49:08 +00:00
|
|
|
context 'when notifications are present' do
|
2017-11-09 13:37:10 +00:00
|
|
|
before do
|
2021-01-31 20:24:57 +00:00
|
|
|
notifications.each(&:reload)
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
|
2021-01-31 20:24:57 +00:00
|
|
|
let(:mention) { Fabricate(:mention) }
|
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
let(:reblog) { Fabricate(:status, reblog: Fabricate(:status)) }
|
|
|
|
let(:follow) { Fabricate(:follow) }
|
|
|
|
let(:follow_request) { Fabricate(:follow_request) }
|
|
|
|
let(:favourite) { Fabricate(:favourite) }
|
|
|
|
let(:poll) { Fabricate(:poll) }
|
|
|
|
|
|
|
|
let(:notifications) do
|
2017-11-09 13:37:10 +00:00
|
|
|
[
|
2021-01-31 20:24:57 +00:00
|
|
|
Fabricate(:notification, type: :mention, activity: mention),
|
|
|
|
Fabricate(:notification, type: :status, activity: status),
|
|
|
|
Fabricate(:notification, type: :reblog, activity: reblog),
|
|
|
|
Fabricate(:notification, type: :follow, activity: follow),
|
|
|
|
Fabricate(:notification, type: :follow_request, activity: follow_request),
|
|
|
|
Fabricate(:notification, type: :favourite, activity: favourite),
|
|
|
|
Fabricate(:notification, type: :poll, activity: poll),
|
2017-11-09 13:37:10 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-06-10 16:38:22 +00:00
|
|
|
context 'with a preloaded target status' do
|
|
|
|
it 'preloads mention' do
|
|
|
|
expect(subject[0].type).to eq :mention
|
|
|
|
expect(subject[0].association(:mention)).to be_loaded
|
|
|
|
expect(subject[0].mention.association(:status)).to be_loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads status' do
|
|
|
|
expect(subject[1].type).to eq :status
|
|
|
|
expect(subject[1].association(:status)).to be_loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads reblog' do
|
|
|
|
expect(subject[2].type).to eq :reblog
|
|
|
|
expect(subject[2].association(:status)).to be_loaded
|
|
|
|
expect(subject[2].status.association(:reblog)).to be_loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads follow as nil' do
|
|
|
|
expect(subject[3].type).to eq :follow
|
|
|
|
expect(subject[3].target_status).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads follow_request as nill' do
|
|
|
|
expect(subject[4].type).to eq :follow_request
|
|
|
|
expect(subject[4].target_status).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads favourite' do
|
|
|
|
expect(subject[5].type).to eq :favourite
|
|
|
|
expect(subject[5].association(:favourite)).to be_loaded
|
|
|
|
expect(subject[5].favourite.association(:status)).to be_loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preloads poll' do
|
|
|
|
expect(subject[6].type).to eq :poll
|
|
|
|
expect(subject[6].association(:poll)).to be_loaded
|
|
|
|
expect(subject[6].poll.association(:status)).to be_loaded
|
|
|
|
end
|
2021-01-31 20:24:57 +00:00
|
|
|
end
|
2017-11-09 13:37:10 +00:00
|
|
|
|
2023-06-10 16:38:22 +00:00
|
|
|
context 'with a cached status' do
|
|
|
|
it 'replaces mention' do
|
|
|
|
expect(subject[0].type).to eq :mention
|
|
|
|
expect(subject[0].target_status.association(:account)).to be_loaded
|
|
|
|
expect(subject[0].target_status).to eq mention.status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces status' do
|
|
|
|
expect(subject[1].type).to eq :status
|
|
|
|
expect(subject[1].target_status.association(:account)).to be_loaded
|
|
|
|
expect(subject[1].target_status).to eq status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces reblog' do
|
|
|
|
expect(subject[2].type).to eq :reblog
|
|
|
|
expect(subject[2].target_status.association(:account)).to be_loaded
|
|
|
|
expect(subject[2].target_status).to eq reblog.reblog
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces follow' do
|
|
|
|
expect(subject[3].type).to eq :follow
|
|
|
|
expect(subject[3].target_status).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces follow_request' do
|
|
|
|
expect(subject[4].type).to eq :follow_request
|
|
|
|
expect(subject[4].target_status).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces favourite' do
|
|
|
|
expect(subject[5].type).to eq :favourite
|
|
|
|
expect(subject[5].target_status.association(:account)).to be_loaded
|
|
|
|
expect(subject[5].target_status).to eq favourite.status
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces poll' do
|
|
|
|
expect(subject[6].type).to eq :poll
|
|
|
|
expect(subject[6].target_status.association(:account)).to be_loaded
|
|
|
|
expect(subject[6].target_status).to eq poll.status
|
|
|
|
end
|
2017-11-09 13:37:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-19 23:33:02 +00:00
|
|
|
end
|