Improve tests

This commit is contained in:
Jonatan Kłosko 2024-11-26 16:54:28 +08:00
parent d9183ef359
commit 20912b87eb
14 changed files with 77 additions and 87 deletions

View file

@ -4,7 +4,7 @@ defmodule Livebook.Hubs.PersonalTest do
alias Livebook.Hubs.Personal
test "get_secrets/1 returns a list of secrets from storage" do
secret = build(:secret, name: "FOO", value: "111")
secret = build(:secret)
Personal.set_secret(secret)
assert secret in Personal.get_secrets()
@ -14,7 +14,7 @@ defmodule Livebook.Hubs.PersonalTest do
end
test "fetch an specific secret" do
secret = insert_secret(name: "FOO", value: "111")
secret = insert_secret()
assert_raise Livebook.Storage.NotFoundError,
~s(could not find entry in "hub_secrets" with ID "NOT_HERE"),

View file

@ -25,19 +25,19 @@ defmodule Livebook.Hubs.ProviderTest do
end
test "get_secrets/1 without startup secrets", %{hub: hub} do
secret = insert_secret(name: "GET_PERSONAL_SECRET")
secret = insert_secret()
assert secret in Provider.get_secrets(hub)
end
test "create_secret/1", %{hub: hub} do
secret = build(:secret, name: "CREATE_PERSONAL_SECRET")
secret = build(:secret)
assert Provider.create_secret(hub, secret) == :ok
assert secret in Provider.get_secrets(hub)
end
test "update_secret/1", %{hub: hub} do
secret = insert_secret(name: "UPDATE_PERSONAL_SECRET")
secret = insert_secret()
assert secret in Provider.get_secrets(hub)
updated_secret = %{secret | value: "123321"}
@ -48,7 +48,7 @@ defmodule Livebook.Hubs.ProviderTest do
end
test "delete_secret/1", %{hub: hub} do
secret = insert_secret(name: "DELETE_PERSONAL_SECRET")
secret = insert_secret()
assert secret in Provider.get_secrets(hub)
assert Provider.delete_secret(hub, secret) == :ok

View file

@ -6,7 +6,7 @@ defmodule Livebook.SecretsTest do
describe "update_secret/2" do
test "returns a valid secret" do
attrs = params_for(:secret, name: "FOO", value: "111")
attrs = params_for(:secret)
assert {:ok, secret} = Secrets.update_secret(%Secret{}, attrs)
assert attrs.name == secret.name

View file

