-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manually authenticate for testing #521
Comments
+1 |
work! |
@Fudoshiki thanks for the response. I tried this but I'm getting |
create this is a FactoryGirl method, we used factories. You can replace create(:user, :admin) on fixture. |
Ok, I was able to fix that, but now I'm getting this from DTA:
Note: I also had to specify It's creating the |
@matisoffn, Here is the approach I'm using: module DeviseMapping
extend ActiveSupport::Concern
included do
before(:each) do
request.env["devise.mapping"] = Devise.mappings[:user]
allow(SecureRandom).to receive(:urlsafe_base64).with(nil, false).and_return("Nbdlw89hmGAd1y1Ch6fhfw")
end
end
[:get, :post, :put, :patch, :delete].each do |http_method|
define_method("auth_#{http_method}") do |action_name, params = {}, headers = {}, flash = {}|
auth_params = {
'access-token' => controller.current_user.last_token,
'client' => controller.current_user.last_client_id,
'uid' => controller.current_user.email
}
params = params.merge(auth_params)
public_send(http_method, action_name, params, headers, flash)
end
end
end
RSpec.configure do |config|
config.include DeviseMapping, type: :controller
end Then just use |
@RomanKapitonov thanks for that, but I'm using Test::Unit not Rspec. Also where do you put this file? |
@matisoffn this is usually put into Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } This will load all files from support directory. Then in your spec you put |
Will this work with Test::Unit? On Feb 9, 2016, at 8:07 AM, Roman Kapitonov <notifications@git.luolix.topmailto:notifications@github.com> wrote: @matisoffnhttps://github.com/matisoffn this is usually put into spec/support and then you have a line in spec_helper.rb or rails_helper.rb (depending on the one you use) Dir[Rails.root.join('spec/support/*/.rb')].each { |f| require f } This will load all files from support directory. Then in your spec you put require 'rails_helper.rb' and hence DeviseMapping gets included into RSpec.config for controllers. — |
@RomanKapitonov I'm getting |
Fixed by updating gem. |
This took me most of a day to solve, so I'm posting my current solution here. Also see #75 In a request spec: @user = FactoryGirl.create :user
@user.confirm
@auth_headers = @user.create_new_auth_token
get current_users_path, params: {}, headers: @auth_headers
expect(response).to have_http_status(:success) |
I also just spent way too long on this. One big fact that I learned: controller tests should be treated differently than request specs for this Here's what I came up with to handle both cases module AuthHelper
module Controller
def sign_in(user)
@request.env["devise.mapping"] = Devise.mappings[:merchant]
@request.headers.merge! user.create_new_auth_token
sign_in user
end
end
module Request
%i(get post put patch delete).each do |http_method|
# auth_get, auth_post, auth_put, auth_patch, auth_delete
define_method("auth_#{http_method}") do |user, action_name, params: {}, headers: {}|
auth_headers = user.create_new_auth_token
headers = headers.merge(auth_headers)
public_send(http_method, action_name, params: params, headers: headers)
end
end
end
end
RSpec.configure do |config|
config.include AuthHelper::Controller, type: :controller
config.include AuthHelper::Request, type: :request
end Controller test usage: describe SomeController, type: :controller do
let(:user) { create(:user) }
subject { post :create, params: params }
before { sign_in user }
...
end Request spec usage: describe SomeRequest, type: :request do
let(:user) { create(:user) }
subject { auth_post user, '/api/some_endpoint', params: params }
...
end |
How can I manually authenticate and set the HTTP headers (client, access-token, uid, etc.)? I'm running a test for a controller that requires authentication.
How can I authenticate in DTA?
The text was updated successfully, but these errors were encountered: