Получение omniauth для имитации входа в систему для спецификации запросов в рельсах
Я написал тест, чтобы утверждать, что определенные запросы могут быть выполнены только зарегистрированными пользователями, которые будут входить в систему с помощью системы Google OAuth2 Devise & Omniauth. Я не могу найти способ заставить Омниаута вернуть зарегистрированного пользователя, взял пример со страницы Вики Omniauth о поддержке интеграционных тестов
Вот спецификация
describe "allows logged in users" do
before(:each) do
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:google, {:uid => '12345'})
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
end
it "new certification form" do
get new_certification_path
expect(response).to be_success
end
it "to create certification" do
certification_attributes = FactoryGirl.attributes_for :certification
expect {
post "/certifications", params: { certification: certification_attributes }
}.to change(Certification, :count)
expect(response).to redirect_to certification_path
end
end
Излишне говорить, что я не могу многое понять, почему или где это происходит из-за данной ошибки, я предполагаю, что это потому, что пользователь не может войти
Failures:
1) Certifications allows logged in users new certification form
Failure/Error: expect(response).to be_success
expected `#<ActionDispatch::TestResponse:0x007fb23b4c2990 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.success?` to return true, got false
# ./spec/requests/certifications_spec.rb:49:in `block (3 levels) in <top (required)>'
2) Certifications allows logged in users to create certification
Failure/Error:
expect {
post "/certifications", params: { certification: certification_attributes }
}.to change(Certification, :count)
expected #count to have changed, but is still 0
# ./spec/requests/certifications_spec.rb:54:in `block (3 levels) in <top (required)>'
1 ответ
Решение
Я использовал неправильный конфиг
это входит в spec/support/rails_helper
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
:provider => "google_oauth2",
:uid => "123456789",
:info => {
:name => "Tony Stark",
:email => "tony@stark.com"
},
:credentials => {
:token => "token",
:refresh_token => "refresh token"
}
}
)
а затем метод перед
before(:each) do
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
end