Skip to content

Commit

Permalink
Metadata not refreshing when credential is updated (#1493)
Browse files Browse the repository at this point in the history
* fix: Metadata not refreshing when credential is updated

* increase timeout for assert_push_event metadata_ready

* use dhis2 schema in tests

* increase timeout

* increase timeout

* set timeout to 60 secs to accomodate CI

* format changelog

* undo wrongly fixed conflict

* New credential doesn't appear in inspector until refresh (#1549)

* WIP: update credential block from form

* update changelog

* update changelog

---------

Co-authored-by: Taylor Downs <[email protected]>
Co-authored-by: Taylor Downs <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2023
1 parent 7197f65 commit 600477f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ and this project adheres to

### Fixed

- New credential doesn't appear in inspector until refresh
[#1531](https://github.com/OpenFn/Lightning/issues/1531)
- Metadata not refreshing when credential is updated
[#791](https://github.com/OpenFn/Lightning/issues/791)
- Adjusted z-index for Monaco Editor's sibling element to resolve layout
conflict [#1329](https://github.com/OpenFn/Lightning/issues/1329)
- Demo script sets up example Runs with their log lines in a consistant order.
Expand Down
9 changes: 9 additions & 0 deletions lib/lightning/credentials.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ defmodule Lightning.Credentials do
"""
def get_credential!(id), do: Repo.get!(Credential, id)

def get_credential_by_project_credential(project_credential_id) do
query =
from c in Credential,
join: pc in assoc(c, :project_credentials),
on: pc.id == ^project_credential_id

Repo.one(query)
end

@doc """
Creates a credential.
Expand Down
8 changes: 7 additions & 1 deletion lib/lightning_web/live/workflow_live/editor_pane.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule LightningWeb.WorkflowLive.EditorPane do
use LightningWeb, :live_component
alias Lightning.Credentials
alias LightningWeb.JobLive.JobBuilderComponents

attr :id, :string, required: true
Expand Down Expand Up @@ -38,7 +39,7 @@ defmodule LightningWeb.WorkflowLive.EditorPane do
form[:adaptor].value
|> Lightning.AdaptorRegistry.resolve_adaptor(),
source: form[:body].value,
credential: form[:credential].value,
credential: fetch_credential(form[:project_credential_id].value),
job_id: form[:id].value
)

Expand Down Expand Up @@ -81,4 +82,9 @@ defmodule LightningWeb.WorkflowLive.EditorPane do

{:noreply, socket}
end

defp fetch_credential(project_credential_id) do
project_credential_id &&
Credentials.get_credential_by_project_credential(project_credential_id)
end
end
15 changes: 13 additions & 2 deletions lib/lightning_web/live/workflow_live/job_view.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule LightningWeb.WorkflowLive.JobView do
use LightningWeb, :component
alias Lightning.Credentials
alias LightningWeb.WorkflowLive.EditorPane

import LightningWeb.WorkflowLive.Components
Expand Down Expand Up @@ -73,7 +74,9 @@ defmodule LightningWeb.WorkflowLive.JobView do
<div class="flex h-14 place-content-stretch">
<div class="basis-1/3 flex items-center gap-4 pl-4">
<.adaptor_block adaptor={@job.adaptor} />
<.credential_block credential={@job.credential} />
<.credential_block credential={
fetch_credential(@form[:project_credential_id].value)
} />
</div>
<div class="basis-1/3 font-semibold flex items-center justify-center">
<%= @job.name %>
Expand Down Expand Up @@ -144,7 +147,10 @@ defmodule LightningWeb.WorkflowLive.JobView do

defp credential_block(assigns) do
~H"""
<div class="flex items-center gap-2 whitespace-nowrap">
<div
id="modal-header-credential-block"
class="flex items-center gap-2 whitespace-nowrap"
>
<%= if @credential do %>
<Heroicons.lock_closed class="w-6 h-6 text-gray-500" />
Expand Down Expand Up @@ -197,4 +203,9 @@ defmodule LightningWeb.WorkflowLive.JobView do
</div>
"""
end

defp fetch_credential(project_credential_id) do
project_credential_id &&
Credentials.get_credential_by_project_credential(project_credential_id)
end
end
15 changes: 15 additions & 0 deletions test/lightning/credentials_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ defmodule Lightning.CredentialsTest do
end
end

describe "get_credential_by_project_credential/1" do
test "sreturns the credential with given project_credential id" do
refute Credentials.get_credential_by_project_credential(
Ecto.UUID.generate()
)

project_credential = insert(:project_credential)

credential =
Credentials.get_credential_by_project_credential(project_credential.id)

assert credential.id == project_credential.credential.id
end
end

describe "create_credential/1" do
test "suceeds with raw schema" do
valid_attrs = %{
Expand Down
80 changes: 80 additions & 0 deletions test/lightning_web/live/workflow_live/editor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
project: project,
workflow: workflow
} do
project_credential =
insert(:project_credential,
project: project,
credential:
build(:credential,
name: "dummytestcred",
schema: "http",
body: %{
username: "test",
password: "test"
}
)
)

job = workflow.jobs |> hd()

{:ok, view, _html} =
Expand Down Expand Up @@ -70,6 +84,32 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
assert {"phx-hook", "JobEditor"} in actual_attrs
assert {"phx-target", "1"} in actual_attrs
assert {"phx-update", "ignore"} in actual_attrs

# try changing the assigned credential

credential_block =
element(view, "#modal-header-credential-block") |> render()

assert credential_block =~ "No Credential"
refute credential_block =~ project_credential.credential.name

view
|> form("#workflow-form",
workflow: %{
jobs: %{
"0" => %{
"project_credential_id" => project_credential.id
}
}
}
)
|> render_change()

credential_block =
element(view, "#modal-header-credential-block") |> render()

refute credential_block =~ "No Credential"
assert credential_block =~ project_credential.credential.name
end

describe "manual runs" do
Expand Down Expand Up @@ -393,6 +433,20 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
project: project,
workflow: workflow
} do
project_credential =
insert(:project_credential,
project: project,
credential:
build(:credential,
schema: "http",
body: %{
baseUrl: "http://localhost:4002",
username: "test",
password: "test"
}
)
)

job = workflow.jobs |> hd()

{:ok, view, _html} =
Expand All @@ -408,6 +462,32 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
|> render_click("request_metadata", %{})

assert_push_event(view, "metadata_ready", %{"error" => "no_credential"})

view
|> form("#workflow-form",
workflow: %{
jobs: %{
"0" => %{
"project_credential_id" => project_credential.id
}
}
}
)
|> render_change()

assert view
|> with_target("#job-editor-pane-#{job.id}")
|> render_click("request_metadata", %{})

# set timeout to 60 secs because of CI
assert_push_event(
view,
"metadata_ready",
%{
"error" => "no_metadata_function"
},
60000
)
end
end
end

0 comments on commit 600477f

Please sign in to comment.