<commit_before># coding: UTF-8
require 'spec_helper'

describe User do
  before(:all) do
    puts "\n[rspec][user_spec] Creating test user databases..."
    @new_user = new_user
    @user     = create_user :email => '<EMAIL>', :username => 'admin', :password => '<PASSWORD>'
    @user2    = create_user :email => '<EMAIL>',  :username => 'user',  :password => '<PASSWORD>'

    puts "[rspec][user_spec] Loading user data..."
    reload_user_data(@user) && @user.reload

    puts "[rspec][user_spec] Running..."
  end

  it "should set up a user after create" do
    @new_user.save
    @new_user.reload
    @new_user.should_not be_new
    @new_user.in_database.test_connection.should == true
    @new_user.database_name.should_not be_nil
  end

  it "should have a crypted password" do
    @user.crypted_password.should_not be_blank
    @user.crypted_password.should_not == '<PASSWORD>'
  end

  it "should authenticate if given email and password are correct" do
    User.authenticate('<EMAIL>', 'admin123').should == @user
    User.authenticate('<EMAIL>', 'admin321').should be_nil
    User.authenticate('', '').should be_nil
  end

  it "should authenticate with case-insensitive email and username" do
    User.authenticate('<EMAIL>', 'admin123').should == @user
    User.authenticate('<EMAIL>', 'admin123').should == @user
    User.authenticate('admin', 'admin123').should == @user
    User.authenticate('ADMIN', 'admin123').should == @user
  end

  it "should only allow legal usernames" do
    illegal_usernames = %w(si$mon 'sergio estella' j@vi sergio£££ simon_tokumine simon.tokumine SIM<NAME>)
    legal_usernames   = %w(simon javier-de-la-torre sergio-leiva sergio99)

    illegal_usernames.each do |name|
      @user.username = name
      @user.valid?.should be_false
      @user.errors[:username].should be_present
    end

    legal_usernames.each do |name|
      @user.username = name
      @user.valid?.should be_true
      @user.errors[:username].should be_blank
    end
  end

  it "should validate that password is present if record is new and crypted_password or salt are blank" do
    user = User.new
    user.username = "adminipop"
    user.email = "<EMAIL>"

    user.valid?.should be_false
    user.errors[:password].should be_present

    another_user = new_user(user.values.merge(:password => "<PASSWORD>"))
    user.crypted_password = <PASSWORD>
    user.salt = another_user.salt
    user.valid?.should be_true
    user.save

    # Let's ensure that crypted_password and salt does not change
    user_check = User[user.id]
    user_check.crypted_password.should == <PASSWORD>
    user_check.salt.should == another_user.salt

    user.password = <PASSWORD>
    user.password_confirmation = <PASSWORD>
    user.valid?.should be_true
  end

  it "should read api calls from external service" do
    file = Tempfile.new 'foo'
    wadus = {"per_day" => [1, 2, 3, 4, 5], "total" => 15 }
    file.write(wadus.to_json)
    file.rewind
    @user.stubs(:open).returns(file)
    
    @user.set_api_calls
    @user.get_api_calls['per_day'].should == wadus['per_day']
    @user.get_api_calls['total'].should == wadus['total']
    @user.get_api_calls['updated_at'].should be <= Time.now.to_i
    file.close
  end

  it "should have many tables" do
    @user2.tables.should be_empty
    create_table :user_id => @user2.id, :name => 'My first table', :privacy => Table::PUBLIC
    @user2.reload
    @user2.tables_count.should == 1
    @user2.tables.all.should == [Table.first(:user_id => @user2.id)]
  end

  it "should update remaining quotas when adding or removing tables" do
    initial_quota = @user2.remaining_quota

    expect { create_table :user_id => @user2.id, :privacy => Table::PUBLIC }
      .to change { @user2.remaining_table_quota }.by(-1)
<commit_msg>Extend user api calls testcase<commit_after># coding: UTF-8
require 'spec_helper'

