From 895e157b4dcab61892ee1d4d76b445dd88354ff7 Mon Sep 17 00:00:00 2001 From: Daniel M Brasil Date: Wed, 14 Jun 2023 10:43:50 -0300 Subject: [PATCH] Migrate to request specs in `/api/v1/follow_requests` (#25411) --- .../api/v1/follow_requests_controller_spec.rb | 72 ----------- spec/requests/api/v1/follow_requests_spec.rb | 119 ++++++++++++++++++ 2 files changed, 119 insertions(+), 72 deletions(-) delete mode 100644 spec/controllers/api/v1/follow_requests_controller_spec.rb create mode 100644 spec/requests/api/v1/follow_requests_spec.rb diff --git a/spec/controllers/api/v1/follow_requests_controller_spec.rb b/spec/controllers/api/v1/follow_requests_controller_spec.rb deleted file mode 100644 index 0a2c27d9ed..0000000000 --- a/spec/controllers/api/v1/follow_requests_controller_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Api::V1::FollowRequestsController do - render_views - - let(:user) { Fabricate(:user, account_attributes: { locked: true }) } - let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } - let(:follower) { Fabricate(:account) } - - before do - FollowService.new.call(follower, user.account) - allow(controller).to receive(:doorkeeper_token) { token } - end - - describe 'GET #index' do - let(:scopes) { 'read:follows' } - - before do - get :index, params: { limit: 1 } - end - - it 'returns http success' do - expect(response).to have_http_status(200) - end - end - - describe 'POST #authorize' do - let(:scopes) { 'write:follows' } - - before do - post :authorize, params: { id: follower.id } - end - - it 'returns http success' do - expect(response).to have_http_status(200) - end - - it 'allows follower to follow' do - expect(follower.following?(user.account)).to be true - end - - it 'returns JSON with followed_by=true' do - json = body_as_json - - expect(json[:followed_by]).to be true - end - end - - describe 'POST #reject' do - let(:scopes) { 'write:follows' } - - before do - post :reject, params: { id: follower.id } - end - - it 'returns http success' do - expect(response).to have_http_status(200) - end - - it 'removes follow request' do - expect(FollowRequest.where(target_account: user.account, account: follower).count).to eq 0 - end - - it 'returns JSON with followed_by=false' do - json = body_as_json - - expect(json[:followed_by]).to be false - end - end -end diff --git a/spec/requests/api/v1/follow_requests_spec.rb b/spec/requests/api/v1/follow_requests_spec.rb new file mode 100644 index 0000000000..9d4ef8cd55 --- /dev/null +++ b/spec/requests/api/v1/follow_requests_spec.rb @@ -0,0 +1,119 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Follow requests' do + let(:user) { Fabricate(:user, account_attributes: { locked: true }) } + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } + let(:scopes) { 'read:follows write:follows' } + let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } + + describe 'GET /api/v1/follow_requests' do + subject do + get '/api/v1/follow_requests', headers: headers, params: params + end + + let(:accounts) { Fabricate.times(5, :account) } + let(:params) { {} } + + let(:expected_response) do + accounts.map do |account| + a_hash_including( + id: account.id.to_s, + username: account.username, + acct: account.acct + ) + end + end + + before do + accounts.each { |account| FollowService.new.call(account, user.account) } + end + + it_behaves_like 'forbidden for wrong scope', 'write write:follows' + + it 'returns http success' do + subject + + expect(response).to have_http_status(200) + end + + it 'returns the expected content from accounts requesting to follow' do + subject + + expect(body_as_json).to match_array(expected_response) + end + + context 'with limit param' do + let(:params) { { limit: 2 } } + + it 'returns only the requested number of follow requests' do + subject + + expect(body_as_json.size).to eq(params[:limit]) + end + end + end + + describe 'POST /api/v1/follow_requests/:account_id/authorize' do + subject do + post "/api/v1/follow_requests/#{follower.id}/authorize", headers: headers + end + + let(:follower) { Fabricate(:account) } + + before do + FollowService.new.call(follower, user.account) + end + + it_behaves_like 'forbidden for wrong scope', 'read read:follows' + + it 'returns http success' do + subject + + expect(response).to have_http_status(200) + end + + it 'allows the requesting follower to follow' do + expect { subject }.to change { follower.following?(user.account) }.from(false).to(true) + end + + it 'returns JSON with followed_by set to true' do + subject + + expect(body_as_json[:followed_by]).to be true + end + end + + describe 'POST /api/v1/follow_requests/:account_id/reject' do + subject do + post "/api/v1/follow_requests/#{follower.id}/reject", headers: headers + end + + let(:follower) { Fabricate(:account) } + + before do + FollowService.new.call(follower, user.account) + end + + it_behaves_like 'forbidden for wrong scope', 'read read:follows' + + it 'returns http success' do + subject + + expect(response).to have_http_status(200) + end + + it 'removes the follow request' do + subject + + expect(FollowRequest.where(target_account: user.account, account: follower)).to_not exist + end + + it 'returns JSON with followed_by set to false' do + subject + + expect(body_as_json[:followed_by]).to be false + end + end +end