threading_concern_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Status::ThreadingConcern 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!(:reply_to_status) { Fabricate(:status, thread: status, account: jeff) }
  10. let!(:reply_to_first_reply) { Fabricate(:status, thread: reply_to_status, account: bob) }
  11. let!(:reply_to_second_reply) { Fabricate(:status, thread: reply_to_first_reply, account: alice) }
  12. let!(:viewer) { Fabricate(:account, username: 'viewer') }
  13. it 'returns conversation history' do
  14. expect(reply_to_second_reply.ancestors(4)).to include(status, reply_to_status, reply_to_first_reply)
  15. end
  16. it 'does not return conversation history user is not allowed to see' do
  17. reply_to_status.update(visibility: :private)
  18. status.update(visibility: :direct)
  19. expect(reply_to_second_reply.ancestors(4, viewer)).to_not include(reply_to_status, status)
  20. end
  21. it 'does not return conversation history from blocked users' do
  22. viewer.block!(jeff)
  23. expect(reply_to_second_reply.ancestors(4, viewer)).to_not include(reply_to_status)
  24. end
  25. it 'does not return conversation history from muted users' do
  26. viewer.mute!(jeff)
  27. expect(reply_to_second_reply.ancestors(4, viewer)).to_not include(reply_to_status)
  28. end
  29. it 'does not return conversation history from silenced and not followed users' do
  30. jeff.silence!
  31. expect(reply_to_second_reply.ancestors(4, viewer)).to_not include(reply_to_status)
  32. end
  33. it 'does not return conversation history from blocked domains' do
  34. viewer.block_domain!('example.com')
  35. expect(reply_to_second_reply.ancestors(4, viewer)).to_not include(reply_to_first_reply)
  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!(:reply_to_status_from_alice) { Fabricate(:status, thread: status, account: alice) }
  68. let!(:reply_to_status_from_bob) { Fabricate(:status, thread: status, account: bob) }
  69. let!(:reply_to_alice_reply_from_jeff) { Fabricate(:status, thread: reply_to_status_from_alice, account: jeff) }
  70. let!(:viewer) { Fabricate(:account, username: 'viewer') }
  71. it 'returns replies' do
  72. expect(status.descendants(4)).to include(reply_to_status_from_alice, reply_to_status_from_bob, reply_to_alice_reply_from_jeff)
  73. end
  74. it 'does not return replies user is not allowed to see' do
  75. reply_to_status_from_alice.update(visibility: :private)
  76. reply_to_alice_reply_from_jeff.update(visibility: :direct)
  77. expect(status.descendants(4, viewer)).to_not include(reply_to_status_from_alice, reply_to_alice_reply_from_jeff)
  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(reply_to_alice_reply_from_jeff)
  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(reply_to_alice_reply_from_jeff)
  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(reply_to_alice_reply_from_jeff)
  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(reply_to_status_from_bob)
  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