How to login a user in RefineryCMS tests?

南笙酒味 提交于 2019-12-24 00:45:55

问题


I'm developing a custom controller in refineryCMS, that placed in the root of the app

class ExchangeController < ApplicationController
    def exchange
    end
end

It works fine, but to have it tested I need to login a refinery user like this

describe "Exchange action" do
   login_refinery_user
   it 'should return a 200' do
    get '/exchange' 
    response.code.should == '200'
   end
end

But it just doesn't work :( (undefined local variable or method `login_refinery_user') Without calling that login method im getting Could not find table 'refinery_roles'

Could someone point me in the right direction?


回答1:


For Refinery 2, all that's needed are the following lines in spec/support/devise.rb:

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  config.extend  ::Refinery::Testing::ControllerMacros::Authentication, :type => :controller
  config.extend  ::Refinery::Testing::RequestMacros::Authentication, :type => :request

end

Now "login_refinery_user" will be available along with the other helpers from https://github.com/resolve/refinerycms/blob/master/testing/lib/refinery/testing/controller_macros/authentication.rb and https://github.com/resolve/refinerycms/blob/master/testing/lib/refinery/testing/request_macros/authentication.rb




回答2:


This is how I'm doing it with Refinery 1.0.9, it may need to be tweaked for Refinery 2

Create a file named "controller_macros.rb" and place it under spec/support/refinery/

In controller_macros.rb define login_refinery_user

module Refinery
  module ControllerMacros
    def login_refinery_user
      before (:each) do
        @refinery_user = Factory(:refinery_user)
        @request.env["devise.mapping"] = Devise.mappings[:admin]
        sign_in @refinery_user
      end
    end
  end
end

In your factories.rb file be sure you have the following two factories

factory :user do
  sequence(:username) { |n| "person#{n}" }
  sequence(:email) { |n| "person#{n}@cucumber.com" }
  password  "greenandjuicy"
  password_confirmation "greenandjuicy"
end

factory :refinery_user, :parent => :user do
  roles { [ Role[:refinery] ] }

  after_create do |user|
    Refinery::Plugins.registered.each_with_index do |plugin, index|
      user.plugins.create(:name => plugin.name, :position => index)
    end
  end
end

In your spec_helper.rb file include the controller macros module into your controller tests, and also make sure you have the Devise test helpers

RSpec.configure do |config|
  config.include ::Devise::TestHelpers, :type => :controller
  config.extend  ::Refinery::ControllerMacros, :type => :controller
end



回答3:


What helped me is to make sure you add your plugin/engine name to the list of plugins the currently logged in user is allowed to access:

refinery_login_with :refinery_user 

before do
  logged_in_user.plugins = logged_in_user.plugins | %w(<plugin name>)
end


来源:https://stackoverflow.com/questions/10666365/how-to-login-a-user-in-refinerycms-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!