PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/features/oauth_login_spec.rb

https://bitbucket.org/terrchen/gitlab-ce
Ruby | 120 lines | 88 code | 29 blank | 3 comment | 1 complexity | 83de38abc30616f5810555f4ccf5d0f5 MD5 | raw file
Possible License(s): Apache-2.0, CC0-1.0
  1. require 'spec_helper'
  2. feature 'OAuth Login', :js, :allow_forgery_protection do
  3. include DeviseHelpers
  4. def enter_code(code)
  5. fill_in 'user_otp_attempt', with: code
  6. click_button 'Verify code'
  7. end
  8. def stub_omniauth_config(provider)
  9. OmniAuth.config.add_mock(provider, OmniAuth::AuthHash.new(provider: provider.to_s, uid: "12345"))
  10. stub_omniauth_provider(provider)
  11. end
  12. providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2,
  13. :facebook, :cas3, :auth0, :authentiq]
  14. before(:all) do
  15. # The OmniAuth `full_host` parameter doesn't get set correctly (it gets set to something like `http://localhost`
  16. # here), and causes integration tests to fail with 404s. We set the `full_host` by removing the request path (and
  17. # anything after it) from the request URI.
  18. @omniauth_config_full_host = OmniAuth.config.full_host
  19. OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(/#{request['REQUEST_PATH']}.*/, '') }
  20. end
  21. after(:all) do
  22. OmniAuth.config.full_host = @omniauth_config_full_host
  23. end
  24. def login_with_provider(provider, enter_two_factor: false)
  25. login_via(provider.to_s, user, uid, remember_me: remember_me)
  26. enter_code(user.current_otp) if enter_two_factor
  27. end
  28. providers.each do |provider|
  29. context "when the user logs in using the #{provider} provider" do
  30. let(:uid) { 'my-uid' }
  31. let(:remember_me) { false }
  32. let(:user) { create(:omniauth_user, extern_uid: uid, provider: provider.to_s) }
  33. let(:two_factor_user) { create(:omniauth_user, :two_factor, extern_uid: uid, provider: provider.to_s) }
  34. before do
  35. stub_omniauth_config(provider)
  36. end
  37. context 'when two-factor authentication is disabled' do
  38. it 'logs the user in' do
  39. login_with_provider(provider)
  40. expect(current_path).to eq root_path
  41. end
  42. end
  43. context 'when two-factor authentication is enabled' do
  44. let(:user) { two_factor_user }
  45. it 'logs the user in' do
  46. login_with_provider(provider, enter_two_factor: true)
  47. expect(current_path).to eq root_path
  48. end
  49. end
  50. context 'when "remember me" is checked' do
  51. let(:remember_me) { true }
  52. context 'when two-factor authentication is disabled' do
  53. it 'remembers the user after a browser restart' do
  54. login_with_provider(provider)
  55. clear_browser_session
  56. visit(root_path)
  57. expect(current_path).to eq root_path
  58. end
  59. end
  60. context 'when two-factor authentication is enabled' do
  61. let(:user) { two_factor_user }
  62. it 'remembers the user after a browser restart' do
  63. login_with_provider(provider, enter_two_factor: true)
  64. clear_browser_session
  65. visit(root_path)
  66. expect(current_path).to eq root_path
  67. end
  68. end
  69. end
  70. context 'when "remember me" is not checked' do
  71. context 'when two-factor authentication is disabled' do
  72. it 'does not remember the user after a browser restart' do
  73. login_with_provider(provider)
  74. clear_browser_session
  75. visit(root_path)
  76. expect(current_path).to eq new_user_session_path
  77. end
  78. end
  79. context 'when two-factor authentication is enabled' do
  80. let(:user) { two_factor_user }
  81. it 'does not remember the user after a browser restart' do
  82. login_with_provider(provider, enter_two_factor: true)
  83. clear_browser_session
  84. visit(root_path)
  85. expect(current_path).to eq new_user_session_path
  86. end
  87. end
  88. end
  89. end
  90. end
  91. end