From d422095054f5cc5cdefed606368bf782cd3f9fe4 Mon Sep 17 00:00:00 2001 From: Mauricio Giacomini Girardello Date: Sat, 8 Jun 2019 15:16:39 -0300 Subject: [PATCH 1/2] Remove all plugs --- lib/triplex/plugs/ensure_plug.ex | 24 ------ lib/triplex/plugs/ensure_plug_config.ex | 15 ---- lib/triplex/plugs/param_plug.ex | 34 -------- lib/triplex/plugs/param_plug_config.ex | 14 ---- lib/triplex/plugs/plug.ex | 72 ----------------- lib/triplex/plugs/session_plug.ex | 29 ------- lib/triplex/plugs/session_plug_config.ex | 18 ----- lib/triplex/plugs/subdomain_plug.ex | 44 ----------- lib/triplex/plugs/subdomain_plug_config.ex | 19 ----- test/triplex/plugs/ensure_plug_test.exs | 32 -------- test/triplex/plugs/param_plug_test.exs | 35 --------- test/triplex/plugs/plug_test.exs | 90 ---------------------- test/triplex/plugs/session_plug_test.exs | 38 --------- test/triplex/plugs/subdomain_plug_test.exs | 29 ------- 14 files changed, 493 deletions(-) delete mode 100644 lib/triplex/plugs/ensure_plug.ex delete mode 100644 lib/triplex/plugs/ensure_plug_config.ex delete mode 100644 lib/triplex/plugs/param_plug.ex delete mode 100644 lib/triplex/plugs/param_plug_config.ex delete mode 100644 lib/triplex/plugs/plug.ex delete mode 100644 lib/triplex/plugs/session_plug.ex delete mode 100644 lib/triplex/plugs/session_plug_config.ex delete mode 100644 lib/triplex/plugs/subdomain_plug.ex delete mode 100644 lib/triplex/plugs/subdomain_plug_config.ex delete mode 100644 test/triplex/plugs/ensure_plug_test.exs delete mode 100644 test/triplex/plugs/param_plug_test.exs delete mode 100644 test/triplex/plugs/plug_test.exs delete mode 100644 test/triplex/plugs/session_plug_test.exs delete mode 100644 test/triplex/plugs/subdomain_plug_test.exs diff --git a/lib/triplex/plugs/ensure_plug.ex b/lib/triplex/plugs/ensure_plug.ex deleted file mode 100644 index a78971e..0000000 --- a/lib/triplex/plugs/ensure_plug.ex +++ /dev/null @@ -1,24 +0,0 @@ -if Code.ensure_loaded?(Plug) do - defmodule Triplex.EnsurePlug do - @moduledoc """ - This is a basic plug that ensure the tenant is loaded. - - To plug it on your router, you can use: - - plug Triplex.EnsurePlug, - callback: &TenantHelper.callback/2 - failure_callback: &TenantHelper.failure_callback/2 - - See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags. - """ - - alias Triplex.EnsurePlugConfig - alias Triplex.Plug - - @doc false - def init(opts), do: struct(EnsurePlugConfig, opts) - - @doc false - def call(conn, config), do: Plug.ensure_tenant(conn, config) - end -end diff --git a/lib/triplex/plugs/ensure_plug_config.ex b/lib/triplex/plugs/ensure_plug_config.ex deleted file mode 100644 index 8e0a9bd..0000000 --- a/lib/triplex/plugs/ensure_plug_config.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Triplex.EnsurePlugConfig do - @moduledoc """ - This is a struct that holds the configuration for `Triplex.EnsurePlug`. - - Here are the config keys allowed: - - - `assign`: the name of the assign where we must save the tenant. - - `callback`: function that might be called when the plug succeeded. It - must return a connection. - - `failure_callback`: function that might be called when the plug failed. - It must return a connection. - """ - - defstruct [:callback, :failure_callback, assign: :current_tenant] -end diff --git a/lib/triplex/plugs/param_plug.ex b/lib/triplex/plugs/param_plug.ex deleted file mode 100644 index a873591..0000000 --- a/lib/triplex/plugs/param_plug.ex +++ /dev/null @@ -1,34 +0,0 @@ -if Code.ensure_loaded?(Plug) do - defmodule Triplex.ParamPlug do - @moduledoc """ - This is a basic plug that loads the current tenant assign from a given - param. - - To plug it on your router, you can use: - - plug Triplex.ParamPlug, - param: :subdomain, - tenant_handler: &TenantHelper.tenant_handler/1 - - See `Triplex.ParamPlugConfig` to check all the allowed `config` flags. - """ - - alias Triplex.ParamPlugConfig - alias Triplex.Plug - - @doc false - def init(opts), do: struct(ParamPlugConfig, opts) - - @doc false - def call(conn, config), do: Plug.put_tenant(conn, get_param(conn, config), config) - - defp get_param(conn, %ParamPlugConfig{param: key}), - do: get_param(conn, key) - - defp get_param(conn, key) when is_atom(key), - do: get_param(conn, Atom.to_string(key)) - - defp get_param(conn, key), - do: conn.params[key] - end -end diff --git a/lib/triplex/plugs/param_plug_config.ex b/lib/triplex/plugs/param_plug_config.ex deleted file mode 100644 index 8f31442..0000000 --- a/lib/triplex/plugs/param_plug_config.ex +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Triplex.ParamPlugConfig do - @moduledoc """ - This is a struct that holds all configuration for `Triplex.ParamPlug`. - - Here are the config keys allowed: - - - `tenant_handler`: function to handle the tenant param. Its return will - be used as the tenant. - - `assign`: the name of the assign where we must save the tenant. - - `param`: the param name to load the tenant from. - """ - - defstruct [:tenant_handler, assign: :current_tenant, param: "tenant"] -end diff --git a/lib/triplex/plugs/plug.ex b/lib/triplex/plugs/plug.ex deleted file mode 100644 index f0fcc4d..0000000 --- a/lib/triplex/plugs/plug.ex +++ /dev/null @@ -1,72 +0,0 @@ -if Code.ensure_loaded?(Plug) do - defmodule Triplex.Plug do - @moduledoc """ - This module have some basic functions for our triplex plugs. - - The plugs we have for now are: - - - `Triplex.ParamPlug` - loads the tenant from a body or query param - - `Triplex.SessionPlug` - loads the tenant from a session param - - `Triplex.SubdomainPlug` - loads the tenant from the url subdomain - - `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not - """ - - alias Plug.Conn - - @raw_tenant_assign :raw_current_tenant - - @doc """ - Puts the given `tenant` as an assign on the given `conn`, but only if the - tenant is not reserved. - - The `config` map/struct must have: - - - `tenant_handler`: function to handle the tenant param. Its return will - be used as the tenant. - - `assign`: the name of the assign where we must save the tenant. - """ - def put_tenant(conn, tenant, config) do - if conn.assigns[config.assign] do - conn - else - conn = Conn.assign(conn, @raw_tenant_assign, tenant) - tenant = tenant_handler(tenant, config.tenant_handler) - - if Triplex.reserved_tenant?(tenant) do - conn - else - Conn.assign(conn, config.assign, tenant) - end - end - end - - @doc """ - Ensure the tenant is loaded, and if not, halts the `conn`. - - The `config` map/struct must have: - - - `assign`: the name of the assign where we must save the tenant. - """ - def ensure_tenant(conn, config) do - if loaded_tenant = conn.assigns[config.assign] do - callback(conn, loaded_tenant, config.callback) - else - conn - |> callback(conn.assigns[@raw_tenant_assign], config.failure_callback) - |> Conn.halt() - end - end - - defp tenant_handler(tenant, nil), - do: tenant - - defp tenant_handler(tenant, handler) when is_function(handler), - do: handler.(tenant) - - defp callback(conn, _, nil), - do: conn - - defp callback(conn, tenant, callback) when is_function(callback), - do: callback.(conn, tenant) - end -end diff --git a/lib/triplex/plugs/session_plug.ex b/lib/triplex/plugs/session_plug.ex deleted file mode 100644 index 1e7efd1..0000000 --- a/lib/triplex/plugs/session_plug.ex +++ /dev/null @@ -1,29 +0,0 @@ -if Code.ensure_loaded?(Plug) do - defmodule Triplex.SessionPlug do - @moduledoc """ - This is a basic plug that loads the current tenant assign from a given - value set on session. - - To plug it on your router, you can use: - - plug Triplex.SessionPlug, - session: :subdomain, - tenant_handler: &TenantHelper.tenant_handler/1 - - See `Triplex.SessionPlugConfig` to check all the allowed `config` flags. - """ - - alias Plug.Conn - - alias Triplex.SessionPlugConfig - alias Triplex.Plug - - @doc false - def init(opts), do: struct(SessionPlugConfig, opts) - - @doc false - def call(conn, config) do - Plug.put_tenant(conn, Conn.get_session(conn, config.session), config) - end - end -end diff --git a/lib/triplex/plugs/session_plug_config.ex b/lib/triplex/plugs/session_plug_config.ex deleted file mode 100644 index 0a76ffe..0000000 --- a/lib/triplex/plugs/session_plug_config.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Triplex.SessionPlugConfig do - @moduledoc """ - This is a struct that holds the configuration for `Triplex.SessionPlug`. - - Here are the config keys allowed: - - - `tenant_handler`: function to handle the tenant param. Its return will - be used as the tenant. - - `assign`: the name of the assign where we must save the tenant. - - `session`: the session param name to load the tenant from. - """ - - defstruct [ - :tenant_handler, - assign: :current_tenant, - session: :tenant - ] -end diff --git a/lib/triplex/plugs/subdomain_plug.ex b/lib/triplex/plugs/subdomain_plug.ex deleted file mode 100644 index 249b83b..0000000 --- a/lib/triplex/plugs/subdomain_plug.ex +++ /dev/null @@ -1,44 +0,0 @@ -if Code.ensure_loaded?(Plug) do - defmodule Triplex.SubdomainPlug do - @moduledoc """ - This is a basic plug that loads the current tenant assign from a given - value set on subdomain. - - To plug it on your router, you can use: - - plug Triplex.SubdomainPlug, - endpoint: MyApp.Endpoint, - tenant_handler: &TenantHelper.tenant_handler/1 - - See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags. - """ - - alias Plug.Conn - - alias Triplex.SubdomainPlugConfig - alias Triplex.Plug - - @doc false - def init(opts), do: struct(SubdomainPlugConfig, opts) - - @doc false - def call(conn, config), do: Plug.put_tenant(conn, get_subdomain(conn, config), config) - - defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do - nil - end - - defp get_subdomain( - %Conn{host: host}, - %SubdomainPlugConfig{endpoint: endpoint} - ) do - root_host = endpoint.config(:url)[:host] - - if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do - nil - else - String.replace(host, ~r/.?#{root_host}/, "") - end - end - end -end diff --git a/lib/triplex/plugs/subdomain_plug_config.ex b/lib/triplex/plugs/subdomain_plug_config.ex deleted file mode 100644 index 08784c5..0000000 --- a/lib/triplex/plugs/subdomain_plug_config.ex +++ /dev/null @@ -1,19 +0,0 @@ -defmodule Triplex.SubdomainPlugConfig do - @moduledoc """ - This is a struct that holds the configuration for `Triplex.SubdomainPlug`. - - Here are the config keys allowed: - - - `tenant_handler`: function to handle the tenant param. Its return will - be used as the tenant. - - `assign`: the name of the assign where we must save the tenant. - - `endpoint`: the Phoenix.Endpoint to get the host name to dicover the - subdomain. - """ - - defstruct [ - :endpoint, - :tenant_handler, - assign: :current_tenant - ] -end diff --git a/test/triplex/plugs/ensure_plug_test.exs b/test/triplex/plugs/ensure_plug_test.exs deleted file mode 100644 index 6e79724..0000000 --- a/test/triplex/plugs/ensure_plug_test.exs +++ /dev/null @@ -1,32 +0,0 @@ -defmodule Triplex.EnsurePlugTest do - use ExUnit.Case - - import Plug.Test - alias Triplex.EnsurePlug - - test "call/2 calls callback on success" do - callback = fn conn, _ -> Plug.Conn.assign(conn, :lala, "lolo") end - - conn = - :get - |> conn("/") - |> Plug.Conn.assign(:current_tenant, "lele") - |> EnsurePlug.call(EnsurePlug.init(callback: callback)) - - assert conn.assigns[:current_tenant] == "lele" - assert conn.assigns[:lala] == "lolo" - end - - test "call/2 call failure callback on fail" do - callback = fn conn, _ -> Plug.Conn.assign(conn, :lele, "lili") end - - conn = - :get - |> conn("/", lol: "") - |> EnsurePlug.call(EnsurePlug.init(failure_callback: callback)) - - assert conn.assigns[:current_tenant] == nil - assert conn.assigns[:lele] == "lili" - assert conn.halted == true - end -end diff --git a/test/triplex/plugs/param_plug_test.exs b/test/triplex/plugs/param_plug_test.exs deleted file mode 100644 index 104d1e8..0000000 --- a/test/triplex/plugs/param_plug_test.exs +++ /dev/null @@ -1,35 +0,0 @@ -defmodule Triplex.ParamPlugTest do - use ExUnit.Case - - import Plug.Test - alias Triplex.ParamPlug - - test "call/2 must set the tenant to assign" do - conn = - :get - |> conn("/", tenant: "oi") - |> ParamPlug.call(ParamPlug.init([])) - - assert conn.assigns[:current_tenant] == "oi" - end - - test "call/2 must call the tenant handler to the a good tenant" do - handler = fn "oi" -> "olá" end - - conn = - :get - |> conn("/", tenant: "oi") - |> ParamPlug.call(ParamPlug.init(tenant_handler: handler)) - - assert conn.assigns[:current_tenant] == "olá" - end - - test "call/2 must read from the given param and write in the given assign" do - conn = - :get - |> conn("/", ten: "tchau") - |> ParamPlug.call(ParamPlug.init(param: :ten, assign: :tenant)) - - assert conn.assigns[:tenant] == "tchau" - end -end diff --git a/test/triplex/plugs/plug_test.exs b/test/triplex/plugs/plug_test.exs deleted file mode 100644 index 1ab5079..0000000 --- a/test/triplex/plugs/plug_test.exs +++ /dev/null @@ -1,90 +0,0 @@ -defmodule Triplex.PlugTest do - use ExUnit.Case - - import Plug.Conn - import Plug.Test - alias Triplex.Plug - alias Triplex.ParamPlugConfig - alias Triplex.EnsurePlugConfig - - test "put_tenant/3 must set the tenant to the default assign" do - conn = - :get - |> conn("/") - |> Plug.put_tenant("power", %ParamPlugConfig{}) - - assert conn.assigns[:current_tenant] == "power" - end - - test "put_tenant/3 must call the handler" do - handler = fn "oi" -> "olá" end - - conn = - :get - |> conn("/") - |> Plug.put_tenant("oi", %ParamPlugConfig{tenant_handler: handler}) - - assert conn.assigns[:current_tenant] == "olá" - end - - test "put_tenant/3 must set the tenant on the given assign" do - conn = - :get - |> conn("/") - |> Plug.put_tenant("power", %ParamPlugConfig{assign: :tenant}) - - assert conn.assigns[:tenant] == "power" - end - - test "put_tenant/3 must not set the tenant if it is already set" do - conn = - :get - |> conn("/") - |> assign(:current_tenant, "already_set") - |> Plug.put_tenant("power", %ParamPlugConfig{}) - - assert conn.assigns[:current_tenant] == "already_set" - end - - test "put_tenant/3 must not set the tenant if it is reserved" do - conn = - :get - |> conn("/") - |> Plug.put_tenant("www", %ParamPlugConfig{}) - - assert conn.assigns[:current_tenant] == nil - end - - test "ensure_tenant/3 must halts the conn" do - conn = - :get - |> conn("/") - |> Plug.put_tenant("power", %ParamPlugConfig{}) - |> Plug.ensure_tenant(%EnsurePlugConfig{}) - - assert conn.halted == false - end - - test "ensure_tenant/3 must call the success callback" do - callback = fn conn, _ -> assign(conn, :test, "blag") end - - conn = - :get - |> conn("/") - |> Plug.put_tenant("power", %ParamPlugConfig{}) - |> Plug.ensure_tenant(%EnsurePlugConfig{callback: callback}) - - assert conn.assigns[:test] == "blag" - end - - test "ensure_tenant/3 must call the failure callback" do - callback = fn conn, _ -> assign(conn, :test, "blog") end - - conn = - :get - |> conn("/") - |> Plug.ensure_tenant(%EnsurePlugConfig{failure_callback: callback}) - - assert conn.assigns[:test] == "blog" - end -end diff --git a/test/triplex/plugs/session_plug_test.exs b/test/triplex/plugs/session_plug_test.exs deleted file mode 100644 index cd7561c..0000000 --- a/test/triplex/plugs/session_plug_test.exs +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Triplex.SessionPlugTest do - use ExUnit.Case - - import Plug.Test - alias Triplex.SessionPlug - - test "call/2 must set the tenant to the default assign" do - conn = - :get - |> conn("/") - |> init_test_session(%{tenant: "oi"}) - |> SessionPlug.call(SessionPlug.init([])) - - assert conn.assigns[:current_tenant] == "oi" - end - - test "call/2 must call the handler to get a good tenant" do - handler = fn "oi" -> "olá" end - - conn = - :get - |> conn("/") - |> init_test_session(%{tenant: "oi"}) - |> SessionPlug.call(SessionPlug.init(tenant_handler: handler)) - - assert conn.assigns[:current_tenant] == "olá" - end - - test "call/2 must read from the given session and writer to the given assign" do - conn = - :get - |> conn("/") - |> init_test_session(%{ten: "tchau"}) - |> SessionPlug.call(SessionPlug.init(session: :ten, assign: :tenant)) - - assert conn.assigns[:tenant] == "tchau" - end -end diff --git a/test/triplex/plugs/subdomain_plug_test.exs b/test/triplex/plugs/subdomain_plug_test.exs deleted file mode 100644 index 4eb2e6e..0000000 --- a/test/triplex/plugs/subdomain_plug_test.exs +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Triplex.SubdomainPlugTest do - use ExUnit.Case - - import Plug.Test - alias Triplex.SubdomainPlug - alias Triplex.FakeEndpoint - - test "call/2 must set the tenant" do - conn = - :get - |> conn("/") - |> (&%{&1 | host: "oi.lvh.me"}).() - |> SubdomainPlug.call(SubdomainPlug.init(endpoint: FakeEndpoint)) - - assert conn.assigns[:current_tenant] == "oi" - end - - test "call/2 must call the handler" do - handler = fn "oi" -> "olá" end - - conn = - :get - |> conn("/") - |> (&%{&1 | host: "oi.lvh.me"}).() - |> SubdomainPlug.call(SubdomainPlug.init(endpoint: FakeEndpoint, tenant_handler: handler)) - - assert conn.assigns[:current_tenant] == "olá" - end -end From b50f723c563ca4a3ccf40d336a7f2ad96f741f75 Mon Sep 17 00:00:00 2001 From: Mauricio Giacomini Girardello Date: Sat, 8 Jun 2019 15:17:16 -0300 Subject: [PATCH 2/2] Remove plug dependency --- mix.exs | 1 - 1 file changed, 1 deletion(-) diff --git a/mix.exs b/mix.exs index cd38b7b..7413a55 100644 --- a/mix.exs +++ b/mix.exs @@ -53,7 +53,6 @@ defmodule Triplex.Mixfile do {:excoveralls, "~> 0.10", only: :test}, {:inch_ex, ">= 0.0.0", only: :docs}, {:mariaex, "~> 0.9.0", optional: true}, - {:plug, "~> 1.6", optional: true}, {:postgrex, ">= 0.14.0", optional: true} ] end