From 668c0947b564d1ab1484f9636fee34e18c82e476 Mon Sep 17 00:00:00 2001 From: Michael Squance Date: Tue, 11 Mar 2025 10:44:39 +0000 Subject: [PATCH] Modifying the custom claim capture --- app/controllers/auth_controller.rb | 9 ++-- spec/requests/auth/callback_spec.rb | 72 +++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/app/controllers/auth_controller.rb b/app/controllers/auth_controller.rb index d57439b1ca..043dec390d 100644 --- a/app/controllers/auth_controller.rb +++ b/app/controllers/auth_controller.rb @@ -3,10 +3,6 @@ def callback auth = omniauth_params course_booking_uri = course_redirect_params - Sentry.configure_scope do |scope| - scope.set_context("oauth_custom_claim", auth.info) - end - user_exists = User.exists?(stem_user_id: auth.info.stem_user_id) user = User.from_auth(auth.uid, auth.credentials, auth.info) @@ -22,7 +18,10 @@ def callback Achiever::FetchUsersCompletedCoursesFromAchieverJob.perform_later(user) rescue => e - Sentry.capture_exception(e) + Sentry.with_scope do |scope| + scope.set_context("oauth_custom_claim", auth&.info) + Sentry.capture_exception(e) + end raise e end diff --git a/spec/requests/auth/callback_spec.rb b/spec/requests/auth/callback_spec.rb index 97d2802311..456d3a93e9 100644 --- a/spec/requests/auth/callback_spec.rb +++ b/spec/requests/auth/callback_spec.rb @@ -1,28 +1,47 @@ require "rails_helper" RSpec.describe AuthController do - describe "#callback" do - let(:integration_key) { "13251241" } - let(:user) { create(:user, stem_user_id: integration_key) } - let(:auth0_hash) { - OmniAuth::AuthHash.new( - provider: "stem", - uid: "auth0|user@example.com", - credentials: { - expires_at: 1_546_601_180, - token: "14849048797785647933" - }, - info: { - achiever_contact_no: "b44cb53f-c690-4535-bd79-89e893337ec6", - first_name: "Jane", - last_name: "Doe", - email: "user@example.com", - stem_user_id: integration_key + let(:integration_key) { "13251241" } + let(:user) { create(:user, stem_user_id: integration_key) } + let(:auth0_hash) { + OmniAuth::AuthHash.new( + provider: "stem", + uid: "auth0|user@example.com", + credentials: { + expires_at: 1_546_601_180, + token: "14849048797785647933" + }, + info: { + achiever_contact_no: "b44cb53f-c690-4535-bd79-89e893337ec6", + first_name: "Jane", + last_name: "Doe", + email: "user@example.com", + stem_user_id: integration_key + + } + ) + } + + let(:auth0_invalid_hash) { + OmniAuth::AuthHash.new( + provider: "stem", + uid: "auth0|user@example.com", + credentials: { + expires_at: 1_546_601_180, + token: "14849048797785647933" + }, + info: { + achiever_contact_no: nil, + first_name: "Jane", + last_name: "Doe", + email: "user@example.com", + stem_user_id: nil - } - ) - } + } + ) + } + describe "#callback" do before do OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[:stem] = auth0_hash @@ -53,6 +72,19 @@ end end + context "invalid info" do + before do + OmniAuth.config.test_mode = true + OmniAuth.config.mock_auth[:stem] = auth0_invalid_hash + end + + it "raises exception when data missing" do + expect { + get callback_path + }.to raise_error(StandardError) + end + end + context "when source_uri is not present" do before do OmniAuth.config.test_mode = true