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
|
|
|
|
|
|
|
|
setup do
|
|
|
|
on_exit(&Hubs.clean_hubs/0)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render hub selection cards", %{conn: conn} do
|
2022-08-30 22:32:48 +08:00
|
|
|
{:ok, _view, html} = live(conn, Routes.hub_path(conn, :new))
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert html =~ "Fly"
|
|
|
|
assert html =~ "Livebook Enterprise"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "fly" do
|
|
|
|
test "persists fly", %{conn: conn} do
|
2022-08-30 22:32:48 +08:00
|
|
|
fly_bypass("123456789")
|
2022-08-18 21:34:27 +08:00
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
{:ok, view, _html} = live(conn, Routes.hub_path(conn, :new))
|
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",
|
|
|
|
"hub_color" => "#FF00FF"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-08-18 21:34:27 +08:00
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
|
|
|
|> render() =~ ~s/style="color: #FF00FF"/
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
|
|
|
|> render() =~ "/hub/fly-123456789"
|
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
|
|
|
|> render() =~ "My Foo Hub"
|
|
|
|
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
|
|
|
|
2022-08-30 22:32:48 +08:00
|
|
|
{:ok, view, _html} = live(conn, Routes.hub_path(conn, :new))
|
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",
|
|
|
|
"hub_color" => "#FF00FF"
|
|
|
|
}
|
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"
|
|
|
|
|
2022-08-18 21:34:27 +08:00
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
2022-08-30 22:32:48 +08:00
|
|
|
|> render() =~ ~s/style="color: #{hub.hub_color}"/
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
2022-08-30 22:32:48 +08:00
|
|
|
|> render() =~ Routes.hub_path(conn, :edit, hub.id)
|
2022-08-18 21:34:27 +08:00
|
|
|
|
|
|
|
assert view
|
|
|
|
|> element("#hubs")
|
2022-08-30 22:32:48 +08:00
|
|
|
|> render() =~ hub.hub_name
|
2022-08-18 21:34:27 +08:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
response =
|
|
|
|
case Jason.decode!(body) do
|
|
|
|
%{"variables" => %{"appId" => ^app_id}} -> fetch_app_response(app_id)
|
|
|
|
%{"variables" => %{}} -> fetch_apps_response(app_id)
|
|
|
|
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,
|
|
|
|
"status" => "running"
|
|
|
|
}
|
|
|
|
|
|
|
|
%{"data" => %{"app" => app}}
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
|
|
|
end
|