From 06dfee5b577210d649cc6482a5bc4e83f67382bd Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 11 Aug 2023 15:38:00 +0300 Subject: [PATCH] Add tests for user login. --- CHANGELOG.org | 2 ++ .../api/v1/user_controller_test.exs | 31 ++++++++++++++++--- test/support/fixtures/accounts_fixtures.ex | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 16c517b..34ed999 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -61,3 +61,5 @@ Add the authentication for API sing in. ** 0.8.2 <2023-08-10 Thu> Add the auth pipeline for the Link API path. +** 0.8.3 <2023-08-11 Fri> + Add tests for user login. diff --git a/test/link_shortener_web/controllers/api/v1/user_controller_test.exs b/test/link_shortener_web/controllers/api/v1/user_controller_test.exs index 40a26e5..961cd6e 100644 --- a/test/link_shortener_web/controllers/api/v1/user_controller_test.exs +++ b/test/link_shortener_web/controllers/api/v1/user_controller_test.exs @@ -6,7 +6,7 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do alias LinkShortener.Accounts.User @create_attrs %{ - email: "some_email@mail.com", + email: "user@mail.com", password: "some password" } @@ -15,6 +15,11 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do password: "some updated password" } + @invalid_password_attrs %{ + email: "user@mail.com", + password: "" + } + @invalid_attrs %{ email: nil, encrypted_password: nil @@ -24,11 +29,11 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do {:ok, conn: put_req_header(conn, "accept", "application/json")} end - describe "create user" do + describe "create user with sign up" do test "renders user when data is valid", %{conn: conn} do conn = post(conn, Routes.v1_user_path(conn, :create), user: @create_attrs) assert %{ - "email" => "some_email@mail.com", + "email" => "user@mail.com", "token" => token } = json_response(conn, 201) end @@ -39,8 +44,26 @@ defmodule LinkShortenerWeb.Api.V1.UserControllerTest do end end + describe "user sign in" do + setup [:create_user] + + test "renders user when data is valid", %{conn: conn} do + conn = post(conn, Routes.v1_user_path(conn, :signin), @create_attrs) + + assert %{ + "email" => email, + "token" => token, + } = json_response(conn, 201) + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.v1_user_path(conn, :signin), @invalid_password_attrs) + assert "Unauthorized" == json_response(conn, 401) + end + end + defp create_user(_) do - user = user_fixture() + user = user_fixture(@create_attrs) %{user: user} end end diff --git a/test/support/fixtures/accounts_fixtures.ex b/test/support/fixtures/accounts_fixtures.ex index 82f6ecf..96bc2e7 100644 --- a/test/support/fixtures/accounts_fixtures.ex +++ b/test/support/fixtures/accounts_fixtures.ex @@ -33,7 +33,7 @@ defmodule LinkShortener.AccountsFixtures do def user_token_fixture(attrs \\ %{}) do user_params = %{ email: "user@mail.com", - password: "hello world 12345" + password: "some password" } {:ok, %User{} = user} = Accounts.register_user(user_params)