<commit_before># -*- encoding: utf-8 -*-
require 'rails_helper'

describe User do
  #pending "add some examples to (or delete) #{__FILE__}"
  fixtures :all

  it 'should create an user' do
    FactoryGirl.create(:user)
  end

  it 'should destroy an user' do
    user = FactoryGirl.create(:user)
    user.destroy.should be_truthy
  end

  it 'should respond to has_role(Administrator)' do
    admin = FactoryGirl.create(:admin)
    admin.has_role?('Administrator').should be_truthy
  end

  it 'should respond to has_role(Librarian)' do
    librarian = FactoryGirl.create(:librarian)
    librarian.has_role?('Administrator').should be_falsy
    librarian.has_role?('Librarian').should be_truthy
    librarian.has_role?('User').should be_truthy
  end

  it 'should respond to has_role(User)' do
    user = FactoryGirl.create(:user)
    user.has_role?('Administrator').should be_falsy
    user.has_role?('Librarian').should be_falsy
    user.has_role?('User').should be_truthy
  end

  it 'should lock an user' do
    user = FactoryGirl.create(:user)
    user.locked = '1'
    user.save
    user.active_for_authentication?.should be_falsy
  end

  it 'should unlock an user' do
    user = FactoryGirl.create(:user)
    user.lock_access!
    user.locked = '0'
    user.save
    user.active_for_authentication?.should be_truthy
  end

  it "should create user" do
    user = FactoryGirl.create(:user)
    assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
  end

  it "should require username" do
    old_count = User.count
    user = FactoryGirl.build(:user, :username => nil)
    user.save
    user.errors[:username].should be_truthy
    User.count.should eq old_count
  end

  it "should require password" do
    user = FactoryGirl.build(:user, :password => <PASSWORD>)
    user.save
    user.errors[:password].should be_truthy
  end

  it "should not require password_confirmation on create" do
    user = FactoryGirl.build(:user, :password => '<PASSWORD>', :password_confirmation => nil)
    user.save
    user.errors[:email].should be_empty
  end

  it "should reset password" do
    users(:user1).password = '<PASSWORD>'
    users(:user1).password_confirmation = '<PASSWORD>'
    users(:user1).save
    users(:user1).valid_password?('<PASSWORD>').should be_truthy
  end

  it "should set temporary_password" do
    user = users(:user1)
    old_password = user.encrypted_password
    user.set_auto_generated_password
    user.save
    old_password.should_not eq user.encrypted_password
    user.valid_password?('<PASSWORD>').should be_falsy
  end

  it "should get highest_role" do
    users(:admin).role.name.should eq 'Administrator'
  end

  it "should lock all expired users" do
    User.lock_expired_users
    users(:user4).active_for_authentication?.should be_falsy
  end

  it "should lock_expired users" do
    user = users(:user1)
    users(:user1).active_for_authentication?.should be_truthy
    user.expired_at = 1.day.ago
    user.save
    users(:user1).active_for_authentication?.should be_falsy
  end

  if defined?(EnjuQuestion)
    it "should reset answer_feed_token" do
      users(:user1).reset_answer_feed_token
      users(:user1).answer_feed_token.should be_truthy
    end

    it "should delete answer_feed_token" do
      users(:user1).delete_answer_feed_token
      users(:user1).answer_feed_token.should be_nil
    end
  end

  if defined?(EnjuCirculation)
    it "should get checked_item_count" do
      count = users(:user1).checked_item_count
      count.should eq({:book=>2, :serial=>1, :cd=>0})
    end

    it "should get reserves_count" do
      users(:user1).reserves.waiting.count.should eq 1
    end
  end

  if defined?(EnjuCirculation)
    it "should send_message" do
      assert users(:librarian1).send_message('reservation_expired_for_patron', :manifestations => users(:librarian1).reserves.not_sent_expiration_notice_to_patron.collect(&:manifestation))
      users(:librarian1).reload
      users(:librarian1).reserves.not_sent_expiration_notice_to_patron.should be_empty
    end
  end

  describe ".export" do
    it "should export all user's information" do
      lines = User.export
      CSV.parse(lines)
      expect(lines).not_to be_empty
      expect(lines.split(/\r\n/).size).to eq User.all.size + 1
    end

    it "should export share_bookmarks and save_search_history" do
      user = FactoryGirl.create(:user,
        profile: FactoryGirl.create(:profile,
          share_bookmarks: true,
          save_search_history: true))
      lines = User.export
      rows = CSV.new(lines, col_sep: "\t", headers: true)
      rows.each do |row|
        if row["username"] == user.username
          expect(row["share_bookmarks"]).to eq "true"
          expect(row["save_search_history"]).to eq "true"
        end
      end
    end

    it "should work even if EnjuBookmark module is undefined" do
      Object.send(:remove_const, :EnjuBookmark)
      lines = User.export
      expect(lines).not_to be_empty
      expect(lines.split(/\r\n/).size).to eq User.all.size + 1
    end
  end
end

# == Schema Information
#
# Table name: users<commit_msg>update spec file<commit_after># -*- encoding: utf-8 -*-
require 'rails_helper'

