<commit_before>require 'test/unit'
require 'active_record'
require 'letmein'
require 'sqlite3'

ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
$stdout_orig = $stdout
$stdout = StringIO.new

class User < ActiveRecord::Base
  # example values for password info:
  #   pass: <PASSWORD>
  #   salt: <PASSWORD>
  letmein :username, :pass_crypt, :pass_salt
end

class LetMeInTest < Test::Unit::TestCase
  def setup
    ActiveRecord::Base.logger
    ActiveRecord::Schema.define(:version => 1) do
      create_table :users do |t|
        t.column :username,   :string
        t.column :pass_crypt, :string
        t.column :pass_salt,  :string
      end
    end
  end
  
  def teardown
    ActiveRecord::Base.connection.tables.each do |table|
      ActiveRecord::Base.connection.drop_table(table)
    end
  end
  
  def test_configuration_defaults
    assert config = LetMeIn::Configuration.new
    assert_equal nil,             config.model
    assert_equal 'email',         config.identifier
    assert_equal 'password_hash', config.password
    assert_equal 'password_salt', config.salt
  end
  
  def test_configuration_initialization
    conf = LetMeIn.configuration
    assert_equal 'User',        conf.model
    assert_equal 'username',    conf.identifier
    assert_equal 'pass_crypt',  conf.password
    assert_equal 'pass_salt',   conf.salt
  end
  
  def test_model_password_saving
    user = User.new(:username => 'test', :password => '<PASSWORD>')
    user.save!
    user = User.find(user.id)
    assert_equal nil, user.password
    assert_match /.{60}/, user.pass_crypt
    assert_match /.{29}/, user.pass_salt
  end
  
  def test_session_initialization
    session = LetMeIn::Session.new(:username => 'test_user', :password => '<PASSWORD>')
    assert_equal 'test_user', session.identifier
    assert_equal 'test_user', session.username
    assert_equal 'test_pass', session.password
    
    session.username = 'new_user'
    assert_equal 'new_user', session.identifier
    assert_equal 'new_user', session.username
    
    assert_equal nil, session.authenticated_object
    assert_equal nil, session.user
  end
  
  def test_session_authentication
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.create(:username => 'test', :password => '<PASSWORD>')
    assert session.errors.blank?
    assert_equal user, session.authenticated_object
    assert_equal user, session.user
    raise user.inspect
  end
  
  def test_session_authentication_failure
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.create(:username => 'test', :password => '<PASSWORD>')
    assert session.errors.present?
    assert_equal 'Failed to authenticate', session.errors[:base].first
    assert_equal nil, session.authenticated_object
    assert_equal nil, session.user
  end
  
  def test_session_authentication_exception
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.new(:username => 'test', :password => '<PASSWORD>')
    begin
      session.save!
    rescue LetMeIn::Error => e
      assert_equal 'Failed to authenticate', e.to_s
    end
    assert_equal nil, session.authenticated_object
  end
end<commit_msg>removing a debug i forgot<commit_after>require 'test/unit'
require 'active_record'
require 'letmein'
require 'sqlite3'

ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
$stdout_orig = $stdout
$stdout = StringIO.new

class User < ActiveRecord::Base
  # example values for password info:
  #   pass: <PASSWORD>DcKPJeD1.FzqkcOZfEKNZ/DNN.w8xOwuFdBCm
  #   salt: $2a$10$0MeSaaE3I7.0FQ5ZDcKPJe
  letmein :username, :pass_crypt, :pass_salt
end

class LetMeInTest < Test::Unit::TestCase
  def setup
    ActiveRecord::Base.logger
    ActiveRecord::Schema.define(:version => 1) do
      create_table :users do |t|
        t.column :username,   :string
        t.column :pass_crypt, :string
        t.column :pass_salt,  :string
      end
    end
  end
  
  def teardown
    ActiveRecord::Base.connection.tables.each do |table|
      ActiveRecord::Base.connection.drop_table(table)
    end
  end
  
  def test_configuration_defaults
    assert config = LetMeIn::Configuration.new
    assert_equal nil,             config.model
    assert_equal 'email',         config.identifier
    assert_equal 'password_hash', config.password
    assert_equal 'password_salt', config.salt
  end
  
  def test_configuration_initialization
    conf = LetMeIn.configuration
    assert_equal 'User',        conf.model
    assert_equal 'username',    conf.identifier
    assert_equal 'pass_crypt',  conf.password
    assert_equal 'pass_salt',   conf.salt
  end
  
  def test_model_password_saving
    user = User.new(:username => 'test', :password => '<PASSWORD>')
    user.save!
    user = User.find(user.id)
    assert_equal nil, user.password
    assert_match /.{60}/, user.pass_crypt
    assert_match /.{29}/, user.pass_salt
  end
  
  def test_session_initialization
    session = LetMeIn::Session.new(:username => 'test_user', :password => '<PASSWORD>')
    assert_equal 'test_user', session.identifier
    assert_equal 'test_user', session.username
    assert_equal 'test_pass', session.password
    
    session.username = 'new_user'
    assert_equal 'new_user', session.identifier
    assert_equal 'new_user', session.username
    
    assert_equal nil, session.authenticated_object
    assert_equal nil, session.user
  end
  
  def test_session_authentication
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.create(:username => 'test', :password => '<PASSWORD>')
    assert session.errors.blank?
    assert_equal user, session.authenticated_object
    assert_equal user, session.user
  end
  
  def test_session_authentication_failure
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.create(:username => 'test', :password => '<PASSWORD>')
    assert session.errors.present?
    assert_equal 'Failed to authenticate', session.errors[:base].first
    assert_equal nil, session.authenticated_object
    assert_equal nil, session.user
  end
  
  def test_session_authentication_exception
    user = User.create!(:username => 'test', :password => '<PASSWORD>')
    session = LetMeIn::Session.new(:username => 'test', :password => '<PASSWORD>')
    begin
      session.save!
    rescue LetMeIn::Error => e
      assert_equal 'Failed to authenticate', e.to_s
    end
    assert_equal nil, session.authenticated_object
  end
end
{'fraction_non_alphanumeric': 0.11096075778078485, 'fraction_numerical': 0.0036084799278304014, 'mean_word_length': 3.7514285714285713, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 62, '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': '2057441', 'n_tokens_mistral': 2082, 'n_tokens_neox': 2032, 'n_words': 459}