status_cache_hydrator_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe StatusCacheHydrator do
  4. let(:status) { Fabricate(:status) }
  5. let(:account) { Fabricate(:account) }
  6. describe '#hydrate' do
  7. let(:compare_to_hash) { InlineRenderer.render(status, account, :status) }
  8. shared_examples 'shared behavior' do
  9. context 'when handling a new status' do
  10. let(:poll) { Fabricate(:poll) }
  11. let(:status) { Fabricate(:status, poll: poll) }
  12. it 'renders the same attributes as a full render' do
  13. expect(subject).to eql(compare_to_hash)
  14. end
  15. end
  16. context 'when handling a new status with own poll' do
  17. let(:poll) { Fabricate(:poll, account: account) }
  18. let(:status) { Fabricate(:status, poll: poll, account: account) }
  19. it 'renders the same attributes as a full render' do
  20. expect(subject).to eql(compare_to_hash)
  21. end
  22. end
  23. context 'when handling a filtered status' do
  24. let(:status) { Fabricate(:status, text: 'this toot is about that banned word') }
  25. before do
  26. account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  27. end
  28. it 'renders the same attributes as a full render' do
  29. expect(subject).to eql(compare_to_hash)
  30. end
  31. end
  32. context 'when handling a reblog' do
  33. let(:reblog) { Fabricate(:status) }
  34. let(:status) { Fabricate(:status, reblog: reblog) }
  35. context 'that has been favourited' do
  36. before do
  37. FavouriteService.new.call(account, reblog)
  38. end
  39. it 'renders the same attributes as a full render' do
  40. expect(subject).to eql(compare_to_hash)
  41. end
  42. end
  43. context 'that has been reblogged' do
  44. before do
  45. ReblogService.new.call(account, reblog)
  46. end
  47. it 'renders the same attributes as a full render' do
  48. expect(subject).to eql(compare_to_hash)
  49. end
  50. end
  51. context 'that has been pinned' do
  52. let(:reblog) { Fabricate(:status, account: account) }
  53. before do
  54. StatusPin.create!(account: account, status: reblog)
  55. end
  56. it 'renders the same attributes as a full render' do
  57. expect(subject).to eql(compare_to_hash)
  58. end
  59. end
  60. context 'that has been followed tags' do
  61. let(:followed_tag) { Fabricate(:tag) }
  62. before do
  63. reblog.tags << Fabricate(:tag)
  64. reblog.tags << followed_tag
  65. TagFollow.create!(tag: followed_tag, account: account, rate_limit: false)
  66. end
  67. it 'renders the same attributes as a full render' do
  68. expect(subject).to eql(compare_to_hash)
  69. end
  70. end
  71. context 'that has a poll authored by the user' do
  72. let(:poll) { Fabricate(:poll, account: account) }
  73. let(:reblog) { Fabricate(:status, poll: poll, account: account) }
  74. it 'renders the same attributes as a full render' do
  75. expect(subject).to eql(compare_to_hash)
  76. end
  77. end
  78. context 'that has been voted in' do
  79. let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
  80. let(:reblog) { Fabricate(:status, poll: poll) }
  81. before do
  82. VoteService.new.call(account, poll, [0])
  83. end
  84. it 'renders the same attributes as a full render' do
  85. expect(subject).to eql(compare_to_hash)
  86. end
  87. end
  88. context 'that matches account filters' do
  89. let(:reblog) { Fabricate(:status, text: 'this toot is about that banned word') }
  90. before do
  91. account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  92. end
  93. it 'renders the same attributes as a full render' do
  94. expect(subject).to eql(compare_to_hash)
  95. end
  96. end
  97. end
  98. end
  99. context 'when cache is warm' do
  100. subject do
  101. Rails.cache.write("fan-out/#{status.id}", InlineRenderer.render(status, nil, :status))
  102. described_class.new(status).hydrate(account.id)
  103. end
  104. it_behaves_like 'shared behavior'
  105. end
  106. context 'when cache is cold' do
  107. subject do
  108. Rails.cache.delete("fan-out/#{status.id}")
  109. described_class.new(status).hydrate(account.id)
  110. end
  111. it_behaves_like 'shared behavior'
  112. end
  113. end
  114. end