File: rack_attack_reset_spec.rb

package info (click to toggle)
ruby-rack-attack 6.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: ruby: 2,689; makefile: 4
file content (90 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download
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
# frozen_string_literal: true

require_relative "spec_helper"

describe "Rack::Attack.reset!" do
  it "raises an error when is not supported by cache store" do
    Rack::Attack.cache.store = Class.new
    assert_raises(Rack::Attack::IncompatibleStoreError) do
      Rack::Attack.reset!
    end
  end

  if defined?(Redis)
    it "should delete rack attack keys" do
      redis = Redis.new
      redis.set("key", "value")
      redis.set("#{Rack::Attack.cache.prefix}::key", "value")
      Rack::Attack.cache.store = redis
      Rack::Attack.reset!

      _(redis.get("key")).must_equal "value"
      _(redis.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil
    end
  end

  if defined?(Redis::Store)
    it "should delete rack attack keys" do
      redis_store = Redis::Store.new
      redis_store.set("key", "value")
      redis_store.set("#{Rack::Attack.cache.prefix}::key", "value")
      Rack::Attack.cache.store = redis_store
      Rack::Attack.reset!

      _(redis_store.get("key")).must_equal "value"
      _(redis_store.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil
    end
  end

  if defined?(Redis) && defined?(ActiveSupport::Cache::RedisCacheStore)
    it "should delete rack attack keys" do
      redis_cache_store = ActiveSupport::Cache::RedisCacheStore.new
      redis_cache_store.write("key", "value")
      redis_cache_store.write("#{Rack::Attack.cache.prefix}::key", "value")
      Rack::Attack.cache.store = redis_cache_store
      Rack::Attack.reset!

      _(redis_cache_store.read("key")).must_equal "value"
      _(redis_cache_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
    end

    describe "with a namespaced cache" do
      it "should delete rack attack keys" do
        redis_cache_store = ActiveSupport::Cache::RedisCacheStore.new(namespace: "ns")
        redis_cache_store.write("key", "value")
        redis_cache_store.write("#{Rack::Attack.cache.prefix}::key", "value")
        Rack::Attack.cache.store = redis_cache_store
        Rack::Attack.reset!

        _(redis_cache_store.read("key")).must_equal "value"
        _(redis_cache_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
      end
    end
  end

  if defined?(ActiveSupport::Cache::MemoryStore)
    it "should delete rack attack keys" do
      memory_store = ActiveSupport::Cache::MemoryStore.new
      memory_store.write("key", "value")
      memory_store.write("#{Rack::Attack.cache.prefix}::key", "value")
      Rack::Attack.cache.store = memory_store
      Rack::Attack.reset!

      _(memory_store.read("key")).must_equal "value"
      _(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
    end

    describe "with a namespaced cache" do
      it "should delete rack attack keys" do
        memory_store = ActiveSupport::Cache::MemoryStore.new(namespace: "ns")
        memory_store.write("key", "value")
        memory_store.write("#{Rack::Attack.cache.prefix}::key", "value")
        Rack::Attack.cache.store = memory_store
        Rack::Attack.reset!

        _(memory_store.read("key")).must_equal "value"
        _(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
      end
    end
  end
end