Skip to content
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
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
erlang 25.3.2.15
elixir 1.14.5
7 changes: 4 additions & 3 deletions lib/ja_resource/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ defmodule JaResource.Repo do
@doc false
def default_repo do
quote do
if Application.get_env(:ja_resource, :repo) do
def repo, do: Application.get_env(:ja_resource, :repo)
defoverridable [repo: 0]
@ja_resource_repo Application.compile_env(:ja_resource, :repo)
if @ja_resource_repo do
Comment on lines +28 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i think harder I could figure it out but easier to ask: why compile vs get?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the issue with elixir 1.14. it doesn't want you to use fetch_env/get_env when you are outside of a function body because the value should be defined before compile time.

def repo, do: @ja_resource_repo
defoverridable repo: 0
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions test/ja_resource/delete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ defmodule JaResource.DeleteTest do
use JaResource.Delete
def repo, do: JaResourceTest.Repo
def model, do: JaResourceTest.Post

def handle_delete(conn, record) do
case conn.assigns[:user] do
%{is_admin: true} -> super(conn, record)
_ -> send_resp(conn, 401, "ah ah ah")
_ -> send_resp(conn, 401, "ah ah ah")
end
end
end
Expand All @@ -43,13 +44,6 @@ defmodule JaResource.DeleteTest do
assert response.status == 204
end

test "failing on delete returns 422 if model fails on changeset validation" do
{:ok, post} = JaResourceTest.Repo.insert(%JaResourceTest.FailingOnDeletePost{id: 422})
conn = prep_conn(:delete, "/posts/#{post.id}", %{"id" => post.id})
response = Delete.call(FailingOnDeleteController, conn)
assert response.status == 422
end

test "custom implementation retuns 401 if not admin" do
{:ok, post} = JaResourceTest.Repo.insert(%JaResourceTest.Post{id: 401})
conn = prep_conn(:delete, "/posts/#{post.id}", %{"id" => post.id})
Expand All @@ -58,24 +52,30 @@ defmodule JaResource.DeleteTest do
end

test "custom implementation retuns 404 if no model" do
conn = prep_conn(:delete, "/posts/404", %{"id" => 404})
|> assign(:user, %{is_admin: true})
conn =
prep_conn(:delete, "/posts/404", %{"id" => 404})
|> assign(:user, %{is_admin: true})

response = Delete.call(CustomController, conn)
assert response.status == 404
end

test "custom implementation retuns 204 if record found" do
{:ok, post} = JaResourceTest.Repo.insert(%JaResourceTest.Post{id: 200})
conn = prep_conn(:delete, "/posts/#{post.id}", %{"id" => post.id})
|> assign(:user, %{is_admin: true})

conn =
prep_conn(:delete, "/posts/#{post.id}", %{"id" => post.id})
|> assign(:user, %{is_admin: true})

response = Delete.call(DefaultController, conn)
assert response.status == 204
end

def prep_conn(method, path, params \\ %{}) do
params = Map.merge(params, %{"_format" => "json"})

conn(method, path, params)
|> fetch_query_params
|> Phoenix.Controller.put_view(JaResourceTest.PostView)
|> fetch_query_params
|> Phoenix.Controller.put_view(JaResourceTest.PostView)
end
end