defmodule MicrowavepropWeb.RoverLocationsLive.ShowTest do use MicrowavepropWeb.ConnCase, async: true import Phoenix.LiveViewTest alias Microwaveprop.Rover describe "show" do test "renders the location and a map container", %{conn: conn} do user = Microwaveprop.AccountsFixtures.user_fixture() {:ok, loc} = Rover.create_location(user, %{ lat: 32.5, lon: -97.5, status: :ideal, notes: "great spot" }) {:ok, lv, html} = live(conn, ~p"/rover-locations/#{loc.id}") assert html =~ "Rover Location" assert html =~ "great spot" assert html =~ "EM12gm" assert has_element?(lv, "#location-map-#{loc.id}[phx-hook=LocationMap]") end test "redirects when the location does not exist", %{conn: conn} do missing_id = Ecto.UUID.generate() assert {:error, {:live_redirect, %{to: "/rover-locations"}}} = live(conn, ~p"/rover-locations/#{missing_id}") end test "redirects when the id is not a valid UUID", %{conn: conn} do assert {:error, {:live_redirect, %{to: "/rover-locations"}}} = live(conn, ~p"/rover-locations/not-a-uuid") end test "owner sees a Delete button; visitors do not", %{conn: conn} do user = Microwaveprop.AccountsFixtures.user_fixture() {:ok, loc} = Rover.create_location(user, %{lat: 32.5, lon: -97.5, status: :ideal}) # Anonymous: no Delete {:ok, _lv, html} = live(conn, ~p"/rover-locations/#{loc.id}") refute html =~ "Delete" # Owner: Delete present conn = log_in_user(conn, user) {:ok, _lv, html} = live(conn, ~p"/rover-locations/#{loc.id}") assert html =~ "Delete" end test "admin sees Delete on someone else's location", %{conn: conn} do owner = Microwaveprop.AccountsFixtures.user_fixture() admin = Microwaveprop.AccountsFixtures.user_fixture() {:ok, admin} = Microwaveprop.Accounts.admin_update_user(admin, %{is_admin: true}) {:ok, loc} = Rover.create_location(owner, %{lat: 32.5, lon: -97.5, status: :ideal}) conn = log_in_user(conn, admin) {:ok, _lv, html} = live(conn, ~p"/rover-locations/#{loc.id}") assert html =~ "Delete" end test "owner can delete from the show page and is redirected", %{conn: conn} do user = Microwaveprop.AccountsFixtures.user_fixture() {:ok, loc} = Rover.create_location(user, %{lat: 32.5, lon: -97.5, status: :ideal}) conn = log_in_user(conn, user) {:ok, lv, _html} = live(conn, ~p"/rover-locations/#{loc.id}") assert {:ok, _index_lv, _html} = lv |> element("button", "Delete") |> render_click() |> follow_redirect(conn, ~p"/rover-locations") end end end