From 8fbeabc208ff260da5537527c0031a3b95771197 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Gilberto=20Balsini=20Moura?=
 <joaobalsini@Joaos-MacBook-Pro-2.local>
Date: Mon, 14 Sep 2020 07:35:58 -0300
Subject: [PATCH] Place our index route under authentication

---
 lib/real_estate_web/router.ex                |  9 +++------
 test/real_estate_web/live/page_live_test.exs | 13 ++++++++++++-
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/lib/real_estate_web/router.ex b/lib/real_estate_web/router.ex
index a42338fb..1f0b4f41 100644
--- a/lib/real_estate_web/router.ex
+++ b/lib/real_estate_web/router.ex
@@ -17,12 +17,6 @@ defmodule RealEstateWeb.Router do
     plug :accepts, ["json"]
   end
 
-  scope "/", RealEstateWeb do
-    pipe_through :browser
-
-    live "/", PageLive, :index
-  end
-
   # Other scopes may use custom stacks.
   # scope "/api", RealEstateWeb do
   #   pipe_through :api
@@ -66,6 +60,9 @@ defmodule RealEstateWeb.Router do
     put "/users/settings/update_password", UserSettingsController, :update_password
     put "/users/settings/update_email", UserSettingsController, :update_email
     get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
+
+    # This line was moved
+    live "/", PageLive, :index
   end
 
   scope "/", RealEstateWeb do
diff --git a/test/real_estate_web/live/page_live_test.exs b/test/real_estate_web/live/page_live_test.exs
index 5d205d58..03a5d543 100644
--- a/test/real_estate_web/live/page_live_test.exs
+++ b/test/real_estate_web/live/page_live_test.exs
@@ -2,8 +2,19 @@ defmodule RealEstateWeb.PageLiveTest do
   use RealEstateWeb.ConnCase
 
   import Phoenix.LiveViewTest
+  import RealEstate.AccountsFixtures
+
+  test "disconnected and connected render without authentication should redirect to login page",
+       %{conn: conn} do
+    # If we don't previously log in we will be redirected to the login page
+    assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/")
+  end
+
+  test "disconnected and connected render with authentication should redirect to index page", %{
+    conn: conn
+  } do
+    conn = conn |> log_in_user(user_fixture())
 
-  test "disconnected and connected render", %{conn: conn} do
     {:ok, page_live, disconnected_html} = live(conn, "/")
     assert disconnected_html =~ "Welcome to Phoenix!"
     assert render(page_live) =~ "Welcome to Phoenix!"