ip_blocks_controller_spec.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::IpBlocksController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'GET #index' do
  10. it 'returns http success' do
  11. get :index
  12. expect(response).to have_http_status(:success)
  13. end
  14. end
  15. describe 'GET #new' do
  16. it 'returns http success and renders view' do
  17. get :new
  18. expect(response).to have_http_status(:success)
  19. expect(response).to render_template(:new)
  20. end
  21. end
  22. describe 'POST #create' do
  23. context 'with valid data' do
  24. it 'creates a new ip block and redirects' do
  25. expect do
  26. post :create, params: { ip_block: { ip: '1.1.1.1', severity: 'no_access', expires_in: 1.day.to_i.to_s } }
  27. end.to change(IpBlock, :count).by(1)
  28. expect(response).to redirect_to(admin_ip_blocks_path)
  29. expect(flash.notice).to match(I18n.t('admin.ip_blocks.created_msg'))
  30. end
  31. end
  32. context 'with invalid data' do
  33. it 'does not create new a ip block and renders new' do
  34. expect do
  35. post :create, params: { ip_block: { ip: '1.1.1.1' } }
  36. end.to_not change(IpBlock, :count)
  37. expect(response).to have_http_status(:success)
  38. expect(response).to render_template(:new)
  39. end
  40. end
  41. end
  42. end