diff --git a/config/test.exs b/config/test.exs index 55f96f8..6f3c5c7 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/lib/infinity_one/permissions/permissions.ex b/lib/infinity_one/permissions/permissions.ex index f4446e8..0c3c331 100644 --- a/lib/infinity_one/permissions/permissions.ex +++ b/lib/infinity_one/permissions/permissions.ex @@ -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 @@ -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 @@ -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 diff --git a/mix.exs b/mix.exs index b90a3cc..562a795 100644 --- a/mix.exs +++ b/mix.exs @@ -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"}, @@ -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}, @@ -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"} @@ -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 diff --git a/plugins/one_chat/test/ucc_chat/accounts/accounts_test.exs b/plugins/one_chat/test/ucc_chat/accounts/accounts_test.exs index 34726be..f8c41de 100644 --- a/plugins/one_chat/test/ucc_chat/accounts/accounts_test.exs +++ b/plugins/one_chat/test/ucc_chat/accounts/accounts_test.exs @@ -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 diff --git a/test/infinity_one/permissions/permissions_test.exs b/test/infinity_one/permissions/permissions_test.exs index 23cb341..91ed1f7 100644 --- a/test/infinity_one/permissions/permissions_test.exs +++ b/test/infinity_one/permissions/permissions_test.exs @@ -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 @@ -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( @@ -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 diff --git a/test/support/test_helpers.ex b/test/support/test_helpers.ex index 514d78f..c69920e 100644 --- a/test/support/test_helpers.ex +++ b/test/support/test_helpers.ex @@ -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