<commit_before>RSpec.describe 'LdapAuthenticatableTiny' do

  def test_strategy
    warden_env = nil
    Devise::Strategies::LdapAuthenticatableTiny.new(warden_env)
  end

  def test_tls_options_text
    '{"tls_foo": "tls_bar"}'
  end

  def test_options(login, password)
    {
      :auth => {
        :method=>:simple,
        :username=>login,
        :password=>password
      },
      :encryption => {
        :method=>:simple_tls,
        :tls_options=>{
          :tls_foo => 'tls_bar'
        }
      },
      :host => 'foo.com',
      :port => 12345
    }
  end

  describe '#authenticate! integration tests across districts' do
    def mocked_test_strategy(options = {})
      strategy = test_strategy
      allow(strategy).to receive_messages({
        authentication_hash: { login_text: options[:login_text] },
        password: options[:password]
      })
      allow(strategy).to receive(:is_authorized_by_ldap?).with(MockLDAP, options[:ldap_login], options[:password]).and_return options[:is_authorized_by_ldap?]
      strategy
    end

    # just sugar
    def outcomes_after_authenticate!(strategy)
      strategy.authenticate!
      [strategy.result, strategy.message, strategy.user]
    end

    it 'works for Somerville' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::SOMERVILLE))
      pals = TestPals.create!(email_domain: 'k12.<EMAIL>.<EMAIL>')
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'works for New Bedford' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::NEW_BEDFORD))
      pals = TestPals.create!(email_domain: 'newbedfordschools.org')
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'works for Bedford' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::BEDFORD))
      pals = TestPals.create!(email_domain: 'bedfordps.org')
      strategy = mocked_test_strategy({
        login_text: 'uri',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'fails for demo' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::DEMO))
      TestPals.create!
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:failure, :error, nil]
    end
  end

  describe '#authenticate! when methods are mocked' do
    let!(:pals) { TestPals.create! }

    it 'calls fail when email not found' do
      strategy = test_strategy
      allow(strategy).to receive(:authentication_hash) { { login_text: '<EMAIL>' } }
      strategy.authenticate!

      expect(strategy.result).to eq :failure<commit_msg>LDAP strategy works for demo now, with revising support in MockLDAP<commit_after>RSpec.describe 'LdapAuthenticatableTiny' do

  def test_strategy
    warden_env = nil
    Devise::Strategies::LdapAuthenticatableTiny.new(warden_env)
  end

  def test_tls_options_text
    '{"tls_foo": "tls_bar"}'
  end

  def test_options(login, password)
    {
      :auth => {
        :method=>:simple,
        :username=>login,
        :password=>password
      },
      :encryption => {
        :method=>:simple_tls,
        :tls_options=>{
          :tls_foo => 'tls_bar'
        }
      },
      :host => 'foo.com',
      :port => 12345
    }
  end

  describe '#authenticate! integration tests across districts, with is_authorized_by_ldap mocked' do
    def mocked_test_strategy(options = {})
      strategy = test_strategy
      allow(strategy).to receive_messages({
        authentication_hash: { login_text: options[:login_text] },
        password: options[:password]
      })
      allow(strategy).to receive(:is_authorized_by_ldap?).with(MockLDAP, options[:ldap_login], options[:password]).and_return options[:is_authorized_by_ldap?]
      strategy
    end

    # just sugar
    def outcomes_after_authenticate!(strategy)
      strategy.authenticate!
      [strategy.result, strategy.message, strategy.user]
    end

    it 'works for Somerville' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::SOMERVILLE))
      pals = TestPals.create!(email_domain: 'k12.somerville.ma.us')
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'works for New Bedford' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::NEW_BEDFORD))
      pals = TestPals.create!(email_domain: 'newbedfordschools.org')
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'works for Bedford' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::BEDFORD))
      pals = TestPals.create!(email_domain: 'bedfordps.org')
      strategy = mocked_test_strategy({
        login_text: 'uri',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end

    it 'works for demo' do
      allow(PerDistrict).to receive(:new).and_return(PerDistrict.new(district_key: PerDistrict::DEMO))
      TestPals.create!
      strategy = mocked_test_strategy({
        login_text: '<EMAIL>',
        password: '<PASSWORD>',
        ldap_login: '<EMAIL>',
        is_authorized_by_ldap?: true
      })
      expect(outcomes_after_authenticate!(strategy)).to eq [:success, nil, pals.uri]
    end
  end

  describe '#authenticate! when methods are mocked' do
    let!(:pals) { TestPals.create! }

    it 'calls fail when email not found' do
      strategy = test_strategy
      allow(strategy).to receive(:authentication_hash) { { login_text: '<EMAIL>' } }
      strategy.authenticate!

      expect(strategy.result).to eq :failure
{'fraction_non_alphanumeric': 0.12312355658198615, 'fraction_numerical': 0.0020207852193995382, 'mean_word_length': 3.214720194647202, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 49, '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': '3858879', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2222, 'n_words': 412}