You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

  1. require 'rails_helper'
  2. RSpec.describe FollowService 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(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob')).account }
  8. before do
  9. subject.call(sender, bob.acct)
  10. end
  11. it 'creates a follow request' do
  12. expect(FollowRequest.find_by(account: sender, target_account: bob)).to_not be_nil
  13. end
  14. end
  15. describe 'unlocked account' do
  16. let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  17. before do
  18. subject.call(sender, bob.acct)
  19. end
  20. it 'creates a following relation' do
  21. expect(sender.following?(bob)).to be true
  22. end
  23. end
  24. describe 'already followed account' do
  25. let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  26. before do
  27. sender.follow!(bob)
  28. subject.call(sender, bob.acct)
  29. end
  30. it 'keeps a following relation' do
  31. expect(sender.following?(bob)).to be true
  32. end
  33. end
  34. end
  35. context 'remote account' do
  36. describe 'locked account' do
  37. let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, locked: true, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com')).account }
  38. before do
  39. stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
  40. subject.call(sender, bob.acct)
  41. end
  42. it 'creates a follow request' do
  43. expect(FollowRequest.find_by(account: sender, target_account: bob)).to_not be_nil
  44. end
  45. it 'sends a follow request salmon slap' do
  46. expect(a_request(:post, "http://salmon.example.com/").with { |req|
  47. xml = OStatus2::Salmon.new.unpack(req.body)
  48. xml.match(TagManager::VERBS[:request_friend])
  49. }).to have_been_made.once
  50. end
  51. end
  52. describe 'unlocked account' do
  53. let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com', hub_url: 'http://hub.example.com')).account }
  54. before do
  55. stub_request(:post, "http://salmon.example.com/").to_return(:status => 200, :body => "", :headers => {})
  56. stub_request(:post, "http://hub.example.com/").to_return(status: 202)
  57. subject.call(sender, bob.acct)
  58. end
  59. it 'creates a following relation' do
  60. expect(sender.following?(bob)).to be true
  61. end
  62. it 'sends a follow salmon slap' do
  63. expect(a_request(:post, "http://salmon.example.com/").with { |req|
  64. xml = OStatus2::Salmon.new.unpack(req.body)
  65. xml.match(TagManager::VERBS[:follow])
  66. }).to have_been_made.once
  67. end
  68. it 'subscribes to PuSH' do
  69. expect(a_request(:post, "http://hub.example.com/")).to have_been_made.once
  70. end
  71. end
  72. describe 'already followed account' do
  73. let(:bob) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', domain: 'example.com', salmon_url: 'http://salmon.example.com', hub_url: 'http://hub.example.com')).account }
  74. before do
  75. sender.follow!(bob)
  76. subject.call(sender, bob.acct)
  77. end
  78. it 'keeps a following relation' do
  79. expect(sender.following?(bob)).to be true
  80. end
  81. it 'does not send a follow salmon slap' do
  82. expect(a_request(:post, "http://salmon.example.com/")).not_to have_been_made
  83. end
  84. it 'does not subscribe to PuSH' do
  85. expect(a_request(:post, "http://hub.example.com/")).not_to have_been_made
  86. end
  87. end
  88. end
  89. end