notification_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Notification do
  4. describe '#target_status' do
  5. let(:notification) { Fabricate(:notification, activity: activity) }
  6. let(:status) { Fabricate(:status) }
  7. let(:reblog) { Fabricate(:status, reblog: status) }
  8. let(:favourite) { Fabricate(:favourite, status: status) }
  9. let(:mention) { Fabricate(:mention, status: status) }
  10. context 'when Activity is reblog' do
  11. let(:activity) { reblog }
  12. it 'returns status' do
  13. expect(notification.target_status).to eq status
  14. end
  15. end
  16. context 'when Activity is favourite' do
  17. let(:type) { :favourite }
  18. let(:activity) { favourite }
  19. it 'returns status' do
  20. expect(notification.target_status).to eq status
  21. end
  22. end
  23. context 'when Activity is mention' do
  24. let(:activity) { mention }
  25. it 'returns status' do
  26. expect(notification.target_status).to eq status
  27. end
  28. end
  29. end
  30. describe '#type' do
  31. it 'returns :reblog for a Status' do
  32. notification = described_class.new(activity: Status.new)
  33. expect(notification.type).to eq :reblog
  34. end
  35. it 'returns :mention for a Mention' do
  36. notification = described_class.new(activity: Mention.new)
  37. expect(notification.type).to eq :mention
  38. end
  39. it 'returns :favourite for a Favourite' do
  40. notification = described_class.new(activity: Favourite.new)
  41. expect(notification.type).to eq :favourite
  42. end
  43. it 'returns :follow for a Follow' do
  44. notification = described_class.new(activity: Follow.new)
  45. expect(notification.type).to eq :follow
  46. end
  47. end
  48. describe 'Setting account from activity_type' do
  49. context 'when activity_type is a Status' do
  50. it 'sets the notification from_account correctly' do
  51. status = Fabricate(:status)
  52. notification = Fabricate.build(:notification, activity_type: 'Status', activity: status)
  53. expect(notification.from_account).to eq(status.account)
  54. end
  55. end
  56. context 'when activity_type is a Follow' do
  57. it 'sets the notification from_account correctly' do
  58. follow = Fabricate(:follow)
  59. notification = Fabricate.build(:notification, activity_type: 'Follow', activity: follow)
  60. expect(notification.from_account).to eq(follow.account)
  61. end
  62. end
  63. context 'when activity_type is a Favourite' do
  64. it 'sets the notification from_account correctly' do
  65. favourite = Fabricate(:favourite)
  66. notification = Fabricate.build(:notification, activity_type: 'Favourite', activity: favourite)
  67. expect(notification.from_account).to eq(favourite.account)
  68. end
  69. end
  70. context 'when activity_type is a FollowRequest' do
  71. it 'sets the notification from_account correctly' do
  72. follow_request = Fabricate(:follow_request)
  73. notification = Fabricate.build(:notification, activity_type: 'FollowRequest', activity: follow_request)
  74. expect(notification.from_account).to eq(follow_request.account)
  75. end
  76. end
  77. context 'when activity_type is a Poll' do
  78. it 'sets the notification from_account correctly' do
  79. poll = Fabricate(:poll)
  80. notification = Fabricate.build(:notification, activity_type: 'Poll', activity: poll)
  81. expect(notification.from_account).to eq(poll.account)
  82. end
  83. end
  84. context 'when activity_type is a Report' do
  85. it 'sets the notification from_account correctly' do
  86. report = Fabricate(:report)
  87. notification = Fabricate.build(:notification, activity_type: 'Report', activity: report)
  88. expect(notification.from_account).to eq(report.account)
  89. end
  90. end
  91. context 'when activity_type is a Mention' do
  92. it 'sets the notification from_account correctly' do
  93. mention = Fabricate(:mention)
  94. notification = Fabricate.build(:notification, activity_type: 'Mention', activity: mention)
  95. expect(notification.from_account).to eq(mention.status.account)
  96. end
  97. end
  98. context 'when activity_type is an Account' do
  99. it 'sets the notification from_account correctly' do
  100. account = Fabricate(:account)
  101. notification = Fabricate.build(:notification, activity_type: 'Account', account: account)
  102. expect(notification.account).to eq(account)
  103. end
  104. end
  105. context 'when activity_type is an AccountWarning' do
  106. it 'sets the notification from_account to the recipient of the notification' do
  107. account = Fabricate(:account)
  108. account_warning = Fabricate(:account_warning, target_account: account)
  109. notification = Fabricate.build(:notification, activity_type: 'AccountWarning', activity: account_warning, account: account)
  110. expect(notification.from_account).to eq(account)
  111. end
  112. end
  113. end
  114. describe '.paginate_groups_by_max_id' do
  115. let(:account) { Fabricate(:account) }
  116. let!(:notifications) do
  117. ['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
  118. .map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
  119. end
  120. context 'without since_id or max_id' do
  121. it 'returns the most recent notifications, only keeping one notification per group' do
  122. expect(described_class.without_suspended.paginate_groups_by_max_id(4).pluck(:id))
  123. .to eq [notifications[7], notifications[6], notifications[4], notifications[2]].pluck(:id)
  124. end
  125. end
  126. context 'with since_id' do
  127. it 'returns the most recent notifications, only keeping one notification per group' do
  128. expect(described_class.without_suspended.paginate_groups_by_max_id(4, since_id: notifications[4].id).pluck(:id))
  129. .to eq [notifications[7], notifications[6]].pluck(:id)
  130. end
  131. end
  132. context 'with max_id' do
  133. it 'returns the most recent notifications after max_id, only keeping one notification per group' do
  134. expect(described_class.without_suspended.paginate_groups_by_max_id(4, max_id: notifications[7].id).pluck(:id))
  135. .to eq [notifications[6], notifications[5], notifications[4], notifications[2]].pluck(:id)
  136. end
  137. end
  138. end
  139. describe '.paginate_groups_by_min_id' do
  140. let(:account) { Fabricate(:account) }
  141. let!(:notifications) do
  142. ['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
  143. .map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
  144. end
  145. context 'without min_id or max_id' do
  146. it 'returns the oldest notifications, only keeping one notification per group' do
  147. expect(described_class.without_suspended.paginate_groups_by_min_id(4).pluck(:id))
  148. .to eq [notifications[0], notifications[2], notifications[3], notifications[4]].pluck(:id)
  149. end
  150. end
  151. context 'with max_id' do
  152. it 'returns the oldest notifications, stopping at max_id, only keeping one notification per group' do
  153. expect(described_class.without_suspended.paginate_groups_by_min_id(4, max_id: notifications[4].id).pluck(:id))
  154. .to eq [notifications[0], notifications[2], notifications[3]].pluck(:id)
  155. end
  156. end
  157. context 'with min_id' do
  158. it 'returns the most oldest notifications after min_id, only keeping one notification per group' do
  159. expect(described_class.without_suspended.paginate_groups_by_min_id(4, min_id: notifications[0].id).pluck(:id))
  160. .to eq [notifications[1], notifications[2], notifications[3], notifications[4]].pluck(:id)
  161. end
  162. end
  163. end
  164. describe '.preload_cache_collection_target_statuses' do
  165. subject do
  166. described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|
  167. # preload account for testing instead of using cache_collection
  168. Status.preload(:account).where(id: target_statuses.map(&:id))
  169. end
  170. end
  171. context 'when notifications are empty' do
  172. let(:notifications) { [] }
  173. it 'returns []' do
  174. expect(subject).to eq []
  175. end
  176. end
  177. context 'when notifications are present' do
  178. before do
  179. notifications.each(&:reload)
  180. end
  181. let(:mention) { Fabricate(:mention) }
  182. let(:status) { Fabricate(:status) }
  183. let(:reblog) { Fabricate(:status, reblog: Fabricate(:status)) }
  184. let(:follow) { Fabricate(:follow) }
  185. let(:follow_request) { Fabricate(:follow_request) }
  186. let(:favourite) { Fabricate(:favourite) }
  187. let(:poll) { Fabricate(:poll) }
  188. let(:notifications) do
  189. [
  190. Fabricate(:notification, type: :mention, activity: mention),
  191. Fabricate(:notification, type: :status, activity: status),
  192. Fabricate(:notification, type: :reblog, activity: reblog),
  193. Fabricate(:notification, type: :follow, activity: follow),
  194. Fabricate(:notification, type: :follow_request, activity: follow_request),
  195. Fabricate(:notification, type: :favourite, activity: favourite),
  196. Fabricate(:notification, type: :poll, activity: poll),
  197. ]
  198. end
  199. context 'with a preloaded target status and a cached status' do
  200. it 'preloads association records and replaces association records' do
  201. expect(subject)
  202. .to contain_exactly(
  203. mention_attributes,
  204. status_attributes,
  205. reblog_attributes,
  206. follow_attributes,
  207. follow_request_attributes,
  208. favourite_attributes,
  209. poll_attributes
  210. )
  211. end
  212. def mention_attributes
  213. have_attributes(
  214. type: :mention,
  215. target_status: eq(mention.status).and(have_loaded_association(:account)),
  216. mention: have_loaded_association(:status)
  217. ).and(have_loaded_association(:mention))
  218. end
  219. def status_attributes
  220. have_attributes(
  221. type: :status,
  222. target_status: eq(status).and(have_loaded_association(:account))
  223. ).and(have_loaded_association(:status))
  224. end
  225. def reblog_attributes
  226. have_attributes(
  227. type: :reblog,
  228. status: have_loaded_association(:reblog),
  229. target_status: eq(reblog.reblog).and(have_loaded_association(:account))
  230. ).and(have_loaded_association(:status))
  231. end
  232. def follow_attributes
  233. have_attributes(
  234. type: :follow,
  235. target_status: be_nil
  236. )
  237. end
  238. def follow_request_attributes
  239. have_attributes(
  240. type: :follow_request,
  241. target_status: be_nil
  242. )
  243. end
  244. def favourite_attributes
  245. have_attributes(
  246. type: :favourite,
  247. favourite: have_loaded_association(:status),
  248. target_status: eq(favourite.status).and(have_loaded_association(:account))
  249. ).and(have_loaded_association(:favourite))
  250. end
  251. def poll_attributes
  252. have_attributes(
  253. type: :poll,
  254. poll: have_loaded_association(:status),
  255. target_status: eq(poll.status).and(have_loaded_association(:account))
  256. ).and(have_loaded_association(:poll))
  257. end
  258. end
  259. end
  260. end
  261. end
  262. RSpec::Matchers.define :have_loaded_association do |association|
  263. match do |record|
  264. record.association(association).loaded?
  265. end
  266. failure_message do |record|
  267. "expected #{record} to have loaded association #{association} but it did not."
  268. end
  269. end