memEx/test/memex_web/controllers/user_registration_controller_test.exs

54 lines
1.6 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserRegistrationControllerTest do
2022-02-25 21:53:15 -05:00
@moduledoc """
Tests user registration
"""
2022-07-25 19:31:54 -04:00
use MemexWeb.ConnCase, async: true
2021-03-11 21:12:55 -05:00
2022-02-25 21:53:15 -05:00
@moduletag :user_registration_controller_test
2021-03-11 21:12:55 -05:00
describe "GET /users/register" do
test "renders registration page", %{conn: conn} do
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/register")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "register"
assert response =~ "log in"
2021-03-11 21:12:55 -05:00
end
test "redirects if already logged in", %{conn: conn} do
2023-04-13 23:29:29 -04:00
conn = conn |> log_in_user(user_fixture()) |> get(~p"/users/register")
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2021-03-11 21:12:55 -05:00
end
end
describe "POST /users/register" do
@tag :capture_log
test "creates account and logs the user in", %{conn: conn} do
email = unique_user_email()
2023-04-14 20:08:03 -04:00
conn = post(conn, ~p"/users/register", %{user: valid_user_attributes(email: email)})
2021-03-11 21:12:55 -05:00
2022-02-25 21:53:15 -05:00
assert get_session(conn, :phoenix_flash) == %{
2023-03-22 22:08:37 -04:00
"info" => "please check your email to verify your account"
2022-02-25 21:53:15 -05:00
}
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) =~ ~p"/"
2023-04-15 21:40:57 -04:00
# Now do a logged in request and assert on the menu
conn = get(conn, ~p"/")
response = html_response(conn, 200)
# user's email is recorded as admin
assert response =~ email
2021-03-11 21:12:55 -05:00
end
test "render errors for invalid data", %{conn: conn} do
conn =
2023-04-14 20:08:03 -04:00
post(conn, ~p"/users/register", %{user: %{email: "with spaces", password: "too short"}})
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "register"
2021-03-11 21:12:55 -05:00
assert response =~ "must have the @ sign and no spaces"
assert response =~ "should be at least 12 character"
end
end
end