forest-rails icon indicating copy to clipboard operation
forest-rails copied to clipboard

Rspec on Smart Actions

Open lucasgerard opened this issue 6 years ago • 4 comments

Expected behavior

I should be able to write request specs on my Forest Smart Actions.

Actual behavior

Running into issues with ForestLiana Authentication I guess...

Failure Logs

Context

  • Package Version: 2.8.0
  • Rails Version: 4.2.10
  • Database Dialect: PG
  • Database Version: PostgreSQL 9.6.6

lucasgerard avatar Jun 25 '18 08:06 lucasgerard

Hi @lucasgerard, can you detail your request with anything that could give more context (pieces of code for example)?

Thank you.

arnaudbesnier avatar Jun 25 '18 10:06 arnaudbesnier

+1

pawelwiewiora avatar Mar 28 '19 17:03 pawelwiewiora

I know it's not the best practice, but my work around I current have is having all my forest controllers inherit from a base controller, and then in that base controller I have

module Forest
  class BaseController < ForestLiana::SmartActionsController
    # ...
    skip_before_action :reject_unauthorized_ip if Rails.env.test?
    skip_before_action :smart_action_pre_perform_checks if Rails.env.test?
    skip_before_action :authenticate_user_from_jwt if Rails.env.test?
    # ...
  end
end

and now I can test my controllers normally with rspec just making sure i pass the correct params needed to fulfill my actions.

jordanell avatar Feb 09 '23 05:02 jordanell

@jordanell It's probably a bit safer to skip the actions in a before hook in RSpec:

before do
  allow_any_instance_of(Forest::CustomersController).to receive(:reject_unauthorized_ip)
  allow_any_instance_of(Forest::CustomersController).to receive(:authenticate_user_from_jwt)
  allow_any_instance_of(Forest::CustomersController).to receive(:check_permission_for_smart_route)
end

Then you don't need to change the behaviour of production code.

YoranBrondsema avatar Apr 21 '23 07:04 YoranBrondsema