I wrote a test to assert certain requests can only be performed by logged in users, who will be logging in using Devise & Omniauth's Google OAuth2 system. I cannot find a way to mock omniauth into returning a logged in user, picked up the example from Omniauth Wiki page about Integration Tests support
Here's the spec
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
Needless to say I cannot decipher much on why or where it is failing from the given error, I assume it is because the user cannot login
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)>'
I was using the wrong config
this goes into 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 => "[email protected]"
},
:credentials => {
:token => "token",
:refresh_token => "refresh token"
}
}
)
and then the before method
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