From 4fcee3bbbad0650240dc4910fc4d7b64e6fd417f Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 27 Aug 2025 14:15:21 +0530 Subject: [PATCH] feat: mock login flow --- app/controllers/omniauth_controller.rb | 38 ++++++----- .../controllers/omniauth_controller_spec.rb | 66 +++++++++++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 spec/enterprise/controllers/omniauth_controller_spec.rb diff --git a/app/controllers/omniauth_controller.rb b/app/controllers/omniauth_controller.rb index 9f90bc09f..fcd8bd31b 100644 --- a/app/controllers/omniauth_controller.rb +++ b/app/controllers/omniauth_controller.rb @@ -1,8 +1,5 @@ class OmniauthController < ApplicationController - skip_before_action :verify_authenticity_token - skip_before_action :authenticate_user! skip_before_action :set_current_user - skip_before_action :check_authorization def request # This will be handled by OmniAuth middleware @@ -13,22 +10,29 @@ class OmniauthController < ApplicationController auth = request.env['omniauth.auth'] account_id = params[:account_id] + # Check if SAML is enabled for this account + saml_settings = AccountSamlSettings.find_by(account_id: account_id, enabled: true) + return render json: { error: 'SAML not enabled for this account' }, status: :unauthorized unless saml_settings + if auth.present? - render json: { - message: 'SAML authentication successful', - account_id: account_id, - provider: auth.provider, - uid: auth.uid, - info: { - email: auth.info.email, - name: auth.info.name, - first_name: auth.info.first_name, - last_name: auth.info.last_name - }, - extra: { - raw_info: auth.extra.raw_info + # Find existing user by email + email = auth['info']['email'] + user = User.find_by(email: email) + + if user + render json: { + message: 'Login successful', + user: { + id: user.id, + email: user.email, + name: user.name + } } - } + else + render json: { + error: 'User not found' + }, status: :not_found + end else render json: { error: 'SAML authentication failed', diff --git a/spec/enterprise/controllers/omniauth_controller_spec.rb b/spec/enterprise/controllers/omniauth_controller_spec.rb new file mode 100644 index 000000000..f8ced16ee --- /dev/null +++ b/spec/enterprise/controllers/omniauth_controller_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +RSpec.describe 'SAML Authentication API', type: :request do + let(:account) { create(:account) } + let!(:saml_settings) do + create(:account_saml_settings, + :enabled, + account: account, + sso_url: 'https://mocksaml.com/sso') + end + let(:existing_user) do + create(:user, email: 'john.doe@example.com', name: 'John Doe') + end + + describe 'POST /auth/saml/:account_id/callback' do + context 'with existing user' do + let(:auth_hash) do + { + 'provider' => 'saml', + 'uid' => 'john.doe@example.com', + 'info' => { + 'email' => 'john.doe@example.com', + 'name' => 'John Doe' + } + } + end + + before do + existing_user + end + + it 'successfully logs in the user' do + post "/auth/saml/#{account.id}/callback", + env: { 'omniauth.auth' => auth_hash }, + as: :json + + expect(response).to have_http_status(:ok) + json_response = JSON.parse(response.body) + expect(json_response['message']).to eq('Login successful') + expect(json_response['user']['email']).to eq('john.doe@example.com') + end + end + + context 'when user does not exist' do + let(:auth_hash) do + { + 'provider' => 'saml', + 'uid' => 'nonexistent@example.com', + 'info' => { + 'email' => 'nonexistent@example.com' + } + } + end + + it 'returns user not found error' do + post "/auth/saml/#{account.id}/callback", + env: { 'omniauth.auth' => auth_hash }, + as: :json + + expect(response).to have_http_status(:not_found) + json_response = JSON.parse(response.body) + expect(json_response['error']).to eq('User not found') + end + end + end +end