wardencommunity/warden

Add the possibility to access warden object in test helpers

acaron opened this issue · 12 comments

We need to access to the object contains in env['warden'] for our test. To make that possible, this class: https://github.com/hassox/warden/blob/master/spec/helpers/request_helper.rb is needed. Right now, we have to copy it into our tests helper. We don't have access to env['warden'] since it is not controller tests.

We can do:

class MiniTest::Spec
  include Warden::Spec::Helpers

  before :all do
    Warden.test_mode!
  end

  after do
    Warden.test_reset!
  end

  def warden
    @warden ||= begin
      env = env_with_params
      setup_rack(success_app).call(env)
      env['warden']
    end
  end
end

In our minitest helper and call warden directly. Can you implement a similar solution in Warden tests helper available in /lib?

I'm confused. Do you have an env somewhere? What kind of test are you running where you use warden but do not have a rack env?

@hassox In a gem that act like Devise gem for example. Functionalities must be tested without any controller.

Rack::Mock can help you construct an env hash. Then you just need to get warden into it. If you have something specific in mind I'd totally take a look at a PR.

@hassox how can you get warden into it? Your env_with_params and setup_rack function doesn't do all that (mock then put warden into it)? So those two functions are what I need

Yes I'm guessing it'd be something close to 'lift the spec helper up into the lib' but in a nice way ;)

@hassox yes! that is why I chose to write an issue and not a PR. I'm not sure you would like that if I only copy the class there ;) how do you think it should be done?

Id probably not copy the whole class, just the methods into the existing spec helper. I imagine it would have two parts. 1. Provide an env, 2: add warden to it, that way if you have an env already you can just add warden to it, and if you don't, you can construct an env and add warden to it in one go.

Does that make sense?

Yes, I don't think any other way possible so! I'll post a PR tomorrow after testing adding methods, testing it with my app and add automate tests to it! Thank you

Awesome!

@hassox checkout the pull request and my comment :) thank you!

Hey, I added a comment to the PR but I don't think there was an update.

@hassox yes, I saw! I'm doing this first thing tomorrow morning :) Thank you for being so helpful!