<commit_before>require 'rails_helper'

class Devise::SessionsController < ActionController::Base
  # The important parts from devise
  def resource_class
    User
  end

  def destroy
    sign_out
    redirect_to after_sign_out_path_for(:user)
  end

  def require_no_authentication
  end
end

require_relative '../../../app/controllers/devise/saml_sessions_controller'


describe Devise::SamlSessionsController, type: :controller do
  let(:saml_config) { Devise.saml_config }

  describe '#new' do
    it 'redirects to the SAML Auth Request endpoint' do
      get :new
      expect(response).to redirect_to(%r(\Ahttp://localhost:8009/saml/auth\?SAMLRequest=))
    end
  end

  describe '#metadata' do
    it 'generates metadata' do
      get :metadata

      # Remove ID that can vary across requests
      expected_metadata = OneLogin::RubySaml::Metadata.new.generate(saml_config)
      metadata_pattern = Regexp.escape(expected_metadata).gsub(/ ID='[^']+'/, " ID='[\\w-]+'")
      expect(response.body).to match(Regexp.new(metadata_pattern))
    end
  end

  describe '#destroy' do
    it 'signs out and redirects to the IdP' do
      expect(controller).to receive(:sign_out)
      delete :destroy
      expect(response).to redirect_to(%r(\Ahttp://localhost:8009/saml/logout\?SAMLRequest=))
    end
  end

  describe '#idp_sign_out' do<commit_msg>Verify metadata endpoint still returns sp metadata with idp adapter present<commit_after>require 'rails_helper'

class Devise::SessionsController < ActionController::Base
  # The important parts from devise
  def resource_class
    User
  end

  def destroy
    sign_out
    redirect_to after_sign_out_path_for(:user)
  end

  def require_no_authentication
  end
end

require_relative '../../../app/controllers/devise/saml_sessions_controller'


describe Devise::SamlSessionsController, type: :controller do
  let(:saml_config) { Devise.saml_config }

  before do
    @original_saml_config = Devise.saml_config
  end
  after do
    Devise.saml_config = @original_saml_config
    Devise.idp_settings_adapter = nil
  end

  describe '#new' do
    it 'redirects to the SAML Auth Request endpoint' do
      get :new
      expect(response).to redirect_to(%r(\Ahttp://localhost:8009/saml/auth\?SAMLRequest=))
    end
  end

  describe '#metadata' do
    context "with the default configuration" do
      it 'generates metadata' do
        get :metadata

        # Remove ID that can vary across requests
        expected_metadata = OneLogin::RubySaml::Metadata.new.generate(saml_config)
        metadata_pattern = Regexp.escape(expected_metadata).gsub(/ ID='[^']+'/, " ID='[\\w-]+'")
        expect(response.body).to match(Regexp.new(metadata_pattern))
      end
    end

    context "with a specified IDP" do
      let(:idp_providers_adapter) {
        Class.new {
          def self.settings(idp_entity_id)
            {
              assertion_consumer_service_url: "acs_url",
              assertion_consumer_service_binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST",
              name_identifier_format: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress",
              issuer: "sp_issuer",
              idp_entity_id: "http://www.example.com",
              authn_context: "",
              idp_slo_target_url: "idp_slo_url",
              idp_sso_target_url: "idp_sso_url",
              idp_cert: "idp_cert"
            }
          end
        }
      }
      let(:saml_config) { controller.saml_config(idp_entity_id: "anything") }

      before do
        Devise.idp_settings_adapter = idp_providers_adapter
        Devise.saml_configure do |settings|
          settings.assertion_consumer_service_url     = "http://localhost:3000/users/saml/auth"
          settings.assertion_consumer_service_binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
          settings.name_identifier_format             = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
          settings.issuer                             = "http://localhost:3000"
        end
      end

      it "generates the same service metadata" do
        get :metadata

        # Remove ID that can vary across requests
        expected_metadata = OneLogin::RubySaml::Metadata.new.generate(saml_config)
        metadata_pattern = Regexp.escape(expected_metadata).gsub(/ ID='[^']+'/, " ID='[\\w-]+'")
        expect(response.body).to match(Regexp.new(metadata_pattern))
      end
    end
  end

  describe '#destroy' do
    it 'signs out and redirects to the IdP' do
      expect(controller).to receive(:sign_out)
      delete :destroy
      expect(response).to redirect_to(%r(\Ahttp://localhost:8009/saml/logout\?SAMLRequest=))
    end
  end

  describe '#idp_sign_out' do
{'fraction_non_alphanumeric': 0.10502864417568428, 'fraction_numerical': 0.006789730532569488, 'mean_word_length': 3.0394173093401884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2272290', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1467, 'n_words': 302}