diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:13:54 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:13:54 -0600 |
commit | 2055742911201258e6f755b3eb4031a1b09407f1 (patch) | |
tree | a8e0471cab55329e2e00b5d3e2011d37bb67fdb6 /test/support | |
download | aggiedit-2055742911201258e6f755b3eb4031a1b09407f1.tar.gz aggiedit-2055742911201258e6f755b3eb4031a1b09407f1.zip |
Initial commit; generate auth code with phx.gen.auth; added room model and association; generate room model on domain of user emails; allow users to change their email
Diffstat (limited to 'test/support')
-rw-r--r-- | test/support/channel_case.ex | 36 | ||||
-rw-r--r-- | test/support/conn_case.ex | 65 | ||||
-rw-r--r-- | test/support/data_case.ex | 51 | ||||
-rw-r--r-- | test/support/fixtures/accounts_fixtures.ex | 31 | ||||
-rw-r--r-- | test/support/fixtures/rooms_fixtures.ex | 20 |
5 files changed, 203 insertions, 0 deletions
diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex new file mode 100644 index 0000000..3afe09b --- /dev/null +++ b/test/support/channel_case.ex @@ -0,0 +1,36 @@ +defmodule AggieditWeb.ChannelCase do + @moduledoc """ + This module defines the test case to be used by + channel tests. + + Such tests rely on `Phoenix.ChannelTest` and also + import other functionality to make it easier + to build common data structures and query the data layer. + + Finally, if the test case interacts with the database, + we enable the SQL sandbox, so changes done to the database + are reverted at the end of every test. If you are using + PostgreSQL, you can even run database tests asynchronously + by setting `use AggieditWeb.ChannelCase, async: true`, although + this option is not recommended for other databases. + """ + + use ExUnit.CaseTemplate + + using do + quote do + # Import conveniences for testing with channels + import Phoenix.ChannelTest + import AggieditWeb.ChannelCase + + # The default endpoint for testing + @endpoint AggieditWeb.Endpoint + end + end + + setup tags do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Aggiedit.Repo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + :ok + end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex new file mode 100644 index 0000000..03f5170 --- /dev/null +++ b/test/support/conn_case.ex @@ -0,0 +1,65 @@ +defmodule AggieditWeb.ConnCase do + @moduledoc """ + This module defines the test case to be used by + tests that require setting up a connection. + + Such tests rely on `Phoenix.ConnTest` and also + import other functionality to make it easier + to build common data structures and query the data layer. + + Finally, if the test case interacts with the database, + we enable the SQL sandbox, so changes done to the database + are reverted at the end of every test. If you are using + PostgreSQL, you can even run database tests asynchronously + by setting `use AggieditWeb.ConnCase, async: true`, although + this option is not recommended for other databases. + """ + + use ExUnit.CaseTemplate + + using do + quote do + # Import conveniences for testing with connections + import Plug.Conn + import Phoenix.ConnTest + import AggieditWeb.ConnCase + + alias AggieditWeb.Router.Helpers, as: Routes + + # The default endpoint for testing + @endpoint AggieditWeb.Endpoint + end + end + + setup tags do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Aggiedit.Repo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + {:ok, conn: Phoenix.ConnTest.build_conn()} + end + + @doc """ + Setup helper that registers and logs in users. + + setup :register_and_log_in_user + + It stores an updated connection and a registered user in the + test context. + """ + def register_and_log_in_user(%{conn: conn}) do + user = Aggiedit.AccountsFixtures.user_fixture() + %{conn: log_in_user(conn, user), user: user} + end + + @doc """ + Logs the given `user` into the `conn`. + + It returns an updated `conn`. + """ + def log_in_user(conn, user) do + token = Aggiedit.Accounts.generate_user_session_token(user) + + conn + |> Phoenix.ConnTest.init_test_session(%{}) + |> Plug.Conn.put_session(:user_token, token) + end +end diff --git a/test/support/data_case.ex b/test/support/data_case.ex new file mode 100644 index 0000000..7718d18 --- /dev/null +++ b/test/support/data_case.ex @@ -0,0 +1,51 @@ +defmodule Aggiedit.DataCase do + @moduledoc """ + This module defines the setup for tests requiring + access to the application's data layer. + + You may define functions here to be used as helpers in + your tests. + + Finally, if the test case interacts with the database, + we enable the SQL sandbox, so changes done to the database + are reverted at the end of every test. If you are using + PostgreSQL, you can even run database tests asynchronously + by setting `use Aggiedit.DataCase, async: true`, although + this option is not recommended for other databases. + """ + + use ExUnit.CaseTemplate + + using do + quote do + alias Aggiedit.Repo + + import Ecto + import Ecto.Changeset + import Ecto.Query + import Aggiedit.DataCase + end + end + + setup tags do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Aggiedit.Repo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + :ok + end + + @doc """ + A helper that transforms changeset errors into a map of messages. + + assert {:error, changeset} = Accounts.create_user(%{password: "short"}) + assert "password is too short" in errors_on(changeset).password + assert %{password: ["password is too short"]} = errors_on(changeset) + + """ + def errors_on(changeset) do + Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> + Regex.replace(~r"%{(\w+)}", message, fn _, key -> + opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() + end) + end) + end +end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex new file mode 100644 index 0000000..0ce0fdf --- /dev/null +++ b/test/support/fixtures/accounts_fixtures.ex @@ -0,0 +1,31 @@ +defmodule Aggiedit.AccountsFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Aggiedit.Accounts` context. + """ + + def unique_user_email, do: "user#{System.unique_integer()}@example.com" + def valid_user_password, do: "hello world!" + + def valid_user_attributes(attrs \\ %{}) do + Enum.into(attrs, %{ + email: unique_user_email(), + password: valid_user_password() + }) + end + + def user_fixture(attrs \\ %{}) do + {:ok, user} = + attrs + |> valid_user_attributes() + |> Aggiedit.Accounts.register_user() + + user + end + + def extract_user_token(fun) do + {:ok, captured_email} = fun.(&"[TOKEN]#{&1}[TOKEN]") + [_, token | _] = String.split(captured_email.text_body, "[TOKEN]") + token + end +end diff --git a/test/support/fixtures/rooms_fixtures.ex b/test/support/fixtures/rooms_fixtures.ex new file mode 100644 index 0000000..fd5671e --- /dev/null +++ b/test/support/fixtures/rooms_fixtures.ex @@ -0,0 +1,20 @@ +defmodule Aggiedit.RoomsFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Aggiedit.Rooms` context. + """ + + @doc """ + Generate a room. + """ + def room_fixture(attrs \\ %{}) do + {:ok, room} = + attrs + |> Enum.into(%{ + domain: "some domain" + }) + |> Aggiedit.Rooms.create_room() + + room + end +end |