File: cache_store_config_for_fail2ban_spec.rb

package info (click to toggle)
ruby-rack-attack 6.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 2,626; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require_relative "../spec_helper"
require "minitest/stub_const"

describe "Cache store config when using fail2ban" do
  before do
    Rack::Attack.blocklist("fail2ban pentesters") do |request|
      Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do
        request.path.include?("private-place")
      end
    end
  end

  it "gives semantic error if no store was configured" do
    assert_raises(Rack::Attack::MissingStoreError) do
      get "/private-place"
    end
  end

  it "gives semantic error if store is missing #read method" do
    raised_exception = nil

    fake_store_class = Class.new do
      def write(key, value); end

      def increment(key, count, options = {}); end
    end

    Object.stub_const(:FakeStore, fake_store_class) do
      Rack::Attack.cache.store = FakeStore.new

      raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
        get "/private-place"
      end
    end

    assert_equal "Configured store FakeStore doesn't respond to #read method", raised_exception.message
  end

  it "gives semantic error if store is missing #write method" do
    raised_exception = nil

    fake_store_class = Class.new do
      def read(key); end

      def increment(key, count, options = {}); end
    end

    Object.stub_const(:FakeStore, fake_store_class) do
      Rack::Attack.cache.store = FakeStore.new

      raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
        get "/private-place"
      end
    end

    assert_equal "Configured store FakeStore doesn't respond to #write method", raised_exception.message
  end

  it "gives semantic error if store is missing #increment method" do
    raised_exception = nil

    fake_store_class = Class.new do
      def read(key); end

      def write(key, value); end
    end

    Object.stub_const(:FakeStore, fake_store_class) do
      Rack::Attack.cache.store = FakeStore.new

      raised_exception = assert_raises(Rack::Attack::MisconfiguredStoreError) do
        get "/private-place"
      end
    end

    assert_equal "Configured store FakeStore doesn't respond to #increment method", raised_exception.message
  end

  it "works with any object that responds to #read, #write and #increment" do
    FakeStore = Class.new do
      attr_accessor :backend

      def initialize
        @backend = {}
      end

      def read(key)
        @backend[key]
      end

      def write(key, value, _options = {})
        @backend[key] = value
      end

      def increment(key, _count, _options = {})
        @backend[key] ||= 0
        @backend[key] += 1
      end
    end

    Rack::Attack.cache.store = FakeStore.new

    get "/"
    assert_equal 200, last_response.status

    get "/private-place"
    assert_equal 403, last_response.status

    get "/private-place"
    assert_equal 403, last_response.status

    get "/"
    assert_equal 403, last_response.status
  end
end