2020-12-15 14:24:14 +01:00
|
|
|
defmodule RecycledCloudWeb.UserRegistrationControllerTest do
|
|
|
|
use RecycledCloudWeb.ConnCase, async: true
|
|
|
|
|
|
|
|
import RecycledCloud.AccountsFixtures
|
|
|
|
|
|
|
|
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)
|
|
|
|
assert response =~ "<h1>Register</h1>"
|
|
|
|
assert response =~ "Log in</a>"
|
|
|
|
assert response =~ "Register</a>"
|
|
|
|
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
|
2021-01-12 09:01:38 +01:00
|
|
|
username = unique_user_name()
|
2020-12-15 14:24:14 +01:00
|
|
|
email = unique_user_email()
|
|
|
|
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_registration_path(conn, :create), %{
|
2021-01-12 09:01:38 +01:00
|
|
|
"user" => %{"username" => username, "email" => email, "password" => valid_user_password()}
|
2020-12-15 14:24:14 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
assert get_session(conn, :user_token)
|
|
|
|
assert redirected_to(conn) =~ "/"
|
|
|
|
|
|
|
|
# Now do a logged in request and assert on the menu
|
|
|
|
conn = get(conn, "/")
|
|
|
|
response = html_response(conn, 200)
|
|
|
|
assert response =~ "Settings</a>"
|
|
|
|
assert response =~ "Log out</a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "render errors for invalid data", %{conn: conn} do
|
|
|
|
conn =
|
|
|
|
post(conn, Routes.user_registration_path(conn, :create), %{
|
2021-01-12 09:01:38 +01:00
|
|
|
"user" => %{"username" => "with spaces", "email" => "with spaces", "password" => "too"}
|
2020-12-15 14:24:14 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
response = html_response(conn, 200)
|
|
|
|
assert response =~ "<h1>Register</h1>"
|
|
|
|
assert response =~ "must have the @ sign and no spaces"
|
2021-01-12 09:01:38 +01:00
|
|
|
assert response =~ "should be at least 6 character"
|
2020-12-15 14:24:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|