~cytrogen/masto-fe

b4e19f9610e97066b6f5881324d440d49c3fb31d — Daniel M Brasil 2 years ago 4301d8c
Migrate to request specs in `/api/v1/admin/ip_blocks` (#25331)

R spec/controllers/api/v1/admin/ip_blocks_controller_spec.rb => spec/requests/api/v1/admin/ip_blocks_spec.rb +88 -122
@@ 2,22 2,19 @@

require 'rails_helper'

describe Api::V1::Admin::IpBlocksController do
  render_views

RSpec.describe 'IP Blocks' do
  let(:role)    { UserRole.find_by(name: 'Admin') }
  let(:user)    { Fabricate(:user, role: role) }
  let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  let(:scopes)  { 'admin:read:ip_blocks admin:write:ip_blocks' }

  before do
    allow(controller).to receive(:doorkeeper_token) { token }
  end
  let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

  shared_examples 'forbidden for wrong scope' do |wrong_scope|
    let(:scopes) { wrong_scope }

    it 'returns http forbidden' do
      subject

      expect(response).to have_http_status(403)
    end
  end


@@ 26,41 23,34 @@ describe Api::V1::Admin::IpBlocksController do
    let(:role) { UserRole.find_by(name: wrong_role) }

    it 'returns http forbidden' do
      subject

      expect(response).to have_http_status(403)
    end
  end

  describe 'GET #index' do
    context 'with wrong scope' do
      before do
        get :index
      end

      it_behaves_like 'forbidden for wrong scope', 'admin:write:ip_blocks'
  describe 'GET /api/v1/admin/ip_blocks' do
    subject do
      get '/api/v1/admin/ip_blocks', headers: headers, params: params
    end

    context 'with wrong role' do
      before do
        get :index
      end
    let(:params) { {} }

      it_behaves_like 'forbidden for wrong role', ''
      it_behaves_like 'forbidden for wrong role', 'Moderator'
    end
    it_behaves_like 'forbidden for wrong scope', 'admin:write:ip_blocks'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      get :index
      subject

      expect(response).to have_http_status(200)
    end

    context 'when there is no ip block' do
      it 'returns an empty body' do
        get :index

        json = body_as_json
        subject

        expect(json).to be_empty
        expect(body_as_json).to be_empty
      end
    end



@@ 86,56 76,42 @@ describe Api::V1::Admin::IpBlocksController do
      end

      it 'returns the correct blocked ips' do
        get :index

        json = body_as_json
        subject

        expect(json).to match_array(expected_response)
        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 ip blocks' do
          get :index, params: params
          subject

          json = body_as_json

          expect(json.size).to eq(params[:limit])
          expect(body_as_json.size).to eq(params[:limit])
        end
      end
    end
  end

  describe 'GET #show' do
    let!(:ip_block) { IpBlock.create(ip: '192.0.2.0/24', severity: :no_access) }
    let(:params) { { id: ip_block.id } }

    context 'with wrong scope' do
      before do
        get :show, params: params
      end

      it_behaves_like 'forbidden for wrong scope', 'admin:write:ip_blocks'
  describe 'GET /api/v1/admin/ip_blocks/:id' do
    subject do
      get "/api/v1/admin/ip_blocks/#{ip_block.id}", headers: headers
    end

    context 'with wrong role' do
      before do
        get :show, params: params
      end
    let!(:ip_block) { IpBlock.create(ip: '192.0.2.0/24', severity: :no_access) }

      it_behaves_like 'forbidden for wrong role', ''
      it_behaves_like 'forbidden for wrong role', 'Moderator'
    end
    it_behaves_like 'forbidden for wrong scope', 'admin:write:ip_blocks'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      get :show, params: params
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the correct ip block' do
      get :show, params: params
      subject

      json = body_as_json



@@ 145,41 121,32 @@ describe Api::V1::Admin::IpBlocksController do

    context 'when ip block does not exist' do
      it 'returns http not found' do
        get :show, params: { id: 0 }
        get '/api/v1/admin/ip_blocks/-1', headers: headers

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'POST #create' do
    let(:params) { { ip: '151.0.32.55', severity: 'no_access', comment: 'Spam' } }

    context 'with wrong scope' do
      before do
        post :create, params: params
      end

      it_behaves_like 'forbidden for wrong scope', 'admin:read:ip_blocks'
  describe 'POST /api/v1/admin/ip_blocks' do
    subject do
      post '/api/v1/admin/ip_blocks', headers: headers, params: params
    end

    context 'with wrong role' do
      before do
        post :create, params: params
      end
    let(:params) { { ip: '151.0.32.55', severity: 'no_access', comment: 'Spam' } }

      it_behaves_like 'forbidden for wrong role', ''
      it_behaves_like 'forbidden for wrong role', 'Moderator'
    end
    it_behaves_like 'forbidden for wrong scope', 'admin:read:ip_blocks'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      post :create, params: params
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the correct ip block' do
      post :create, params: params
      subject

      json = body_as_json



@@ 188,119 155,118 @@ describe Api::V1::Admin::IpBlocksController do
      expect(json[:comment]).to eq(params[:comment])
    end

    context 'when ip is not provided' do
    context 'when the required ip param is not provided' do
      let(:params) { { ip: '', severity: 'no_access' } }

      it 'returns http unprocessable entity' do
        post :create, params: params
        subject

        expect(response).to have_http_status(422)
      end
    end

    context 'when severity is not provided' do
    context 'when the required severity param is not provided' do
      let(:params) { { ip: '173.65.23.1', severity: '' } }

      it 'returns http unprocessable entity' do
        post :create, params: params
        subject

        expect(response).to have_http_status(422)
      end
    end

    context 'when provided ip is already blocked' do
    context 'when the given ip address is already blocked' do
      before do
        IpBlock.create(params)
      end

      it 'returns http unprocessable entity' do
        post :create, params: params
        subject

        expect(response).to have_http_status(422)
      end
    end

    context 'when provided ip address is invalid' do
    context 'when the given ip address is invalid' do
      let(:params) { { ip: '520.13.54.120', severity: 'no_access' } }

      it 'returns http unprocessable entity' do
        post :create, params: params
        subject

        expect(response).to have_http_status(422)
      end
    end
  end

  describe 'PUT #update' do
    context 'when ip block exists' do
      let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access', comment: 'Spam', expires_in: 48.hours) }
      let(:params) { { id: ip_block.id, severity: 'sign_up_requires_approval', comment: 'Decreasing severity' } }
  describe 'PUT /api/v1/admin/ip_blocks/:id' do
    subject do
      put "/api/v1/admin/ip_blocks/#{ip_block.id}", headers: headers, params: params
    end

      it 'returns http success' do
        put :update, params: params
    let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access', comment: 'Spam', expires_in: 48.hours) }
    let(:params)    { { severity: 'sign_up_requires_approval', comment: 'Decreasing severity' } }

        expect(response).to have_http_status(200)
      end
    it 'returns http success' do
      subject

      it 'returns the correct ip block' do
        put :update, params: params
      expect(response).to have_http_status(200)
    end

        json = body_as_json
    it 'returns the correct ip block' do
      subject

        expect(json).to match(hash_including({
          ip: "#{ip_block.ip}/#{ip_block.ip.prefix}",
          severity: 'sign_up_requires_approval',
          comment: 'Decreasing severity',
        }))
      end
      expect(body_as_json).to match(hash_including({
        ip: "#{ip_block.ip}/#{ip_block.ip.prefix}",
        severity: 'sign_up_requires_approval',
        comment: 'Decreasing severity',
      }))
    end

      it 'updates the severity correctly' do
        expect { put :update, params: params }.to change { ip_block.reload.severity }.from('no_access').to('sign_up_requires_approval')
      end
    it 'updates the severity correctly' do
      expect { subject }.to change { ip_block.reload.severity }.from('no_access').to('sign_up_requires_approval')
    end

      it 'updates the comment correctly' do
        expect { put :update, params: params }.to change { ip_block.reload.comment }.from('Spam').to('Decreasing severity')
      end
    it 'updates the comment correctly' do
      expect { subject }.to change { ip_block.reload.comment }.from('Spam').to('Decreasing severity')
    end

    context 'when ip block does not exist' do
      it 'returns http not found' do
        put :update, params: { id: 0 }
        put '/api/v1/admin/ip_blocks/-1', headers: headers, params: params

        expect(response).to have_http_status(404)
      end
    end
  end

  describe 'DELETE #destroy' do
    context 'when ip block exists' do
      let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access') }
      let(:params) { { id: ip_block.id } }
  describe 'DELETE /api/v1/admin/ip_blocks/:id' do
    subject do
      delete "/api/v1/admin/ip_blocks/#{ip_block.id}", headers: headers
    end

      it 'returns http success' do
        delete :destroy, params: params
    let!(:ip_block) { IpBlock.create(ip: '185.200.13.3', severity: 'no_access') }

        expect(response).to have_http_status(200)
      end
    it 'returns http success' do
      subject

      it 'returns an empty body' do
        delete :destroy, params: params
      expect(response).to have_http_status(200)
    end

        json = body_as_json
    it 'returns an empty body' do
      subject

        expect(json).to be_empty
      end
      expect(body_as_json).to be_empty
    end

      it 'deletes the ip block' do
        delete :destroy, params: params
    it 'deletes the ip block' do
      subject

        expect(IpBlock.find_by(id: ip_block.id)).to be_nil
      end
      expect(IpBlock.find_by(id: ip_block.id)).to be_nil
    end

    context 'when ip block does not exist' do
      it 'returns http not found' do
        delete :destroy, params: { id: 0 }
        delete '/api/v1/admin/ip_blocks/-1', headers: headers

        expect(response).to have_http_status(404)
      end