2022-08-30 22:32:48 +08:00
|
|
|
defmodule LivebookWeb.Hub.EditLiveTest do
|
|
|
|
use LivebookWeb.ConnCase
|
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
|
|
|
|
|
|
|
alias Livebook.Hubs
|
|
|
|
|
|
|
|
describe "fly" do
|
2022-10-11 22:27:27 +08:00
|
|
|
setup do
|
|
|
|
bypass = Bypass.open()
|
|
|
|
Application.put_env(:livebook, :fly_graphql_endpoint, "http://localhost:#{bypass.port}")
|
|
|
|
|
|
|
|
{:ok, bypass: bypass}
|
|
|
|
end
|
|
|
|
|
2022-09-15 00:24:32 +08:00
|
|
|
test "updates hub", %{conn: conn, bypass: bypass} do
|
2022-09-02 23:54:28 +08:00
|
|
|
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
|
|
|
|
|
|
|
|
app_id = Livebook.Utils.random_short_id()
|
|
|
|
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
|
|
|
|
fly_bypass(bypass, app_id, pid)
|
2022-08-30 22:32:48 +08:00
|
|
|
|
|
|
|
{:ok, view, html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
|
|
|
|
2022-09-07 17:37:45 +08:00
|
|
|
assert html =~ "Manage app on Fly"
|
|
|
|
assert html =~ "https://fly.io/apps/#{hub.application_id}"
|
2022-08-30 22:32:48 +08:00
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
refute html =~ "FOO_ENV_VAR"
|
|
|
|
assert html =~ "LIVEBOOK_PASSWORD"
|
|
|
|
assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
attrs = %{
|
|
|
|
"hub_name" => "Personal Hub",
|
2023-01-13 04:37:12 +08:00
|
|
|
"hub_emoji" => "🐈"
|
2022-08-30 22:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
view
|
|
|
|
|> element("#fly-form")
|
|
|
|
|> render_change(%{"fly" => attrs})
|
|
|
|
|
|
|
|
refute view
|
|
|
|
|> element("#fly-form .invalid-feedback")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
assert {:ok, view, _html} =
|
|
|
|
view
|
|
|
|
|> element("#fly-form")
|
|
|
|
|> render_submit(%{"fly" => attrs})
|
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
assert render(view) =~ "Hub updated successfully"
|
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
assert_hub(view, conn, %{hub | hub_emoji: attrs["hub_emoji"], hub_name: attrs["hub_name"]})
|
2022-08-30 22:32:48 +08:00
|
|
|
refute Hubs.fetch_hub!(hub.id) == hub
|
|
|
|
end
|
|
|
|
|
2023-02-13 12:52:25 +08:00
|
|
|
test "deletes hub", %{conn: conn, bypass: bypass} do
|
|
|
|
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
|
|
|
|
app_id = Livebook.Utils.random_short_id()
|
|
|
|
hub_id = "fly-#{app_id}"
|
|
|
|
|
2023-02-14 04:18:06 +08:00
|
|
|
hub = insert_hub(:fly, id: hub_id, hub_name: "My Deletable Hub", application_id: app_id)
|
2023-02-13 12:52:25 +08:00
|
|
|
fly_bypass(bypass, app_id, pid)
|
|
|
|
|
2023-02-14 04:18:06 +08:00
|
|
|
{:ok, view, _html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
2023-02-13 12:52:25 +08:00
|
|
|
|
|
|
|
assert {:ok, view, _html} =
|
|
|
|
view
|
|
|
|
|> render_click("delete_hub", %{"id" => hub_id})
|
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
hubs_html = view |> element("#hubs") |> render()
|
|
|
|
|
|
|
|
refute hubs_html =~ Routes.hub_path(conn, :edit, hub.id)
|
|
|
|
refute hubs_html =~ hub.hub_name
|
|
|
|
|
|
|
|
assert Hubs.get_hub(hub_id) == :error
|
|
|
|
end
|
|
|
|
|
2022-09-15 00:24:32 +08:00
|
|
|
test "add env var", %{conn: conn, bypass: bypass} do
|
2022-09-02 23:54:28 +08:00
|
|
|
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
|
|
|
|
|
|
|
|
app_id = Livebook.Utils.random_short_id()
|
|
|
|
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
|
|
|
|
fly_bypass(bypass, app_id, pid)
|
|
|
|
|
|
|
|
{:ok, view, html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
|
|
|
|
2022-09-07 17:37:45 +08:00
|
|
|
assert html =~ "Manage app on Fly"
|
|
|
|
assert html =~ "https://fly.io/apps/#{hub.application_id}"
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
refute html =~ "FOO_ENV_VAR"
|
|
|
|
assert html =~ "LIVEBOOK_PASSWORD"
|
|
|
|
assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
|
2022-09-15 00:24:32 +08:00
|
|
|
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"
|
|
|
|
|
2022-09-17 07:37:01 +08:00
|
|
|
attrs = params_for(:env_var, name: "FOO_ENV_VAR")
|
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
view
|
|
|
|
|> element("#env-var-form")
|
2022-09-17 07:37:01 +08:00
|
|
|
|> render_change(%{"env_var" => attrs})
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
refute view
|
|
|
|
|> element("#env-var-form button[disabled]")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
:ok = Agent.update(pid, fn state -> %{state | type: :add} end)
|
|
|
|
|
|
|
|
assert {:ok, _view, html} =
|
|
|
|
view
|
|
|
|
|> element("#env-var-form")
|
2022-09-17 07:37:01 +08:00
|
|
|
|> render_submit(%{"env_var" => attrs})
|
2022-09-02 23:54:28 +08:00
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
assert html =~ "Environment variable added"
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
assert html =~ "FOO_ENV_VAR"
|
|
|
|
assert html =~ "LIVEBOOK_PASSWORD"
|
|
|
|
assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
end
|
|
|
|
|
2022-09-15 00:24:32 +08:00
|
|
|
test "update env var", %{conn: conn, bypass: bypass} do
|
2022-09-02 23:54:28 +08:00
|
|
|
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :foo} end)
|
|
|
|
|
|
|
|
app_id = Livebook.Utils.random_short_id()
|
|
|
|
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
|
|
|
|
fly_bypass(bypass, app_id, pid)
|
|
|
|
|
|
|
|
{:ok, view, html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
|
|
|
|
2022-09-07 17:37:45 +08:00
|
|
|
assert html =~ "Manage app on Fly"
|
|
|
|
assert html =~ "https://fly.io/apps/#{hub.application_id}"
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
assert html =~ "FOO_ENV_VAR"
|
|
|
|
|
|
|
|
view
|
2022-09-15 00:24:32 +08:00
|
|
|
|> element("#env-var-FOO_ENV_VAR-edit")
|
|
|
|
|> 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"
|
2022-09-02 23:54:28 +08:00
|
|
|
|
2022-09-17 07:37:01 +08:00
|
|
|
attrs = params_for(:env_var, name: "FOO_ENV_VAR")
|
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
view
|
|
|
|
|> element("#env-var-form")
|
2022-09-17 07:37:01 +08:00
|
|
|
|> render_change(%{"env_var" => attrs})
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
refute view
|
|
|
|
|> element("#env-var-form button[disabled]")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
:ok = Agent.update(pid, fn state -> %{state | type: :updated_foo} end)
|
2022-08-30 22:32:48 +08:00
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
assert {:ok, _view, html} =
|
|
|
|
view
|
|
|
|
|> element("#env-var-form")
|
2022-09-17 07:37:01 +08:00
|
|
|
|> render_submit(%{"env_var" => attrs})
|
2022-09-02 23:54:28 +08:00
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
assert html =~ "Environment variable updated"
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
assert html =~ "FOO_ENV_VAR"
|
|
|
|
end
|
|
|
|
|
2022-09-15 00:24:32 +08:00
|
|
|
test "delete env var", %{conn: conn, bypass: bypass} do
|
2022-09-02 23:54:28 +08:00
|
|
|
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :add} end)
|
|
|
|
|
|
|
|
app_id = Livebook.Utils.random_short_id()
|
|
|
|
hub = insert_hub(:fly, id: "fly-#{app_id}", application_id: app_id)
|
|
|
|
fly_bypass(bypass, app_id, pid)
|
|
|
|
|
|
|
|
{:ok, view, html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
|
|
|
|
2022-09-07 17:37:45 +08:00
|
|
|
assert html =~ "Manage app on Fly"
|
|
|
|
assert html =~ "https://fly.io/apps/#{hub.application_id}"
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
assert html =~ "FOO_ENV_VAR"
|
|
|
|
assert html =~ "LIVEBOOK_PASSWORD"
|
|
|
|
assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
|
|
|
|
:ok = Agent.update(pid, fn state -> %{state | type: :mount} end)
|
|
|
|
|
|
|
|
assert {:ok, _view, html} =
|
|
|
|
view
|
|
|
|
|> with_target("#fly-form-component")
|
2022-09-15 00:24:32 +08:00
|
|
|
|> render_click("delete_env_var", %{"env_var" => "FOO_ENV_VAR"})
|
2022-09-02 23:54:28 +08:00
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
assert html =~ "Environment variable deleted"
|
|
|
|
assert html =~ "Environment Variables"
|
|
|
|
refute html =~ "FOO_ENV_VAR"
|
|
|
|
assert html =~ "LIVEBOOK_PASSWORD"
|
|
|
|
assert html =~ "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-11 22:27:27 +08:00
|
|
|
describe "enterprise" do
|
|
|
|
test "updates hub", %{conn: conn} do
|
|
|
|
hub = insert_hub(:enterprise)
|
|
|
|
{:ok, view, _html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
|
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
attrs = %{"hub_emoji" => "🐈"}
|
2022-10-11 22:27:27 +08:00
|
|
|
|
|
|
|
view
|
|
|
|
|> element("#enterprise-form")
|
|
|
|
|> render_change(%{"enterprise" => attrs})
|
|
|
|
|
|
|
|
refute view
|
|
|
|
|> element("#enterprise-form .invalid-feedback")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
assert {:ok, view, _html} =
|
|
|
|
view
|
|
|
|
|> element("#enterprise-form")
|
|
|
|
|> render_submit(%{"enterprise" => attrs})
|
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
|
|
|
assert render(view) =~ "Hub updated successfully"
|
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
assert_hub(view, conn, %{hub | hub_emoji: attrs["hub_emoji"]})
|
2022-10-11 22:27:27 +08:00
|
|
|
refute Hubs.fetch_hub!(hub.id) == hub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp assert_hub(view, conn, hub) do
|
|
|
|
hubs_html = view |> element("#hubs") |> render()
|
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
assert hubs_html =~ hub.hub_emoji
|
2022-10-11 22:27:27 +08:00
|
|
|
assert hubs_html =~ Routes.hub_path(conn, :edit, hub.id)
|
|
|
|
assert hubs_html =~ hub.hub_name
|
|
|
|
end
|
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
defp fly_bypass(bypass, app_id, agent_pid) do
|
2022-08-30 22:32:48 +08:00
|
|
|
Bypass.expect(bypass, "POST", "/", fn conn ->
|
|
|
|
{:ok, body, conn} = Plug.Conn.read_body(conn)
|
2022-09-02 23:54:28 +08:00
|
|
|
body = Jason.decode!(body)
|
2022-08-30 22:32:48 +08:00
|
|
|
|
|
|
|
response =
|
2022-09-02 23:54:28 +08:00
|
|
|
cond do
|
|
|
|
body["query"] =~ "setSecrets" ->
|
2022-10-07 03:44:04 +08:00
|
|
|
set_secrets_response()
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
body["query"] =~ "unsetSecrets" ->
|
2022-10-07 03:44:04 +08:00
|
|
|
unset_secrets_response()
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
true ->
|
|
|
|
Agent.get(agent_pid, fn
|
|
|
|
%{fun: fun, type: type} -> fun.(app_id, type)
|
|
|
|
%{fun: fun} -> fun.()
|
|
|
|
end)
|
2022-08-30 22:32:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.put_resp_content_type("application/json")
|
|
|
|
|> Plug.Conn.resp(200, Jason.encode!(response))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-09-02 23:54:28 +08:00
|
|
|
defp fetch_app_response(app_id, type) do
|
2022-08-30 22:32:48 +08:00
|
|
|
app = %{
|
|
|
|
"id" => app_id,
|
|
|
|
"name" => app_id,
|
|
|
|
"hostname" => app_id <> ".fly.dev",
|
|
|
|
"platformVersion" => "nomad",
|
|
|
|
"deployed" => true,
|
2022-09-02 23:54:28 +08:00
|
|
|
"status" => "running",
|
|
|
|
"secrets" => secrets(type)
|
2022-08-30 22:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
%{"data" => %{"app" => app}}
|
|
|
|
end
|
2022-09-02 23:54:28 +08:00
|
|
|
|
|
|
|
defp secrets(:mount) do
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "123",
|
|
|
|
"name" => "LIVEBOOK_PASSWORD"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "456",
|
|
|
|
"name" => "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp secrets(:add) do
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "789",
|
|
|
|
"name" => "FOO_ENV_VAR"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "123",
|
|
|
|
"name" => "LIVEBOOK_PASSWORD"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "456",
|
|
|
|
"name" => "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp secrets(:foo) do
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"createdAt" => "2022-08-31 14:47:39.904338Z",
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "123456789",
|
|
|
|
"name" => "FOO_ENV_VAR"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp secrets(:updated_foo) do
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"createdAt" => "2022-08-31 14:47:41.632669Z",
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => "123456789",
|
|
|
|
"name" => "FOO_ENV_VAR"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2022-10-07 03:44:04 +08:00
|
|
|
defp set_secrets_response do
|
2022-09-02 23:54:28 +08:00
|
|
|
%{"data" => %{"setSecrets" => %{"app" => %{"secrets" => secrets(:add)}}}}
|
|
|
|
end
|
|
|
|
|
2022-10-07 03:44:04 +08:00
|
|
|
defp unset_secrets_response do
|
2022-09-02 23:54:28 +08:00
|
|
|
%{"data" => %{"unsetSecrets" => %{"app" => %{"secrets" => secrets(:mount)}}}}
|
|
|
|
end
|
2022-08-30 22:32:48 +08:00
|
|
|
end
|