require 'spec_helper'

describe 'Edit fixxpert settings' do

  let(:fixxpert) { create :fixxpert, name: 'Jhon Doe', email: 'jhon@emain.com' }

  describe 'Update user details' do
    it 'should fail without valid values' do
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'fixxpert_name', with: ''
      select VALID_SESSION_LENGTHS.last.to_s, from: 'fixxpert_min_session_length'
      select VALID_SESSION_LENGTHS.first.to_s, from: 'fixxpert_max_session_length'
      click_button 'settings_submit_button'
      page.should have_content "can't be blank"
      page.should have_content "must be greater than or equal to "
      User.last.name.should eq 'Jhon Doe'
    end

    it 'should update user details' do
      @school = create :school
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'

      fill_in 'fixxpert_name', with: 'Fixxpert changed'
      fill_in 'fixxpert_about_me', with: 'Fixxpert about me'
      fill_in 'fixxpert_discounts', with: 'Fixxpert discounts'
      fill_in 'fixxpert_response_time', with: 'Fixxpert response time'
      fill_in 'fixxpert_education', with: 'Fixxpert education'
      fill_in 'fixxpert_header', with: 'Fixxpert header'

      select VALID_SESSION_LENGTHS.first.to_s, from: 'fixxpert_session_duration'
      fill_in 'fixxpert_rate', with: '50,60'
      select VALID_SESSION_LENGTHS.first.to_s, from: 'fixxpert_min_session_length'
      select VALID_SESSION_LENGTHS.last.to_s, from: 'fixxpert_max_session_length'

      click_button 'settings_submit_button'

      page.should_not have_content "Can't save this record"
      page.should have_content 'Fixxpert changed'
      page.should have_content 'Fixxpert about me'
      page.should have_content 'Fixxpert discounts'
      page.should have_content 'Fixxpert response time'
      page.should have_content 'Fixxpert education'
      page.should have_content 'Fixxpert header'

      fixxpert.reload

      fixxpert.name.should eq 'Fixxpert changed'
      fixxpert.about_me.should eq 'Fixxpert about me'
      fixxpert.session_duration.should eq VALID_SESSION_LENGTHS.first
      fixxpert.rate_in_cents.should eq 5060
    end

  end

  describe 'update user password' do
    it 'should not update password unless filled' do
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'fixxpert_name', with: 'Changed'
      click_button 'settings_submit_button'
      click_link 'logout_link'
      login_as_user fixxpert
      page.should have_content 'Changed'
    end

    it 'should fail with invalid password' do
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'fixxpert_password', with: 'a'
      fill_in 'fixxpert_password_confirmation', with: 'a'
      click_button 'settings_submit_button'
      page.should have_content "must be at least"
    end

    it 'should fail without password confirmation' do
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'fixxpert_password', with: 'new-secret'
      click_button 'settings_submit_button'
      page.should have_content "should match confirmation"
    end

    it 'should change password with valid password' do
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'fixxpert_password', with: 'new-secret'
      fill_in 'fixxpert_password_confirmation', with: 'new-secret'
      click_button 'settings_submit_button'
      click_link 'logout_link'
      login_as_user fixxpert, password: 'new-secret'
      page.should have_content fixxpert.name
    end
  end


  describe 'update avatar' do
    before :each do
      @picture_path = File.join(Rails.root, 'spec', 'fixtures', 'picture.png')
    end
    it 'should update user avatar if attach a file' do
      pending "#FIXME failing in jenkins"
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      page.should_not have_content 'Delete this image'
      attach_file 'fixxpert_avatar', @picture_path
      click_button 'settings_submit_button'
      fixxpert.reload
      fixxpert.avatar_file_name.should eq 'picture.png'
    end


    it 'should destroy fixxpert avatar  if click in destroy avatar' do
      pending "feature not present anymore"
      fixxpert.update_attributes avatar: File.new(@picture_path)
      login_as_user fixxpert
      click_link 'settings_link'
      click_link 'update_settings_button'
      click_link 'destroy_avatar'
      page.should have_content 'Your avatar has been removed.'
      fixxpert.reload
      fixxpert.avatar_file_name.should be_nil
    end

  end


end
