require "rails_helper"

feature "User" do
  let(:user) { create(:user, :with_name, email: "test@example.com") }
  let!(:project) { create(:project, account: user.primary_account, name: "My Video Project") }

  context "when signing in" do
    scenario "is redirected on success" do
      visit "/"

      fill_in "Email", with: "test@example.com"
      fill_in "Password", with: "password"
      check "Remember me"

      click_button "Sign In"

      expect(page).to have_content "My Video Project"
    end

    scenario "signing out" do
      sign_in user

      visit signed_in_root_path
      click_on user.full_name

      click_link "Sign Out"

      expect(page).to have_content "Sign In"
    end
  end

  context "when signed in and accountless" do
    before :each do
      sign_in(create(:user, :accountless))
    end

    scenario "goes to accountless information page" do
      visit signed_in_root_path
      expect(page).to have_content /You are signed in but not added to an account/
    end
  end

  context "when signed in" do
    before :each do
      sign_in(user)
    end

    scenario "sees 404 page when navigating to unknown url" do
      expect { visit "/foo" }.to raise_exception(ActionController::RoutingError)
    end

    scenario "gets redirected to project page when navigating to sessions url" do
      visit new_session_path
      expect(page).to have_content user.primary_account.name
    end

    scenario "sees project page when navigating to known url" do
      visit project_path(project)

      expect(page).to have_content "My Video Project"
    end
  end

  context "when not signed in" do
    scenario "gets redirected to sign in path when navigating to unknown url" do
      expect { visit "/en/foo" }.to raise_exception(ActionController::RoutingError)
    end

    scenario "gets redirected to sign in path when navigating to known url" do
      visit project_path(project)

      expect(page.current_path).to eq "/en/session/new"
    end
  end
end
