Migrate to request specs in /api/v1/admin/domain_allows (#25333)
				
					
				
			This commit is contained in:
		
							parent
							
								
									841c220c40
								
							
						
					
					
						commit
						4301d8cbb3
					
				
					 3 changed files with 215 additions and 141 deletions
				
			
		| 
						 | 
				
			
			@ -1,140 +0,0 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe Api::V1::Admin::DomainAllowsController do
 | 
			
		||||
  render_views
 | 
			
		||||
 | 
			
		||||
  let(:role)   { UserRole.find_by(name: 'Admin') }
 | 
			
		||||
  let(:user)   { Fabricate(:user, role: role) }
 | 
			
		||||
  let(:scopes) { 'admin:read admin:write' }
 | 
			
		||||
  let(:token)  { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    allow(controller).to receive(:doorkeeper_token) { token }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  shared_examples 'forbidden for wrong scope' do |wrong_scope|
 | 
			
		||||
    let(:scopes) { wrong_scope }
 | 
			
		||||
 | 
			
		||||
    it 'returns http forbidden' do
 | 
			
		||||
      expect(response).to have_http_status(403)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  shared_examples 'forbidden for wrong role' do |wrong_role|
 | 
			
		||||
    let(:role) { UserRole.find_by(name: wrong_role) }
 | 
			
		||||
 | 
			
		||||
    it 'returns http forbidden' do
 | 
			
		||||
      expect(response).to have_http_status(403)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET #index' do
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      get :index
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'returns the expected domain allows' do
 | 
			
		||||
      json = body_as_json
 | 
			
		||||
      expect(json.length).to eq 1
 | 
			
		||||
      expect(json[0][:id].to_i).to eq domain_allow.id
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET #show' do
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      get :show, params: { id: domain_allow.id }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'returns expected domain name' do
 | 
			
		||||
      json = body_as_json
 | 
			
		||||
      expect(json[:domain]).to eq domain_allow.domain
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'DELETE #destroy' do
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      delete :destroy, params: { id: domain_allow.id }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'deletes the block' do
 | 
			
		||||
      expect(DomainAllow.find_by(id: domain_allow.id)).to be_nil
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'POST #create' do
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow, domain: 'example.com') }
 | 
			
		||||
 | 
			
		||||
    context 'with a valid domain' do
 | 
			
		||||
      before do
 | 
			
		||||
        post :create, params: { domain: 'foo.bar.com' }
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
      it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
      it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
      it 'returns http success' do
 | 
			
		||||
        expect(response).to have_http_status(200)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns expected domain name' do
 | 
			
		||||
        json = body_as_json
 | 
			
		||||
        expect(json[:domain]).to eq 'foo.bar.com'
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'creates a domain block' do
 | 
			
		||||
        expect(DomainAllow.find_by(domain: 'foo.bar.com')).to_not be_nil
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'with invalid domain name' do
 | 
			
		||||
      before do
 | 
			
		||||
        post :create, params: { domain: 'foo bar' }
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns http unprocessable entity' do
 | 
			
		||||
        expect(response).to have_http_status(422)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when domain name is not specified' do
 | 
			
		||||
      it 'returns http unprocessable entity' do
 | 
			
		||||
        post :create
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(422)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
Fabricator(:domain_allow) do
 | 
			
		||||
  domain 'MyString'
 | 
			
		||||
  domain { sequence(:domain) { |i| "example#{i}.com" } }
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										214
									
								
								spec/requests/api/v1/admin/domain_allows_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										214
									
								
								spec/requests/api/v1/admin/domain_allows_spec.rb
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,214 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe 'Domain Allows' do
 | 
			
		||||
  let(:role)    { UserRole.find_by(name: 'Admin') }
 | 
			
		||||
  let(:user)    { Fabricate(:user, role: role) }
 | 
			
		||||
  let(:scopes)  { 'admin:read admin:write' }
 | 
			
		||||
  let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
  shared_examples 'forbidden for wrong role' do |wrong_role|
 | 
			
		||||
    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 /api/v1/admin/domain_allows' do
 | 
			
		||||
    subject do
 | 
			
		||||
      get '/api/v1/admin/domain_allows', headers: headers, params: params
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    let(:params) { {} }
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      subject
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when there is no allowed domains' do
 | 
			
		||||
      it 'returns an empty body' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(body_as_json).to be_empty
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when there are allowed domains' do
 | 
			
		||||
      let!(:domain_allows) { Fabricate.times(5, :domain_allow) }
 | 
			
		||||
      let(:expected_response) do
 | 
			
		||||
        domain_allows.map do |domain_allow|
 | 
			
		||||
          {
 | 
			
		||||
            id: domain_allow.id.to_s,
 | 
			
		||||
            domain: domain_allow.domain,
 | 
			
		||||
            created_at: domain_allow.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
 | 
			
		||||
          }
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns the correct allowed domains' 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 allowed domains' do
 | 
			
		||||
          subject
 | 
			
		||||
 | 
			
		||||
          expect(body_as_json.size).to eq(params[:limit])
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET /api/v1/admin/domain_allows/:id' do
 | 
			
		||||
    subject do
 | 
			
		||||
      get "/api/v1/admin/domain_allows/#{domain_allow.id}", headers: headers
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow) }
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      subject
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'returns the expected allowed domain name' do
 | 
			
		||||
      subject
 | 
			
		||||
 | 
			
		||||
      expect(body_as_json[:domain]).to eq domain_allow.domain
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when the requested allowed domain does not exist' do
 | 
			
		||||
      it 'returns http not found' do
 | 
			
		||||
        get '/api/v1/admin/domain_allows/-1', headers: headers
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(404)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'POST /api/v1/admin/domain_allows' do
 | 
			
		||||
    subject do
 | 
			
		||||
      post '/api/v1/admin/domain_allows', headers: headers, params: params
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    let(:params) { { domain: 'foo.bar.com' } }
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    context 'with a valid domain name' do
 | 
			
		||||
      it 'returns http success' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(200)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns the expected domain name' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(body_as_json[:domain]).to eq 'foo.bar.com'
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'creates a domain allow' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(DomainAllow.find_by(domain: 'foo.bar.com')).to be_present
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'with invalid domain name' do
 | 
			
		||||
      let(:params) { 'foo bar' }
 | 
			
		||||
 | 
			
		||||
      it 'returns http unprocessable entity' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(422)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when domain name is not specified' do
 | 
			
		||||
      let(:params) { {} }
 | 
			
		||||
 | 
			
		||||
      it 'returns http unprocessable entity' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(422)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when the domain is already allowed' do
 | 
			
		||||
      before do
 | 
			
		||||
        DomainAllow.create(params)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns the existing allowed domain name' do
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(body_as_json[:domain]).to eq(params[:domain])
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'DELETE /api/v1/admin/domain_allows/:id' do
 | 
			
		||||
    subject do
 | 
			
		||||
      delete "/api/v1/admin/domain_allows/#{domain_allow.id}", headers: headers
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    let!(:domain_allow) { Fabricate(:domain_allow) }
 | 
			
		||||
 | 
			
		||||
    it_behaves_like 'forbidden for wrong scope', 'write:statuses'
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', ''
 | 
			
		||||
    it_behaves_like 'forbidden for wrong role', 'Moderator'
 | 
			
		||||
 | 
			
		||||
    it 'returns http success' do
 | 
			
		||||
      subject
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(200)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'deletes the allowed domain' do
 | 
			
		||||
      subject
 | 
			
		||||
 | 
			
		||||
      expect(DomainAllow.find_by(id: domain_allow.id)).to be_nil
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when the allowed domain does not exist' do
 | 
			
		||||
      it 'returns http not found' do
 | 
			
		||||
        delete '/api/v1/admin/domain_allows/-1', headers: headers
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(404)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
		Reference in a new issue