follow_service_spec.rb 4.3 KB

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