From e64db67672742fa33221ff71096d582f84647f48 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Tue, 14 Dec 2021 15:49:31 -0800 Subject: [PATCH] Checkpoint passing tests --- apps/fz_http/lib/fz_http/application.ex | 3 +- .../live/setting_live/security.html.heex | 13 ++++++ .../live/setting_live/security_live.ex | 14 ++++++ .../lib/fz_http_web/views/layout_view.ex | 4 ++ .../channels/notification_channel_test.exs | 45 +++++++++++++------ .../live/device_live/show_test.exs | 4 +- .../live/setting_live/security_test.exs | 6 +-- .../fz_http_web/views/layout_view_test.exs | 7 +-- 8 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 apps/fz_http/lib/fz_http_web/live/setting_live/security.html.heex create mode 100644 apps/fz_http/lib/fz_http_web/live/setting_live/security_live.ex diff --git a/apps/fz_http/lib/fz_http/application.ex b/apps/fz_http/lib/fz_http/application.ex index 3ba5e5fac..031b0564f 100644 --- a/apps/fz_http/lib/fz_http/application.ex +++ b/apps/fz_http/lib/fz_http/application.ex @@ -39,7 +39,8 @@ defmodule FzHttp.Application do FzHttp.Repo, FzHttp.Vault, FzHttpWeb.Endpoint, - {Phoenix.PubSub, name: FzHttp.PubSub} + {Phoenix.PubSub, name: FzHttp.PubSub}, + FzHttpWeb.Presence ] end end diff --git a/apps/fz_http/lib/fz_http_web/live/setting_live/security.html.heex b/apps/fz_http/lib/fz_http_web/live/setting_live/security.html.heex new file mode 100644 index 000000000..93a52b51e --- /dev/null +++ b/apps/fz_http/lib/fz_http_web/live/setting_live/security.html.heex @@ -0,0 +1,13 @@ +<%= render FzHttpWeb.SharedView, "heading.html", page_title: @page_title %> + +
+ <%= render FzHttpWeb.SharedView, "flash.html", assigns %> + +

Security Settings

+ +
+

+ Manage security-related settings. +

+
+
diff --git a/apps/fz_http/lib/fz_http_web/live/setting_live/security_live.ex b/apps/fz_http/lib/fz_http_web/live/setting_live/security_live.ex new file mode 100644 index 000000000..9d313352b --- /dev/null +++ b/apps/fz_http/lib/fz_http_web/live/setting_live/security_live.ex @@ -0,0 +1,14 @@ +defmodule FzHttpWeb.SettingLive.Security do + @moduledoc """ + Manages security LiveView + """ + use FzHttpWeb, :live_view + + @impl Phoenix.LiveView + def mount(params, session, socket) do + {:ok, + socket + |> assign(:page_title, "Security Settings") + |> assign_defaults(params, session)} + end +end diff --git a/apps/fz_http/lib/fz_http_web/views/layout_view.ex b/apps/fz_http/lib/fz_http_web/views/layout_view.ex index f52ac251e..def35d2f7 100644 --- a/apps/fz_http/lib/fz_http_web/views/layout_view.ex +++ b/apps/fz_http/lib/fz_http_web/views/layout_view.ex @@ -30,6 +30,10 @@ defmodule FzHttpWeb.LayoutView do @doc """ Generate class for nav links """ + def nav_class(%{request_path: "/"} = _conn, ~r"devices") do + "is-active has-icon" + end + def nav_class(%{request_path: request_path} = _conn, regex) do if String.match?(request_path, regex) do "is-active has-icon" diff --git a/apps/fz_http/test/fz_http_web/channels/notification_channel_test.exs b/apps/fz_http/test/fz_http_web/channels/notification_channel_test.exs index 6812d3f36..1461f9e32 100644 --- a/apps/fz_http/test/fz_http_web/channels/notification_channel_test.exs +++ b/apps/fz_http/test/fz_http_web/channels/notification_channel_test.exs @@ -1,29 +1,46 @@ defmodule FzHttpWeb.NotificationChannelTest do - use FzHttpWeb.ChannelCase + use FzHttpWeb.ChannelCase, async: true alias FzHttp.UsersFixtures + alias FzHttpWeb.NotificationChannel describe "channel join" do - setup do + setup _tags do + user = UsersFixtures.user() + + socket = + FzHttpWeb.UserSocket + |> socket(user.id, %{remote_ip: "127.0.0.1"}) + %{ - user: FzHttp.UsersFixtures.user(), - socket: socket(user.id, %{}) + user: user, + socket: socket, + token: Phoenix.Token.sign(socket, "channel auth", user.id) } end - test "joins channel with valid token", %{user: user} do - # token = Phoenix.Token.sign + test "joins channel with valid token", %{token: token, socket: socket, user: user} do + payload = %{ + "token" => token, + "user_agent" => "test" + } + + {:ok, _, test_socket} = + socket + |> subscribe_and_join(NotificationChannel, "notification:session", payload) + + assert test_socket.assigns.current_user.id == user.id end - test "prevents joining with expired token", %{user: user} do - end + test "prevents joining with invalid token", %{token: _token, socket: socket, user: _user} do + payload = %{ + "token" => "foobar", + "user_agent" => "test" + } - test "prevents joining with invalid token", %{user: user} do + assert {:error, %{reason: "unauthorized"}} == + socket + |> subscribe_and_join(NotificationChannel, "notification:session", payload) end end - - test "broadcasts are pushed to the client", %{socket: socket} do - broadcast_from!(socket, "broadcast", %{"some" => "data"}) - assert_push "broadcast", %{"some" => "data"} - end end diff --git a/apps/fz_http/test/fz_http_web/live/device_live/show_test.exs b/apps/fz_http/test/fz_http_web/live/device_live/show_test.exs index ad3e9f98d..f9c287f89 100644 --- a/apps/fz_http/test/fz_http_web/live/device_live/show_test.exs +++ b/apps/fz_http/test/fz_http_web/live/device_live/show_test.exs @@ -41,7 +41,7 @@ defmodule FzHttpWeb.DeviceLive.ShowTest do path = Routes.device_show_path(conn, :show, device) {:ok, _view, html} = live(conn, path) assert html =~ "#{device.name}" - assert html =~ "

