Created
December 14, 2015 20:15
-
-
Save lukeledet/af6d3c96dd06d52a2ba2 to your computer and use it in GitHub Desktop.
rack-attack spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
class MockRequest | |
attr_reader :ip, :env | |
def initialize hash | |
@ip = hash[:ip] || '127.0.0.1' | |
@env = hash[:env] || {} | |
end | |
end | |
RSpec.describe 'Block China and Russia' do | |
let(:blacklist) { Rack::Attack.blacklists['block China and Russia'] } | |
it 'has a list of IP addresses to block' do | |
expect(BlockedIP.count).to be > 0 | |
end | |
context 'with a visit from China' do | |
let(:request) { MockRequest.new(ip: '89.22.1.1') } | |
it 'should block the request' do | |
expect(blacklist[request]).to be_truthy | |
end | |
it 'should be fast' do | |
expect(Benchmark.realtime { blacklist[request] }).to be < 0.001 | |
end | |
end | |
context 'with a visit from the US of A' do | |
let(:request) { MockRequest.new(ip: '70.183.119.185') } | |
it 'should not block the request' do | |
expect(blacklist[request]).to be_falsey | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment