Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
midigofrank committed Nov 14, 2024
1 parent 877c897 commit 87d9dba
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 84 deletions.
7 changes: 5 additions & 2 deletions lib/lightning/work_orders.ex
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ defmodule Lightning.WorkOrders do
Step.t() | Ecto.UUID.t(),
[work_order_option(), ...]
) ::
{:ok, Run.t()} | {:error, Ecto.Changeset.t()}
{:ok, Run.t()} | {:error, Ecto.Changeset.t() | :workflow_deleted}
def retry(%Run{id: run_id}, %Step{id: step_id}, opts) do
retry(run_id, step_id, opts)
end
Expand Down Expand Up @@ -508,7 +508,10 @@ defmodule Lightning.WorkOrders do
retry(run_step.run_id, run_step.step_id, opts)
end)

{:ok, Enum.count(results, fn result -> match?({:ok, _}, result) end), 0}
success_count =
Enum.count(results, fn result -> match?({:ok, _}, result) end)

{:ok, success_count, Enum.count(results) - success_count}
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/lightning_web/live/run_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ defmodule LightningWeb.RunLive.Index do
socket
|> put_flash(:error, error_text)}

{:error, :workflow_deleted} ->
{:noreply,
socket
|> put_flash(:error, "Runs for deleted workflows cannot be retried")}

