require 'spec_helper'

describe 'Edit customer settings' do

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

  describe 'Update user details' do
    it 'should fail without valid values' do
      login_as_user customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'customer_name', with: ''
      click_button 'settings_submit_button'
      page.should have_content "can't be blank"
      User.last.name.should eq 'Jhon Doe'
    end

    it 'should update user details' do
      @school = create :school
      login_as_user customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'customer_name', with: 'Changed'
      select @school.name, from: 'customer_school_id'
      click_button 'settings_submit_button'
      page.should_not have_content "Can't save this record"
      page.should have_content 'Changed'
      User.last.name.should eq 'Changed'
      User.last.school.should eq @school
    end

  end

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

    it 'should fail with invalid password' do
      login_as_user customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'customer_password', with: 'a'
      fill_in 'customer_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 customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'customer_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 customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      fill_in 'customer_password', with: 'new-secret'
      fill_in 'customer_password_confirmation', with: 'new-secret'
      click_button 'settings_submit_button'
      click_link 'logout_link'
      login_as_user customer, password: 'new-secret'
      page.should have_content customer.name
    end
  end


  describe 'update avatar' do
    before :each do
      @picture_path = File.join(Rails.root, 'spec', 'fixtures', 'picture.png')
    end

    it 'should update customer avatar if attach a file' do
      pending "#FIXME failing in jenkins - undefined method `exitstatus' for nil:NilClass"
      login_as_user customer
      click_link 'settings_link'
      click_link 'update_settings_button'
      page.should_not have_content 'Delete this image'
      attach_file 'customer_avatar', @picture_path
      click_button 'settings_submit_button'
      customer.reload
      customer.avatar_file_name.should eq 'picture.png'
    end


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

  end
end
