I’m using the OmniAuth Azure Active Directory gem. Which seems to work quite well when you fork it and include the fixes that haven’t been merge in :\

I had a lot of trouble finding how to log a user in for controller integration tests. This may be obvious to Rails full timers, but I only put my Rails hat on every now and then.

Add this to your test helper:

File: test/test-helper.rb
class ActiveSupport::TestCase

  def login
    OmniAuth.config.test_mode = true
    OmniAuth.config.mock_auth[:azureactivedirectory] = OmniAuth::AuthHash.new(auth_hash)
  end

  def auth_hash
    {
        provider: 'azureactivedirectory',
        uid: '1234ZJUF9w_82t1Jw4KjUU4-bv1hWf6C4Xg8q0uaADU',
        info: {
            name: 'Terry Tester',
            email: 'test@test.com',
            first_name: 'Terry',
            last_name: 'Tester'
        },
        # ..snip!
    }
  end
end


Now, from any of your IntegrationTest subclasses, you can simply call login before you do anything else!

File: test/test-helper.rb
class SessionsControllerTest < ActionDispatch::IntegrationTest
  test 'user should be redirected to Active Directory logout URL on logout' do
    login
    get '/logout'
    assert_redirected_to ACTIVE_DIRECTORY_LOGOUT_URL
  end

  test 'unauthorized user should be redirected to login' do
    get root_url
    assert_redirected_to '/login'
  end
end