From 17fdc7085e640cd1b6ae28242a49388d85962f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Tue, 12 Jan 2021 09:01:38 +0100 Subject: [PATCH] Fixing remaining auth tests --- lib/recycledcloud/accounts.ex | 2 +- .../controllers/user_confirmation_controller_test.exs | 8 ++++---- .../controllers/user_registration_controller_test.exs | 8 ++++---- .../controllers/user_session_controller_test.exs | 11 +++++------ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/recycledcloud/accounts.ex b/lib/recycledcloud/accounts.ex index 089a036..9a95a10 100644 --- a/lib/recycledcloud/accounts.ex +++ b/lib/recycledcloud/accounts.ex @@ -377,7 +377,7 @@ defmodule RecycledCloud.Accounts do changeset = user |> User.password_changeset(attrs) - set_user_password(user, changeset, attrs["password"]) + set_user_password(user, changeset, changeset.changes.password) end ## Keys diff --git a/test/recycledcloud_web/controllers/user_confirmation_controller_test.exs b/test/recycledcloud_web/controllers/user_confirmation_controller_test.exs index daed384..3928b48 100644 --- a/test/recycledcloud_web/controllers/user_confirmation_controller_test.exs +++ b/test/recycledcloud_web/controllers/user_confirmation_controller_test.exs @@ -22,7 +22,7 @@ defmodule RecycledCloudWeb.UserConfirmationControllerTest do test "sends a new confirmation token", %{conn: conn, user: user} do conn = post(conn, Routes.user_confirmation_path(conn, :create), %{ - "user" => %{"email" => user.email} + "user" => %{"username" => user.username} }) assert redirected_to(conn) == "/" @@ -35,7 +35,7 @@ defmodule RecycledCloudWeb.UserConfirmationControllerTest do conn = post(conn, Routes.user_confirmation_path(conn, :create), %{ - "user" => %{"email" => user.email} + "user" => %{"username" => user.username} }) assert redirected_to(conn) == "/" @@ -43,10 +43,10 @@ defmodule RecycledCloudWeb.UserConfirmationControllerTest do refute Repo.get_by(Accounts.UserToken, user_id: user.id) end - test "does not send confirmation token if email is invalid", %{conn: conn} do + test "does not send confirmation token if usernameis invalid", %{conn: conn} do conn = post(conn, Routes.user_confirmation_path(conn, :create), %{ - "user" => %{"email" => "unknown@example.com"} + "user" => %{"username" => "unknown"} }) assert redirected_to(conn) == "/" diff --git a/test/recycledcloud_web/controllers/user_registration_controller_test.exs b/test/recycledcloud_web/controllers/user_registration_controller_test.exs index 5dc9b72..20330fb 100644 --- a/test/recycledcloud_web/controllers/user_registration_controller_test.exs +++ b/test/recycledcloud_web/controllers/user_registration_controller_test.exs @@ -21,11 +21,12 @@ defmodule RecycledCloudWeb.UserRegistrationControllerTest do describe "POST /users/register" do @tag :capture_log test "creates account and logs the user in", %{conn: conn} do + username = unique_user_name() email = unique_user_email() conn = post(conn, Routes.user_registration_path(conn, :create), %{ - "user" => %{"email" => email, "password" => valid_user_password()} + "user" => %{"username" => username, "email" => email, "password" => valid_user_password()} }) assert get_session(conn, :user_token) @@ -34,7 +35,6 @@ defmodule RecycledCloudWeb.UserRegistrationControllerTest do # Now do a logged in request and assert on the menu conn = get(conn, "/") response = html_response(conn, 200) - assert response =~ email assert response =~ "Settings" assert response =~ "Log out" end @@ -42,13 +42,13 @@ defmodule RecycledCloudWeb.UserRegistrationControllerTest do test "render errors for invalid data", %{conn: conn} do conn = post(conn, Routes.user_registration_path(conn, :create), %{ - "user" => %{"email" => "with spaces", "password" => "too short"} + "user" => %{"username" => "with spaces", "email" => "with spaces", "password" => "too"} }) response = html_response(conn, 200) assert response =~ "

Register

" assert response =~ "must have the @ sign and no spaces" - assert response =~ "should be at least 12 character" + assert response =~ "should be at least 6 character" end end end diff --git a/test/recycledcloud_web/controllers/user_session_controller_test.exs b/test/recycledcloud_web/controllers/user_session_controller_test.exs index 2c77b5b..8e382e2 100644 --- a/test/recycledcloud_web/controllers/user_session_controller_test.exs +++ b/test/recycledcloud_web/controllers/user_session_controller_test.exs @@ -26,7 +26,7 @@ defmodule RecycledCloudWeb.UserSessionControllerTest do test "logs the user in", %{conn: conn, user: user} do conn = post(conn, Routes.user_session_path(conn, :create), %{ - "user" => %{"email" => user.email, "password" => valid_user_password()} + "user" => %{"username" => user.username, "password" => valid_user_password()} }) assert get_session(conn, :user_token) @@ -35,7 +35,6 @@ defmodule RecycledCloudWeb.UserSessionControllerTest do # Now do a logged in request and assert on the menu conn = get(conn, "/") response = html_response(conn, 200) - assert response =~ user.email assert response =~ "Settings" assert response =~ "Log out" end @@ -44,7 +43,7 @@ defmodule RecycledCloudWeb.UserSessionControllerTest do conn = post(conn, Routes.user_session_path(conn, :create), %{ "user" => %{ - "email" => user.email, + "username" => user.username, "password" => valid_user_password(), "remember_me" => "true" } @@ -60,7 +59,7 @@ defmodule RecycledCloudWeb.UserSessionControllerTest do |> init_test_session(user_return_to: "/foo/bar") |> post(Routes.user_session_path(conn, :create), %{ "user" => %{ - "email" => user.email, + "username" => user.username, "password" => valid_user_password() } }) @@ -71,12 +70,12 @@ defmodule RecycledCloudWeb.UserSessionControllerTest do test "emits error message with invalid credentials", %{conn: conn, user: user} do conn = post(conn, Routes.user_session_path(conn, :create), %{ - "user" => %{"email" => user.email, "password" => "invalid_password"} + "user" => %{"username" => user.username, "password" => "invalid_password"} }) response = html_response(conn, 200) assert response =~ "

Log in

" - assert response =~ "Invalid email or password" + assert response =~ "Invalid username or password" end end