require 'spec_helper'

describe "signin", type: :feature do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }

  it "success" do
    visit signin_path
    within('#landing-inner form.signin-form') do
      fill_in "Email Address:", with: user.email
      fill_in "Password:", with: user.password
      click_button "SIGN IN"
    end

    find('.curtain', text: 'Connecting...')
  end

  it "success with redirect" do
    visit signin_path + '?' + {'redirect-to' => '/'}.to_query
    within('#landing-inner form.signin-form') do
      fill_in "Email Address:", with: user.email
      fill_in "Password:", with: user.password
      click_button "SIGN IN"
    end

    should_be_at_root
  end

  # proves that redirect-to is preserved between failure
  it 'failure, then success with redirect' do

    visit signin_path + '?' + {'redirect-to' => '/'}.to_query
    within('#landing-inner form.signin-form') do
      fill_in "Email Address:", with: user.email
      fill_in "Password:", with: 'wrong'
      click_button "SIGN IN"
    end

    find('h1', text:'sign in or register')
    find('#landing-inner .login-error')

    within('#landing-inner form.signin-form') do
      fill_in "Email Address:", with: user.email
      fill_in "Password:", with: user.password
      click_button "SIGN IN"
    end

    should_be_at_root
  end

  describe "already logged in" do

    it "redirects back to /client" do
      visit signin_path

      within('#landing-inner form.signin-form') do
        fill_in "Email Address:", with: user.email
        fill_in "Password:", with: user.password
        click_button "SIGN IN"
      end

      find('.curtain', text: 'Connecting...')

      visit signin_path

      find('.curtain', text: 'Connecting...')
    end

    it "redirects back to forum if sso=forum" do
      visit signin_path

      within('#landing-inner form.signin-form') do
        fill_in "Email Address:", with: user.email
        fill_in "Password:", with: user.password
        click_button "SIGN IN"
      end
      find('.curtain', text: 'Connecting...')

      visit signin_path + '?' + {:sso => :forums}.to_query

      find('h1', text: 'welcome to fake login page')
    end
  end

  describe "with javascript",  :js => true, :type => :feature, :capybara_feature => true do

    it "shows signup form when asked" do
      visit signin_path
      find('.show-signup-dialog').click
      # toggle back to signin
      find('.show-signin-dialog').click
      # toggle back to signup
      find('.show-signup-dialog').click
    end

    it "signout" do
      sign_in_poltergeist(user)

      sign_out_poltergeist

      wait_until_curtain_gone

      # musicians homecard should be disabled
      find('.homecard.musicians.not-logged-in').click
      find('h1', text: 'Login Required')
      find('.btnClose').click

      # profile homecard should be disabled (this one is handled in homeScreen.js instead of in layout.js)
      find('.homecard.profile.not-logged-in').click
      find('h1', text: 'Login Required')
      find('.btnClose').click

      # sidebar should be disabled
      find('[layout-id="panelSearch"] [layout-panel="expanded"] [layout-panel="header"]').click
      find('h1', text: 'Login Required')
      find('.btnClose').click
    end


    it "signout with custom domain for cookie" do
      sign_in_poltergeist(user)
      original = Rails.application.config.session_cookie_domain

      begin
        Rails.application.config.session_cookie_domain = '.127.0.0.1'
        create_cookie("remember_token", user.remember_token, domain: '127.0.0.1')
        sign_out_poltergeist
      ensure
        Rails.application.config.session_cookie_domain = original
      end

    end


    it "can't signout with custom domain for cookie" do
      sign_in_poltergeist(user)
      original = Rails.application.config.session_cookie_domain

      begin
        Rails.application.config.session_cookie_domain = 'blah'
        sign_out_poltergeist
      ensure
        Rails.application.config.session_cookie_domain = original
      end
    end
  end

end
