<commit_before>require './spec/support/ldap_server'

module Mconf
  class LdapServer
    attr_accessor :running, :port, :uid_field, :name_field, :mail_field

    def initialize(ldap_configs=nil)
      ldap_configs ||= Mconf::LdapServer.default_ldap_configs

      @server = SpecLdapServer.new(port: ldap_configs[:ldap_port])

      # 'admin' toplevel user with password '<PASSWORD>' or use info from the db
      toplevel = ldap_configs[:ldap_user]
      toplevel_password = ldap_configs[:ldap_user_password]
      @server.add_user toplevel, toplevel_password

      # add a user 'mconf' with password '<PASSWORD>'
      @user_tree = ldap_configs[:ldap_user_treebase]
      @uid_field = ldap_configs[:ldap_username_field]
      @name_field = ldap_configs[:ldap_name_field]
      @mail_field = ldap_configs[:ldap_email_field]
    end

    def run
      @server.run_tcpserver
      @running = true
      while @running do
        sleep 10
      end
    end

    def add_user(uid, pass, mail)
      @server.add_user "#{@uid_field}=#{uid},#{@user_tree}", pass,
        { @uid_field => uid, @name_field => uid, @mail_field => mail }
    end

    def add_default_user
      add_user(*Mconf::LdapServer.default_user.values)
    end

    def self.default_user
      {
        username: 'mconf-user',
        password: '<PASSWORD>',
        email: '<EMAIL>'
      }
    end

    def self.default_ldap_configs
      {
        ldap_enabled: true,
        ldap_host: '127.0.0.1',
        ldap_port: 1389,
        ldap_user: "cn=admin,cn=TOPLEVEL,dc=example,dc=com",
        ldap_user_password: "<PASSWORD>",
        ldap_user_treebase: "ou=USERS,dc=example,dc=com",
        ldap_username_field: 'uid',
        ldap_name_field: 'cn',
        ldap_email_field: 'mail'
      }
    end
<commit_msg>Clean Site#ldap_filter when setting up the test LDAP server<commit_after>require './spec/support/ldap_server'

module Mconf
  class LdapServer
    attr_accessor :running, :port, :uid_field, :name_field, :mail_field

    def initialize(ldap_configs=nil)
      ldap_configs ||= Mconf::LdapServer.default_ldap_configs

      @server = SpecLdapServer.new(port: ldap_configs[:ldap_port])

      # 'admin' toplevel user with password 'admin' or use info from the db
      toplevel = ldap_configs[:ldap_user]
      toplevel_password = ldap_configs[:ldap_user_password]
      @server.add_user toplevel, toplevel_password

      # add a user 'mconf' with password '<PASSWORD>'
      @user_tree = ldap_configs[:ldap_user_treebase]
      @uid_field = ldap_configs[:ldap_username_field]
      @name_field = ldap_configs[:ldap_name_field]
      @mail_field = ldap_configs[:ldap_email_field]
    end

    def run
      @server.run_tcpserver
      @running = true
      while @running do
        sleep 10
      end
    end

    def add_user(uid, pass, mail)
      @server.add_user "#{@uid_field}=#{uid},#{@user_tree}", pass,
        { @uid_field => uid, @name_field => uid, @mail_field => mail }
    end

    def add_default_user
      add_user(*Mconf::LdapServer.default_user.values)
    end

    def self.default_user
      {
        username: 'mconf-user',
        password: '<PASSWORD>',
        email: '<EMAIL>'
      }
    end

    def self.default_ldap_configs
      {
        ldap_enabled: true,
        ldap_host: '127.0.0.1',
        ldap_port: 1389,
        ldap_user: "cn=admin,cn=TOPLEVEL,dc=example,dc=com",
        ldap_user_password: "<PASSWORD>",
        ldap_user_treebase: "ou=USERS,dc=example,dc=com",
        ldap_username_field: 'uid',
        ldap_name_field: 'cn',
        ldap_email_field: 'mail',
        ldap_filter: ''
      }
    end

{'fraction_non_alphanumeric': 0.11046831955922864, 'fraction_numerical': 0.006611570247933884, 'mean_word_length': 2.9254054054054053, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6418133', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1283, 'n_words': 237}