2022-08-30 22:32:48 +08:00
|
|
|
defmodule LivebookWeb.Hub.NewLiveTest do
|
|
|
|
use LivebookWeb.ConnCase
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
import Phoenix.LiveViewTest
|
|
|
|
|
|
|
|
alias Livebook.Hubs
|
|
|
|
|
|
|
|
test "render hub selection cards", %{conn: conn} do
|
2023-02-23 02:34:54 +08:00
|
|
|
{:ok, _view, html} = live(conn, ~p"/hub")
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert html =~ "Fly"
|
2023-03-10 04:04:29 +08:00
|
|
|
assert html =~ "Livebook Teams"
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "fly" do
|
2022-10-11 22:27:27 +08:00
|
|
|
test "persists new hub", %{conn: conn} do
|
2022-08-30 22:32:48 +08:00
|
|
|
fly_bypass("123456789")
|
2022-08-18 21:34:27 +08:00
|
|
|
|
2023-02-23 02:34:54 +08:00
|
|
|
{:ok, view, _html} = live(conn, ~p"/hub")
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#fly")
|
|
|
|
|> render_click() =~ "2. Configure your Hub"
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element(~s/input[name="fly[access_token]"]/)
|
|
|
|
|> render_change(%{"fly" => %{"access_token" => "dummy access token"}}) =~
|
|
|
|
~s(<option value="123456789">Foo Bar - 123456789</option>)
|
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
attrs = %{
|
|
|
|
"access_token" => "dummy access token",
|
|
|
|
"application_id" => "123456789",
|
|
|
|
"hub_name" => "My Foo Hub",
|
2023-01-13 04:37:12 +08:00
|
|
|
"hub_emoji" => "🐈"
|
2022-08-23 05:12:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 21:34:27 +08:00
|
|
|
view
|
2022-08-23 05:12:54 +08:00
|
|
|
|> element("#fly-form")
|
|
|
|
|> render_change(%{"fly" => attrs})
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
refute view
|
2022-08-23 05:12:54 +08:00
|
|
|
|> element("#fly-form .invalid-feedback")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
assert {:ok, view, _html} =
|
|
|
|
view
|
|
|
|
|> element("#fly-form")
|
|
|
|
|> render_submit(%{"fly" => attrs})
|
|
|
|
|> follow_redirect(conn)
|
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
assert render(view) =~ "Hub added successfully"
|
2022-08-23 05:12:54 +08:00
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
hubs_html = view |> element("#hubs") |> render()
|
2022-08-18 21:34:27 +08:00
|
|
|
|
2023-01-13 04:37:12 +08:00
|
|
|
assert hubs_html =~ "🐈"
|
|
|
|
assert hubs_html =~ "/hub/fly-123456789"
|
|
|
|
assert hubs_html =~ "My Foo Hub"
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "fails to create existing hub", %{conn: conn} do
|
2022-08-30 22:32:48 +08:00
|
|
|
hub = insert_hub(:fly, id: "fly-foo", application_id: "foo")
|
|
|
|
fly_bypass(hub.application_id)
|
2022-08-18 21:34:27 +08:00
|
|
|
|
2023-02-23 02:34:54 +08:00
|
|
|
{:ok, view, _html} = live(conn, ~p"/hub")
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#fly")
|
|
|
|
|> render_click() =~ "2. Configure your Hub"
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element(~s/input[name="fly[access_token]"]/)
|
|
|
|
|> render_change(%{"fly" => %{"access_token" => "dummy access token"}}) =~
|
|
|
|
~s(<option value="foo">Foo Bar - foo</option>)
|
|
|
|
|
2022-08-23 05:12:54 +08:00
|
|
|
attrs = %{
|
|
|
|
"access_token" => "dummy access token",
|
|
|
|
"application_id" => "foo",
|
|
|
|
"hub_name" => "My Foo Hub",
|
2023-01-13 04:37:12 +08:00
|
|
|
"hub_emoji" => "🐈"
|
2022-08-23 05:12:54 +08:00
|
|
|
}
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
view
|
|
|
|
|> element("#fly-form")
|
2022-08-23 05:12:54 +08:00
|
|
|
|> render_change(%{"fly" => attrs})
|
|
|
|
|
|
|
|
refute view
|
|
|
|
|> element("#fly-form .invalid-feedback")
|
|
|
|
|> has_element?()
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#fly-form")
|
|
|
|
|> render_submit(%{"fly" => attrs}) =~ "already exists"
|
|
|
|
|
2023-02-23 02:34:54 +08:00
|
|
|
assert_hub(view, hub)
|
2022-08-30 22:32:48 +08:00
|
|
|
assert Hubs.fetch_hub!(hub.id) == hub
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
defp fly_bypass(app_id) do
|
2022-08-18 21:34:27 +08:00
|
|
|
bypass = Bypass.open()
|
|
|
|
Application.put_env(:livebook, :fly_graphql_endpoint, "http://localhost:#{bypass.port}")
|
|
|
|
|
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"] =~ "apps" -> fetch_apps_response(app_id)
|
|
|
|
body["query"] =~ "app" -> fetch_app_response(app_id)
|
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
|
|
|
|
|
|
|
|
defp fetch_apps_response(app_id) do
|
2022-08-18 21:34:27 +08:00
|
|
|
app = %{
|
|
|
|
"id" => app_id,
|
|
|
|
"organization" => %{
|
|
|
|
"id" => "l3soyvjmvtmwtl6l2drnbfuvltipprge",
|
|
|
|
"name" => "Foo Bar",
|
|
|
|
"type" => "PERSONAL"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
%{"data" => %{"apps" => %{"nodes" => [app]}}}
|
|
|
|
end
|
2022-08-18 21:34:27 +08:00
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
defp fetch_app_response(app_id) do
|
|
|
|
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" => [
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => Livebook.Utils.random_short_id(),
|
|
|
|
"name" => "LIVEBOOK_PASSWORD"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"createdAt" => to_string(DateTime.utc_now()),
|
|
|
|
"digest" => to_string(Livebook.Utils.random_cookie()),
|
|
|
|
"id" => Livebook.Utils.random_short_id(),
|
|
|
|
"name" => "LIVEBOOK_SECRET_KEY_BASE"
|
|
|
|
}
|
|
|
|
]
|
2022-08-30 22:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
%{"data" => %{"app" => app}}
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
2023-01-13 04:37:12 +08:00
|
|
|
|
2023-02-23 02:34:54 +08:00
|
|
|
defp assert_hub(view, hub) do
|
2023-01-13 04:37:12 +08:00
|
|
|
hubs_html = view |> element("#hubs") |> render()
|
|
|
|
|
|
|
|
assert hubs_html =~ hub.hub_emoji
|
2023-02-23 02:34:54 +08:00
|
|
|
assert hubs_html =~ ~p"/hub/#{hub.id}"
|
2023-01-13 04:37:12 +08:00
|
|
|
assert hubs_html =~ hub.hub_name
|
|
|
|
end
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|