Skip to content

Commit 57adfd6

Browse files
committed
Clear token auth cache before specs
We don't want to accidentally leak auth state across the specs. However, we can't be sure what other information was stored in the cache which should remain. To be a good Ruby citizen we only clear the cache related to our auth changes. We could have kept track of what was added by our mixin. However that is much more error prone and would require logic to track all the changes. The simple way to handle this is to just clear all caches which match our namespace.
1 parent a23a9e6 commit 57adfd6

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def cache_valid_auth(token, force: false, &generate_cache)
4040
shallow_freeze(val)
4141
end
4242

43+
def clear_auth_cache
44+
Rails.cache.delete_matched TOKEN_AUTH_CACHE_PREFIX + "*"
45+
end
46+
4347
def shallow_freeze(val)
4448
# `nil` is frozen in Ruby 2.2 but not in Ruby 2.1
4549
return val if val.frozen? || val.nil?

lib/kracken/rspec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def authenticate_user_with_token!
7575
c.include Kracken::SpecHelper::Request, type: :kracken
7676
c.include Kracken::SpecHelper::Request, type: :request
7777

78+
c.before do
79+
Kracken::Controllers::TokenAuthenticatable.clear_auth_cache
80+
end
81+
7882
c.before(type: :kracken) do
7983
Kracken::SpecHelper.current_user = nil
8084
end

0 commit comments

Comments
 (0)