describe User do
  #pending "add some examples to (or delete) #{__FILE__}"
  fixtures :all

  it 'should create an user' do
    FactoryGirl.create(:user)
  end

  it 'should destroy an user' do
    user = FactoryGirl.create(:user)
    user.destroy.should be_truthy
  end

  it 'should respond to has_role(Administrator)' do
    admin = FactoryGirl.create(:admin)
    admin.has_role?('Administrator').should be_truthy
  end

  it 'should respond to has_role(Librarian)' do
    librarian = FactoryGirl.create(:librarian)
    librarian.has_role?('Administrator').should be_falsy
    librarian.has_role?('Librarian').should be_truthy
    librarian.has_role?('User').should be_truthy
  end

  it 'should respond to has_role(User)' do
    user = FactoryGirl.create(:user)
    user.has_role?('Administrator').should be_falsy
    user.has_role?('Librarian').should be_falsy
    user.has_role?('User').should be_truthy
  end

  it 'should lock an user' do
    user = FactoryGirl.create(:user)
    user.locked = '1'
    user.save
    user.active_for_authentication?.should be_falsy
  end

  it 'should unlock an user' do
    user = FactoryGirl.create(:user)
    user.lock_access!
    user.locked = '0'
    user.save
    user.active_for_authentication?.should be_truthy
  end

  it "should create user" do
    user = FactoryGirl.create(:user)
    assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
  end

  it "should require username" do
    old_count = User.count
    user = FactoryGirl.build(:user, :username => nil)
    user.save
    user.errors[:username].should be_truthy
    User.count.should eq old_count
  end

  it "should require password" do
    user = FactoryGirl.build(:user, :password => nil)
    user.save
    user.errors[:password].should be_truthy
  end

  it "should not require password_confirmation on create" do
    user = FactoryGirl.build(:user, :password => '<PASSWORD>', :password_confirmation => nil)
    user.save
    user.errors[:email].should be_empty
  end

  it "should reset password" do
    users(:user1).password = '<PASSWORD>'
    users(:user1).password_confirmation = '<PASSWORD>'
    users(:user1).save
    users(:user1).valid_password?('<PASSWORD>').should be_truthy
  end

  it "should set temporary_password" do
    user = users(:user1)
    old_password = user.encrypted_password
    user.set_auto_generated_password
    user.save
    old_password.should_not eq user.encrypted_password
    user.valid_password?('<PASSWORD>').should be_falsy
  end

  it "should get highest_role" do
    users(:admin).role.name.should eq 'Administrator'
  end

  it "should lock all expired users" do
    User.lock_expired_users
    users(:user4).active_for_authentication?.should be_falsy
  end

  it "should lock_expired users" do
    user = users(:user1)
    users(:user1).active_for_authentication?.should be_truthy
    user.expired_at = 1.day.ago
    user.save
    users(:user1).active_for_authentication?.should be_falsy
  end

  if defined?(EnjuQuestion)
    it "should reset answer_feed_token" do
      users(:user1).reset_answer_feed_token
      users(:user1).answer_feed_token.should be_truthy
    end

    it "should delete answer_feed_token" do
      users(:user1).delete_answer_feed_token
      users(:user1).answer_feed_token.should be_nil
    end
  end

  if defined?(EnjuCirculation)
    it "should get checked_item_count" do
      count = users(:user1).checked_item_count
      count.should eq({:book=>2, :serial=>1, :cd=>0})
    end

    it "should get reserves_count" do
      users(:user1).reserves.waiting.count.should eq 1
    end
  end

  if defined?(EnjuCirculation)
    it "should send_message" do
      assert users(:librarian1).send_message('reservation_expired_for_patron', :manifestations => users(:librarian1).reserves.not_sent_expiration_notice_to_patron.collect(&:manifestation))
      users(:librarian1).reload
      users(:librarian1).reserves.not_sent_expiration_notice_to_patron.should be_empty
    end
  end

  describe ".export" do
    it "should export all user's information" do
      lines = User.export
      CSV.parse(lines, col_sep: "\t")
      expect(lines).not_to be_empty
      expect(lines.split(/\r\n/).size).to eq User.all.size + 1
    end

    it "should export share_bookmarks and save_search_history" do
      user = FactoryGirl.create(:user,
        profile: FactoryGirl.create(:profile,
          share_bookmarks: true,
          save_search_history: true))
      lines = User.export
      rows = CSV.new(lines, col_sep: "\t", headers: true)
      rows.each do |row|
        if row["username"] == user.username
          expect(row["share_bookmarks"]).to eq "true"
          expect(row["save_search_history"]).to eq "true"
        end
      end
    end

    it "should work even if EnjuBookmark module is undefined" do
      Object.send(:remove_const, :EnjuBookmark)
      lines = User.export
      expect(lines).not_to be_empty
      expect(lines.split(/\r\n/).size).to eq User.all.size + 1
    end
  end
end

# == Schema Information
#
# Table name: users
{'fraction_non_alphanumeric': 0.10683217671781839, 'fraction_numerical': 0.005669045059134004, 'mean_word_length': 3.472027972027972, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 30, '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': '738998', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3303, 'n_words': 759}