|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "spec_helper" |
| 4 | + |
| 5 | +describe "Rack::Attack.reset!" do |
| 6 | + it "raises an error when is not supported by cache store" do |
| 7 | + Rack::Attack.cache.store = Class.new |
| 8 | + assert_raises(Rack::Attack::IncompatibleStoreError) do |
| 9 | + Rack::Attack.reset! |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + if defined?(Redis) |
| 14 | + it "should delete rack attack keys" do |
| 15 | + redis = Redis.new |
| 16 | + redis.set("key", "value") |
| 17 | + redis.set("#{Rack::Attack.cache.prefix}::key", "value") |
| 18 | + Rack::Attack.cache.store = redis |
| 19 | + Rack::Attack.reset! |
| 20 | + |
| 21 | + _(redis.get("key")).must_equal "value" |
| 22 | + _(redis.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + if defined?(Redis::Store) |
| 27 | + it "should delete rack attack keys" do |
| 28 | + redis_store = Redis::Store.new |
| 29 | + redis_store.set("key", "value") |
| 30 | + redis_store.set("#{Rack::Attack.cache.prefix}::key", "value") |
| 31 | + Rack::Attack.cache.store = redis_store |
| 32 | + Rack::Attack.reset! |
| 33 | + |
| 34 | + _(redis_store.get("key")).must_equal "value" |
| 35 | + _(redis_store.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + if defined?(Redis) && defined?(ActiveSupport::Cache::RedisCacheStore) |
| 40 | + it "should delete rack attack keys" do |
| 41 | + redis_cache_store = ActiveSupport::Cache::RedisCacheStore.new |
| 42 | + redis_cache_store.write("key", "value") |
| 43 | + redis_cache_store.write("#{Rack::Attack.cache.prefix}::key", "value") |
| 44 | + Rack::Attack.cache.store = redis_cache_store |
| 45 | + Rack::Attack.reset! |
| 46 | + |
| 47 | + _(redis_cache_store.read("key")).must_equal "value" |
| 48 | + _(redis_cache_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 49 | + end |
| 50 | + |
| 51 | + describe "with a namespaced cache" do |
| 52 | + it "should delete rack attack keys" do |
| 53 | + redis_cache_store = ActiveSupport::Cache::RedisCacheStore.new(namespace: "ns") |
| 54 | + redis_cache_store.write("key", "value") |
| 55 | + redis_cache_store.write("#{Rack::Attack.cache.prefix}::key", "value") |
| 56 | + Rack::Attack.cache.store = redis_cache_store |
| 57 | + Rack::Attack.reset! |
| 58 | + |
| 59 | + _(redis_cache_store.read("key")).must_equal "value" |
| 60 | + _(redis_cache_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + if defined?(ActiveSupport::Cache::MemoryStore) |
| 66 | + it "should delete rack attack keys" do |
| 67 | + memory_store = ActiveSupport::Cache::MemoryStore.new |
| 68 | + memory_store.write("key", "value") |
| 69 | + memory_store.write("#{Rack::Attack.cache.prefix}::key", "value") |
| 70 | + Rack::Attack.cache.store = memory_store |
| 71 | + Rack::Attack.reset! |
| 72 | + |
| 73 | + _(memory_store.read("key")).must_equal "value" |
| 74 | + _(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 75 | + end |
| 76 | + |
| 77 | + describe "with a namespaced cache" do |
| 78 | + it "should delete rack attack keys" do |
| 79 | + memory_store = ActiveSupport::Cache::MemoryStore.new(namespace: "ns") |
| 80 | + memory_store.write("key", "value") |
| 81 | + memory_store.write("#{Rack::Attack.cache.prefix}::key", "value") |
| 82 | + Rack::Attack.cache.store = memory_store |
| 83 | + Rack::Attack.reset! |
| 84 | + |
| 85 | + _(memory_store.read("key")).must_equal "value" |
| 86 | + _(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments