Skip to content

Commit 75e38c8

Browse files
committed
Fix current_user for use with token_authorize
The old token auth logic, which mimiced the session based behavior, did it by overwriting methods on the model. Now that we are using the `token_authorize` helper to we have an expectation that the remainder of the `TokenAuthenticatable` module works as designed. Unfortunately it doesn't. Ideally the patching of the `TokenAuthenticatable` module would simply be removed. However, that would break all of the existing specs which rely on it (as they are not properly sending the auth headers). Until we fix those we need to continue to support this wonky old style. The original `TokenAuthenticatable` looks the user up when `current_user` is called. It does this by calling `find` which raises an exception when the user can't be found. This would break our specs as the patched `authenticate_user_with_token!` will end up calling `find` before appropriate auth data is set; thus raising the exception. To get around this we check to see if this data is populated (thus assuming token authentication was successful) before delegating to the original logic calling `find`.
1 parent 57adfd6 commit 75e38c8

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/kracken/rspec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ def current_user
4949
end
5050
end
5151
module TokenAuthenticatable
52+
alias_method :__original_user__, :current_user
5253
def current_user
53-
Kracken::SpecHelper.current_user
54+
Kracken::SpecHelper.current_user or
55+
(current_user_id && __original_user__)
5456
end
5557

5658
alias_method :__original_auth__, :authenticate_user_with_token!

0 commit comments

Comments
 (0)