Raise exception when trying to edit a secret that does not exist (#2067)

This commit is contained in:
ByeongUk Choi 2023-07-13 20:53:33 +09:00 committed by GitHub
parent 53b8bf101a
commit f5d0dd557d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View file

@ -5,6 +5,16 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
alias Livebook.Hubs.Personal
alias LivebookWeb.LayoutHelpers
defmodule NotFoundError do
@moduledoc false
defexception [:secret, plug_status: 404]
def message(%{secret: secret}) do
"could not find secret matching \"#{secret}\""
end
end
@impl true
def update(assigns, socket) do
socket = assign(socket, assigns)
@ -14,9 +24,8 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
secret_value =
if assigns.live_action == :edit_secret do
secrets
|> Enum.find(&(&1.name == secret_name))
|> Map.get(:value)
Enum.find_value(secrets, &(&1.name == secret_name and &1.value)) ||
raise(NotFoundError, secret: secret_name)
end
{:ok,
@ -198,7 +207,7 @@ defmodule LivebookWeb.Hub.Edit.PersonalComponent do
on_confirm = fn socket ->
{:ok, secret} = Livebook.Secrets.update_secret(%Livebook.Secrets.Secret{}, attrs)
:ok = Livebook.Hubs.delete_secret(hub, secret)
_ = Livebook.Hubs.delete_secret(hub, secret)
socket
|> put_flash(:success, "Secret deleted successfully")

View file

@ -41,6 +41,12 @@ defmodule LivebookWeb.Hub.EditLiveTest do
refute Hubs.fetch_hub!(hub.id) == hub
end
test "raises an error if does not exist secret", %{conn: conn, hub: hub} do
assert_raise LivebookWeb.Hub.Edit.PersonalComponent.NotFoundError, fn ->
live(conn, ~p"/hub/#{hub.id}/secrets/edit/HELLO")
end
end
test "creates secret", %{conn: conn, hub: hub} do
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
secret = build(:secret, name: "PERSONAL_ADD_SECRET")