1
0

ip_block_spec.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe IpBlock do
  4. describe 'Validations' do
  5. subject { Fabricate.build :ip_block }
  6. it { is_expected.to validate_presence_of(:ip) }
  7. it { is_expected.to validate_presence_of(:severity) }
  8. it { is_expected.to validate_uniqueness_of(:ip) }
  9. end
  10. describe '#to_log_human_identifier' do
  11. let(:ip_block) { described_class.new(ip: '192.168.0.1') }
  12. it 'combines the IP and prefix into a string' do
  13. result = ip_block.to_log_human_identifier
  14. expect(result).to eq('192.168.0.1/32')
  15. end
  16. end
  17. describe '.blocked?' do
  18. context 'when the IP is blocked' do
  19. it 'returns true' do
  20. described_class.create!(ip: '127.0.0.1', severity: :no_access)
  21. expect(described_class.blocked?('127.0.0.1')).to be true
  22. end
  23. end
  24. context 'when the IP is not blocked' do
  25. it 'returns false' do
  26. expect(described_class.blocked?('127.0.0.1')).to be false
  27. end
  28. end
  29. end
  30. describe 'after_commit' do
  31. it 'resets the cache' do
  32. allow(Rails.cache).to receive(:delete)
  33. described_class.create!(ip: '127.0.0.1', severity: :no_access)
  34. expect(Rails.cache).to have_received(:delete).with(described_class::CACHE_KEY)
  35. end
  36. end
  37. end