2

I'm writing a Rails app with 100% test coverage. I have feature specs with Capybara for logging in with a username and password, but I don't have specs for logging in through Facebook or LinkedIn.

I didn't find an answer on the devise OmniAuth pages. Is this testable? Should I not be testing this?

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
Dbz
  • 2,721
  • 4
  • 35
  • 53

1 Answers1

1

I would take a look at https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

So what I’ve ended up creating 2 helpers in my support/omniauth.rb file:

def set_omniauth(opts = {})
  default = {:provider => :facebook,
             :uuid     => "1234",
             :facebook => {
                            :email => "foobar@example.com",
                            :gender => "Male",
                            :first_name => "foo",
                            :last_name => "bar"
                          }
            }

  credentials = default.merge(opts)
  provider = credentials[:provider]
  user_hash = credentials[provider]

  OmniAuth.config.test_mode = true

  OmniAuth.config.mock_auth[provider] = {
    'uid' => credentials[:uuid],
    "extra" => {
    "user_hash" => {
      "email" => user_hash[:email],
      "first_name" => user_hash[:first_name],
      "last_name" => user_hash[:last_name],
      "gender" => user_hash[:gender]
      }
    }
  }
end

def set_invalid_omniauth(opts = {})

  credentials = { :provider => :facebook,
                  :invalid  => :invalid_crendentials
                 }.merge(opts)

  OmniAuth.config.test_mode = true
  OmniAuth.config.mock_auth[credentials[:provider]] = credentials[:invalid]

end

With this sweet setup, I can now have multiple defaults in my tests, which makes changes very clean:

background do
   set_omniauth()
   click_link_or_button 'Sign up with Facebook'
end

Happy Hacking

MZaragoza
  • 10,108
  • 9
  • 71
  • 116