Skip to content

Commit

Permalink
fixed permissions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smpallen99 committed May 9, 2018
1 parent 9ea6988 commit d572432
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
10 changes: 9 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ config :logger, :console,
format: "\n$time [$level]$levelpad$metadata$message\n",
metadata: [:module, :function, :line]

# config :infinity_one, InfinityOne.Repo,
# adapter: Ecto.Adapters.Postgres,
# username: "postgres",
# password: "postgres",
# database: "infinity_one_test3",
# hostname: "localhost",
# pool: Ecto.Adapters.SQL.Sandbox

if File.exists? "config/test.secret.exs" do
import_config "prod.secret.exs"
import_config "test.secret.exs"
end
29 changes: 22 additions & 7 deletions lib/infinity_one/permissions/permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ defmodule InfinityOne.Permissions do
GenServer.cast @name, :initialize
end

def initialize(permissions_list) do
GenServer.cast(@name, {:initialize, permissions_list})
end

def delete_all_objects do
GenServer.cast @name, :delete_all_objects
end
Expand Down Expand Up @@ -102,13 +106,8 @@ defmodule InfinityOne.Permissions do

def init_state, do: %{permissions: %{}, roles: %{}}

#################
# Casts

def handle_cast(:initialize, state) do
add_missing_permissions()

Enum.reduce(list_permissions(), state, fn %{name: permission, roles: roles}, acc ->
defp initialize_permissions(permissions_list, state) do
Enum.reduce(permissions_list, state, fn %{name: permission, roles: roles}, acc ->
roles = Enum.map(roles, &(&1.name))
Enum.reduce(roles, put_in(acc, [:permissions, permission], roles), fn role, acc ->
update_in(acc, [:roles, role], fn
Expand All @@ -117,6 +116,22 @@ defmodule InfinityOne.Permissions do
end)
end)
end)
end

#################
# Casts

def handle_cast(:initialize, state) do
add_missing_permissions()

list_permissions()
|> initialize_permissions(state)
|> noreply
end

def handle_cast({:initialize, permissions_list}, state) do
permissions_list
|> initialize_permissions(state)
|> noreply
end

Expand Down
14 changes: 8 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ defmodule InfinityOne.Mixfile do
# {:phoenix, "~> 1.3.0-rc"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
{:mariaex, ">= 0.0.0", only: [:dev, :prod], override: true},
# {:mariaex, ">= 0.0.0", only: [:dev, :prod], override: true},
{:mariaex, ">= 0.0.0", override: true},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.13"},
Expand Down Expand Up @@ -109,7 +110,7 @@ defmodule InfinityOne.Mixfile do
{:slime, github: "smpallen99/slime", override: true},
# {:slime, "~> 1.0", override: true},
{:inflex, "~> 1.8"},
{:postgrex, ">= 0.0.0", only: [:test]},
# {:postgrex, ">= 0.0.0", only: [:test]},
# {:rebel, path: "../rebel"},
{:rebel, github: "smpallen99/rebel"},
{:exactor, "~> 2.2", override: true},
Expand All @@ -118,7 +119,8 @@ defmodule InfinityOne.Mixfile do
{:phoenix_markdown, "~> 1.0"},
{:distillery, "~> 1.4"},
{:conform, "~> 2.5"},
{:ex_syslogger, github: "smpallen99/ex_syslogger", only: [:prod]},
# {:ex_syslogger, github: "smpallen99/ex_syslogger", only: [:prod]},
{:ex_syslogger, github: "smpallen99/ex_syslogger"},
{:gen_smtp, "~> 0.12.0"},
{:exprof, "~> 0.2.0"},
# {:scrivener_ecto, path: "../scrivener_ecto"}
Expand All @@ -141,11 +143,11 @@ defmodule InfinityOne.Mixfile do
"ecto.setup": ["ecto.create", "unbrella.migrate", "unbrella.seed"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
commit: ["deps.get --only #{Mix.env()}", "dialyzer", "credo --strict"],
test: ["ecto.create --quiet", "unbrella.migrate", "test", "unbrella.test"]
# test: ["ecto.create --quiet", "unbrella.migrate", "test", "unbrella.test"]
# # Use the following option if you want to run specific test files
"test": ["ecto.create --quiet", "unbrella.migrate", "test"]
]

# # Use the following option if you want to run specific test files
# "test": ["ecto.create --quiet", "unbrella.migrate", "test"]]
end

defp plugin_deps do
Expand Down
3 changes: 2 additions & 1 deletion plugins/one_chat/test/ucc_chat/accounts/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule OneChat.AccountsTest do
setup do
Helpers.insert_roles()
user = Helpers.insert_user()
{:ok, user: user, account: Helpers.insert_account(user)}
# {:ok, user: user, account: Helpers.insert_account(user)}
{:ok, user: user, account: user.account}
end

test "gets_by_user_id", %{user: user, account: account} do
Expand Down
18 changes: 10 additions & 8 deletions test/infinity_one/permissions/permissions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule InfinityOne.PermissionsTest do
%{name: "perm-4", roles: ["owner", "admin"] },
]
roles = Accounts.create_roles [admin: :global, owner: :rooms, user: :global]
Permissions.create_permissions permissions, roles
Permissions.initialize()
Permissions.create_permissions(permissions, roles)
Permissions.initialize(Permissions.list_permissions())
:ok
end

Expand All @@ -35,20 +35,22 @@ defmodule InfinityOne.PermissionsTest do
test "lists permissions" do
{:ok, _} = Permissions.create_permission(%{name: "perm-one"})
{:ok, _} = Permissions.create_permission(%{name: "perm-one-1"})
[p1, p2] = Permissions.list_permissions()
refute p1.name == p2.name
assert p1.name in ~w(perm-one perm-one-1)
assert p2.name in ~w(perm-one perm-one-1)
permissions = Permissions.list_permissions()
assert Enum.find(permissions, & &1.name == "perm-one")
assert Enum.find(permissions, & &1.name == "perm-one-1")
end
test "delete permissions" do
pre_count = length(Permissions.list_permissions())
{:ok, perm} = Permissions.create_permission(%{name: "perm-one"})
assert length(Permissions.list_permissions()) == pre_count + 1
{:ok, _} = Permissions.delete_permission(perm)
assert Permissions.list_permissions() == []
assert length(Permissions.list_permissions()) == pre_count
end
end

describe "permission_role" do
test "creates and deletes a PermissionRole" do
pre_count = length(Permissions.list_permission_roles())
{:ok, perm} = Permissions.create_permission(%{name: "perm-one"})
{:ok, role} = Accounts.create_role %{name: "user", scope: "global"}
{:ok, pr} = Permissions.create_permission_role(
Expand All @@ -58,7 +60,7 @@ defmodule InfinityOne.PermissionsTest do
assert pr.permission.name == "perm-one"

{:ok, _} = Permissions.delete_permission_role(pr)
assert Permissions.list_permission_roles() == []
assert length(Permissions.list_permission_roles()) == pre_count
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/support/test_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule InfinityOne.TestHelpers do
|> UserRole.changeset(%{user_id: user.id, role_id: role.id})
|> Repo.insert!

Repo.preload user, [roles: :permissions]
Repo.preload(user, [:account, :roles, user_roles: :role])
end

def insert_account(user, attrs \\ %{}) do
Expand Down

0 comments on commit d572432

Please sign in to comment.