{:error, _changeset} ->
{:noreply,
socket
Expand All @@ -362,7 +367,8 @@ defmodule LightningWeb.RunLive.Index do
"New run#{if count > 1, do: "s", else: ""} enqueued for #{count} workorder#{if count > 1, do: "s", else: ""}"
|> then(fn msg ->
if discarded_count > 0 do
msg <> " (#{discarded_count} were discarded due to wiped dataclip)"
msg <>
" (#{discarded_count} were discarded due to wiped dataclip/workflow being deleted)"
else
msg
end
Expand Down
170 changes: 90 additions & 80 deletions lib/lightning_web/live/workflow_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1411,20 +1411,9 @@ defmodule LightningWeb.WorkflowLive.Edit do
end

def handle_event("save", params, socket) do
%{
project: project,
workflow_params: initial_params,
can_edit_workflow: can_edit_workflow,
snapshot_version_tag: tag,
has_presence_edit_priority: has_presence_edit_priority
} =
socket.assigns
%{project: project, workflow_params: initial_params} = socket.assigns

with true <- can_edit_workflow || :not_authorized,
true <- tag == "latest" || :view_only,
true <-
has_presence_edit_priority ||
:presence_low_priority do
with :ok <- check_user_can_save_workflow(socket) do
next_params =
case params do
%{"workflow" => params} ->
Expand Down Expand Up @@ -1480,27 +1469,6 @@ defmodule LightningWeb.WorkflowLive.Edit do
|> put_flash(:error, "Workflow could not be saved")
|> push_patches_applied(initial_params)}
end
else
:view_only ->
{:noreply,
socket
|> put_flash(
:error,
"Cannot save in snapshot mode, switch to the latest version."
)}

:presence_low_priority ->
{:noreply,
socket
|> put_flash(
:error,
"Cannot save in view-only mode"
)}

:not_authorized ->
{:noreply,
socket
|> put_flash(:error, "You are not authorized to perform this action.")}
end
end

Expand Down Expand Up @@ -1601,9 +1569,6 @@ defmodule LightningWeb.WorkflowLive.Edit do
selected_job: selected_job,
current_user: current_user,
workflow_params: workflow_params,
can_edit_workflow: can_edit_workflow,
can_run_workflow: can_run_workflow,
snapshot_version_tag: tag,
has_presence_edit_priority: has_presence_edit_priority,
workflow: workflow,
manual_run_form: form
Expand All @@ -1626,62 +1591,52 @@ defmodule LightningWeb.WorkflowLive.Edit do
get_workflow_by_id(workflow.id)
end

with true <- (can_run_workflow && can_edit_workflow) || :not_authorized,
true <- tag == "latest" || :view_only,
{:ok, %{workorder: workorder, workflow: workflow}} <-
Helpers.run_workflow(
with :ok <- check_user_can_manual_run_workflow(socket) do
case Helpers.run_workflow(
workflow_or_changeset,
params,
project: project,
selected_job: selected_job,
created_by: current_user
) do
%{runs: [run]} = workorder
{:ok, %{workorder: workorder, workflow: workflow}} ->
%{runs: [run]} = workorder

Runs.subscribe(run)
Runs.subscribe(run)

snapshot = snapshot_by_version(workflow.id, workflow.lock_version)

{:noreply,
socket
|> assign_workflow(workflow, snapshot)
|> follow_run(run)
|> push_event("push-hash", %{"hash" => "log"})}
else
{:error, %Ecto.Changeset{data: %WorkOrders.Manual{}} = changeset} ->
{:noreply,
socket
|> assign_manual_run_form(changeset)}
snapshot = snapshot_by_version(workflow.id, workflow.lock_version)

{:error, %Ecto.Changeset{data: %Workflow{}} = changeset} ->
{
:noreply,
socket
|> assign_changeset(changeset)
|> mark_validated()
|> put_flash(:error, "Workflow could not be saved")
}
{:noreply,
socket
|> assign_workflow(workflow, snapshot)
|> follow_run(run)
|> push_event("push-hash", %{"hash" => "log"})}

:not_authorized ->
{:noreply,
socket
|> put_flash(:error, "You are not authorized to perform this action.")}
{:error, %Ecto.Changeset{data: %WorkOrders.Manual{}} = changeset} ->
{:noreply,
socket
|> assign_manual_run_form(changeset)}

:view_only ->
{:noreply,
socket
|> put_flash(:error, "Cannot run in snapshot mode, switch to latest.")}
{:error, %Ecto.Changeset{data: %Workflow{}} = changeset} ->
{
:noreply,
socket
|> assign_changeset(changeset)
|> mark_validated()
|> put_flash(:error, "Workflow could not be saved")
}

{:error, %{text: message}} ->
{:noreply, put_flash(socket, :error, message)}
{:error, %{text: message}} ->
{:noreply, put_flash(socket, :error, message)}

{:error, :deleted} ->
{:noreply,
put_flash(
socket,
:error,
"Oops! You cannot modify a deleted workflow"
)}
{:error, :deleted} ->
{:noreply,
put_flash(
socket,
:error,
"Oops! You cannot modify a deleted workflow"
)}
end
end
end

Expand Down Expand Up @@ -1779,6 +1734,61 @@ defmodule LightningWeb.WorkflowLive.Edit do
{:noreply, socket}
end

defp check_user_can_manual_run_workflow(socket) do
case socket.assigns do
%{
can_edit_workflow: true,
can_run_workflow: true,
snapshot_version_tag: "latest"
} ->
:ok

%{can_edit_workflow: false} ->
{:noreply,
socket
|> put_flash(:error, "You are not authorized to perform this action.")}

%{can_run_workflow: false} ->
{:noreply,
socket
|> put_flash(:error, "You are not authorized to perform this action.")}

_snapshot_not_latest ->
{:noreply,
socket
|> put_flash(:error, "Cannot run in snapshot mode, switch to latest.")}
end
end

defp check_user_can_save_workflow(socket) do
case socket.assigns do
%{
can_edit_workflow: true,
has_presence_edit_priority: true,
snapshot_version_tag: "latest"
} ->
:ok

%{can_edit_workflow: false} ->
{:noreply,
socket
|> put_flash(:error, "You are not authorized to perform this action.")}

%{has_presence_edit_priority: false} ->
{:noreply,
socket
|> put_flash(
:error,
"Cannot save in view-only mode"
)}

_snapshot_not_latest ->
{:noreply,
socket
|> put_flash(:error, "Cannot save in snapshot mode, switch to latest.")}
end
end

defp maybe_disable_canvas(socket) do
%{
has_presence_edit_priority: has_edit_priority,
Expand Down
Loading

0 comments on commit 87d9dba

Please sign in to comment.