Giter VIP home page Giter VIP logo

warden-rspec-rails's Introduction

What

Rails controller spec helpers for Warden. If you're using Warden without Devise in rails, due to how ActionController sets up the test environment, custom test setup code is necessary.

Usage

# Gemfile
group :test do
  gem 'warden-rspec-rails'
end

# spec_helper.rb
RSpec.configure do |c|
  c.include Warden::Test::ControllerHelpers, type: :controller
end

This will define helper methods in controller tests that you can use to manage authentication, such as:

  • warden: Access the Warden::Proxy.
  • login_as: Same as the Warden::Test::Helpers login_as method.
  • logout: Same as the Warden::Test::Helpers logout method.
  • unlogin: Removes the user(s) from the logged-in list, but leaves the session value so the user can be fetched on access.

Thanks

warden-rspec-rails's People

Contributors

ajsharp avatar brianhawley avatar fragoulis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

warden-rspec-rails's Issues

Getting an error when there is not a logger user

In some scenarios, I need to test a controller as an anonymous user. However, I'm getting this error if the method set_user is not called.

it 'works' do
  get :index
end
NoMethodError:
       undefined method `cookies' for {}:Hash

When I set a user everything is working properly:

it 'works' do
  warden.set_user("user_id", User.first.id)

  get :index
end

Is there something I'm missing? How is it supposed to test a private controller using an anonymous user?

Rails 5 compatibility

warden.authenticate! behaves differently in rails 5.

I tracked down the reason to this commit: rails/rails@50176b5 (and this line).

The above change changes the behavior of @controller.send(:performed?) return value here.

performed? is called after the action is processed.

You can see here that prepare! which is an alias of to_a is called on the response in the same way more or less in rails 4 & 5.

However, In rails 5 on prepare! it also marks the response as committed. This affects the performed? outcome as I already mentioned, which in turns affects the conditional in _catch_warden.

Can you give me some insight on why we want to check whether the controller has performed or not?
To my understanding, the change in behavior is more of a bug fix. I want to make a PR on this but need some help.

Returning the response even if Warden interrupts

See this line.

What is the rationale behind this? I want to test certain authentication checks in my controller specs, but this behavior throws me off, because the controller code after my warden.authenticate! calls fails if it is executed without a valid user (which it does not expect due to that guard statement).

incompatible with Ruby 3.0.0

I'm not sure whether this gem is maintained, but it was raising an error for me recently when attempting to ruby 3.0.

The process(*) method here needs to change to process(*, **) to account for ruby 3 arg syntax changes:
https://github.com/ajsharp/warden-rspec-rails/blob/master/lib/warden/test/controller_helpers.rb#L16
https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/

This is an issue similar to:
rails/rails-controller-testing#69

uninitialized constant Warden (NameError)

I did exactly as the README instructed and ran my tests and this is the error I received.

It seems it's not being loaded but not sure what should be required and where.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.