Skip to content

Commit

Permalink
feat!: Use the new API project with breaking changes (#452)
Browse files Browse the repository at this point in the history
* feat: Replace action keyword by listener

* feat!: Use the new API project with breaking changes

* feat!: Upgrade components API

* feat: Change Manifest

* feat: Change Manifest

* feat!: Use the new API project with breaking changes

* build(deps): ex_component_schema version

* test: logs

* test: logs

* fix: Listener name

* fix: Get listener props

* fix: Thanks to my cat...

* feat!: Change app API paths

* fix: Find endpoint

* Apply suggestions from code review

Co-authored-by: Emric Pichonnier <emric.pichonnier@lenra.io>

* style: Fix format

---------

Co-authored-by: Emric Pichonnier <emric.pichonnier@lenra.io>
  • Loading branch information
taorepoara and pichoemr authored Sep 19, 2023
1 parent 80026ff commit 6950f85
Show file tree
Hide file tree
Showing 54 changed files with 303 additions and 966 deletions.
12 changes: 6 additions & 6 deletions apps/lenra/lib/lenra/apps/webhook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ defmodule Lenra.Apps.Webhook do
alias Lenra.Accounts.User
alias Lenra.Apps.{Environment, Webhook}

@derive {Jason.Encoder, only: [:uuid, :action, :props, :environment_id, :user_id]}
@derive {Jason.Encoder, only: [:uuid, :listener, :props, :environment_id, :user_id]}
@primary_key {:uuid, Ecto.UUID, autogenerate: true}
schema "webhooks" do
belongs_to(:environment, Environment)
belongs_to(:user, User)

field(:action, :string)
field(:listener, :string)
field(:props, :map)

timestamps()
end

def changeset(webhook, params \\ %{}) do
webhook
|> cast(params, [:action, :props, :user_id])
|> validate_required([:environment_id, :action])
|> cast(params, [:listener, :props, :user_id])
|> validate_required([:environment_id, :listener])
|> foreign_key_constraint(:environment_id)
|> foreign_key_constraint(:user_id)
end
Expand All @@ -39,8 +39,8 @@ defmodule Lenra.Apps.Webhook do

changeset =
%__MODULE__{}
|> cast(webhook_map, [:uuid, :action, :props, :user_id, :environment_id, :inserted_at, :updated_at])
|> validate_required([:environment_id, :action])
|> cast(webhook_map, [:uuid, :listener, :props, :user_id, :environment_id, :inserted_at, :updated_at])
|> validate_required([:environment_id, :listener])
|> foreign_key_constraint(:environment_id)
|> foreign_key_constraint(:user_id)

Expand Down
2 changes: 0 additions & 2 deletions apps/lenra/lib/lenra/errors/technical_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ defmodule Lenra.Errors.TechnicalError do
{:openfaas_not_reachable, "Openfaas could not be reached."},
{:unhandled_resource_type, "Unknown resource."},
{:application_not_found, "The application was not found in Openfaas."},
{:listener_not_found, "No listener found in app manifest."},
{:openfaas_delete_error, "Openfaas could not delete the application."},
{:timeout, "Openfaas timeout."},
{:no_app_found, "No application found for the current link."},
{:environment_not_built, "This application was not yet build."},
{:widget_not_found, "No Widget found in app manifest."},
{:invalid_ui, "Invalid UI"},
{:datastore_not_found, "Datastore cannot be found"},
{:data_not_found, "Data cannot be found"},
Expand Down
6 changes: 3 additions & 3 deletions apps/lenra/lib/lenra/services/openfaas_services.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Lenra.OpenfaasServices do
@moduledoc """
The service that manage calls to an Openfaas action with `run_action/3`
The service that manage calls to an Openfaas function with `run_action/3`
"""

alias Lenra.Apps
Expand Down Expand Up @@ -114,14 +114,14 @@ defmodule Lenra.OpenfaasServices do
TechnicalError.openfaas_delete_error_tuple()
end

defp response({:error, %Mint.TransportError{reason: reason}}, _action) do
defp response({:error, %Mint.TransportError{reason: reason}}, _function) do
Logger.error("Openfaas could not be reached. It should not happen. \n\t\t reason: #{reason}")
TechnicalError.openfaas_not_reachable_tuple()
end

defp response(
{:ok, %Finch.Response{status: status_code, body: body}},
_action
_function
)
when status_code not in [200, 202] do
case status_code do
Expand Down
16 changes: 11 additions & 5 deletions apps/lenra_web/test/channels/app_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@ defmodule LenraWeb.AppChannelTest do
# alias LenraWeb.UserSocket

# @build_number 1
# @listener_name "HiBob"
# @listener_code Crypto.hash({@listener_name, %{}})
# @_listener "HiBob"
# @listener_code Crypto.hash({@_listener, %{}})

# @manifest %{"manifest" => %{"rootWidget" => "test"}}

# @data %{"data" => %{"user" => %{"name" => "World"}}}
# @data2 %{"data" => %{"user" => %{"name" => "Bob"}}}

# @textfield %{
# "type" => "textfield",
# "_type" => "textfield",
# "value" => "Hello World",
# "onChanged" => %{"action" => @listener_name}
# "onChanged" => %{
# "_type" => "listener",
# "name" => @_listener
# }
# }

# @textfield2 %{
# "type" => "textfield",
# "value" => "Hello Bob",
# "onChanged" => %{"action" => @listener_name}
# "onChanged" => %{
# "_type" => "listener",
# "name" => @_listener
# }
# }

# @transformed_textfield %{
Expand Down
26 changes: 13 additions & 13 deletions apps/lenra_web/test/controllers/webhooks_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ defmodule LenraWeb.WebhooksControllerTest do

@tag auth_user_with_cgu: :dev
test "Get env webhooks should work properly", %{conn: conn, env: env} do
WebhookServices.create(env.id, %{"action" => "test"})
WebhookServices.create(env.id, %{"listener" => "test"})

conn = get(conn, Routes.webhooks_path(conn, :index), %{"env_id" => env.id})

assert [webhook] = json_response(conn, 200)
assert webhook["action"] == "test"
assert webhook["listener"] == "test"
assert webhook["environment_id"] == env.id
end

@tag auth_user_with_cgu: :dev
test "Get session webhooks should work properly", %{conn: conn, user: user, env: env} do
WebhookServices.create(env.id, %{
"action" => "test",
"listener" => "test",
"user_id" => user.id
})

conn = get(conn, Routes.webhooks_path(conn, :index), %{"env_id" => env.id, "user_id" => user.id})

assert [webhook] = json_response(conn, 200)
assert webhook["action"] == "test"
assert webhook["listener"] == "test"
assert webhook["environment_id"] == env.id
assert webhook["user_id"] == user.id
end
Expand All @@ -65,24 +65,24 @@ defmodule LenraWeb.WebhooksControllerTest do
conn =
post(conn, Routes.webhooks_path(conn, :api_create), %{
"env_id" => env.id,
"action" => "test",
"listener" => "test",
"user_id" => user.id
})

assert %{"action" => "test"} = json_response(conn, 200)
assert %{"listener" => "test"} = json_response(conn, 200)

conn! = get(conn, Routes.webhooks_path(conn, :index), %{"env_id" => env.id})

assert [webhook] = json_response(conn!, 200)
assert webhook["action"] == "test"
assert webhook["listener"] == "test"
assert webhook["environment_id"] == env.id
end

@tag auth_user_with_cgu: :dev
test "Create webhook without env_id as parameter should fail", %{conn: conn, user: user} do
conn =
post(conn, Routes.webhooks_path(conn, :api_create), %{
"action" => "test",
"listener" => "test",
"user_id" => user.id
})

Expand All @@ -102,8 +102,8 @@ defmodule LenraWeb.WebhooksControllerTest do
callback.(body_decoded)

case body_decoded do
# Listeners "action" in body
%{"action" => _action} ->
# Listeners "listener" in body
%{"listener" => _listener} ->
Plug.Conn.resp(conn, 200, "")
end
end
Expand All @@ -123,7 +123,7 @@ defmodule LenraWeb.WebhooksControllerTest do

{:ok, webhook} =
WebhookServices.create(env.id, %{
"action" => "test"
"listener" => "test"
})

bypass = Bypass.open(port: 1234)
Expand All @@ -134,7 +134,7 @@ defmodule LenraWeb.WebhooksControllerTest do
"/function/test",
&handle_request(&1, fn body ->
assert body["props"] == nil
assert body["action"] == "test"
assert body["listener"] == "test"
assert body["event"] == %{"payloadData" => "Value"}
end)
)
Expand All @@ -158,7 +158,7 @@ defmodule LenraWeb.WebhooksControllerTest do
} do
{:ok, webhook} =
WebhookServices.create(env.id, %{
"action" => "test"
"listener" => "test"
})

conn =
Expand Down
7 changes: 0 additions & 7 deletions libs/application_runner/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,3 @@ erl_crash.dump

# Ignore package tarball (built via "mix hex.build").
application_runner-*.tar


# Temporary files for e.g. tests
/tmp

.DS_Store
.iex.exs
Loading

0 comments on commit 6950f85

Please sign in to comment.