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

feat(api): API Endpoint to delete Alias #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions lib/shroud_web/controllers/api/v1/email_alias_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ defmodule ShroudWeb.Api.V1.EmailAliasController do
|> render("error.json", error: "Unable to create email alias")
end
end

def delete(conn, %{"address" => address}) do
alias =
EmailAlias
|> where([ea], is_nil(ea.deleted_at))
|> where([ea], ea.user_id == ^conn.assigns.current_user.id)
|> Repo.get_by(address: address)

if is_nil(alias) do
conn
|> put_status(422)
|> put_view(ShroudWeb.ErrorView)
|> render("error.json", error: "Alias not found")
else
Aliases.delete_email_alias(alias.id)

conn
|> send_resp(:no_content, "")
end
end
end
1 change: 1 addition & 0 deletions lib/shroud_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule ShroudWeb.Router do
pipe_through([:api, :require_confirmed_api_user])

resources("/aliases", EmailAliasController, only: [:index, :create])
delete("/aliases/:address", EmailAliasController, :delete)
resources("/domains", DomainController, only: [:index])
end

Expand Down
41 changes: 41 additions & 0 deletions test/shroud_web/controllers/api/v1/email_alias_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ defmodule ShroudWeb.Api.V1.EmailAliasControllerTest do
end
end

describe "delete/2" do
setup do
conn = build_conn()
user = user_fixture()

user =
user
|> Accounts.User.confirm_changeset()
|> Repo.update!(returning: true)

email_alias = alias_fixture(%{user_id: user.id})

%{conn: conn, user: user, address: email_alias.address}
end

test "deletes an email alias", %{conn: conn, user: user, address: address} do

conn = authorized_delete(conn, user, Routes.email_alias_path(conn, :delete, address))
assert response(conn, 204)
end

test "prevents deleting an email alias if user does not own it", %{conn: conn, user: user} do
other_user = user_fixture()
other_alias = alias_fixture(%{user_id: other_user.id})
conn =
authorized_delete(conn, user, Routes.email_alias_path(conn, :delete, other_alias.address))

assert json_response(conn, 422) == %{
"error" => "Alias not found"
}
end
end

defp authorized_get(conn, user, path, params \\ nil) do
token = Accounts.generate_user_session_token(user)

Expand All @@ -186,4 +219,12 @@ defmodule ShroudWeb.Api.V1.EmailAliasControllerTest do
|> put_req_header("authorization", "Bearer #{Base.encode64(token)}")
|> post(path, params)
end

defp authorized_delete(conn, user, path, params \\ nil) do
token = Accounts.generate_user_session_token(user)

conn
|> put_req_header("authorization", "Bearer #{Base.encode64(token)}")
|> delete(path, params)
end
end