From 88e1067816762f0df1eaa4dbe02e0219956bb672 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Wed, 20 May 2020 22:41:32 -0700 Subject: [PATCH] wrap up users --- .../controllers/device_controller.ex | 7 ++- .../fg_http_web/templates/device/new.html.eex | 6 ++ .../lib/fg_http_web/views/device_view.ex | 13 +++++ .../controllers/device_controller_test.exs | 55 ++++++++----------- .../controllers/rule_controller_test.exs | 2 +- apps/fg_http/test/support/conn_case.ex | 24 +++++++- apps/fg_http/test/support/fixtures.ex | 28 ++++++++++ 7 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 apps/fg_http/test/support/fixtures.ex diff --git a/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex b/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex index 8d4f146d2..1bc4abb9b 100644 --- a/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex +++ b/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex @@ -19,7 +19,12 @@ defmodule FgHttpWeb.DeviceController do end def create(conn, %{"device" => %{"public_key" => _public_key} = device_params}) do - our_params = %{"user_id" => conn.assigns.current_user.id, "name" => "Default"} + our_params = %{ + "user_id" => conn.assigns.current_user.id, + "name" => "Default", + "ifname" => "wg0" + } + all_params = Map.merge(device_params, our_params) case Devices.create_device(all_params) do diff --git a/apps/fg_http/lib/fg_http_web/templates/device/new.html.eex b/apps/fg_http/lib/fg_http_web/templates/device/new.html.eex index b2ff5a818..a2366b5bb 100644 --- a/apps/fg_http/lib/fg_http_web/templates/device/new.html.eex +++ b/apps/fg_http/lib/fg_http_web/templates/device/new.html.eex @@ -1,5 +1,11 @@

New Device

+<%= if assigns[:changeset] do %> + The following errors occurred when creating this Device: + + <%= aggregated_errors(@changeset) %> +<% end %> + <%= live_render(@conn, FgHttpWeb.NewDeviceLive, session: %{"current_user_id" => @conn.assigns.current_user.id}) %>

