Skip to content

Commit

Permalink
create and use Auth.PeopleRoles.list_people_roles/0 function to list …
Browse files Browse the repository at this point in the history
…all people_roles data #27 #31 #82 #90
  • Loading branch information
nelsonic committed Aug 20, 2020
1 parent 5a0d8bf commit 3ce9bab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
12 changes: 11 additions & 1 deletion lib/auth/people_roles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Auth.PeopleRoles do
"""
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Auth.Repo
# https://stackoverflow.com/a/47501059/1148249
alias __MODULE__
Expand All @@ -16,14 +17,23 @@ defmodule Auth.PeopleRoles do
timestamps()
end

@doc """
Returns the list of people_roles.
"""
def list_people_roles do
Repo.all(from pr in __MODULE__, preload: [:person, :role])
end



@doc """
grant_role/3 grants a role to the given person
the conn must have conn.assigns.person to check for admin in order to grant the role.
"""
def grant_role(conn, grantee_id, role_id) do
granter = conn.assigns.person
# IO.inspect(granter, label: "granter")
# confirm that the granter is either superadmin (conn.assigns.person.id == 1)
# confirm that the granter is either superadmin (conn.assigns.person.id == 1)
# or has an "admin" role (1 || 2)
if granter.id == 1 do
%PeopleRoles{}
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/permission.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Auth.Permission do
@moduledoc """
Defines Rermission schema and CRUD functions
Defines permissions schema and CRUD functions
"""
use Ecto.Schema
import Ecto.Changeset
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/role.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Auth.Role do
@moduledoc """
Defines Role schema and CRUD functions
Defines roles schema and CRUD functions
"""
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, warn: false
alias Auth.Repo
# https://stackoverflow.com/a/47501059/1148249
alias __MODULE__
alias __MODULE__

schema "roles" do
field :desc, :string
Expand Down
11 changes: 8 additions & 3 deletions test/auth/people_roles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ defmodule AuthWeb.PeopleRolesTest do
test "grant_role/3 happy path", %{conn: conn} do
# login as superadmin
conn = AuthTest.admin_login(conn)
# create a new person
# create a new person
alex = %{email: "[email protected]", auth_provider: "email"}
grantee = Auth.Person.create_person(alex)
role_id = 4
Auth.PeopleRoles.grant_role(conn, grantee.id, role_id)
person_with_role = Auth.Person.get_person_by_id(grantee.id)
person_with_role = Auth.Person.get_person_by_id(grantee.id) # |> IO.inspect()
role = List.first(person_with_role.roles)
assert role_id == role.id

# check the latest people_roles record:
pr = List.last(Auth.PeopleRoles.list_people_roles())
assert pr.granter_id == 1
assert pr.person_id == grantee.id

end

test "attempt to grant_role/3 without admin should 401", %{conn: conn} do
Expand All @@ -20,7 +26,6 @@ defmodule AuthWeb.PeopleRolesTest do
conn = assign(conn, :person, grantee)
role_id = 4
conn = Auth.PeopleRoles.grant_role(conn, grantee.id, role_id)

assert conn.status == 401
end
end
4 changes: 1 addition & 3 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ defmodule AuthWeb.ConnCase do
Ecto.Adapters.SQL.Sandbox.mode(Auth.Repo, {:shared, self()})
end

conn =
Phoenix.ConnTest.build_conn()
|> Phoenix.ConnTest.init_test_session(%{})
conn = Phoenix.ConnTest.init_test_session(Phoenix.ConnTest.build_conn, %{})

# invoke Plug.Test.init_test_session/2 to setup the test session
# before attempting to set a JWT. see:
Expand Down

0 comments on commit 3ce9bab

Please sign in to comment.