follow_requests_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Follow requests' do
  4. let(:user) { Fabricate(:user, account_attributes: { locked: true }) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:follows write:follows' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/follow_requests' do
  9. subject do
  10. get '/api/v1/follow_requests', headers: headers, params: params
  11. end
  12. let(:accounts) { Fabricate.times(2, :account) }
  13. let(:params) { {} }
  14. let(:expected_response) do
  15. accounts.map do |account|
  16. a_hash_including(
  17. id: account.id.to_s,
  18. username: account.username,
  19. acct: account.acct
  20. )
  21. end
  22. end
  23. before do
  24. accounts.each { |account| FollowService.new.call(account, user.account) }
  25. end
  26. it_behaves_like 'forbidden for wrong scope', 'write write:follows'
  27. it 'returns the expected content from accounts requesting to follow', :aggregate_failures do
  28. subject
  29. expect(response).to have_http_status(200)
  30. expect(response.content_type)
  31. .to start_with('application/json')
  32. expect(response.parsed_body).to match_array(expected_response)
  33. end
  34. context 'with limit param' do
  35. let(:params) { { limit: 1 } }
  36. it 'returns only the requested number of follow requests' do
  37. subject
  38. expect(response.parsed_body.size).to eq(params[:limit])
  39. end
  40. end
  41. end
  42. describe 'POST /api/v1/follow_requests/:account_id/authorize' do
  43. subject do
  44. post "/api/v1/follow_requests/#{follower.id}/authorize", headers: headers
  45. end
  46. let(:follower) { Fabricate(:account) }
  47. before do
  48. FollowService.new.call(follower, user.account)
  49. end
  50. it_behaves_like 'forbidden for wrong scope', 'read read:follows'
  51. it 'allows the requesting follower to follow', :aggregate_failures do
  52. expect { subject }.to change { follower.following?(user.account) }.from(false).to(true)
  53. expect(response).to have_http_status(200)
  54. expect(response.content_type)
  55. .to start_with('application/json')
  56. expect(response.parsed_body[:followed_by]).to be true
  57. end
  58. end
  59. describe 'POST /api/v1/follow_requests/:account_id/reject' do
  60. subject do
  61. post "/api/v1/follow_requests/#{follower.id}/reject", headers: headers
  62. end
  63. let(:follower) { Fabricate(:account) }
  64. before do
  65. FollowService.new.call(follower, user.account)
  66. end
  67. it_behaves_like 'forbidden for wrong scope', 'read read:follows'
  68. it 'removes the follow request', :aggregate_failures do
  69. subject
  70. expect(response).to have_http_status(200)
  71. expect(response.content_type)
  72. .to start_with('application/json')
  73. expect(FollowRequest.where(target_account: user.account, account: follower)).to_not exist
  74. expect(response.parsed_body[:followed_by]).to be false
  75. end
  76. end
  77. end