<commit_before>require 'rails_helper'

describe ::BatchInviteJob do
  let(:current_user) { FactoryGirl.create(:user, email: '<EMAIL>', superadmin: true) }
  let(:org) { FactoryGirl.create :organisation, email: '<EMAIL>' }
  let(:params) do
    {invite_list: {org.id => org.email,
                   org.id+1 => org.email},
                   resend_invitation: false}
  end

  before do
    current_user # lazy-loading messes up DB counts
  end

  subject { BatchInviteJob.new(params, current_user).run }

  context 'success' do
    let(:user) { User.find_by_email org.email.downcase }

    it 'a new user is persisted' do
      expect(-> { subject }).to change(User, :count).by(1)
    end

    it 'warning: email may be mutated' do
      subject
      expect(user.email).to_not eq org.email
      expect(user.email).to eq org.email.downcase
    end

    it 'associations can be set' do
      subject
      expect(user.organisation_id).to eq org.id
    end

    it 'sends a custom email' do
      subject
      email = ActionMailer::Base.deliveries.last
      expect(email.from).to eq ['<EMAIL>']
      expect(email.reply_to).to eq ['<EMAIL>']
      expect(email.to).to eq [user.email]
      expect(email.cc).to eq ['<EMAIL>']
      expect(email.subject).to eq 'Welcome to Harrow Community Network!'
    end

    it 'example response for invites with duplicates' do
      expect(subject).to eq(
        {org.id => 'Invited!',
         (org.id+1) => 'Error: Email has already been taken'}
      )
    end


   xit 'resend invitation to a deleted user (not isolated)' do
     deleted_user = FactoryGirl.create(:user, email: '<EMAIL>', deleted_at: "2015-11-11 08:37:14")
     expect(-> { subject }).to change(User, :count).by(1)
   end

    it 'resend invitations to a deleted user' do
      deleted_user = FactoryGirl.create(:deleted_user, email: '<EMAIL>')
      valid_user = instance_double('User', errors: [])
      invalid_user = instance_double('User', errors: ['email has been taken'])
      expect(User).to receive(:purge_deleted_users_where).with(email: ['<EMAIL>', '<EMAIL>'])
      allow(User).to receive(:invite!).and_return(valid_user)
      subject
    end

  end

  context 'failure' do
    let(:params) do
      {invite_list: {org.id => ''},
       resend_invitation: false}
    end

    it 'new user is NOT persisted' do
      expect(-> { subject }).to change(User, :count).by(0)
    end

    it 'no association is set' do
      expect(org.users.any?).to be false
    end<commit_msg>deleted unnecessary test code from batch_invite_job_spec<commit_after>require 'rails_helper'

describe ::BatchInviteJob do
  let(:current_user) { FactoryGirl.create(:user, email: '<EMAIL>', superadmin: true) }
  let(:org) { FactoryGirl.create :organisation, email: '<EMAIL>' }
  let(:params) do
    {invite_list: {org.id => org.email,
                   org.id+1 => org.email},
                   resend_invitation: false}
  end

  before do
    current_user # lazy-loading messes up DB counts
  end

  subject { BatchInviteJob.new(params, current_user).run }

  context 'success' do
    let(:user) { User.find_by_email org.email.downcase }

    it 'a new user is persisted' do
      expect(-> { subject }).to change(User, :count).by(1)
    end

    it 'warning: email may be mutated' do
      subject
      expect(user.email).to_not eq org.email
      expect(user.email).to eq org.email.downcase
    end

    it 'associations can be set' do
      subject
      expect(user.organisation_id).to eq org.id
    end

    it 'sends a custom email' do
      subject
      email = ActionMailer::Base.deliveries.last
      expect(email.from).to eq ['<EMAIL>']
      expect(email.reply_to).to eq ['<EMAIL>']
      expect(email.to).to eq [user.email]
      expect(email.cc).to eq ['<EMAIL>']
      expect(email.subject).to eq 'Welcome to Harrow Community Network!'
    end

    it 'example response for invites with duplicates' do
      expect(subject).to eq(
        {org.id => 'Invited!',
         (org.id+1) => 'Error: Email has already been taken'}
      )
    end


   xit 'resend invitation to a deleted user (not isolated)' do
     deleted_user = FactoryGirl.create(:user, email: '<EMAIL>', deleted_at: "2015-11-11 08:37:14")
     expect(-> { subject }).to change(User, :count).by(1)
   end

    it 'resend invitations to a deleted user' do
      deleted_user = FactoryGirl.create(:deleted_user, email: '<EMAIL>')
      valid_user = instance_double('User', errors: [])
      expect(User).to receive(:purge_deleted_users_where).with(email: ['<EMAIL>', '<EMAIL>'])
      allow(User).to receive(:invite!).and_return(valid_user)
      subject
    end

  end

  context 'failure' do
    let(:params) do
      {invite_list: {org.id => ''},
       resend_invitation: false}
    end

    it 'new user is NOT persisted' do
      expect(-> { subject }).to change(User, :count).by(0)
    end

    it 'no association is set' do
      expect(org.users.any?).to be false
    end
{'fraction_non_alphanumeric': 0.13130294473578055, 'fraction_numerical': 0.007664380798709157, 'mean_word_length': 2.87421875, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 37, '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': '4724754', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1585, 'n_words': 435}