diff --git a/apps/fg_http/lib/fg_http_web/views/device_view.ex b/apps/fg_http/lib/fg_http_web/views/device_view.ex index 2963163b4..c21ec491f 100644 --- a/apps/fg_http/lib/fg_http_web/views/device_view.ex +++ b/apps/fg_http/lib/fg_http_web/views/device_view.ex @@ -1,3 +1,16 @@ defmodule FgHttpWeb.DeviceView do use FgHttpWeb, :view + import Ecto.Changeset, only: [traverse_errors: 2] + + def aggregated_errors(changeset) do + traverse_errors(changeset, fn {msg, opts} -> + Enum.reduce(opts, msg, fn {key, value}, acc -> + String.replace(acc, "%{#{key}}", to_string(value)) + end) + end) + |> Enum.reduce("", fn {key, value}, acc -> + joined_errors = Enum.join(value, "; ") + "#{acc}#{key}: #{joined_errors}\n" + end) + end end diff --git a/apps/fg_http/test/fg_http_web/controllers/device_controller_test.exs b/apps/fg_http/test/fg_http_web/controllers/device_controller_test.exs index f29d160b0..173cd25a2 100644 --- a/apps/fg_http/test/fg_http_web/controllers/device_controller_test.exs +++ b/apps/fg_http/test/fg_http_web/controllers/device_controller_test.exs @@ -1,53 +1,42 @@ defmodule FgHttpWeb.DeviceControllerTest do - use FgHttpWeb.ConnCase + use FgHttpWeb.ConnCase, async: true - alias FgHttp.Devices - alias FgHttp.Users + import FgHttp.Fixtures - @create_attrs %{name: "some name", ifname: "wg0", public_key: "foobar"} + @create_attrs %{public_key: "foobar"} @update_attrs %{name: "some updated name"} - @invalid_attrs %{user_id: nil} - - def fixture(:user) do - attrs = %{email: "test", password: "foobar", password_confirmation: "foobar"} - {:ok, user} = Users.create_user(attrs) - user - end - - def fixture(:device) do - {:ok, device} = Devices.create_device(Map.merge(%{user_id: fixture(:user).id}, @create_attrs)) - device - end + @invalid_attrs %{public_key: nil} describe "index" do - test "lists all devices", %{conn: conn} do - # Mock authentication - conn = Plug.Conn.assign(conn, :current_user, fixture(:user)) - + test "lists all devices", %{authed_conn: conn} do conn = get(conn, Routes.device_path(conn, :index)) assert html_response(conn, 200) =~ "Listing Devices" end end describe "new device" do - test "renders form", %{conn: conn} do - # Mock authentication - conn = Plug.Conn.assign(conn, :current_user, fixture(:user)) - + test "renders form", %{authed_conn: conn} do conn = get(conn, Routes.device_path(conn, :new)) assert html_response(conn, 200) =~ "New Device" end end + describe "create device" do + test "redirects when data is valid", %{authed_conn: conn} do + conn = post(conn, Routes.device_path(conn, :create), device: @create_attrs) + assert html_response(conn, 302) =~ "redirected" + end + + test "renders errors when data is invalid", %{authed_conn: conn} do + conn = post(conn, Routes.device_path(conn, :create), device: @invalid_attrs) + assert html_response(conn, 200) =~ "public_key: can't be blank" + end + end + describe "edit device" do setup [:create_device] - test "renders form for editing chosen device", %{conn: conn, device: device} do - conn = - conn - |> Plug.Conn.assign(:current_user, fixture(:user)) - |> Plug.Conn.assign(:current_session, fixture(:user)) - + test "renders form for editing chosen device", %{authed_conn: conn, device: device} do conn = get(conn, Routes.device_path(conn, :edit, device)) assert html_response(conn, 200) =~ "Edit Device" end @@ -56,7 +45,7 @@ defmodule FgHttpWeb.DeviceControllerTest do describe "update device" do setup [:create_device] - test "redirects when data is valid", %{conn: conn, device: device} do + test "redirects when data is valid", %{authed_conn: conn, device: device} do conn = put(conn, Routes.device_path(conn, :update, device), device: @update_attrs) assert redirected_to(conn) == Routes.device_path(conn, :show, device) @@ -64,7 +53,7 @@ defmodule FgHttpWeb.DeviceControllerTest do assert html_response(conn, 200) =~ "some updated name" end - test "renders errors when data is invalid", %{conn: conn, device: device} do + test "renders errors when data is invalid", %{authed_conn: conn, device: device} do conn = put(conn, Routes.device_path(conn, :update, device), device: @invalid_attrs) assert html_response(conn, 200) =~ "Edit Device" end @@ -73,7 +62,7 @@ defmodule FgHttpWeb.DeviceControllerTest do describe "delete device" do setup [:create_device] - test "deletes chosen device", %{conn: conn, device: device} do + test "deletes chosen device", %{authed_conn: conn, device: device} do conn = delete(conn, Routes.device_path(conn, :delete, device)) assert redirected_to(conn) == Routes.device_path(conn, :index) diff --git a/apps/fg_http/test/fg_http_web/controllers/rule_controller_test.exs b/apps/fg_http/test/fg_http_web/controllers/rule_controller_test.exs index ce6ae46d5..1c082df85 100644 --- a/apps/fg_http/test/fg_http_web/controllers/rule_controller_test.exs +++ b/apps/fg_http/test/fg_http_web/controllers/rule_controller_test.exs @@ -1,5 +1,5 @@ defmodule FgHttpWeb.RuleControllerTest do - use FgHttpWeb.ConnCase + use FgHttpWeb.ConnCase, async: true describe "index" do end diff --git a/apps/fg_http/test/support/conn_case.ex b/apps/fg_http/test/support/conn_case.ex index 68dffa4aa..d2a6af700 100644 --- a/apps/fg_http/test/support/conn_case.ex +++ b/apps/fg_http/test/support/conn_case.ex @@ -19,6 +19,8 @@ defmodule FgHttpWeb.ConnCase do alias Ecto.Adapters.SQL.Sandbox + import FgHttp.Fixtures + using do quote do # Import conveniences for testing with connections @@ -31,6 +33,26 @@ defmodule FgHttpWeb.ConnCase do end end + def new_conn do + Phoenix.ConnTest.build_conn() + end + + def authed_conn do + user = fixture(:user) + + session = + fixture(:session, %{ + user_id: user.id, + user_password: "test", + user_email: "test" + }) + + new_conn() + |> Plug.Conn.assign(:current_user, user) + |> Plug.Conn.assign(:current_session, session) + |> Plug.Conn.assign(:user_signed_in?, true) + end + setup tags do :ok = Sandbox.checkout(FgHttp.Repo) @@ -38,6 +60,6 @@ defmodule FgHttpWeb.ConnCase do Sandbox.mode(FgHttp.Repo, {:shared, self()}) end - {:ok, conn: Phoenix.ConnTest.build_conn()} + {:ok, unauthed_conn: new_conn(), authed_conn: authed_conn()} end end diff --git a/apps/fg_http/test/support/fixtures.ex b/apps/fg_http/test/support/fixtures.ex new file mode 100644 index 000000000..31f5122fc --- /dev/null +++ b/apps/fg_http/test/support/fixtures.ex @@ -0,0 +1,28 @@ +defmodule FgHttp.Fixtures do + @moduledoc """ + Convenience helpers for inserting records + """ + alias FgHttp.{Devices, Repo, Sessions, Users, Users.User} + + def fixture(:user) do + case Repo.get_by(User, email: "test") do + nil -> + attrs = %{email: "test", password: "test", password_confirmation: "test"} + {:ok, user} = Users.create_user(attrs) + user + + %User{} = user -> + user + end + end + + def fixture(:device) do + attrs = %{public_key: "foobar", ifname: "wg0", name: "factory"} + {:ok, device} = Devices.create_device(Map.merge(%{user_id: fixture(:user).id}, attrs)) + device + end + + def fixture(:session, attrs \\ %{}) do + {:ok, _session} = Sessions.create_session(attrs) + end +end