<commit_before>require 'spec_helper'

describe User do

  subject(:user){ described_class.new }

  it { should have_many :email_addresses }
  it { should have_many :organization_memberships }
  it { should have_many :organizations }
  it { should have_many :messages }
  it { should have_many :conversations }
  it { should have_and_belong_to_many :tasks }

  it { should validate_presence_of :name }
  # it { should validate_presence_of :email_addresses } # somehow this is broken now

  def build_user_with_password password
    FactoryGirl.build(:user, password: password, password_confirmation: password)
  end

  it "should ensure the user has a primary email address" do
    user = User.new
    user.valid?
    expect(user).to have(1).errors_on(:email_address)
    user = User.new email_address: '<EMAIL>'
    user.valid?
    expect(user).to have(0).errors_on(:email_address)
  end

  describe "password validations" do
    def user_attributes
      {}
    end

    let(:user){ build(:user, user_attributes) }

    subject{ user.errors_on(:password) }

    before{ user.valid? }

    it { should be_empty }

    context "when password is too short" do
      def user_attributes
        { password: "<PASSWORD>", password_confirmation: "<PASSWORD>" }
      end
      it { should eq ["is too short (minimum is 6 characters)"] }
    end

    context "when passwords do not match" do
      subject{ user.errors_on(:password_confirmation) }
      def user_attributes
        { password: "<PASSWORD>", password_confirmation: "<PASSWORD>" }
      end
      it { should eq ["doesn't match Password"] }
    end

    context "when password is present but password_confirmation is blank" do
      subject{ user.errors_on(:password_confirmation) }
      def user_attributes
        { password: "<PASSWORD>" }
      end
      it { should eq ["can't be blank"] }
    end

  end

  describe "factory" do
    def factory_options
      {email_address: '<EMAIL>'}
    end

    subject(:user){ build(:user, factory_options) }

    it "should have a valid factory" do
      expect(user).to be_valid
    end

    it "should use the gravatar as the default avatar when none is supplied" do
      expect(user.avatar_url).to eq '//gravatar.com/avatar/b48def645758b95537d4424c84d1a9ff.png?s=48&d=retro'
    end

    it "should not have a password" do
      expect(user.password).to be_nil
    end

    context "when web_enabled is true" do
      def factory_options
        super.merge(web_enabled: true)
      end
      it "should have a password" do
        expect(user.encrypted_password).to be_present
      end<commit_msg>fixed ./spec/models/user_spec.rb<commit_after>require 'spec_helper'

describe User do

  subject(:user){ described_class.new }

  it { should have_many :email_addresses }
  it { should have_many :organization_memberships }
  it { should have_many :organizations }
  it { should have_many :messages }
  it { should have_many :conversations }
  it { should have_and_belong_to_many :tasks }

  it { should validate_presence_of :name }
  # it { should validate_presence_of :email_addresses } # somehow this is broken now

  def build_user_with_password password
    FactoryGirl.build(:user, password: password, password_confirmation: password)
  end

  it "should ensure the user has a primary email address" do
    user = User.new
    user.valid?
    expect(user).to have(1).errors_on(:email_address)
    user = User.new email_address: '<EMAIL>'
    user.valid?
    expect(user).to have(0).errors_on(:email_address)
  end

  describe "password validations" do
    def user_attributes
      {}
    end

    let(:user){ build(:user, user_attributes) }

    subject{ user.errors_on(:password) }

    before{ user.valid? }

    it { should be_empty }

    context "when password is too short" do
      def user_attributes
        { password: "<PASSWORD>", password_confirmation: "<PASSWORD>" }
      end
      it { should eq ["is too short (minimum is 6 characters)"] }
    end

    context "when passwords do not match" do
      subject{ user.errors_on(:password_confirmation) }
      def user_attributes
        { password: "<PASSWORD>", password_confirmation: "<PASSWORD>" }
      end
      it { should eq ["doesn't match Password"] }
    end

    context "when password is present but password_confirmation is blank" do
      subject{ user.errors_on(:password_confirmation) }
      def user_attributes
        { password: "<PASSWORD>" }
      end
      it { should eq ["can't be blank"] }
    end

  end

  describe "factory" do
    def factory_options
      {email_address: '<EMAIL>'}
    end

    subject(:user){ build(:user, factory_options) }

    it "should have a valid factory" do
      expect(user).to be_valid
    end

    it "should use the gravatar as the default avatar when none is supplied" do
      expect(user.avatar_url).to eq '//gravatar.com/avatar/b48def645758b95537d4424c84d1a9ff.png?s=70&d=retro'
    end

    it "should not have a password" do
      expect(user.password).to be_nil
    end

    context "when web_enabled is true" do
      def factory_options
        super.merge(web_enabled: true)
      end
      it "should have a password" do
        expect(user.encrypted_password).to be_present
      end
{'fraction_non_alphanumeric': 0.09073543457497613, 'fraction_numerical': 0.009933142311365807, 'mean_word_length': 3.1066666666666665, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5362146', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1556, 'n_words': 472}