From 2cd4dcce32e8438c9d66946749d4c76ebee0509f Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Mon, 8 Jan 2024 14:47:07 +0100 Subject: [PATCH] feat: Handle app Logo in database and auth provider (#510) --- apps/identity_web/assets/css/app.css | 7 +- .../lib/identity_web/views/oauth_helpers.ex | 52 ++-- apps/lenra/lib/lenra/apps.ex | 72 ++++++ apps/lenra/lib/lenra/apps/image.ex | 39 +++ apps/lenra/lib/lenra/apps/logo.ex | 44 ++++ ...20231206131107_manage_app_and_env_logo.exs | 21 ++ .../test/lenra/apps/environment_test.exs | 2 +- apps/lenra/test/lenra/apps/logo_test.exs | 222 ++++++++++++++++++ .../lenra_web/controllers/logo_controller.ex | 57 +++++ apps/lenra_web/lib/lenra_web/router.ex | 7 + .../test/controllers/apps_controller_test.exs | 2 +- .../environment_controller_test.exs | 2 +- .../test/controllers/logo_controller_test.exs | 96 ++++++++ config/dev.exs | 3 + config/runtime.exs | 3 + 15 files changed, 601 insertions(+), 28 deletions(-) create mode 100644 apps/lenra/lib/lenra/apps/image.ex create mode 100644 apps/lenra/lib/lenra/apps/logo.ex create mode 100644 apps/lenra/priv/repo/migrations/20231206131107_manage_app_and_env_logo.exs create mode 100644 apps/lenra/test/lenra/apps/logo_test.exs create mode 100644 apps/lenra_web/lib/lenra_web/controllers/logo_controller.ex create mode 100644 apps/lenra_web/test/controllers/logo_controller_test.exs diff --git a/apps/identity_web/assets/css/app.css b/apps/identity_web/assets/css/app.css index 0ec437cb..cbb800ff 100644 --- a/apps/identity_web/assets/css/app.css +++ b/apps/identity_web/assets/css/app.css @@ -122,7 +122,7 @@ header.external-client>p>a::after { --logo-bg-color: rgba(var(--red-data), var(--disabled-opacity)); } -.logo::before { +.logo[data-letter]::before { content: attr(data-letter); display: block; width: var(--logo-vertical-height); @@ -137,6 +137,11 @@ header.external-client>p>a::after { text-align: center; } +.logo>img { + width: var(--logo-vertical-height); + height: var(--logo-vertical-height); +} + header.external-client>h1.logo::before { margin-bottom: 2rem; } \ No newline at end of file diff --git a/apps/identity_web/lib/identity_web/views/oauth_helpers.ex b/apps/identity_web/lib/identity_web/views/oauth_helpers.ex index a732e4fd..6b409c10 100644 --- a/apps/identity_web/lib/identity_web/views/oauth_helpers.ex +++ b/apps/identity_web/lib/identity_web/views/oauth_helpers.ex @@ -2,9 +2,10 @@ defmodule IdentityWeb.OAuthHelpers do @moduledoc """ Conveniences for translating OAuth scopes. """ - use Phoenix.HTML + alias Lenra.Apps + @colors ["blue", "green", "red", "yellow"] @colors_length length(@colors) @@ -22,23 +23,14 @@ defmodule IdentityWeb.OAuthHelpers do Render the header of the OAuth pages. """ def oauth_header("consent", %{"metadata" => %{"environment_id" => env_id}}) - when is_binary(env_id) do + when is_integer(env_id) or is_binary(env_id) do content_tag :header, class: "external-client" do {:ok, app} = Lenra.Apps.fetch_app_for_env(env_id) - letter = - app.name - |> String.slice(0..0) - |> String.upcase() - [ content_tag :ul do [ - content_tag(:li, app.name, - class: "logo", - "data-letter": letter, - "data-color": get_color(app.service_name) - ), + create_logo_tag(app, env_id, :li), content_tag(:li, "Lenra", class: "lenra") ] end, @@ -51,21 +43,12 @@ defmodule IdentityWeb.OAuthHelpers do end def oauth_header(_context, %{"metadata" => %{"environment_id" => env_id}}) - when is_binary(env_id) do + when is_integer(env_id) or is_binary(env_id) do content_tag :header, class: "external-client" do {:ok, app} = Lenra.Apps.fetch_app_for_env(env_id) - letter = - app.name - |> String.slice(0..0) - |> String.upcase() - [ - content_tag(:h1, app.name, - class: "logo", - "data-letter": letter, - "data-color": get_color(app.service_name) - ), + create_logo_tag(app, env_id, :h1), content_tag :p do [ "Powered by ", @@ -76,7 +59,7 @@ defmodule IdentityWeb.OAuthHelpers do end end - def oauth_header(_context, _client) do + def oauth_header(_context, client) do content_tag :header do content_tag(:h1, "Lenra") end @@ -102,4 +85,25 @@ defmodule IdentityWeb.OAuthHelpers do def get_translated_scope_description(scope) do Gettext.dgettext(IdentityWeb.Gettext, "oauth", "scope." <> scope) end + + defp create_logo_tag(app, env_id, tagname) do + case Apps.get_logo(app.id, env_id) do + nil -> + letter = + app.name + |> String.slice(0..0) + |> String.upcase() + + content_tag(tagname, app.name, + class: "logo", + "data-letter": letter, + "data-color": get_color(app.service_name) + ) + + logo -> + content_tag tagname, class: "logo" do + img_tag("#{Application.fetch_env!(:lenra_web, :public_api_url)}/apps/images/#{logo.image_id}", alt: app.name) + end + end + end end diff --git a/apps/lenra/lib/lenra/apps.ex b/apps/lenra/lib/lenra/apps.ex index 1366092a..f3171c56 100644 --- a/apps/lenra/lib/lenra/apps.ex +++ b/apps/lenra/lib/lenra/apps.ex @@ -33,6 +33,8 @@ defmodule Lenra.Apps do Build, Deployment, Environment, + Image, + Logo, MainEnv, OAuth2Client, UserEnvironmentAccess @@ -635,4 +637,74 @@ defmodule Lenra.Apps do {:halt, error_tuple} end) end + + # Manage Images + + def set_logo(user_id, %{"app_id" => app_id} = params) do + Ecto.Multi.new() + # create the image + |> Ecto.Multi.insert(:inserted_image, Image.new(user_id, params)) + # check if there already a logo for this app (or env ?) + |> Ecto.Multi.one( + :old_logo, + case params do + %{"env_id" => env_id} -> + from( + l in Logo, + where: + l.application_id == ^app_id and + l.environment_id == ^env_id + ) + + _params -> + from( + l in Logo, + where: + l.application_id == ^app_id and + is_nil(l.environment_id) + ) + end + ) + # update the app/env with the image id + |> Ecto.Multi.run(:new_logo, fn repo, state -> upsert_logo(repo, state, params) end) + |> Repo.transaction() + end + + defp upsert_logo(transaction, %{inserted_image: image, old_logo: old_logo} = state, %{"app_id" => app_id} = params) do + case old_logo do + nil -> + transaction.insert(Logo.new(app_id, params["env_id"], %{image_id: image.id})) + + %Logo{image_id: old_logo_image_id} -> + result = transaction.update(Logo.changeset(old_logo, %{image_id: image.id})) + + # delete the previous image if it's not used anymore + if !transaction.exists?(from(l in Logo, where: l.image_id == ^old_logo_image_id)) do + old_image = transaction.get!(Image, old_logo_image_id) + transaction.delete(old_image) + end + + result + end + end + + def get_logo(app_id, env_id) do + Logger.debug("#{__MODULE__} get logo for app id #{app_id} and env id #{env_id}") + + Repo.one( + from(l in Logo, + where: l.application_id == ^app_id and (l.environment_id == ^env_id or is_nil(l.environment_id)), + order_by: is_nil(l.environment_id), + limit: 1 + ) + ) + end + + def fetch_image(image_id) do + Repo.fetch(Image, image_id) + end + + def get_image(image_id) do + Repo.get(Image, image_id) + end end diff --git a/apps/lenra/lib/lenra/apps/image.ex b/apps/lenra/lib/lenra/apps/image.ex new file mode 100644 index 00000000..a75a8d23 --- /dev/null +++ b/apps/lenra/lib/lenra/apps/image.ex @@ -0,0 +1,39 @@ +defmodule Lenra.Apps.Image do + @moduledoc """ + The image schema. + """ + + use Lenra.Schema + import Ecto.Changeset + alias Lenra.Accounts.User + + @type t :: %__MODULE__{} + + @derive {Jason.Encoder, + only: [ + :data, + :type, + :creator_id + ]} + schema "images" do + field(:data, :binary) + field(:type, :string) + belongs_to(:creator, User) + + timestamps() + end + + def changeset(image, params \\ %{}) do + image + |> cast(params, [:data, :type]) + |> validate_required([:data, :type, :creator_id]) + |> foreign_key_constraint(:creator_id) + end + + def new(creator_id, params) do + %__MODULE__{ + creator_id: creator_id + } + |> __MODULE__.changeset(params) + end +end diff --git a/apps/lenra/lib/lenra/apps/logo.ex b/apps/lenra/lib/lenra/apps/logo.ex new file mode 100644 index 00000000..6d85b288 --- /dev/null +++ b/apps/lenra/lib/lenra/apps/logo.ex @@ -0,0 +1,44 @@ +defmodule Lenra.Apps.Logo do + @moduledoc """ + The logo schema. + """ + + use Lenra.Schema + import Ecto.Changeset + alias Lenra.Apps.App + alias Lenra.Apps.Environment + alias Lenra.Apps.Image + + @type t :: %__MODULE__{} + + @derive {Jason.Encoder, + only: [ + :application_id, + :environment_id, + :image_id + ]} + schema "logos" do + belongs_to(:application, App) + belongs_to(:environment, Environment) + belongs_to(:image, Image) + + timestamps() + end + + def changeset(logo, params) do + logo + |> cast(params, [:image_id]) + |> validate_required([:application_id, :image_id]) + |> foreign_key_constraint(:application_id) + |> foreign_key_constraint(:environment_id) + |> foreign_key_constraint(:image_id) + end + + def new(application_id, environment_id, params \\ %{}) do + %__MODULE__{ + application_id: application_id, + environment_id: environment_id + } + |> __MODULE__.changeset(params) + end +end diff --git a/apps/lenra/priv/repo/migrations/20231206131107_manage_app_and_env_logo.exs b/apps/lenra/priv/repo/migrations/20231206131107_manage_app_and_env_logo.exs new file mode 100644 index 00000000..7bf931da --- /dev/null +++ b/apps/lenra/priv/repo/migrations/20231206131107_manage_app_and_env_logo.exs @@ -0,0 +1,21 @@ +defmodule Lenra.Repo.Migrations.ManageAppAndEnvLogo do + use Ecto.Migration + + def change do + create table(:images) do + add(:creator_id, references(:users)) + add(:data, :binary) + add(:type, :string) + timestamps() + end + + create table(:logos) do + add(:application_id, references(:applications)) + add(:environment_id, references(:applications), null: true) + add(:image_id, references(:images)) + timestamps() + end + + create(unique_index(:logos, [:application_id, :environment_id], name: :logos_application_id_environment_id_index)) + end +end diff --git a/apps/lenra/test/lenra/apps/environment_test.exs b/apps/lenra/test/lenra/apps/environment_test.exs index 22d7dd3a..fcda002c 100644 --- a/apps/lenra/test/lenra/apps/environment_test.exs +++ b/apps/lenra/test/lenra/apps/environment_test.exs @@ -2,7 +2,7 @@ defmodule Lenra.Apps.EnvironmentTest do @moduledoc """ Test the environment services """ - use Lenra.RepoCase, async: true + use Lenra.RepoCase, async: false alias Lenra.Repo diff --git a/apps/lenra/test/lenra/apps/logo_test.exs b/apps/lenra/test/lenra/apps/logo_test.exs new file mode 100644 index 00000000..49d91d60 --- /dev/null +++ b/apps/lenra/test/lenra/apps/logo_test.exs @@ -0,0 +1,222 @@ +defmodule Lenra.Apps.LogoTest do + @moduledoc """ + Test the logo services + """ + use Lenra.RepoCase, async: false + + alias Lenra.Apps + alias Lenra.Apps.Logo + alias Lenra.Repo + + setup do + {:ok, %{inserted_user: user}} = UserTestHelper.register_john_doe() + {:ok, %{inserted_application: app, inserted_env: env}} = create_and_return_application(user, "logo test1") + + {:ok, %{inserted_application: _other_app, inserted_env: other_env}} = + create_and_return_application(user, "logo test2") + + {:ok, + app: app, + env: env, + other_env: other_env, + png_image: %{ + data: File.read!(Application.app_dir(:identity_web, "/priv/static/images/appicon.png")), + type: "image/png" + }, + svg_image: %{ + data: File.read!(Application.app_dir(:identity_web, "/priv/static/images/logo.svg")), + type: "image/svg+xml" + }} + end + + defp create_and_return_application(user, name) do + Apps.create_app(user.id, %{ + name: name, + color: "FFFFFF", + icon: "60189" + }) + end + + describe "put app logo" do + test "not existing", %{app: app, png_image: png_image} do + assert {:ok, %{inserted_image: image, new_logo: logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert image.creator_id == app.creator_id + assert image.type == png_image.type + assert image.data == png_image.data + + assert logo.application_id == app.id + assert logo.environment_id == nil + assert logo.image_id == image.id + end + + test "existing", %{ + app: app, + png_image: png_image, + svg_image: svg_image + } do + assert {:ok, %{inserted_image: initial_image, old_logo: initial_old_logo, new_logo: initial_logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert is_nil(initial_old_logo) + + png_image_id = initial_image.id + png_logo_id = initial_logo.id + + assert initial_image.type == png_image.type + assert initial_logo.image_id == initial_image.id + + assert {:ok, %{inserted_image: image, old_logo: old_logo, new_logo: logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "data" => svg_image.data, + "type" => svg_image.type + }) + + assert !is_nil(old_logo) + + assert image.type == svg_image.type + assert logo.image_id == image.id + assert image.id != png_image_id, "image id should be different" + assert old_logo.id == png_logo_id, "logo id should be the same" + assert logo.id == png_logo_id, "logo id should be the same" + end + + test "existing and reused image", %{ + app: app, + env: env, + png_image: png_image, + svg_image: svg_image + } do + assert {:ok, %{inserted_image: initial_image, old_logo: _initial_old_logo, new_logo: initial_logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert {:ok, new_logo} = Repo.insert(Logo.new(app.id, env.id, %{image_id: initial_image.id})) + + assert initial_image.type == png_image.type + assert initial_logo.image_id == initial_image.id + assert new_logo.image_id == initial_image.id + + assert {:ok, %{inserted_image: image, old_logo: old_logo, new_logo: logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "data" => svg_image.data, + "type" => svg_image.type + }) + + assert image.type == svg_image.type + assert logo.image_id == image.id + assert image.id != initial_image.id, "image id should be different" + assert old_logo.id == logo.id, "logo id should be the same" + assert initial_logo.id == logo.id, "logo id should be the same" + end + end + + describe "put env logo" do + test "not existing", %{app: app, env: env, png_image: png_image} do + assert {:ok, %{inserted_image: initial_image, old_logo: _initial_old_logo, new_logo: initial_logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "env_id" => env.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert initial_image.creator_id == app.creator_id + assert initial_image.type == png_image.type + assert initial_image.data == png_image.data + + assert initial_logo.application_id == app.id + assert initial_logo.environment_id == env.id + assert initial_logo.image_id == initial_image.id + end + + test "existing", %{ + app: app, + env: env, + png_image: png_image, + svg_image: svg_image + } do + assert {:ok, %{inserted_image: initial_image, old_logo: initial_old_logo, new_logo: initial_logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "env_id" => env.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert is_nil(initial_old_logo) + + png_image_id = initial_image.id + png_logo_id = initial_logo.id + + assert initial_image.type == png_image.type + assert initial_logo.image_id == initial_image.id + + assert {:ok, %{inserted_image: image, old_logo: old_logo, new_logo: logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "env_id" => env.id, + "data" => svg_image.data, + "type" => svg_image.type + }) + + assert !is_nil(old_logo) + + assert image.type == svg_image.type + assert logo.image_id == image.id + assert image.id != png_image_id, "image id should be different" + assert old_logo.id == png_logo_id, "logo id should be the same" + assert logo.id == png_logo_id, "logo id should be the same" + end + + test "existing and reused image", %{ + app: app, + env: env, + other_env: other_env, + png_image: png_image, + svg_image: svg_image + } do + assert {:ok, %{inserted_image: initial_image, old_logo: _initial_old_logo, new_logo: initial_logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "env_id" => env.id, + "data" => png_image.data, + "type" => png_image.type + }) + + assert {:ok, new_logo} = Repo.insert(Logo.new(app.id, other_env.id, %{image_id: initial_image.id})) + + assert initial_image.type == png_image.type + assert initial_logo.image_id == initial_image.id + assert new_logo.image_id == initial_image.id + + assert {:ok, %{inserted_image: image, old_logo: old_logo, new_logo: logo}} = + Apps.set_logo(app.creator_id, %{ + "app_id" => app.id, + "env_id" => env.id, + "data" => svg_image.data, + "type" => svg_image.type + }) + + assert image.type == svg_image.type + assert logo.image_id == image.id + assert image.id != initial_image.id, "image id should be different" + assert old_logo.id == logo.id, "logo id should be the same" + assert initial_logo.id == logo.id, "logo id should be the same" + end + end +end diff --git a/apps/lenra_web/lib/lenra_web/controllers/logo_controller.ex b/apps/lenra_web/lib/lenra_web/controllers/logo_controller.ex new file mode 100644 index 00000000..048a7d66 --- /dev/null +++ b/apps/lenra_web/lib/lenra_web/controllers/logo_controller.ex @@ -0,0 +1,57 @@ +defmodule LenraWeb.LogosController do + use LenraWeb, :controller + + use LenraWeb.Policy, + module: LenraWeb.LogosController.Policy + + alias Lenra.Apps + + def get_image_content(conn, %{"image_id" => image_id}) do + {:ok, image} = Apps.fetch_image(image_id) + + conn + |> put_resp_content_type(image.type) + |> Plug.Conn.send_resp(:ok, image.data) + end + + @spec put_logo(any(), map()) :: any() + def put_logo(conn, %{"app_id" => _app_id, "env_id" => env_id} = params) do + with {:ok, env} <- Apps.fetch_env(env_id), + :ok <- allow(conn, env), + user <- LenraWeb.Auth.current_resource(conn), + decoded_data <- decode_data(params), + {:ok, %{new_logo: logo}} <- + Apps.set_logo(user.id, Map.merge(decoded_data, %{"app_id" => env.application_id, "env_id" => env.id})) do + conn + |> reply(logo) + end + end + + def put_logo(conn, %{"app_id" => app_id} = params) do + with {:ok, app} <- Apps.fetch_app(app_id), + :ok <- allow(conn, app), + user <- LenraWeb.Auth.current_resource(conn), + decoded_data <- decode_data(params), + {:ok, %{new_logo: logo}} <- Apps.set_logo(user.id, Map.merge(decoded_data, %{"app_id" => app.id})) do + conn + |> reply(logo) + end + end + + defp decode_data(%{"data" => data, "type" => type}) when is_binary(data) and is_binary(type) do + %{"data" => Base.decode64!(data), "type" => type} + end +end + +defmodule LenraWeb.LogosController.Policy do + alias Lenra.Accounts.User + alias Lenra.Apps.App + alias Lenra.Apps.Environment + + @impl Bouncer.Policy + def authorize(:put_logo, %User{id: user_id}, %Environment{creator_id: user_id}), do: true + def authorize(:put_logo, %User{id: user_id}, %App{creator_id: user_id}), do: true + + # credo:disable-for-next-line Credo.Check.Readability.StrictModuleLayout + use LenraWeb.Policy.Default +end diff --git a/apps/lenra_web/lib/lenra_web/router.ex b/apps/lenra_web/lib/lenra_web/router.ex index bfe02d21..442e2e5a 100644 --- a/apps/lenra_web/lib/lenra_web/router.ex +++ b/apps/lenra_web/lib/lenra_web/router.ex @@ -111,6 +111,12 @@ defmodule LenraWeb.Router do # Invitations to env resources("/:app_id/environments/:env_id/invitations", UserEnvironmentAccessController, only: [:index, :create]) + # App logo + get("/:app_id/logo", LogosController, :get_logo) + put("/:app_id/logo", LogosController, :put_logo) + get("/:app_id/environments/:env_id/logo", LogosController, :get_logo) + put("/:app_id/environments/:env_id/logo", LogosController, :put_logo) + get("/invitations/:id", UserEnvironmentAccessController, :fetch_one) post("/invitations/:id", UserEnvironmentAccessController, :accept) @@ -149,6 +155,7 @@ defmodule LenraWeb.Router do pipe_through([:api]) post("/apps/:app_uuid/webhooks/:webhook_uuid", WebhooksController, :trigger) + get("/apps/images/:image_id", LogosController, :get_image_content) end # Enables LiveDashboard only for development diff --git a/apps/lenra_web/test/controllers/apps_controller_test.exs b/apps/lenra_web/test/controllers/apps_controller_test.exs index ee80fcd3..0958a341 100644 --- a/apps/lenra_web/test/controllers/apps_controller_test.exs +++ b/apps/lenra_web/test/controllers/apps_controller_test.exs @@ -1,5 +1,5 @@ defmodule LenraWeb.AppsControllerTest do - use LenraWeb.ConnCase, async: true + use LenraWeb.ConnCase, async: false alias Lenra.Apps.App alias Lenra.Repo diff --git a/apps/lenra_web/test/controllers/environment_controller_test.exs b/apps/lenra_web/test/controllers/environment_controller_test.exs index 1a5782ef..aa8ff5a1 100644 --- a/apps/lenra_web/test/controllers/environment_controller_test.exs +++ b/apps/lenra_web/test/controllers/environment_controller_test.exs @@ -1,5 +1,5 @@ defmodule LenraWeb.EnvironmentControllerTest do - use LenraWeb.ConnCase, async: true + use LenraWeb.ConnCase, async: false alias Lenra.Repo alias Lenra.Subscriptions.Subscription diff --git a/apps/lenra_web/test/controllers/logo_controller_test.exs b/apps/lenra_web/test/controllers/logo_controller_test.exs new file mode 100644 index 00000000..d68ab9c4 --- /dev/null +++ b/apps/lenra_web/test/controllers/logo_controller_test.exs @@ -0,0 +1,96 @@ +defmodule LenraWeb.LogoControllerTest do + use LenraWeb.ConnCase, async: false + alias Lenra.Apps + alias Lenra.Apps.Image + alias Lenra.Apps.Logo + alias Lenra.Repo + + setup %{conn: conn} do + {:ok, + conn: conn, + png_image: %{ + data: File.read!(Application.app_dir(:identity_web, "/priv/static/images/appicon.png")), + type: "image/png" + }, + svg_image: %{ + data: File.read!(Application.app_dir(:identity_web, "/priv/static/images/logo.svg")), + type: "image/svg+xml" + }} + end + + defp create_app(conn) do + post(conn, Routes.apps_path(conn, :create), %{ + "name" => "test", + "color" => "ffffff", + "icon" => 12 + }) + end + + defp clean_repo do + Repo.delete_all(Logo) + Repo.delete_all(Image) + end + + @tag auth_user_with_cgs: :dev + describe "put" do + test "app not existing logo", %{conn: conn!, png_image: png_image} do + conn! = create_app(conn!) + assert app = json_response(conn!, 200) + + encoded_data = Base.encode64(png_image.data) + + conn! = + put( + conn!, + Routes.logos_path( + conn!, + :put_logo, + app["id"] + ), + %{"data" => encoded_data, "type" => png_image.type} + ) + + assert %{ + "application_id" => image_app_id, + "environment_id" => nil, + "image_id" => _ + } = json_response(conn!, 200) + + assert image_app_id == app["id"] + clean_repo() + end + + @tag :skip + @tag auth_user_with_cgs: :dev + test "env not existing logo", %{conn: conn!, png_image: png_image} do + conn! = create_app(conn!) + assert app = json_response(conn!, 200) + + {:ok, env} = Apps.fetch_main_env_for_app(app["id"]) + + encoded_data = Base.encode64(png_image.data) + + conn! = + put( + conn!, + Routes.logos_path( + conn!, + :put_logo, + app["id"], + env.id + ), + %{"data" => encoded_data, "type" => png_image.type} + ) + + assert %{ + "application_id" => image_app_id, + "environment_id" => image_env_id, + "image_id" => _ + } = json_response(conn!, 200) + + assert image_app_id == app["id"] + assert image_env_id == env.id + clean_repo() + end + end +end diff --git a/config/dev.exs b/config/dev.exs index 4de3c203..ba44506b 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -63,6 +63,9 @@ config :lenra_web, LenraWeb.Endpoint, code_reloader: true, watchers: [] +config :lenra_web, + public_api_url: "http://localhost:#{String.to_integer(System.get_env("PORT", "4000"))}" + # Do not include metadata nor timestamps in development logs config :logger, :console, format: "[$level] $message\n" diff --git a/config/runtime.exs b/config/runtime.exs index 61b937da..6c8f25ac 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -9,6 +9,9 @@ if config_env() == :prod do secret_key_base: System.fetch_env!("SECRET_KEY_BASE"), url: [host: System.fetch_env!("API_ENDPOINT"), port: System.fetch_env!("PORT")] + config :lenra_web, + public_api_url: System.fetch_env!("LENRA_SERVER_URL") + config :identity_web, IdentityWeb.Endpoint, http: [port: System.get_env("IDENTITY_WEB_PORT", "4010")], secret_key_base: System.fetch_env!("IDENTITY_WEB_SECRET_KEY_BASE"),