status_threading_concern_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe StatusThreadingConcern do
  4. describe '#ancestors' do
  5. let!(:alice) { Fabricate(:account, username: 'alice') }
  6. let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  7. let!(:jeff) { Fabricate(:account, username: 'jeff') }
  8. let!(:status) { Fabricate(:status, account: alice) }
  9. let!(:reply1) { Fabricate(:status, thread: status, account: jeff) }
  10. let!(:reply2) { Fabricate(:status, thread: reply1, account: bob) }
  11. let!(:reply3) { Fabricate(:status, thread: reply2, account: alice) }
  12. let!(:viewer) { Fabricate(:account, username: 'viewer') }
  13. it 'returns conversation history' do
  14. expect(reply3.ancestors(4)).to include(status, reply1, reply2)
  15. end
  16. it 'does not return conversation history user is not allowed to see' do
  17. reply1.update(visibility: :private)
  18. status.update(visibility: :direct)
  19. expect(reply3.ancestors(4, viewer)).to_not include(reply1, status)
  20. end
  21. it 'does not return conversation history from blocked users' do
  22. viewer.block!(jeff)
  23. expect(reply3.ancestors(4, viewer)).to_not include(reply1)
  24. end
  25. it 'does not return conversation history from muted users' do
  26. viewer.mute!(jeff)
  27. expect(reply3.ancestors(4, viewer)).to_not include(reply1)
  28. end
  29. it 'does not return conversation history from silenced and not followed users' do
  30. jeff.silence!
  31. expect(reply3.ancestors(4, viewer)).to_not include(reply1)
  32. end
  33. it 'does not return conversation history from blocked domains' do
  34. viewer.block_domain!('example.com')
  35. expect(reply3.ancestors(4, viewer)).to_not include(reply2)
  36. end
  37. it 'ignores deleted records' do
  38. first_status = Fabricate(:status, account: bob)
  39. second_status = Fabricate(:status, thread: first_status, account: alice)
  40. # Create cache and delete cached record
  41. second_status.ancestors(4)
  42. first_status.destroy
  43. expect(second_status.ancestors(4)).to eq([])
  44. end
  45. it 'can return more records than previously requested' do
  46. first_status = Fabricate(:status, account: bob)
  47. second_status = Fabricate(:status, thread: first_status, account: alice)
  48. third_status = Fabricate(:status, thread: second_status, account: alice)
  49. # Create cache
  50. second_status.ancestors(1)
  51. expect(third_status.ancestors(2)).to eq([first_status, second_status])
  52. end
  53. it 'can return fewer records than previously requested' do
  54. first_status = Fabricate(:status, account: bob)
  55. second_status = Fabricate(:status, thread: first_status, account: alice)
  56. third_status = Fabricate(:status, thread: second_status, account: alice)
  57. # Create cache
  58. second_status.ancestors(2)
  59. expect(third_status.ancestors(1)).to eq([second_status])
  60. end
  61. end
  62. describe '#descendants' do
  63. let!(:alice) { Fabricate(:account, username: 'alice') }
  64. let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  65. let!(:jeff) { Fabricate(:account, username: 'jeff') }
  66. let!(:status) { Fabricate(:status, account: alice) }
  67. let!(:reply1) { Fabricate(:status, thread: status, account: alice) }
  68. let!(:reply2) { Fabricate(:status, thread: status, account: bob) }
  69. let!(:reply3) { Fabricate(:status, thread: reply1, account: jeff) }
  70. let!(:viewer) { Fabricate(:account, username: 'viewer') }
  71. it 'returns replies' do
  72. expect(status.descendants(4)).to include(reply1, reply2, reply3)
  73. end
  74. it 'does not return replies user is not allowed to see' do
  75. reply1.update(visibility: :private)
  76. reply3.update(visibility: :direct)
  77. expect(status.descendants(4, viewer)).to_not include(reply1, reply3)
  78. end
  79. it 'does not return replies from blocked users' do
  80. viewer.block!(jeff)
  81. expect(status.descendants(4, viewer)).to_not include(reply3)
  82. end
  83. it 'does not return replies from muted users' do
  84. viewer.mute!(jeff)
  85. expect(status.descendants(4, viewer)).to_not include(reply3)
  86. end
  87. it 'does not return replies from silenced and not followed users' do
  88. jeff.silence!
  89. expect(status.descendants(4, viewer)).to_not include(reply3)
  90. end
  91. it 'does not return replies from blocked domains' do
  92. viewer.block_domain!('example.com')
  93. expect(status.descendants(4, viewer)).to_not include(reply2)
  94. end
  95. it 'promotes self-replies to the top while leaving the rest in order' do
  96. a = Fabricate(:status, account: alice)
  97. d = Fabricate(:status, account: jeff, thread: a)
  98. e = Fabricate(:status, account: bob, thread: d)
  99. c = Fabricate(:status, account: alice, thread: a)
  100. f = Fabricate(:status, account: bob, thread: c)
  101. expect(a.descendants(20)).to eq [c, d, e, f]
  102. end
  103. end
  104. end