<commit_before>require 'carto/saml_service'
require 'spec_helper_min'

describe Carto::SamlService do
  let(:service) do
    Carto::SamlService.new(@organization)
  end

  before(:all) do
    @organization = FactoryGirl.create(:saml_organization)
  end

  after(:all) do
    @organization.delete
  end

  describe 'configuration support' do
    it 'is disabled if there is no configuration or it is empty' do
      org = FactoryGirl.build(:organization, auth_saml_configuration: nil)
      Carto::SamlService.new(org).enabled?.should be_false

      org = FactoryGirl.build(:organization, auth_saml_configuration: {})
      Carto::SamlService.new(org).enabled?.should be_false
    end

    it 'is enabled if there is configuration' do
      service.enabled?.should be_true
    end
  end

  describe 'Integration logic' do
    # This stubs the SAML external integration
    let(:response_mock) { mock }
    let(:saml_response_param_mock) { mock }
    let(:saml_config) { @organization.auth_saml_configuration }

    before(:each) do
      Cartodb.stubs(:config).returns(saml_authentication: saml_config)
      service.stubs(:get_saml_response).returns(response_mock)
      service.stubs(:debug_response)
    end

    describe '#username' do
      it 'returns nil if response is invalid' do
        response_mock.stubs(:is_valid?).returns(false)

        service.username(saml_response_param_mock).should be_nil
      end

      it 'returns nil if a valid response does not contain the username' do
        response_mock.stubs(:is_valid?).returns(true)
        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => nil)

        service.username(saml_response_param_mock).should be_nil

        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => '')

        service.username(saml_response_param_mock).should be_nil
      end

      it 'returns username from the user with the matching email' do
        user = create_test_saml_user
        response_mock.stubs(:is_valid?).returns(true)
        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => user.email)

        service.username(saml_response_param_mock).should eq user.username
        user.delete
      end
    end
<commit_msg>expects raise error on invalid response<commit_after>require 'carto/saml_service'
require 'spec_helper_min'

describe Carto::SamlService do
  let(:service) do
    Carto::SamlService.new(@organization)
  end

  before(:all) do
    @organization = FactoryGirl.create(:saml_organization)
  end

  after(:all) do
    @organization.delete
  end

  describe 'configuration support' do
    it 'is disabled if there is no configuration or it is empty' do
      org = FactoryGirl.build(:organization, auth_saml_configuration: nil)
      Carto::SamlService.new(org).enabled?.should be_false

      org = FactoryGirl.build(:organization, auth_saml_configuration: {})
      Carto::SamlService.new(org).enabled?.should be_false
    end

    it 'is enabled if there is configuration' do
      service.enabled?.should be_true
    end
  end

  describe 'Integration logic' do
    # This stubs the SAML external integration
    let(:response_mock) { mock }
    let(:saml_response_param_mock) { mock }
    let(:saml_config) { @organization.auth_saml_configuration }

    before(:each) do
      Cartodb.stubs(:config).returns(saml_authentication: saml_config)
      service.stubs(:get_saml_response).returns(response_mock)
      service.stubs(:debug_response)
    end

    describe '#username' do
      it 'returns nil if response is invalid' do
        response_mock.stubs(:is_valid?).returns(false)

        expect {
          service.username(saml_response_param_mock)
        }.to raise_error(Carto::UnprocesableEntityError)
      end

      it 'returns nil if a valid response does not contain the username' do
        response_mock.stubs(:is_valid?).returns(true)
        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => nil)

        service.username(saml_response_param_mock).should be_nil

        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => '')

        service.username(saml_response_param_mock).should be_nil
      end

      it 'returns username from the user with the matching email' do
        user = create_test_saml_user
        response_mock.stubs(:is_valid?).returns(true)
        response_mock.stubs(:attributes).returns(saml_config[:email_attribute] => user.email)

        service.username(saml_response_param_mock).should eq user.username
        user.delete
      end
    end

{'fraction_non_alphanumeric': 0.09162303664921466, 'fraction_numerical': 0.0, 'mean_word_length': 3.6833503575076607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7040738', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1326, 'n_words': 293}