Skip to content

Commit a23a9e6

Browse files
committed
Add token authorization spec helper
This adds a new RSpec authorization helper designed specifically for API endpoints. This leverages the fact that token authorization is temporarily cached to avoid making an external API request to the configured kracken server. This type of helper is necessary as the newer Rails 5 `ActionController::API` base class does not mix in cookie or session support by default. Because of this the existing `sign_in` helper is not properly suited for these controllers. Additionally, for API endpoints the `sign_in` helper hides the fact that authentication _must_ be provided through the `Authorization` header; instead of assuming it was set previously in the session. This could cause some edge cases bugs to slip through where authorization isn't getting set, checked, or goes through the proper process.
1 parent 0566187 commit a23a9e6

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def request_http_token_authentication(realm = 'Application')
3333

3434
TOKEN_AUTH_CACHE_PREFIX = "auth/token/"
3535

36-
def cache_valid_auth(token, &generate_cache)
36+
def cache_valid_auth(token, force: false, &generate_cache)
3737
cache_key = TOKEN_AUTH_CACHE_PREFIX + token
38-
val = Rails.cache.read(cache_key)
38+
val = Rails.cache.read(cache_key) unless force
3939
val ||= store_valid_auth(cache_key, &generate_cache)
4040
shallow_freeze(val)
4141
end

lib/kracken/rspec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ module Request
1414
def sign_in(user = nil)
1515
Kracken::SpecHelper.current_user = user
1616
end
17+
18+
def token_authorize(user, token:)
19+
Kracken::Controllers::TokenAuthenticatable::cache_valid_auth(token, force: true) do
20+
{ id: user.id, team_ids: user.team_ids }
21+
end
22+
end
1723
end
1824

1925
module Controller

0 commit comments

Comments
 (0)