<commit_before>require 'rails_helper'

RSpec.describe User, type: :model do
  pending "add some examples to (or delete) #{__FILE__}"
end<commit_msg>Tests for the User model<commit_after>require 'rails_helper'

RSpec.describe User, type: :model do

  context 'associations' do
    it { is_expected.to have_many(:identities).dependent(:destroy) }
  end

  context 'validations' do
    subject { FactoryGirl.build(:user) }
    it { is_expected.to validate_presence_of(:name) }
    it { is_expected.to validate_presence_of(:locale) }
    it { is_expected.to validate_presence_of(:time_zone) }
    it { is_expected.to validate_uniqueness_of(:email).
          case_insensitive.allow_blank }
    it { is_expected.to validate_inclusion_of(:locale).
          in_array(I18n.available_locales.map(&:to_s)) }
    it { is_expected.to validate_inclusion_of(:time_zone).
          in_array(ActiveSupport::TimeZone::MAPPING.keys) }
  end

  describe '#complete?' do
    subject { FactoryGirl.build(:user) }
    it 'returns true if all main fields are present' do
      expect(subject).to be_complete
    end
    it 'returns false if any main field is blank' do
      subject.email = ''
      expect(subject).to_not be_complete
    end
  end

  describe '#just_created?' do
    subject { FactoryGirl.build(:user) }
    it 'returns false for non-persisted users' do
      expect(subject.just_created?).to be_falsy
    end
    it 'returns true if the user has recently been created and not changed' do
      subject.save!
      expect(subject.just_created?).to be_truthy
    end
    it 'returns false if the user has been changed since creation' do
      subject.save!
      subject.update_attributes(name: '<NAME>')
      expect(subject.just_created?).to be_falsy
    end
  end

  describe '.find_or_create_from_auth_hash!' do
    let(:auth_hash) { {'some' => 'values'} }
    let(:user) { FactoryGirl.build(:user) }
    context 'when the user already exists' do
      it 'returns the existing user' do
        expect(User).to receive(:find_from_auth_hash).
          with(auth_hash).and_return(user)
        returned_user = described_class.find_or_create_from_auth_hash!(auth_hash)
        expect(returned_user).to eq(user)
      end
    end
    context 'when the user does not exist yet' do
      before do
        expect(User).to receive(:find_from_auth_hash).
          with(auth_hash).and_return(nil)
      end
      it 'creates an user and returns it' do
        expect(User).to receive(:create_from_auth_hash!).
          with(auth_hash).and_return(user)
        returned_user = described_class.find_or_create_from_auth_hash!(auth_hash)
        expect(returned_user).to eq(user)
      end
      it 'raises an erro if it can not create the user' do
        expect(User).to receive(:create_from_auth_hash!).
          with(auth_hash).and_raise(RuntimeError, 'something went wrong')
        expect { described_class.find_or_create_from_auth_hash!(auth_hash) }.
          to raise_error(RuntimeError, 'something went wrong')
      end
    end
  end

  describe '.find_from_auth_hash(auth_hash)' do
    subject { FactoryGirl.create(:user_with_identity) }
    let(:identity) { subject.identities.first }
    let(:auth_hash) { {'provider' => identity.provider, 'uid' => identity.uid} }
    context 'when the user exists' do
      it 'returns the user' do
        user = described_class.find_from_auth_hash(auth_hash)
        expect(user).to eq(subject)
      end
    end
    context 'when the user does not exist yet' do
      it 'returns nil' do
        user = described_class.find_from_auth_hash(
          auth_hash.merge('uid' => 'another_uid')
        )
        expect(user).to be_nil
      end
    end
  end

  describe '.create_from_auth_hash!' do
    let(:auth_hash) {
      { 'provider' => "strava",
        'uid' => '123456',
        'info' => {"name"=>"<NAME>", "email"=>"<EMAIL>", "location"=>"New York NY", "last_name"=>"Doe", "first_name"=>"John"},
        'credentials' => {"token"=>"8a7be76f40f91c32f34ef34a129208ab0aad7d99", "expires"=>false},
        'extra' => {"raw_info"=>{"id"=>1234567, "ftp"=>nil, "sex"=>"M", "city"=>"New York", "bikes"=>[{"id"=>"b1500344", "name"=>"Old Bike", "primary"=>false, "distance"=>86716.0, "resource_state"=>2}, {"id"=>"b1234288", "name"=>"Current Bike", "primary"=>true, "distance"=>6057094.0, "resource_state"=>2}], "clubs"=>[{"id"=>12345, "name"=>"Some Club", "profile"=>"https://dgalywyr863hv.cloudfront.net/pictures/clubs/38174/1036347/4/large.jpg", "profile_medium"=>"https://dgalywyr863hv.cloudfront.net/pictures/clubs/38174/1036347/4/medium.jpg", "resource_state"=>2}], "email"=>"<EMAIL>", "shoes"=>[{"id"=>"g199111", "name"=>"Current Shoes", "primary"=>true, "distance"=>9000.0, "resource_state"=>2}], "state"=>"NY", "friend"=>nil, "weight"=>75.0, "country"=>"USA", "premium"=>true, "profile"=>"https://dgalywyr863hv.cloudfront.net/pictures/athletes/1234567/913413/2/large.jpg", "follower"=>nil, "lastname"=>"Doe", "firstname"=>"John", "created_at"=>"2013-08-22T15:32:03Z", "updated_at"=>"2015-10-02T06:02:51Z", "athlete_type"=>0, "friend_count"=>46, "badge_type_id"=>1, "follower_count"=>45, "profile_medium"=>"https://dgalywyr863hv.cloudfront.net/pictures/athletes/1234567/913413/2/medium.jpg", "resource_state"=>3, "date_preference"=>"%d/%m/%Y", "mutual_friend_count"=>0, "measurement_preference"=>"meters"}, "all_ride_totals"=>nil, "ytd_ride_totals"=>nil, "recent_ride_totals"=>nil}
      }
    }
    it 'returns the created user' do
      user = nil
      expect { user = described_class.create_from_auth_hash!(auth_hash) }.
        to change { User.count }.by(1)
      expect(user).to_not be_nil
      expect(user).to be_instance_of(User)
      expect(user).to be_persisted
    end
    it 'creates a new user and identity with values from the auth_hash' do
      user = described_class.create_from_auth_hash!(auth_hash)
      expect(user.name).to eq("<NAME>")
      expect(user.email).to eq("<EMAIL>")
      expect(user.locale).to eq('en')
      expect(user.time_zone).to eq('UTC')
      expect(user.identities.count).to eq(1)
      identity = user.identities.first
      expect(identity.provider).to eq("strava")
      expect(identity.uid).to eq('123456')
      expect(identity.info).to eq(auth_hash['info'])
      expect(identity.credentials).to eq(auth_hash['credentials'])
      expect(identity.extra).to eq(auth_hash['extra'])
    end
    it 'raises an error if the user can not be created' do
      auth_hash['info']['name'] = ''
      expect(User.count).to eq(0)
      expect { described_class.create_from_auth_hash!(auth_hash) }.
        to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
      expect(User.count).to eq(0)
    end
  end

end
{'fraction_non_alphanumeric': 0.1488192484776474, 'fraction_numerical': 0.030001485222040696, 'mean_word_length': 3.8620938628158843, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 77, 'https://': 4, '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': '5770671', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2286, 'n_words': 448}