When a user goes to /auth/facebook
, it gets redirected to FB, then back to my /auth/facebook/callback
if successful.
How can I write an RSpec test that will follow all of these redirects to verify my user has been authenticated?
I would recommend an alternative, simpler approach. What if you tested callback controller directly to see how it reacts to different values passed to it in the omniauth.auth or if env["omniauth.auth"] is missing or incorrect. Following redirects would be equivalent of testing omniauth plugin, which does not test YOUR system.
For example, here is what we have in our tests (this is just a few examples, we have many more that verify other variations of omniauth hash and user state prior to sign-in attempt, such as invitation status, user account being disabled by the admins, etc.):
describe Users::OmniauthCallbacksController do
before :each do
# This a Devise specific thing for functional tests. See https://github.com/plataformatec/devise/issues/608
request.env["devise.mapping"] = Devise.mappings[:user]
end
describe ".create" do
it "should redirect back to sign_up page with an error when omniauth.auth is missing" do
@controller.stub!(:env).and_return({"some_other_key" => "some_other_value"})
get :facebook
flash[:error].should be
flash[:error].should match /Unexpected response from Facebook\./
response.should redirect_to new_user_registration_url
end
it "should redirect back to sign_up page with an error when provider is missing" do
stub_env_for_omniauth(nil)
get :facebook
flash[:error].should be
flash[:error].should match /Unexpected response from Facebook: Provider information is missing/
response.should redirect_to new_user_registration_url
end
end
end
with stub_env_for_omniauth
method defined as follows:
def stub_env_for_omniauth(provider = "facebook", uid = "1234567", email = "[email protected]", name = "John Doe")
env = { "omniauth.auth" => { "provider" => provider, "uid" => uid, "info" => { "email" => email, "name" => name } } }
@controller.stub!(:env).and_return(env)
env
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With