diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index 816d593..8c943ee 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -11,4 +11,8 @@ def destroy reset_session redirect_to root_path, notice: 'ログアウトしました' end + + def failure + redirect_to root_path, notice: 'ログインをキャンセルしました' + end end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 6de5227..084d564 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,5 +1,9 @@ - Rails.application.config.middleware.use OmniAuth::Builder do - provider :github, - Rails.application.credentials.github[:client_id], - Rails.application.credentials.github[:client_secret] - end +Rails.application.config.middleware.use OmniAuth::Builder do + provider :github, + Rails.application.credentials.github[:client_id], + Rails.application.credentials.github[:client_secret] + + OmniAuth.config.on_failure = Proc.new { |env| + OmniAuth::FailureEndpoint.new(env).redirect_to_failure + } +end diff --git a/config/routes.rb b/config/routes.rb index d14a46c..ba18eb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,5 +9,6 @@ # Defines the root path route ("/") root "groups#index" get "auth/:provider/callback" => "user_sessions#create" + get "auth/failure" => "user_sessions#failure" delete "/logout" => "user_sessions#destroy" end diff --git a/spec/requests/user_sessions_spec.rb b/spec/requests/user_sessions_spec.rb index 5534fd7..6452be3 100644 --- a/spec/requests/user_sessions_spec.rb +++ b/spec/requests/user_sessions_spec.rb @@ -57,4 +57,11 @@ expect(response).to redirect_to(root_path) end end + + describe 'GET /failure' do + it 'redirects to root_path' do + get '/auth/failure' + expect(response).to redirect_to(root_path) + end + end end diff --git a/spec/system/users_spec.rb b/spec/system/users_spec.rb index e6f82ba..2669535 100644 --- a/spec/system/users_spec.rb +++ b/spec/system/users_spec.rb @@ -4,34 +4,57 @@ RSpec.describe 'Users', type: :system do describe 'user authentication' do - it 'allows users to login' do - visit root_path - expect(page).to have_content 'GitHubアカウントが必要です' + context 'when authentication is successful' do + it 'allows users to login' do + visit root_path + expect(page).to have_content 'GitHubアカウントが必要です' + + expect do + click_button 'サインアップ / ログインをして2次会グループを作成' + + expect(page).to have_content 'ログインしました' + end.to change(User, :count).by(1) + + expect(page).to have_current_path(new_group_path) + end + end + + context 'when user logs out' do + it 'allows users to logout' do + visit root_path + expect(page).to have_content 'GitHubアカウントが必要です' + expect(page).not_to have_content 'ログアウト' - expect do click_button 'サインアップ / ログインをして2次会グループを作成' + expect(page).to have_current_path(new_group_path) - expect(page).to have_content 'ログインしました' - end.to change(User, :count).by(1) + click_link 'キャンセル' + expect(page).to have_current_path(groups_path) + expect(page).not_to have_content 'GitHubアカウントが必要です' - expect(page).to have_current_path(new_group_path) + click_button 'ログアウト' + expect(page).to have_content 'ログアウトしました' + expect(page).to have_content 'GitHubアカウントが必要です' + end end - it 'allows users to logout' do - visit root_path - expect(page).to have_content 'GitHubアカウントが必要です' - expect(page).not_to have_content 'ログアウト' + context 'when authentication is failed' do + before do + OmniAuth.config.mock_auth[:github] = :invalid_credentials + end + + it 'redirects to root_path' do + visit root_path + expect(page).to have_content 'GitHubアカウントが必要です' - click_button 'サインアップ / ログインをして2次会グループを作成' - expect(page).to have_current_path(new_group_path) + expect do + click_button 'サインアップ / ログインをして2次会グループを作成' - click_link 'キャンセル' - expect(page).to have_current_path(groups_path) - expect(page).not_to have_content 'GitHubアカウントが必要です' + expect(page).to have_content 'ログインをキャンセルしました' + end.not_to change(User, :count) - click_button 'ログアウト' - expect(page).to have_content 'ログアウトしました' - expect(page).to have_content 'GitHubアカウントが必要です' + expect(page).to have_current_path(root_path) + end end end end