@ -4106,21 +4106,21 @@ defmodule Livebook.Session.DataTest do
test "adds secret and updates hub secret names" do
data = Data.new()
secret = Livebook.Factory.insert_secret(name: "SET_SECRET_SECRET", value: "value")
%{name: secret_name} = secret = Livebook.Factory.insert_secret()
operation = {:set_secret, @cid, secret}
assert {:ok,
%{
secrets: %{"SET_SECRET_SECRET" => ^secret},
notebook: %{hub_secret_names: ["SET_SECRET_SECRET"]}
secrets: %{^secret_name => ^secret},
notebook: %{hub_secret_names: [^secret_name]}
}, []} = Data.apply_operation(data, operation)
end
end
describe "apply_operation/2 given :unset_secret" do
test "removes secret and updates hub secret names" do
secret = Livebook.Factory.insert_secret(name: "SET_SECRET_SECRET", value: "value")
secret = Livebook.Factory.insert_secret()
data =
data_after_operations!([

View file

@ -11,30 +11,34 @@ defmodule Livebook.SettingsTest do
refute env_var in Settings.fetch_env_vars()
end
test "fetch_env_var!/1 returns one persisted fly" do
test "fetch_env_var!/1 returns one persisted environment variable" do
%{name: name} = build(:env_var)
assert_raise Livebook.Storage.NotFoundError,
~s/could not find entry in \"env_vars\" with ID "123456"/,
~s/could not find entry in \"env_vars\" with ID "#{name}"/,
fn ->
Settings.fetch_env_var!("123456")
Settings.fetch_env_var!(name)
end
env_var = insert_env_var(:env_var, name: "123456")
assert Settings.fetch_env_var!("123456") == env_var
env_var = insert_env_var(:env_var, name: name)
assert Settings.fetch_env_var!(name) == env_var
Settings.unset_env_var("123456")
Settings.unset_env_var(name)
end
test "env_var_exists?/1" do
refute Settings.env_var_exists?("FOO")
insert_env_var(:env_var, name: "FOO")
assert Settings.env_var_exists?("FOO")
%{name: name} = build(:env_var)
Settings.unset_env_var("FOO")
refute Settings.env_var_exists?(name)
insert_env_var(:env_var, name: name)
assert Settings.env_var_exists?(name)
Settings.unset_env_var(name)
end
describe "set_env_var/1" do
test "creates an environment variable" do
attrs = params_for(:env_var, name: "FOO_BAR_BAZ")
attrs = params_for(:env_var)
assert {:ok, env_var} = Settings.set_env_var(attrs)
assert attrs.name == env_var.name

View file

@ -47,13 +47,13 @@ defmodule Livebook.ZTA.TailscaleTest do
assert %{id: "1234567890", email: "john@example.org", name: "John"} = user
end
defmodule TestPlug do
def init(options), do: options
def call(conn, _opts), do: Livebook.ZTA.TailscaleTest.valid_user_response(conn)
end
@tag :tmp_dir
test "returns valid user via unix socket", %{options: options, conn: conn, tmp_dir: tmp_dir} do
defmodule TestPlug do
def init(options), do: options
def call(conn, _opts), do: Livebook.ZTA.TailscaleTest.valid_user_response(conn)
end
socket = Path.relative_to_cwd("#{tmp_dir}/bandit.sock")
options = Keyword.put(options, :identity_key, socket)
start_supervised!({Bandit, plug: TestPlug, ip: {:local, socket}, port: 0})

View file

@ -10,7 +10,7 @@ defmodule Livebook.Integration.AppsTest do
hub = create_team_hub(user, node)
hub_id = hub.id
secret = insert_secret(name: "APP_DB_PASSWORD", value: "postgres", hub_id: hub.id)
secret = insert_secret(hub_id: hub.id)
secret_name = secret.name
slug = Livebook.Utils.random_short_id()

View file

@ -15,7 +15,7 @@ defmodule LivebookWeb.Hub.NewLiveTest do
describe "new-org" do
test "persist a new hub", %{conn: conn, node: node, user: user} do
name = "new-org-test"
%{name: name} = build(:org)
{:ok, view, _html} = live(conn, ~p"/hub")
@ -69,7 +69,7 @@ defmodule LivebookWeb.Hub.NewLiveTest do
describe "join-org" do
test "persist a new hub", %{conn: conn, node: node, user: user} do
name = "join-org-test"
%{name: name} = build(:org)
teams_key = Livebook.Teams.Org.teams_key()
key_hash = Org.key_hash(build(:org, teams_key: teams_key))

View file

@ -90,12 +90,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
# render the secret modal
assert_patch(view, ~p"/sessions/#{session.id}/secrets")
secret =
build(:secret,
name: "BIG_IMPORTANT_SECRET",
value: "123",
hub_id: team.id
)
secret = build(:secret, hub_id: team.id)
attrs = %{
secret: %{
@ -157,12 +152,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
Session.set_notebook_hub(session.pid, team.id)
# creates a new secret
secret =
build(:secret,
name: "POSTGRES_PASSWORD",
value: "123456789",
hub_id: team.id
)
secret = build(:secret, hub_id: team.id)
assert Livebook.Hubs.create_secret(team, secret) == :ok
@ -179,12 +169,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
%{conn: conn, user: user, node: node, session: session} do
team = create_team_hub(user, node)
secret =
build(:secret,
name: "MYSQL_PASS",
value: "admin",
hub_id: team.id
)
secret = build(:secret, hub_id: team.id)
# selects the notebook's hub with team hub id
Session.set_notebook_hub(session.pid, team.id)
@ -233,12 +218,7 @@ defmodule LivebookWeb.Integration.SessionLiveTest do
%{conn: conn, user: user, node: node, session: session} do
team = create_team_hub(user, node)
secret =
build(:secret,
name: "PGPASS",
value: "admin",
hub_id: team.id
)
secret = build(:secret, hub_id: team.id)
# selects the notebook's hub with team hub id
Session.set_notebook_hub(session.pid, team.id)

View file

@ -115,10 +115,12 @@ defmodule LivebookWeb.AppSessionLiveTest do
Livebook.App.close(app.pid)
end
test "shows an error message when session errors", %{conn: conn, test: test} do
test "shows an error message when session errors", %{conn: conn} do
slug = Livebook.Utils.random_short_id()
app_settings = %{Livebook.Notebook.AppSettings.new() | slug: slug}
id = Livebook.Utils.random_short_id() |> String.to_atom()
notebook = %{
Livebook.Notebook.new()
| app_settings: app_settings,
@ -140,8 +142,8 @@ defmodule LivebookWeb.AppSessionLiveTest do
| id: "error-cell",
source: """
# Fail on the first run
unless :persistent_term.get(#{inspect(test)}, false) do
:persistent_term.put(#{inspect(test)}, true)
unless :persistent_term.get(#{inspect(id)}, false) do
:persistent_term.put(#{inspect(id)}, true)
raise "oops"
end
"""
@ -193,6 +195,8 @@ defmodule LivebookWeb.AppSessionLiveTest do
attrs: %{type: :number, default: 1, label: "Name", debounce: :blur}
}
id = Livebook.Utils.random_short_id() |> String.to_atom()
notebook = %{
Livebook.Notebook.new()
| app_settings: app_settings,
@ -213,8 +217,8 @@ defmodule LivebookWeb.AppSessionLiveTest do
| id: "error-cell",
source: """
# Fail on the first run
unless :persistent_term.get(#{inspect(test)}, false) do
:persistent_term.put(#{inspect(test)}, true)
unless :persistent_term.get(#{inspect(id)}, false) do
:persistent_term.put(#{inspect(id)}, true)
raise "oops"
end
"""

View file

@ -39,6 +39,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert_sidebar_hub(view, id, hub.hub_name, attrs["hub_emoji"])
refute Hubs.fetch_hub!(hub.id) == hub
Hubs.save_hub(hub)
end
test "raises an error if does not exist secret", %{conn: conn, hub: hub} do
@ -49,7 +51,7 @@ defmodule LivebookWeb.Hub.EditLiveTest do
test "creates secret", %{conn: conn, hub: hub} do
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
secret = build(:secret, name: "PERSONAL_ADD_SECRET")
secret = build(:secret)
attrs = %{
secret: %{
@ -82,13 +84,13 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert_receive {:secret_created, ^secret}
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_ADD_SECRET added successfully"
assert render(view) =~ "Secret #{secret.name} added successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert secret in Livebook.Hubs.get_secrets(hub)
end
test "updates secret", %{conn: conn, hub: hub} do
secret = insert_secret(name: "PERSONAL_EDIT_SECRET", value: "GetTheBonk")
secret = insert_secret()
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
@ -125,13 +127,13 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert_receive {:secret_updated, ^updated_secret}
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_EDIT_SECRET updated successfully"
assert render(view) =~ "Secret #{secret.name} updated successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert updated_secret in Livebook.Hubs.get_secrets(hub)
end
test "deletes secret", %{conn: conn, hub: hub} do
secret = insert_secret(name: "PERSONAL_DELETE_SECRET", value: "GetTheBonk")
secret = insert_secret()
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
@ -147,7 +149,7 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert_receive {:secret_deleted, ^secret}
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_DELETE_SECRET deleted successfully"
assert render(view) =~ "Secret #{secret.name} deleted successfully"
refute secret in Livebook.Hubs.get_secrets(hub)
end

View file

@ -2019,7 +2019,7 @@ defmodule LivebookWeb.SessionLiveTest do
test "adds a secret from form", %{conn: conn, session: session} do
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/secrets")
secret = build(:secret, name: "FOO", value: "123", hub_id: nil)
secret = build(:secret, hub_id: nil)
view
|> element(~s{form[phx-submit="save"]})
@ -2032,7 +2032,7 @@ defmodule LivebookWeb.SessionLiveTest do
test "adds a livebook secret from form", %{conn: conn, session: session, hub: hub} do
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/secrets")
secret = build(:secret, name: "BAR", value: "456")
secret = build(:secret)
view
|> element(~s{form[phx-submit="save"]})
@ -2044,8 +2044,8 @@ defmodule LivebookWeb.SessionLiveTest do
end
test "syncs secrets", %{conn: conn, session: session, hub: hub} do
session_secret = insert_secret(name: "FOO", value: "123")
secret = build(:secret, name: "FOO", value: "456")
session_secret = insert_secret(value: "123")
secret = build(:secret, name: session_secret.name, value: "456")
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/secrets")
@ -2061,7 +2061,7 @@ defmodule LivebookWeb.SessionLiveTest do
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/secrets")
Session.set_secret(session.pid, session_secret)
secret = build(:secret, name: "FOO", value: "789")
secret = build(:secret, name: session_secret.name, value: "789")
view
|> element(~s{form[phx-submit="save"]})
@ -2075,8 +2075,8 @@ defmodule LivebookWeb.SessionLiveTest do
test "never syncs secrets when updating from session",
%{conn: conn, session: session, hub: hub} do
hub_secret = insert_secret(name: "FOO", value: "123")
secret = build(:secret, name: "FOO", value: "456", hub_id: nil)
hub_secret = insert_secret(value: "123")
secret = build(:secret, name: hub_secret.name, value: "456", hub_id: nil)
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}/secrets")
Session.set_secret(session.pid, hub_secret)
@ -2093,7 +2093,7 @@ defmodule LivebookWeb.SessionLiveTest do
end
test "shows the 'Add secret' button for missing secrets", %{conn: conn, session: session} do
secret = build(:secret, name: "ANOTHER_GREAT_SECRET", value: "123456", hub_id: nil)
secret = build(:secret, hub_id: nil)
Session.subscribe(session.id)
section_id = insert_section(session.pid)
code = ~s{System.fetch_env!("LB_#{secret.name}")}
@ -2111,7 +2111,7 @@ defmodule LivebookWeb.SessionLiveTest do
test "adding a missing secret using 'Add secret' button",
%{conn: conn, session: session, hub: hub} do
secret = build(:secret, name: "MYUNAVAILABLESECRET", value: "123456", hub_id: nil)
secret = build(:secret, hub_id: nil)
# Subscribe and executes the code to trigger
# the `System.EnvError` exception and outputs the 'Add secret' button
@ -2153,7 +2153,7 @@ defmodule LivebookWeb.SessionLiveTest do
test "granting access for unavailable secret using 'Add secret' button",
%{conn: conn, session: session, hub: hub} do
secret = insert_secret(name: "UNAVAILABLESECRET", value: "123456")
secret = insert_secret()
# Subscribe and executes the code to trigger
# the `System.EnvError` exception and outputs the 'Add secret' button
@ -2198,7 +2198,7 @@ defmodule LivebookWeb.SessionLiveTest do
end
test "reloading outdated secret value", %{conn: conn, session: session} do
hub_secret = insert_secret(name: "FOO", value: "123")
hub_secret = insert_secret(value: "123")
Session.set_secret(session.pid, hub_secret)
{:ok, updated_hub_secret} = Livebook.Secrets.update_secret(hub_secret, %{value: "456"})
@ -2221,9 +2221,7 @@ defmodule LivebookWeb.SessionLiveTest do
Session.subscribe(session.id)
# creates a secret
secret_name = "SECRET_TO_BE_UPDATED_OR_DELETED"
secret_value = "123"
insert_secret(name: secret_name, value: secret_value)
%{name: secret_name, value: secret_value} = insert_secret()
# receives the operation event
assert_receive {:operation, {:sync_hub_secrets, "__server__"}}
@ -2272,10 +2270,12 @@ defmodule LivebookWeb.SessionLiveTest do
Session.subscribe(session.id)
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}")
name = "MY_ENV_#{System.unique_integer([:positive])}"
section_id = insert_section(session.pid)
cell_id =
insert_text_cell(session.pid, section_id, :code, ~s{System.get_env("MY_AWESOME_ENV")})
insert_text_cell(session.pid, section_id, :code, ~s{System.get_env("#{name}")})
view
|> element(~s{[data-el-session]})
@ -2285,7 +2285,7 @@ defmodule LivebookWeb.SessionLiveTest do
{:add_cell_evaluation_response, _, ^cell_id,
terminal_text("\e[35mnil\e[0m"), _}}
attrs = params_for(:env_var, name: "MY_AWESOME_ENV", value: "MyEnvVarValue")
attrs = params_for(:env_var, name: name, value: "MyEnvVarValue")
Settings.set_env_var(attrs)
view
@ -2306,7 +2306,7 @@ defmodule LivebookWeb.SessionLiveTest do
{:add_cell_evaluation_response, _, ^cell_id,
terminal_text("\e[32m\"OTHER_VALUE\"\e[0m"), _}}
Settings.unset_env_var("MY_AWESOME_ENV")
Settings.unset_env_var(name)
view
|> element(~s{[data-el-session]})
@ -2786,7 +2786,7 @@ defmodule LivebookWeb.SessionLiveTest do
end
test "shows a warning when any session secrets are defined", %{conn: conn, session: session} do
secret = build(:secret, name: "FOO", value: "456", hub_id: nil)
secret = build(:secret, hub_id: nil)
Session.set_secret(session.pid, secret)
{:ok, view, _} = live(conn, ~p"/sessions/#{session.id}")

View file

@ -14,7 +14,7 @@ defmodule LivebookWeb.SettingsLiveTest do
end
test "adds an environment variable", %{conn: conn} do
attrs = params_for(:env_var, name: "JAKE_PERALTA_ENV_VAR")
attrs = params_for(:env_var)
{:ok, view, html} = live(conn, ~p"/settings/env-var/new")

View file

@ -42,8 +42,8 @@ defmodule Livebook.Factory do
def build(:env_var) do
%Livebook.Settings.EnvVar{
name: "BAR",
value: "foo"
name: unique_value("BAR_"),
value: Livebook.Utils.random_short_id()
}
end
@ -142,7 +142,7 @@ defmodule Livebook.Factory do
factory_name |> build() |> struct!(attrs)
end
def params_for(factory_name, attrs) do
def params_for(factory_name, attrs \\ []) do
factory_name |> build() |> struct!(attrs) |> Map.from_struct()
end