describe User do
  before(:all) do
    puts "\n[rspec][user_spec] Creating test user databases..."
    @new_user = new_user
    @user     = create_user :email => '<EMAIL>', :username => 'admin', :password => '<PASSWORD>'
    @user2    = create_user :email => '<EMAIL>',  :username => 'user',  :password => '<PASSWORD>'

    puts "[rspec][user_spec] Loading user data..."
    reload_user_data(@user) && @user.reload

    puts "[rspec][user_spec] Running..."
  end

  it "should set up a user after create" do
    @new_user.save
    @new_user.reload
    @new_user.should_not be_new
    @new_user.in_database.test_connection.should == true
    @new_user.database_name.should_not be_nil
  end

  it "should have a crypted password" do
    @user.crypted_password.should_not be_blank
    @user.crypted_password.should_not == '<PASSWORD>'
  end

  it "should authenticate if given email and password are correct" do
    User.authenticate('<EMAIL>', '<PASSWORD>123').should == @user
    User.authenticate('<EMAIL>', '<PASSWORD>321').should be_nil
    User.authenticate('', '').should be_nil
  end

  it "should authenticate with case-insensitive email and username" do
    User.authenticate('<EMAIL>', 'admin123').should == @user
    User.authenticate('<EMAIL>', 'admin123').should == @user
    User.authenticate('admin', 'admin123').should == @user
    User.authenticate('ADMIN', 'admin123').should == @user
  end

  it "should only allow legal usernames" do
    illegal_usernames = %w(si$mon 'sergio estella' j@vi sergio£££ simon_tokumine simon.tokumine SIM<NAME>)
    legal_usernames   = %w(simon javier-de-la-torre sergio-leiva sergio99)

    illegal_usernames.each do |name|
      @user.username = name
      @user.valid?.should be_false
      @user.errors[:username].should be_present
    end

    legal_usernames.each do |name|
      @user.username = name
      @user.valid?.should be_true
      @user.errors[:username].should be_blank
    end
  end

  it "should validate that password is present if record is new and crypted_password or salt are blank" do
    user = User.new
    user.username = "adminipop"
    user.email = "<EMAIL>"

    user.valid?.should be_false
    user.errors[:password].should be_present

    another_user = new_user(user.values.merge(:password => "<PASSWORD>"))
    user.crypted_password = <PASSWORD>_user.crypted_password
    user.salt = another_user.salt
    user.valid?.should be_true
    user.save

    # Let's ensure that crypted_password and salt does not change
    user_check = User[user.id]
    user_check.crypted_password.should == another_user.crypted_password
    user_check.salt.should == another_user.salt

    user.password = <PASSWORD>
    user.password_confirmation = nil
    user.valid?.should be_true
  end

  it "should read api calls from external service" do
    file = Tempfile.new 'foo'
    wadus = {"per_day" => [1, 2, 3, 4, 5], "total" => 15 }
    file.write(wadus.to_json)
    file.rewind
    @user.stubs(:open).returns(file)
    
    @user.set_api_calls
    @user.get_api_calls['per_day'].should == wadus['per_day']
    @user.get_api_calls['total'].should == wadus['total']
    @user.get_api_calls['updated_at'].should be <= Time.now.to_i
    file.close

    # Should update api calls only once every 24 hours
    $users_metadata.HMSET @user.key, 'api_calls', {"updated_at" => 25.hours.ago}.to_json
    @user.expects(:open).times(1)
    @user.set_api_calls
    $users_metadata.HMSET @user.key, 'api_calls', {"updated_at" => 20.hours.ago}.to_json
    @user.expects(:open).times(0)
    @user.set_api_calls
  end

  it "should have many tables" do
    @user2.tables.should be_empty
    create_table :user_id => @user2.id, :name => 'My first table', :privacy => Table::PUBLIC
    @user2.reload
    @user2.tables_count.should == 1
    @user2.tables.all.should == [Table.first(:user_id => @user2.id)]
  end

  it "should update remaining quotas when adding or removing tables" do
    initial_quota = @user2.remaining_quota

    expect { create_table :user_id => @user2.id, :privacy => Table::PUBLIC }
      .to change { @user2.remaining_table_quota }.by(-1)

{'fraction_non_alphanumeric': 0.1316350859580876, 'fraction_numerical': 0.011042790814405823, 'mean_word_length': 3.5963091118800463, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 67, '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': '3067330', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2666, 'n_words': 629}