I am building a Rails JSON API and attempting to write tests to ensure requests without a valid auth_token are met with a 401 status.
I have included the Devise test helpers in both my spec_helper.rb (config.include Devise::TestHelpers, type: :controller
) and in my controller spec (shown below). I am also setting @request.env['devise.mapping'] = Devise.mappings[:user]
before each test. All of my tests where the user is properly authenticated are working fine, but any unauthorized requests result in:
ArgumentError:
uncaught throw :warden
Since Devise has removed their implementation of :token_authenticatable, I have adapted the more secure solution described here.
It may also be worth noting that I am using the rails-api gem, so my ApplicationController inherits from ActionController::API rather than Base.
I have scoured the Internet and many, many Stack Overflow questions regarding this error and tried everything I could think of but no luck. To the best of my knowledge, the Devise test helpers alone should be enough, as they are supposed to catch :warden throws and give the appropriate 401.
Any suggestions you all could provide would be greatly appreciated, and I can amend this question with more details if necessary. Thanks!
ArgumentError
? – p4sh4