follow_service_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. require 'rails_helper'
  2. RSpec.describe FollowService, type: :service do
  3. let(:sender) { Fabricate(:account, username: 'alice') }
  4. subject { FollowService.new }
  5. context 'local account' do
  6. describe 'locked account' do
  7. let(:bob) { Fabricate(:account, locked: true, username: 'bob') }
  8. before do
  9. subject.call(sender, bob)
  10. end
  11. it 'creates a follow request with reblogs' do
  12. expect(FollowRequest.find_by(account: sender, target_account: bob, show_reblogs: true)).to_not be_nil
  13. end
  14. end
  15. describe 'locked account, no reblogs' do
  16. let(:bob) { Fabricate(:account, locked: true, username: 'bob') }
  17. before do
  18. subject.call(sender, bob, reblogs: false)
  19. end
  20. it 'creates a follow request without reblogs' do
  21. expect(FollowRequest.find_by(account: sender, target_account: bob, show_reblogs: false)).to_not be_nil
  22. end
  23. end
  24. describe 'unlocked account, from silenced account' do
  25. let(:bob) { Fabricate(:account, username: 'bob') }
  26. before do
  27. sender.touch(:silenced_at)
  28. subject.call(sender, bob)
  29. end
  30. it 'creates a follow request with reblogs' do
  31. expect(FollowRequest.find_by(account: sender, target_account: bob, show_reblogs: true)).to_not be_nil
  32. end
  33. end
  34. describe 'unlocked account, from a muted account' do
  35. let(:bob) { Fabricate(:account, username: 'bob') }
  36. before do
  37. bob.mute!(sender)
  38. subject.call(sender, bob)
  39. end
  40. it 'creates a following relation with reblogs' do
  41. expect(sender.following?(bob)).to be true
  42. expect(sender.muting_reblogs?(bob)).to be false
  43. end
  44. end
  45. describe 'unlocked account' do
  46. let(:bob) { Fabricate(:account, username: 'bob') }
  47. before do
  48. subject.call(sender, bob)
  49. end
  50. it 'creates a following relation with reblogs' do
  51. expect(sender.following?(bob)).to be true
  52. expect(sender.muting_reblogs?(bob)).to be false
  53. end
  54. end
  55. describe 'unlocked account, no reblogs' do
  56. let(:bob) { Fabricate(:account, username: 'bob') }
  57. before do
  58. subject.call(sender, bob, reblogs: false)
  59. end
  60. it 'creates a following relation without reblogs' do
  61. expect(sender.following?(bob)).to be true
  62. expect(sender.muting_reblogs?(bob)).to be true
  63. end
  64. end
  65. describe 'already followed account' do
  66. let(:bob) { Fabricate(:account, username: 'bob') }
  67. before do
  68. sender.follow!(bob)
  69. subject.call(sender, bob)
  70. end
  71. it 'keeps a following relation' do
  72. expect(sender.following?(bob)).to be true
  73. end
  74. end
  75. describe 'already followed account, turning reblogs off' do
  76. let(:bob) { Fabricate(:account, username: 'bob') }
  77. before do
  78. sender.follow!(bob, reblogs: true)
  79. subject.call(sender, bob, reblogs: false)
  80. end
  81. it 'disables reblogs' do
  82. expect(sender.muting_reblogs?(bob)).to be true
  83. end
  84. end
  85. describe 'already followed account, turning reblogs on' do
  86. let(:bob) { Fabricate(:account, username: 'bob') }
  87. before do
  88. sender.follow!(bob, reblogs: false)
  89. subject.call(sender, bob, reblogs: true)
  90. end
  91. it 'disables reblogs' do
  92. expect(sender.muting_reblogs?(bob)).to be false
  93. end
  94. end
  95. describe 'already followed account, changing languages' do
  96. let(:bob) { Fabricate(:account, username: 'bob') }
  97. before do
  98. sender.follow!(bob)
  99. subject.call(sender, bob, languages: %w(en es))
  100. end
  101. it 'changes languages' do
  102. expect(Follow.find_by(account: sender, target_account: bob)&.languages).to match_array %w(en es)
  103. end
  104. end
  105. end
  106. context 'remote ActivityPub account' do
  107. let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  108. before do
  109. stub_request(:post, "http://example.com/inbox").to_return(status: 200, body: "", headers: {})
  110. subject.call(sender, bob)
  111. end
  112. it 'creates follow request' do
  113. expect(FollowRequest.find_by(account: sender, target_account: bob)).to_not be_nil
  114. end
  115. it 'sends a follow activity to the inbox' do
  116. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
  117. end
  118. end
  119. end