Skip to content

Commit

Permalink
Add spec
Browse files Browse the repository at this point in the history
Oh, and a transaction block. Because the controller after hooks tried to update the DB which resulted in
  PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block

Even for a small rescue statement, it's worth adding a spec. You never know what might not be working!
  • Loading branch information
dacook committed Feb 12, 2025
1 parent 9b935be commit 61d0df1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def openid_connect
OidcAccount.link(spree_current_user, request.env["omniauth.auth"])
ActiveRecord::Base.transaction do
OidcAccount.link(spree_current_user, request.env["omniauth.auth"])
end

redirect_to admin_oidc_settings_path
rescue ActiveRecord::RecordNotUnique
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/omniauth_callbacks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def request!
expect(account.uid).to eq "ofn@example.com"
expect(response.status).to eq(302)
end

context 'when OIDC account already linked with a different user' do
before do
other_user = create(:user, email: "ofn@elsewhere.com")
OidcAccount.create! user_id: other_user.id, uid: "ofn@example.com"
end

it 'fails with error message' do
expect { request! }.not_to change { OidcAccount.count }

expect(response.status).to eq(302)
expect(flash[:error]).to match "is already associated with another account"
end
end
end

context 'when the omniauth openid_connect is mocked with an error' do
Expand All @@ -46,6 +60,7 @@ def request!
expect { request! }.not_to change { OidcAccount.count }

expect(response.status).to eq(302)
expect(flash[:error]).to match "Could not sign in"
end
end
end

0 comments on commit 61d0df1

Please sign in to comment.