|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +describe Auth::OmniauthCallbacksController, type: :controller do |
| 6 | + describe 'GET #qiita' do |
| 7 | + include_context 'mock qiita omniauth' |
| 8 | + |
| 9 | + let(:omniauth_auth) { qiita_auth } |
| 10 | + before do |
| 11 | + request.env['devise.mapping'] = Devise.mappings[:user] |
| 12 | + request.env['omniauth.auth'] = omniauth_auth |
| 13 | + request.env['omniauth.origin'] = try(:omniauth_origin) |
| 14 | + end |
| 15 | + |
| 16 | + subject { -> { get :qiita } } |
| 17 | + |
| 18 | + context 'when signed in' do |
| 19 | + before { sign_in(user) } |
| 20 | + let(:user) { Fabricate(:user) } |
| 21 | + |
| 22 | + context 'and current user is not linked to an qiita account' do |
| 23 | + it 'links the user to the qiita account' do |
| 24 | + is_expected.to change { user.reload.qiita_authorization }.from(nil).to(be_truthy) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'and omniauth.origin is empty' do |
| 29 | + it 'redirect_to root_path' do |
| 30 | + subject.call |
| 31 | + expect(response).to be_redirect |
| 32 | + expect(response).to redirect_to(root_path) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context 'and omniauth.origin is about_url' do |
| 37 | + let(:omniauth_origin) { about_url } |
| 38 | + |
| 39 | + it 'redirect_to root_path' do |
| 40 | + subject.call |
| 41 | + expect(response).to be_redirect |
| 42 | + expect(response).to redirect_to(root_path) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context 'and omniauth.origin is settings_qiita_authorizations_url' do |
| 47 | + let(:omniauth_origin) { settings_qiita_authorizations_url } |
| 48 | + |
| 49 | + it 'redirect_to settings_qiita_authorizations_path' do |
| 50 | + subject.call |
| 51 | + expect(response).to be_redirect |
| 52 | + expect(response).to redirect_to(settings_qiita_authorizations_path) |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'when not signed in' do |
| 58 | + context 'and there are a user linked to the qiita account' do |
| 59 | + let(:user) { Fabricate(:user) } |
| 60 | + let!(:qiita_authorization) { Fabricate(:qiita_authorization, uid: omniauth_auth[:uid], user: user) } |
| 61 | + |
| 62 | + it 'logs the user in' do |
| 63 | + subject.call |
| 64 | + expect(controller.current_user).to eq user |
| 65 | + end |
| 66 | + |
| 67 | + context 'and omniauth.origin is empty' do |
| 68 | + it 'redirect_to root_path' do |
| 69 | + subject.call |
| 70 | + expect(response).to be_redirect |
| 71 | + expect(response).to redirect_to(root_path) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'and omniauth.origin is about_url' do |
| 76 | + let(:omniauth_origin) { about_url } |
| 77 | + |
| 78 | + it 'redirect_to root_path' do |
| 79 | + subject.call |
| 80 | + expect(response).to be_redirect |
| 81 | + expect(response).to redirect_to(root_path) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'and omniauth.origin is settings_qiita_authorizations_url' do |
| 86 | + let(:omniauth_origin) { settings_qiita_authorizations_url } |
| 87 | + |
| 88 | + it 'redirect_to settings_qiita_authorizations_path' do |
| 89 | + subject.call |
| 90 | + expect(response).to be_redirect |
| 91 | + expect(response).to redirect_to(settings_qiita_authorizations_path) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'and there are no user linked to the qiita account' do |
| 97 | + it 'redirects to new_user_oauth_registration_path' do |
| 98 | + subject.call |
| 99 | + expect(response).to redirect_to(new_user_oauth_registration_path) |
| 100 | + end |
| 101 | + |
| 102 | + context 'when omniauth.origin is a url' do |
| 103 | + let(:omniauth_origin) { root_url } |
| 104 | + |
| 105 | + it 'stores the omniauth origin to session' do |
| 106 | + subject.call |
| 107 | + expect(session[:devise_omniauth_origin]).to be(omniauth_origin) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + it 'stores the omniauth data to session to build form' do |
| 112 | + subject.call |
| 113 | + expect(Form::OauthRegistration.from_omniauth_auth(session[:devise_omniauth_auth])).to have_attributes({ |
| 114 | + provider: omniauth_auth[:provider], |
| 115 | + avatar: omniauth_auth[:info][:image], |
| 116 | + uid: omniauth_auth[:uid], |
| 117 | + username: omniauth_auth[:uid], |
| 118 | + token: omniauth_auth[:credentials][:token], |
| 119 | + }) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments