Unify styling environment variables forms (#1403)

This commit is contained in:
Alexandre de Souza 2022-09-14 13:24:32 -03:00 committed by GitHub
parent 092d9b5577
commit e539661189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 175 additions and 294 deletions

View file

@ -1,4 +1,4 @@
defmodule LivebookWeb.SettingsLive.EnvVarComponent do defmodule LivebookWeb.EnvVarComponent do
use LivebookWeb, :live_component use LivebookWeb, :live_component
alias Livebook.Settings alias Livebook.Settings
@ -21,21 +21,22 @@ defmodule LivebookWeb.SettingsLive.EnvVarComponent do
@impl true @impl true
def render(assigns) do def render(assigns) do
assigns = assign_new(assigns, :on_save, fn -> "save" end)
~H""" ~H"""
<div class="p-6 flex flex-col space-y-5"> <div class="p-6 flex flex-col space-y-5">
<h3 class="text-2xl font-semibold text-gray-800"> <h3 class="text-2xl font-semibold text-gray-800">
<%= if @operation == :new, do: "Add environment variable", else: "Edit environment variable" %> <%= if @operation == :new, do: "Add environment variable", else: "Edit environment variable" %>
</h3> </h3>
<p class="text-gray-700"> <p class="text-gray-700">
Configure your application global environment variables. <%= @headline %>
</p> </p>
<.form <.form
id={"#{@id}-form"} id={"#{@id}-form"}
let={f} let={f}
for={@changeset} for={@changeset}
phx-target={@myself} phx-submit={@on_save}
phx-submit="save" phx-change={JS.push("validate", target: @myself)}
phx-change="validate"
autocomplete="off" autocomplete="off"
spellcheck="false" spellcheck="false"
> >
@ -51,20 +52,19 @@ defmodule LivebookWeb.SettingsLive.EnvVarComponent do
<%= text_input(f, :value, class: "input", autofocus: @operation == :edit) %> <%= text_input(f, :value, class: "input", autofocus: @operation == :edit) %>
</.input_wrapper> </.input_wrapper>
<%= text_input(f, :operation, type: "hidden", value: @operation) %>
<div class="flex space-x-2"> <div class="flex space-x-2">
<%= submit("Save", <%= submit("Save",
class: "button-base button-blue", class: "button-base button-blue",
disabled: not @changeset.valid?, disabled: not @changeset.valid?,
phx_disabled_with: "Adding..." phx_disabled_with: "Adding..."
) %> ) %>
<button <%= live_patch("Cancel",
type="button" to: @return_to,
phx-click="cancel" type: "button",
phx-target={@myself} class: "button-base button-outlined-gray"
class="button-base button-outlined-gray" ) %>
>
Cancel
</button>
</div> </div>
</div> </div>
</.form> </.form>
@ -76,22 +76,4 @@ defmodule LivebookWeb.SettingsLive.EnvVarComponent do
def handle_event("validate", %{"env_var" => attrs}, socket) do def handle_event("validate", %{"env_var" => attrs}, socket) do
{:noreply, assign(socket, changeset: Settings.change_env_var(socket.assigns.env_var, attrs))} {:noreply, assign(socket, changeset: Settings.change_env_var(socket.assigns.env_var, attrs))}
end end
def handle_event("cancel", _, socket) do
{:noreply, push_patch(socket, to: socket.assigns.return_to)}
end
def handle_event("save", %{"env_var" => attrs}, socket) do
if socket.assigns.changeset.valid? do
case Settings.set_env_var(socket.assigns.env_var, attrs) do
{:ok, _} ->
{:noreply, push_patch(socket, to: socket.assigns.return_to)}
{:error, changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
else
{:noreply, socket}
end
end
end end

View file

@ -1,22 +1,23 @@
defmodule LivebookWeb.SettingsLive.EnvVarsComponent do defmodule LivebookWeb.EnvVarsComponent do
use LivebookWeb, :live_component use LivebookWeb, :live_component
alias Livebook.Settings
@impl true @impl true
def render(assigns) do def render(assigns) do
assigns = assign_new(assigns, :target, fn -> nil end)
~H""" ~H"""
<div id={@id} class="flex flex-col space-y-4"> <div id={@id} class="flex flex-col space-y-4">
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
<%= for env_var <- @env_vars do %> <%= for env_var <- @env_vars do %>
<div class="flex items-center justify-between border border-gray-200 rounded-lg p-4"> <div class="flex items-center justify-between border border-gray-200 rounded-lg p-4">
<.env_var_info socket={@socket} env_var={env_var} myself={@myself} /> <.env_var_info socket={@socket} env_var={env_var} target={@target} />
</div> </div>
<% end %> <% end %>
</div> </div>
<div class="flex"> <div class="flex">
<%= live_patch("Add environment variable", <%= live_patch("Add environment variable",
to: Routes.settings_path(@socket, :add_env_var), to: @add_env_var_path,
id: "add-env-var",
class: "button-base button-blue" class: "button-base button-blue"
) %> ) %>
</div> </div>
@ -45,7 +46,7 @@ defmodule LivebookWeb.SettingsLive.EnvVarsComponent do
id={"env-var-#{@env_var.key}-edit"} id={"env-var-#{@env_var.key}-edit"}
type="button" type="button"
phx-click={JS.push("edit_env_var", value: %{env_var: @env_var.key})} phx-click={JS.push("edit_env_var", value: %{env_var: @env_var.key})}
phx-target={@myself} phx-target={@target}
role="menuitem" role="menuitem"
class="menu-item text-gray-600" class="menu-item text-gray-600"
> >
@ -64,7 +65,7 @@ defmodule LivebookWeb.SettingsLive.EnvVarsComponent do
confirm_icon: "delete-bin-6-line" confirm_icon: "delete-bin-6-line"
) )
} }
phx-target={@myself} phx-target={@target}
role="menuitem" role="menuitem"
class="menu-item text-red-600" class="menu-item text-red-600"
> >
@ -77,14 +78,4 @@ defmodule LivebookWeb.SettingsLive.EnvVarsComponent do
</div> </div>
""" """
end end
@impl true
def handle_event("edit_env_var", %{"env_var" => key}, socket) do
{:noreply, push_patch(socket, to: Routes.settings_path(socket, :edit_env_var, key))}
end
def handle_event("delete_env_var", %{"env_var" => key}, socket) do
Settings.delete_env_var(key)
{:noreply, socket}
end
end end

View file

@ -8,6 +8,12 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
def update(assigns, socket) do def update(assigns, socket) do
changeset = Fly.change_hub(assigns.hub) changeset = Fly.change_hub(assigns.hub)
{:ok, app} = FlyClient.fetch_app(assigns.hub) {:ok, app} = FlyClient.fetch_app(assigns.hub)
env_vars = env_vars_from_secrets(app["secrets"])
env_var =
if key = assigns.env_var_id do
Enum.find(env_vars, &(&1.key == key))
end
{:ok, {:ok,
socket socket
@ -15,17 +21,21 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
|> assign( |> assign(
app_url: "https://fly.io/apps/#{app["name"]}", app_url: "https://fly.io/apps/#{app["name"]}",
changeset: changeset, changeset: changeset,
env_vars: app["secrets"], env_vars: env_vars,
env_var_data: %{}, env_var: env_var
operation: :new,
valid_env_var?: false
)} )}
end end
defp env_vars_from_secrets(secrets) do
for secret <- secrets do
%Livebook.Settings.EnvVar{key: secret["name"]}
end
end
@impl true @impl true
def render(assigns) do def render(assigns) do
~H""" ~H"""
<div id={@id <> "-component"}> <div id={"#{@id}-component"}>
<div class="flex flex-col space-y-10"> <div class="flex flex-col space-y-10">
<div class="flex flex-col space-y-2"> <div class="flex flex-col space-y-2">
<div class="flex items-center justify-between border border-gray-200 rounded-lg p-4"> <div class="flex items-center justify-between border border-gray-200 rounded-lg p-4">
@ -89,151 +99,36 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
Environment Variables Environment Variables
</h2> </h2>
<div class="flex flex-col space-y-4"> <.live_component
<%= for env_var <- @env_vars do %> module={LivebookWeb.EnvVarsComponent}
<.environment_variable_card myself={@myself} env_var={env_var} /> id="env-vars"
<% end %> env_vars={@env_vars}
</div> return_to={Routes.hub_path(@socket, :edit, @hub.id)}
add_env_var_path={Routes.hub_path(@socket, :add_env_var, @hub.id)}
<button target={@myself}
class="button-base button-blue"
type="button"
phx-click={show_modal("environment-variable-modal")}
>
Add environment variable
</button>
</div>
</div>
<.environment_variable_modal
id="environment-variable-modal"
on_save={hide_modal("environment-variable-modal")}
data={@env_var_data}
valid?={@valid_env_var?}
myself={@myself}
/> />
</div> </div>
"""
end
defp environment_variable_card(assigns) do
~H"""
<div
id={"env-var-" <> @env_var["id"]}
class="flex items-center justify-between border border-gray-200 rounded-lg p-4"
>
<div class="grid grid-cols-1 md:grid-cols-3 w-full">
<div class="place-content-start">
<.labeled_text label="Name">
<%= @env_var["name"] %>
</.labeled_text>
</div> </div>
<div class="flex place-content-end"> <%= if @live_action in [:add_env_var, :edit_env_var] do %>
<.labeled_text label="Created at"> <.modal
<%= @env_var["createdAt"] %> id="env-var-modal"
</.labeled_text> show
</div> class="w-full max-w-3xl"
target={@myself}
<div class="flex items-center place-content-end"> patch={Routes.hub_path(@socket, :edit, @hub.id)}
<.menu id={"env-var-#{@env_var["id"]}-menu"}>
<:toggle>
<button class="icon-button" aria-label="open session menu" type="button">
<.remix_icon icon="more-2-fill" class="text-xl" />
</button>
</:toggle>
<:content>
<button
id={"env-var-" <> @env_var["id"] <> "-edit"}
type="button"
phx-click={
show_modal("environment-variable-modal")
|> JS.push("edit", value: %{env_var: @env_var})
}
phx-target={@myself}
role="menuitem"
class="menu-item text-gray-600"
> >
<.remix_icon icon="file-edit-line" /> <.live_component
<span class="font-medium">Edit</span> module={LivebookWeb.EnvVarComponent}
</button> id="env-var"
<button on_save={JS.push("save_env_var", target: @myself)}
id={"env-var-" <> @env_var["id"] <> "-delete"} env_var={@env_var}
type="button" headline="Configute your Fly application system environment variables"
phx-click={ return_to={Routes.hub_path(@socket, :edit, @hub.id)}
with_confirm( />
JS.push("delete", value: %{env_var: @env_var}),
title: "Delete #{@env_var["name"]}",
description: "Are you sure you want to delete environment variable?",
confirm_text: "Delete",
confirm_icon: "delete-bin-6-line"
)
}
phx-target={@myself}
role="menuitem"
class="menu-item text-red-600"
>
<.remix_icon icon="delete-bin-line" />
<span class="font-medium">Delete</span>
</button>
</:content>
</.menu>
</div>
</div>
</div>
"""
end
defp environment_variable_modal(assigns) do
~H"""
<.modal id={@id} class="w-full max-w-lg">
<div class="p-6 max-w-4xl flex flex-col space-y-5">
<h3 class="text-2xl font-semibold text-gray-800">
Add environment variable
</h3>
<div class="flex-col space-y-5">
<p class="text-gray-700">
Enter the environment variable name and its value.
</p>
<.form
id="env-var-form"
let={f}
for={:env_var}
phx-submit={@on_save |> JS.push("save")}
phx-change="validate"
autocomplete="off"
phx-target={@myself}
>
<div class="flex flex-col space-y-4">
<div>
<div class="input-label">
Key <span class="text-xs text-gray-500">(alphanumeric and underscore)</span>
</div>
<%= text_input(f, :key,
value: @data["key"],
class: "input",
autofocus: true,
spellcheck: "false"
) %>
</div>
<div>
<div class="input-label">Value</div>
<%= text_input(f, :value,
value: @data["value"],
class: "input",
spellcheck: "false"
) %>
</div>
</div>
<%= submit("Add environment variable",
class: "mt-5 button-base button-blue",
phx_disable_with: "Adding...",
disabled: not @valid?
) %>
</.form>
</div>
</div>
</.modal> </.modal>
<% end %>
</div>
""" """
end end
@ -242,26 +137,6 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
handle_event("validate", %{"fly" => %{"hub_color" => HexColor.random()}}, socket) handle_event("validate", %{"fly" => %{"hub_color" => HexColor.random()}}, socket)
end end
def handle_event("edit", %{"env_var" => %{"name" => name}}, socket) do
{:noreply, assign(socket, operation: :edit, env_var_data: %{"key" => name})}
end
def handle_event("delete", %{"env_var" => %{"name" => key}}, socket) do
case FlyClient.delete_secrets(socket.assigns.hub, [key]) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:success, "Environment variable deleted")
|> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))}
{:error, _} ->
{:noreply,
socket
|> put_flash(:error, "Failed to delete environment variable")
|> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))}
end
end
def handle_event("save", %{"fly" => params}, socket) do def handle_event("save", %{"fly" => params}, socket) do
case Fly.update_hub(socket.assigns.hub, params) do case Fly.update_hub(socket.assigns.hub, params) do
{:ok, hub} -> {:ok, hub} ->
@ -275,16 +150,21 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
end end
end end
def handle_event("save", %{"env_var" => params}, socket) do def handle_event("validate", %{"fly" => attrs}, socket) do
if socket.assigns.valid_env_var? do {:noreply, assign(socket, changeset: Fly.change_hub(socket.assigns.hub, attrs))}
case FlyClient.put_secrets(socket.assigns.hub, [params]) do end
# EnvVar component callbacks
def handle_event("save_env_var", %{"env_var" => attrs}, socket) do
{env_operation, attrs} = Map.pop!(attrs, "operation")
case FlyClient.put_secrets(socket.assigns.hub, [attrs]) do
{:ok, _} -> {:ok, _} ->
message = message =
if socket.assigns.operation == :new do if env_operation == "new",
"Environment variable added" do: "Environment variable added",
else else: "Environment variable updated"
"Environment variable updated"
end
{:noreply, {:noreply,
socket socket
@ -293,29 +173,35 @@ defmodule LivebookWeb.Hub.Edit.FlyComponent do
{:error, _} -> {:error, _} ->
message = message =
if socket.assigns.operation == :new do if env_operation == "new",
"Failed to add environment variable" do: "Failed to add environment variable",
else else: "Failed to update environment variable"
"Failed to update environment variable"
end
{:noreply, {:noreply,
socket socket
|> put_flash(:error, message) |> put_flash(:error, message)
|> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))} |> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))}
end end
else
{:noreply, socket}
end
end end
def handle_event("validate", %{"fly" => attrs}, socket) do def handle_event("edit_env_var", %{"env_var" => key}, socket) do
changeset = Fly.change_hub(socket.assigns.hub, attrs) {:noreply,
{:noreply, assign(socket, changeset: changeset)} push_patch(socket, to: Routes.hub_path(socket, :edit_env_var, socket.assigns.hub.id, key))}
end end
def handle_event("validate", %{"env_var" => attrs}, socket) do def handle_event("delete_env_var", %{"env_var" => key}, socket) do
valid? = String.match?(attrs["key"], ~r/^\w+$/) and attrs["value"] not in ["", nil] case FlyClient.delete_secrets(socket.assigns.hub, [key]) do
{:noreply, assign(socket, valid_env_var?: valid?, env_var_data: attrs)} {:ok, _} ->
{:noreply,
socket
|> put_flash(:success, "Environment variable deleted")
|> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))}
{:error, _} ->
{:noreply,
socket
|> put_flash(:error, "Failed to delete environment variable")
|> push_redirect(to: Routes.hub_path(socket, :edit, socket.assigns.hub.id))}
end
end end
end end

View file

@ -8,16 +8,23 @@ defmodule LivebookWeb.Hub.EditLive do
on_mount LivebookWeb.SidebarHook on_mount LivebookWeb.SidebarHook
@impl true @impl true
def mount(%{"id" => id}, _session, socket) do def mount(_params, _session, socket) do
hub = Hubs.fetch_hub!(id) {:ok, assign(socket, hub: nil, type: nil, page_title: "Livebook - Hub", env_var_id: nil)}
end
@impl true
def handle_params(params, _url, socket) do
hub = Hubs.fetch_hub!(params["id"])
type = Provider.type(hub) type = Provider.type(hub)
if type == "local" do if type == "local" do
{:ok, {:ok,
socket |> redirect(to: "/") |> put_flash(:warning, "You can't edit the localhost Hub")} socket |> redirect(to: "/") |> put_flash(:warning, "You can't edit the localhost Hub")}
else else
{:ok, assign(socket, hub: hub, type: type, page_title: "Livebook - Hub")} {:ok, assign(socket, hub: hub, type: type, page_title: "Livebook - Hub", params: params)}
end end
{:noreply, assign(socket, hub: hub, type: type, env_var_id: params["env_var_id"])}
end end
@impl true @impl true
@ -35,7 +42,13 @@ defmodule LivebookWeb.Hub.EditLive do
</div> </div>
<%= if @type == "fly" do %> <%= if @type == "fly" do %>
<.live_component module={LivebookWeb.Hub.Edit.FlyComponent} hub={@hub} id="fly-form" /> <.live_component
module={LivebookWeb.Hub.Edit.FlyComponent}
hub={@hub}
id="fly-form"
live_action={@live_action}
env_var_id={@env_var_id}
/>
<% end %> <% end %>
</div> </div>
</LayoutHelpers.layout> </LayoutHelpers.layout>

View file

@ -19,7 +19,6 @@ defmodule LivebookWeb.LiveHelpers do
|> assign_new(:patch, fn -> nil end) |> assign_new(:patch, fn -> nil end)
|> assign_new(:navigate, fn -> nil end) |> assign_new(:navigate, fn -> nil end)
|> assign_new(:class, fn -> "" end) |> assign_new(:class, fn -> "" end)
|> assign_new(:on_close, fn -> %JS{} end)
|> assign(:attrs, assigns_to_attributes(assigns, [:id, :show, :patch, :navigate, :class])) |> assign(:attrs, assigns_to_attributes(assigns, [:id, :show, :patch, :navigate, :class]))
~H""" ~H"""
@ -40,8 +39,8 @@ defmodule LivebookWeb.LiveHelpers do
aria-modal="true" aria-modal="true"
tabindex="0" tabindex="0"
autofocus autofocus
phx-window-keydown={hide_modal(@on_close, @id)} phx-window-keydown={hide_modal(@id)}
phx-click-away={hide_modal(@on_close, @id)} phx-click-away={hide_modal(@id)}
phx-key="escape" phx-key="escape"
> >
<%= if @patch do %> <%= if @patch do %>
@ -54,7 +53,7 @@ defmodule LivebookWeb.LiveHelpers do
type="button" type="button"
class="absolute top-6 right-6 text-gray-400 flex space-x-1 items-center" class="absolute top-6 right-6 text-gray-400 flex space-x-1 items-center"
aria_label="close modal" aria_label="close modal"
phx-click={hide_modal(@on_close, @id)} phx-click={hide_modal(@id)}
> >
<span class="text-sm">(esc)</span> <span class="text-sm">(esc)</span>
<.remix_icon icon="close-line" class="text-2xl" /> <.remix_icon icon="close-line" class="text-2xl" />

View file

@ -34,7 +34,7 @@ defmodule LivebookWeb.SettingsLive do
current_user={@current_user} current_user={@current_user}
saved_hubs={@saved_hubs} saved_hubs={@saved_hubs}
> >
<div class="p-4 sm:px-8 md:px-16 sm:py-7 max-w-screen-md mx-auto space-y-16"> <div id="settings-page" class="p-4 sm:px-8 md:px-16 sm:py-7 max-w-screen-md mx-auto space-y-16">
<!-- System settings section --> <!-- System settings section -->
<div class="flex flex-col space-y-10"> <div class="flex flex-col space-y-10">
<div> <div>
@ -127,10 +127,12 @@ defmodule LivebookWeb.SettingsLive do
application and is accessible only to the current machine. application and is accessible only to the current machine.
</p> </p>
<.live_component <.live_component
module={LivebookWeb.SettingsLive.EnvVarsComponent} module={LivebookWeb.EnvVarsComponent}
id="env-vars" id="env-vars"
env_vars={@env_vars} env_vars={@env_vars}
return_to={Routes.settings_path(@socket, :page)} return_to={Routes.settings_path(@socket, :page)}
add_env_var_path={Routes.settings_path(@socket, :add_env_var)}
target={@socket.view}
/> />
</div> </div>
</div> </div>
@ -205,9 +207,10 @@ defmodule LivebookWeb.SettingsLive do
patch={Routes.settings_path(@socket, :page)} patch={Routes.settings_path(@socket, :page)}
> >
<.live_component <.live_component
module={LivebookWeb.SettingsLive.EnvVarComponent} module={LivebookWeb.EnvVarComponent}
id="env-var" id="env-var"
env_var={@env_var} env_var={@env_var}
headline="Configure your application global environment variables."
return_to={Routes.settings_path(@socket, :page)} return_to={Routes.settings_path(@socket, :page)}
/> />
</.modal> </.modal>
@ -269,7 +272,7 @@ defmodule LivebookWeb.SettingsLive do
{:noreply, assign(socket, file_system_id: file_system_id)} {:noreply, assign(socket, file_system_id: file_system_id)}
end end
def handle_params(_params, _url, socket), do: {:noreply, socket} def handle_params(_params, _url, socket), do: {:noreply, assign(socket, env_var: nil)}
@impl true @impl true
def handle_event("cancel_autosave_path", %{}, socket) do def handle_event("cancel_autosave_path", %{}, socket) do
@ -320,8 +323,25 @@ defmodule LivebookWeb.SettingsLive do
{:noreply, assign(socket, :update_check_enabled, enabled)} {:noreply, assign(socket, :update_check_enabled, enabled)}
end end
def handle_event("clear_env_var", _, socket) do def handle_event("save", %{"env_var" => attrs}, socket) do
{:noreply, assign(socket, env_var: nil)} env_var = %Livebook.Settings.EnvVar{}
case Livebook.Settings.set_env_var(socket.assigns.env_var || env_var, attrs) do
{:ok, _} ->
{:noreply, push_patch(socket, to: Routes.settings_path(socket, :page))}
{:error, _changeset} ->
{:noreply, socket}
end
end
def handle_event("edit_env_var", %{"env_var" => key}, socket) do
{:noreply, push_patch(socket, to: Routes.settings_path(socket, :edit_env_var, key))}
end
def handle_event("delete_env_var", %{"env_var" => key}, socket) do
Livebook.Settings.delete_env_var(key)
{:noreply, socket}
end end
@impl true @impl true
@ -338,7 +358,7 @@ defmodule LivebookWeb.SettingsLive do
end end
def handle_info({:env_vars_changed, env_vars}, socket) do def handle_info({:env_vars_changed, env_vars}, socket) do
{:noreply, assign(socket, env_vars: env_vars)} {:noreply, assign(socket, env_vars: env_vars, env_var: nil)}
end end
def handle_info(_message, socket), do: {:noreply, socket} def handle_info(_message, socket), do: {:noreply, socket}

View file

@ -59,6 +59,8 @@ defmodule LivebookWeb.Router do
live "/hub", Hub.NewLive, :new, as: :hub live "/hub", Hub.NewLive, :new, as: :hub
live "/hub/:id", Hub.EditLive, :edit, as: :hub live "/hub/:id", Hub.EditLive, :edit, as: :hub
live "/hub/:id/env-var/new", Hub.EditLive, :add_env_var, as: :hub
live "/hub/:id/env-var/edit/:env_var_id", Hub.EditLive, :edit_env_var, as: :hub
live "/sessions/:id", SessionLive, :page live "/sessions/:id", SessionLive, :page
live "/sessions/:id/shortcuts", SessionLive, :shortcuts live "/sessions/:id/shortcuts", SessionLive, :shortcuts

View file

@ -17,7 +17,7 @@ defmodule LivebookWeb.Hub.EditLiveTest do
end end
describe "fly" do describe "fly" do
test "updates fly", %{conn: conn, bypass: bypass} do test "updates hub", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end) {:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
app_id = Livebook.Utils.random_short_id() app_id = Livebook.Utils.random_short_id()
@ -70,7 +70,7 @@ defmodule LivebookWeb.Hub.EditLiveTest do
refute Hubs.fetch_hub!(hub.id) == hub refute Hubs.fetch_hub!(hub.id) == hub
end end
test "add secret", %{conn: conn, bypass: bypass} do test "add env var", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end) {:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
app_id = Livebook.Utils.random_short_id() app_id = Livebook.Utils.random_short_id()
@ -87,6 +87,13 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert html =~ "LIVEBOOK_PASSWORD" assert html =~ "LIVEBOOK_PASSWORD"
assert html =~ "LIVEBOOK_SECRET_KEY_BASE" assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
view
|> element("#add-env-var")
|> render_click(%{})
assert_patch(view, Routes.hub_path(conn, :add_env_var, hub.id))
assert render(view) =~ "Add environment variable"
view view
|> element("#env-var-form") |> element("#env-var-form")
|> render_change(%{"env_var" => %{"key" => "FOO_ENV_VAR", "value" => "12345"}}) |> render_change(%{"env_var" => %{"key" => "FOO_ENV_VAR", "value" => "12345"}})
@ -110,19 +117,9 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert html =~ "LIVEBOOK_SECRET_KEY_BASE" assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
end end
test "update secret", %{conn: conn, bypass: bypass} do test "update env var", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :foo} end) {:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :foo} end)
old_env_var =
:foo
|> secrets()
|> Enum.find(&(&1["name"] == "FOO_ENV_VAR"))
new_env_var =
:updated_foo
|> secrets()
|> Enum.find(&(&1["name"] == "FOO_ENV_VAR"))
app_id = Livebook.Utils.random_short_id() app_id = Livebook.Utils.random_short_id()
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id) hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
fly_bypass(bypass, app_id, pid) fly_bypass(bypass, app_id, pid)
@ -134,11 +131,13 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert html =~ "Environment Variables" assert html =~ "Environment Variables"
assert html =~ "FOO_ENV_VAR" assert html =~ "FOO_ENV_VAR"
assert html =~ old_env_var["createdAt"]
view view
|> element("#env-var-#{old_env_var["id"]}-edit") |> element("#env-var-FOO_ENV_VAR-edit")
|> render_click(%{"env_var" => old_env_var}) |> render_click(%{"env_var" => "FOO_ENV_VAR"})
assert_patch(view, Routes.hub_path(conn, :edit_env_var, hub.id, "FOO_ENV_VAR"))
assert render(view) =~ "Edit environment variable"
view view
|> element("#env-var-form") |> element("#env-var-form")
@ -159,18 +158,11 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert html =~ "Environment variable updated" assert html =~ "Environment variable updated"
assert html =~ "Environment Variables" assert html =~ "Environment Variables"
assert html =~ "FOO_ENV_VAR" assert html =~ "FOO_ENV_VAR"
refute html =~ old_env_var["createdAt"]
assert html =~ new_env_var["createdAt"]
end end
test "delete secret", %{conn: conn, bypass: bypass} do test "delete env var", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :add} end) {:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :add} end)
env_var =
:add
|> secrets()
|> Enum.find(&(&1["name"] == "FOO_ENV_VAR"))
app_id = Livebook.Utils.random_short_id() app_id = Livebook.Utils.random_short_id()
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id) hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
fly_bypass(bypass, app_id, pid) fly_bypass(bypass, app_id, pid)
@ -190,7 +182,7 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert {:ok, _view, html} = assert {:ok, _view, html} =
view view
|> with_target("#fly-form-component") |> with_target("#fly-form-component")
|> render_click("delete", %{"env_var" => env_var}) |> render_click("delete_env_var", %{"env_var" => "FOO_ENV_VAR"})
|> follow_redirect(conn) |> follow_redirect(conn)
assert html =~ "Environment variable deleted" assert html =~ "Environment variable deleted"

View file

@ -46,9 +46,7 @@ defmodule LivebookWeb.SettingsLiveTest do
assert html =~ env_var.key assert html =~ env_var.key
view render_click(view, "edit_env_var", %{"env_var" => env_var.key})
|> with_target("#env-vars")
|> render_click("edit_env_var", %{"env_var" => env_var.key})
assert_patch(view, Routes.settings_path(conn, :edit_env_var, env_var.key)) assert_patch(view, Routes.settings_path(conn, :edit_env_var, env_var.key))
assert render(view) =~ "Edit environment variable" assert render(view) =~ "Edit environment variable"
@ -77,9 +75,7 @@ defmodule LivebookWeb.SettingsLiveTest do
assert html =~ env_var.key assert html =~ env_var.key
view render_click(view, "delete_env_var", %{"env_var" => env_var.key})
|> with_target("#env-vars")
|> render_click("delete_env_var", %{"env_var" => env_var.key})
refute render(view) =~ env_var.key refute render(view) =~ env_var.key
end end