Skip to content

Commit

Permalink
Adding more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ghatighorias committed Jun 13, 2017
1 parent 7f5a08e commit d752bd0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ end

defimpl Canada.Can, for: User do

def can?(%User{}, action, Myproject.PartialAccessController)
when action in [:index, :show], do: true
def can?(%User{}, action, Myproject.PartialAccessController)
when action in [:new, :create, :update, :delete], do: false

def can?(%User{}, :index, Myproject.SampleController), do: true

def can?(%User{id: _user_id}, action, Myproject.SampleController)
Expand Down Expand Up @@ -1674,5 +1679,35 @@ defmodule PlugTest do
expected = Plug.Conn.assign(conn, :authorized, false)

assert authorize_controller(conn, opts) == expected

# when an action is restricted on a controller
params = %{"id" => 1}
conn = conn(
%Plug.Conn{
private: %{phoenix_controller: Myproject.PartialAccessController},
assigns: %{current_user: %User{id: 1}, canary_action: :new}
},
:post,
"/posts",
params
)
expected = Plug.Conn.assign(conn, :authorized, false)

assert authorize_controller(conn, opts) == expected

# when an action is authorized on a controller
params = %{"id" => 1}
conn = conn(
%Plug.Conn{
private: %{phoenix_controller: Myproject.PartialAccessController},
assigns: %{current_user: %User{id: 1}, canary_action: :show}
},
:post,
"/posts",
params
)
expected = Plug.Conn.assign(conn, :authorized, true)

assert authorize_controller(conn, opts) == expected
end
end

0 comments on commit d752bd0

Please sign in to comment.