Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple sender validation #44

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/keila/mailings/mailings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ defmodule Keila.Mailings do
:ok
end

@doc """
Tests if emails can be sent from Sender credentials with given ID.
"""
@spec try_credentials(Sender.id()) :: {:ok, Sender.t()} | {:error, term()}
def try_credentials(id) when is_id(id) do
sender = get_sender(id)

email = %Swoosh.Email{
to: [{"", "[email protected]"}],
from: {sender.from_name, sender.from_email},
text_body: "Testing sender #{sender.id}"
}

case Keila.Mailer.deliver(email, Sender.Config.to_swoosh_config(sender.config)) do
{:ok, _} -> {:ok, sender}
{:error, reason} -> {:error, reason}
end
end

@doc """
Retrieves Campaign with given `id`.
"""
Expand Down
53 changes: 35 additions & 18 deletions lib/keila_web/controllers/sender_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ defmodule KeilaWeb.SenderController do
|> render("index.html")
end

@spec edit(Plug.Conn.t(), map()) :: Plug.Conn.t()
def edit(conn, _params) do
conn
|> put_meta(:title, conn.assigns.sender.name)
|> render_edit(Ecto.Changeset.change(conn.assigns.sender))
end

@spec post_edit(Plug.Conn.t(), map()) :: Plug.Conn.t()
def post_edit(conn, params = %{"id" => id}) do
project = current_project(conn)

case Mailings.update_sender(id, params["sender"] || %{}) do
{:ok, _sender} -> redirect(conn, to: Routes.sender_path(conn, :index, project.id))
{:error, changeset} -> render_edit(conn, 400, changeset)
end
end

@spec new(Plug.Conn.t(), map()) :: Plug.Conn.t()
def new(conn, _) do
changeset =
Expand All @@ -47,7 +30,22 @@ defmodule KeilaWeb.SenderController do
project = current_project(conn)

case Mailings.create_sender(project.id, params) do
{:ok, _} -> redirect(conn, to: Routes.sender_path(conn, :index, project.id))
{:ok, sender} -> try_credentials_and_redirect(conn, sender)
{:error, changeset} -> render_edit(conn, 400, changeset)
end
end

@spec edit(Plug.Conn.t(), map()) :: Plug.Conn.t()
def edit(conn, _params) do
conn
|> put_meta(:title, conn.assigns.sender.name)
|> render_edit(Ecto.Changeset.change(conn.assigns.sender))
end

@spec post_edit(Plug.Conn.t(), map()) :: Plug.Conn.t()
def post_edit(conn, params = %{"id" => id}) do
case Mailings.update_sender(id, params["sender"] || %{}) do
{:ok, sender} -> try_credentials_and_redirect(conn, sender)
{:error, changeset} -> render_edit(conn, 400, changeset)
end
end
Expand All @@ -59,6 +57,25 @@ defmodule KeilaWeb.SenderController do
|> render("edit.html")
end

defp try_credentials_and_redirect(conn, sender) do
project = current_project(conn)

case Mailings.try_credentials(sender.id) do
{:ok, _sender} ->
redirect(conn, to: Routes.sender_path(conn, :index, project.id))

_ ->
conn
|> put_flash(
:error,
gettext(
"Sender settings were saved but Keila was unable to send a test email with your provided credentials."
)
)
|> redirect(to: Routes.sender_path(conn, :index, project.id))
end
end

@spec delete(Plug.Conn.t(), any) :: Plug.Conn.t()
def delete(conn, _) do
sender = conn.assigns.sender
Expand Down
3 changes: 0 additions & 3 deletions lib/keila_web/controllers/template_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ defmodule KeilaWeb.TemplateController do

plug :authorize when not (action in [:index, :new, :post_new, :delete])

@default_text_body File.read!("priv/email_templates/default-text-content.txt")
@default_markdown_body File.read!("priv/email_templates/default-markdown-content.md")

@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, _params) do
templates = Templates.get_project_templates(current_project(conn).id)
Expand Down
3 changes: 1 addition & 2 deletions lib/keila_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ defmodule KeilaWeb.Endpoint do
at: "/",
from: :keila,
gzip: false,
only:
~w(css fonts images js favicon.ico robots.txt keila_import_template.csv keila_import_template.ods)
only: ~w(css fonts images js downloads favicon.ico robots.txt)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
Expand Down
2 changes: 1 addition & 1 deletion lib/keila_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p class="container py-5 my-0"><%= get_flash(@conn, :info) %></p>
</div>
<% end %>
<%= if get_flash(@conn, :errors) do %>
<%= if get_flash(@conn, :error) do %>
<div class="bg-yellow-200" role="alert">
<p class="container py-5 my-0"><%= get_flash(@conn, :error) %></p>
</div>
Expand Down
45 changes: 34 additions & 11 deletions test/keila/mailings/mailings_senders_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,50 @@ defmodule Keila.Mailings.SenderTest do
alias Keila.Mailings
alias Mailings.Sender

# Structurally valid but incorrect credentials
@smtp_params %{
"name" => "Sender",
"from_email" => "[email protected]",
"config" => %{
"type" => "smtp",
"smtp_username" => "user",
"smtp_password" => "password",
"smtp_relay" => "mail.example.com"
}
}

# Correct credentials
@test_params %{
"name" => "Sender 2",
"from_email" => "[email protected]",
"config" => %{
"type" => "test"
}
}

@tag :mailings
test "Create senders" do
group = insert!(:group)
project = insert!(:project, group: group)

{:ok, %Sender{}} =
Mailings.create_sender(project.id, %{
"name" => "Sender",
"from_email" => "[email protected]",
"config" => %{
"type" => "smtp",
"smtp_username" => "foo",
"smtp_password" => "foo",
"smtp_relay" => "example.com"
}
})
{:ok, %Sender{}} = Mailings.create_sender(project.id, @smtp_params)
end

test "Delete senders" do
end

test "Update senders" do
end

@tag :mailings
test "Try credentials" do
group = insert!(:group)
project = insert!(:project, group: group)

{:ok, %Sender{id: sender_id}} = Mailings.create_sender(project.id, @smtp_params)
assert {:error, _} = Mailings.try_credentials(sender_id)

{:ok, %Sender{id: sender_id}} = Mailings.create_sender(project.id, @test_params)
assert {:ok, _} = Mailings.try_credentials(sender_id)
end
end