Details

" + assert html =~ "

Details

" end test "opens modal", %{authed_conn: conn, device: device} do @@ -220,7 +220,7 @@ defmodule FzHttpWeb.DeviceLive.ShowTest do {:ok, view, _html} = live(conn, path) view - |> element("a", "Delete") + |> element("button", "Delete Device #{device.name}") |> render_click() _flash = assert_redirected(view, Routes.device_index_path(conn, :index)) diff --git a/apps/fz_http/test/fz_http_web/live/setting_live/security_test.exs b/apps/fz_http/test/fz_http_web/live/setting_live/security_test.exs index 63cb5bfc7..65ab4ef9b 100644 --- a/apps/fz_http/test/fz_http_web/live/setting_live/security_test.exs +++ b/apps/fz_http/test/fz_http_web/live/setting_live/security_test.exs @@ -3,16 +3,16 @@ defmodule FzHttpWeb.SettingLive.SecurityTest do describe "authenticated mount" do test "loads the active sessions table", %{authed_conn: conn} do - path = Routes.setting_security_path(conn, :show) + path = Routes.setting_security_path(conn, :security) {:ok, _view, html} = live(conn, path) - assert html =~ "Active Browser Sessions" + assert html =~ "

Security Settings

" end end describe "unauthenticated mount" do test "redirects to not authorized", %{unauthed_conn: conn} do - path = Routes.setting_security_path(conn, :show) + path = Routes.setting_security_path(conn, :security) expected_path = Routes.session_path(conn, :new) assert {:error, {:redirect, %{to: ^expected_path}}} = live(conn, path) diff --git a/apps/fz_http/test/fz_http_web/views/layout_view_test.exs b/apps/fz_http/test/fz_http_web/views/layout_view_test.exs index 22115fdaa..2b9eca150 100644 --- a/apps/fz_http/test/fz_http_web/views/layout_view_test.exs +++ b/apps/fz_http/test/fz_http_web/views/layout_view_test.exs @@ -9,15 +9,16 @@ defmodule FzHttpWeb.LayoutViewTest do # import Phoenix.HTML describe "nav_class/2" do test "it computes nav class for root route" do - assert LayoutView.nav_class("/", "devices") == "is-active has-icon" + assert LayoutView.nav_class(%{request_path: "/"}, ~r"devices") == "is-active has-icon" end test "it computes nav class for account route" do - assert LayoutView.nav_class("/account", "account") == "is-active has-icon" + assert LayoutView.nav_class(%{request_path: "/account"}, ~r"account") == + "is-active has-icon" end test "it defaults to has-icon" do - assert LayoutView.nav_class("Blah", "foo") == " has-icon" + assert LayoutView.nav_class(%{request_path: "Blah"}, ~r"foo") == "has-icon" end end end