Mock authentication for tests

This commit is contained in:
Jamil Bou Kheir
2020-05-13 01:16:13 -05:00
parent 7a31b6f776
commit 7d88274536
3 changed files with 7 additions and 5 deletions

View File

@@ -8,15 +8,14 @@ defmodule FgHttpWeb.NewDeviceLive do
alias FgHttp.Devices.Device
alias FgHttpWeb.Router.Helpers, as: Routes
def mount(_params, %{}, socket) do
user_id = "1"
if connected?(socket), do: wait_for_device(socket)
def mount(_params, %{"current_user_id" => user_id}, socket) do
if connected?(socket), do: wait_for_device_connect(socket)
device = %Device{id: "1", user_id: user_id}
{:ok, assign(socket, :device, device)}
end
defp wait_for_device(_socket) do
defp wait_for_device_connect(_socket) do
# XXX: pass socket to fg_vpn somehow
:timer.send_after(3000, self(), :update)
end

View File

@@ -1,6 +1,6 @@
<h1>New Device</h1>
<%= live_render(@conn, FgHttpWeb.NewDeviceLive) %>
<%= live_render(@conn, FgHttpWeb.NewDeviceLive, session: %{"current_user_id" => @conn.assigns.current_user.id}) %>
<p>
<%= link "Back", to: Routes.device_path(@conn, :index) %>

View File

@@ -27,6 +27,9 @@ defmodule FgHttpWeb.DeviceControllerTest do
describe "new device" do
test "renders form", %{conn: conn} do
# Mock authentication
conn = Plug.Conn.assign(conn, :current_user, fixture(:user))
conn = get(conn, Routes.device_path(conn, :new))
assert html_response(conn, 200) =~ "New Device"
end