Merge pull request #259 from Retrospring/service-attach-errors

Improve error messaging when trying to attach a service connected to another account
This commit is contained in:
Karina Kwiatek 2022-01-01 22:32:59 +01:00 committed by GitHub
commit a04031d8f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 2 deletions

View File

@ -9,11 +9,16 @@ class ServicesController < ApplicationController
def create def create
service = Service.initialize_from_omniauth( omniauth_hash ) service = Service.initialize_from_omniauth( omniauth_hash )
service.user = current_user
if current_user.services << service if service.save
flash[:success] = t('flash.service.create.success') flash[:success] = t('flash.service.create.success')
else else
flash[:error] = t('flash.service.create.error') if service.errors.details.has_key?(:uid) && service.errors.details[:uid].any? { |err| err[:error] == :taken }
flash[:error] = "The #{service.type.split('::').last.titleize} account you are trying to connect is already connected to another #{APP_CONFIG['site_name']} account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring."
else
flash[:error] = t('flash.service.create.error')
end
end end
if origin if origin

View File

@ -0,0 +1,41 @@
require 'rails_helper'
describe ServicesController, type: :controller do
context 'successful Twitter sign in' do
let(:user) { FactoryBot.create(:user) }
before do
sign_in user
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
'provider' => 'twitter',
'uid' => '12',
'info' => { 'nickname' => 'jack' },
'credentials' => { 'token' => 'AAAA', 'secret' => 'BBBB' }
})
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end
after do
OmniAuth.config.mock_auth[:twitter] = nil
end
subject { get :create, params: { provider: 'twitter' } }
context 'no services connected' do
it 'creates a service integration' do
expect { subject }.to change { Service.count }.by(1)
end
end
context 'a user has a service connected' do
let(:other_user) { FactoryBot.create(:user) }
let!(:service) { Services::Twitter.create(user: other_user, uid: 12) }
it 'shows an error when trying to attach a service account which is already connected' do
subject
expect(flash[:error]).to eq("The Twitter account you are trying to connect is already connected to another #{APP_CONFIG['site_name']} account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring.")
end
end
end
end