2021-03-11 21:12:55 -05:00
|
|
|
defmodule CanneryWeb.UserRegistrationControllerTest do
|
2022-02-16 21:07:37 -05:00
|
|
|
@moduledoc """
|
|
|
|
Tests user registration
|
|
|
|
"""
|
|
|
|
|
2021-03-11 21:12:55 -05:00
|
|
|
use CanneryWeb.ConnCase, async: true
|
|
|
|
|
2022-02-16 21:07:37 -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
|
|
|
|
conn = get(conn, Routes.user_registration_path(conn, :new))
|
|
|
|
response = html_response(conn, 200)
|
2023-03-28 21:55:49 -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
|
|
|
|
conn = conn |> log_in_user(user_fixture()) |> get(Routes.user_registration_path(conn, :new))
|
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
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()
|
|
|
|
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_registration_path(conn, :create), %{
|
2023-03-28 21:55:49 -04:00
|
|
|
user: valid_user_attributes(email: email)
|
2021-03-11 21:12:55 -05:00
|
|
|
})
|
|
|
|
|
2022-02-16 21:20:04 -05:00
|
|
|
assert get_session(conn, :phoenix_flash) == %{
|
2023-03-28 21:55:49 -04:00
|
|
|
"info" => "Please check your email to verify your account"
|
2022-02-16 21:20:04 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 21:12:55 -05:00
|
|
|
assert redirected_to(conn) =~ "/"
|
|
|
|
|
|
|
|
# Now do a logged in request and assert on the menu
|
|
|
|
conn = get(conn, "/")
|
|
|
|
response = html_response(conn, 200)
|
2022-02-16 21:07:37 -05:00
|
|
|
# user's email is recorded as admin
|
2021-03-11 21:12:55 -05:00
|
|
|
assert response =~ email
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render errors for invalid data", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_registration_path(conn, :create), %{
|
2023-03-28 21:55:49 -04:00
|
|
|
user: %{email: "with spaces", password: "too short"}
|
2021-03-11 21:12:55 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
response = html_response(conn, 200)
|
2023-03-28 21:55:49 -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
|