4
votes

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!

1
What lines of code exactly throw the ArgumentError?p4sh4

1 Answers

0
votes

I recently had the same issue and found a thread on GitHub. I haven't actually had a chance to test the answer I received yet, but I figured I'd provide it for you anyways - maybe it'll work for you.

https://github.com/plataformatec/devise/issues/1029#issuecomment-74778193