Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cc1a83e34 | ||
|
|
93631f195f |
@@ -51,3 +51,7 @@
|
|||||||
Add dependencies for API Authentication.
|
Add dependencies for API Authentication.
|
||||||
** 0.6.0 <2023-08-05 Sat>
|
** 0.6.0 <2023-08-05 Sat>
|
||||||
Add User model.
|
Add User model.
|
||||||
|
** 0.7.0 <2023-08-05 Sat>
|
||||||
|
Add the API Authentication.
|
||||||
|
** 0.7.1 <2023-08-06 Sun>
|
||||||
|
Add tests for UserController, Accounts, Generators.
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ config :logger, :console,
|
|||||||
# Use Jason for JSON parsing in Phoenix
|
# Use Jason for JSON parsing in Phoenix
|
||||||
config :phoenix, :json_library, Jason
|
config :phoenix, :json_library, Jason
|
||||||
|
|
||||||
|
config :link_shortener, LinkShortenerWeb.Auth.Guardian,
|
||||||
|
issuer: "link_shortener",
|
||||||
|
secret_key: System.get_env("SECRET_KEY_BASE")
|
||||||
|
|
||||||
# Import environment specific config. This must remain at the bottom
|
# Import environment specific config. This must remain at the bottom
|
||||||
# of this file so it overrides the configuration defined above.
|
# of this file so it overrides the configuration defined above.
|
||||||
import_config "#{config_env()}.exs"
|
import_config "#{config_env()}.exs"
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
defmodule LinkShortenerWeb.Auth.Guardian do
|
||||||
|
use Guardian, otp_app: :link_shortener
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts
|
||||||
|
|
||||||
|
def subject_for_token(user, _claims) do
|
||||||
|
sub = to_string(user.id)
|
||||||
|
{:ok, sub}
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_from_claims(claims) do
|
||||||
|
id = claims["sub"]
|
||||||
|
resource = Accounts.get_user!(id)
|
||||||
|
{:ok, resource}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -3,41 +3,24 @@ defmodule LinkShortenerWeb.Api.V1.UserController do
|
|||||||
|
|
||||||
alias LinkShortener.Accounts
|
alias LinkShortener.Accounts
|
||||||
alias LinkShortener.Accounts.User
|
alias LinkShortener.Accounts.User
|
||||||
|
alias LinkShortenerWeb.Auth.Guardian
|
||||||
|
|
||||||
action_fallback LinkShortenerWeb.FallbackController
|
action_fallback LinkShortenerWeb.FallbackController
|
||||||
|
|
||||||
def index(conn, _params) do
|
|
||||||
users = Accounts.list_users()
|
|
||||||
render(conn, "index.json", users: users)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create(conn, %{"user" => user_params}) do
|
def create(conn, %{"user" => user_params}) do
|
||||||
with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
|
with {:ok, %User{} = user} <- Accounts.create_user(user_params),
|
||||||
|
{:ok, token, _claims} <- Guardian.encode_and_sign(user) do
|
||||||
conn
|
conn
|
||||||
|> put_status(:created)
|
|> put_status(:created)
|
||||||
|> put_resp_header("location", Routes.v1_user_path(conn, :show, user))
|
|> render("user.json", %{user: user, token: token})
|
||||||
|> render("show.json", user: user)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
def signin(conn, %{"email" => email, "password" => password}) do
|
||||||
user = Accounts.get_user!(id)
|
with {:ok, user, token} <- Guardian.authenticate(email, password) do
|
||||||
render(conn, "show.json", user: user)
|
conn
|
||||||
end
|
|> put_status(:created)
|
||||||
|
|> render("user.json", %{user: user, token: token})
|
||||||
def update(conn, %{"id" => id, "user" => user_params}) do
|
|
||||||
user = Accounts.get_user!(id)
|
|
||||||
|
|
||||||
with {:ok, %User{} = user} <- Accounts.update_user(user, user_params) do
|
|
||||||
render(conn, "show.json", user: user)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete(conn, %{"id" => id}) do
|
|
||||||
user = Accounts.get_user!(id)
|
|
||||||
|
|
||||||
with {:ok, %User{}} <- Accounts.delete_user(user) do
|
|
||||||
send_resp(conn, :no_content, "")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ defmodule LinkShortenerWeb.Router do
|
|||||||
|
|
||||||
scope "/v1", Api.V1, as: :v1 do
|
scope "/v1", Api.V1, as: :v1 do
|
||||||
resources "/links", LinkController
|
resources "/links", LinkController
|
||||||
|
post "/users/signup", UserController, :create
|
||||||
|
post "/users/signin", UserController, :signin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,12 @@
|
|||||||
defmodule LinkShortenerWeb.Api.V1.UserView do
|
defmodule LinkShortenerWeb.Api.V1.UserView do
|
||||||
use LinkShortenerWeb, :view
|
use LinkShortenerWeb, :view
|
||||||
alias LinkShortenerWeb.UserView
|
|
||||||
|
|
||||||
def render("index.json", %{users: users}) do
|
alias LinkShortenerWeb.Api.V1.UserView
|
||||||
%{data: render_many(users, UserView, "user.json")}
|
|
||||||
end
|
|
||||||
|
|
||||||
def render("show.json", %{user: user}) do
|
def render("user.json", %{user: user, token: token}) do
|
||||||
%{data: render_one(user, UserView, "user.json")}
|
|
||||||
end
|
|
||||||
|
|
||||||
def render("user.json", %{user: user}) do
|
|
||||||
%{
|
%{
|
||||||
id: user.id,
|
|
||||||
email: user.email,
|
email: user.email,
|
||||||
encrypted_password: user.encrypted_password
|
token: token
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
defmodule LinkShortener.AccountsTest do
|
||||||
|
use LinkShortener.DataCase
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts
|
||||||
|
|
||||||
|
describe "users" do
|
||||||
|
alias LinkShortener.Accounts.User
|
||||||
|
|
||||||
|
import LinkShortener.AccountsFixtures
|
||||||
|
|
||||||
|
@invalid_attrs %{email: nil, password: nil}
|
||||||
|
|
||||||
|
test "list_users/0 returns all users" do
|
||||||
|
user = user_fixture()
|
||||||
|
assert Accounts.list_users() == [user]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get_user!/1 returns the user with given id" do
|
||||||
|
user = user_fixture()
|
||||||
|
assert Accounts.get_user!(user.id) == user
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_user/1 with valid data creates a user" do
|
||||||
|
valid_attrs = %{email: "some email", password: "some encrypted_password"}
|
||||||
|
|
||||||
|
assert {:ok, %User{} = user} = Accounts.create_user(valid_attrs)
|
||||||
|
assert user.email == "some email"
|
||||||
|
assert user.encrypted_password != "some encrypted_password"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create_user/1 with invalid data returns error changeset" do
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Accounts.create_user(@invalid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_user/2 with valid data updates the user" do
|
||||||
|
user = user_fixture()
|
||||||
|
update_attrs = %{email: "some updated email", password: "some updated encrypted_password"}
|
||||||
|
|
||||||
|
assert {:ok, %User{} = user} = Accounts.update_user(user, update_attrs)
|
||||||
|
assert user.email == "some updated email"
|
||||||
|
assert user.encrypted_password != "some updated encrypted_password"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update_user/2 with invalid data returns error changeset" do
|
||||||
|
user = user_fixture()
|
||||||
|
assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, @invalid_attrs)
|
||||||
|
assert user == Accounts.get_user!(user.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "delete_user/1 deletes the user" do
|
||||||
|
user = user_fixture()
|
||||||
|
assert {:ok, %User{}} = Accounts.delete_user(user)
|
||||||
|
assert_raise Ecto.NoResultsError, fn -> Accounts.get_user!(user.id) end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "change_user/1 returns a user changeset" do
|
||||||
|
user = user_fixture()
|
||||||
|
assert %Ecto.Changeset{} = Accounts.change_user(user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
defmodule LinkShortener.Generators.LinkWithRandomShortenTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
alias LinkShortener.Generators.LinkWithRandomShorten
|
||||||
|
|
||||||
|
@name "Some name"
|
||||||
|
@attrs %{
|
||||||
|
name: @name
|
||||||
|
}
|
||||||
|
|
||||||
|
test "generate/1 returns random safe string with 10 as length" do
|
||||||
|
assert %{
|
||||||
|
name: @name,
|
||||||
|
shorten: shorten
|
||||||
|
} = LinkWithRandomShorten.generate_one(@attrs)
|
||||||
|
|
||||||
|
assert String.length(shorten) == 10
|
||||||
|
end
|
||||||
|
|
||||||
|
test "generate/2 returns random safe string with 5 as length" do
|
||||||
|
expected_length = 5
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
name: @name,
|
||||||
|
shorten: shorten
|
||||||
|
} = LinkWithRandomShorten.generate_one(@attrs, expected_length)
|
||||||
|
|
||||||
|
assert String.length(shorten) == expected_length
|
||||||
|
end
|
||||||
|
|
||||||
|
test "generate/3 returns random safe string using a lambda function" do
|
||||||
|
random_generator = fn _ -> "random_shorten" end
|
||||||
|
expected_shorten = random_generator.(14)
|
||||||
|
expected_length = expected_shorten |> String.length()
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
name: @name,
|
||||||
|
shorten: shorten
|
||||||
|
} = LinkWithRandomShorten.generate_one(@attrs, expected_length, random_generator)
|
||||||
|
|
||||||
|
assert String.length(shorten) == expected_length
|
||||||
|
assert shorten == expected_shorten
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
defmodule LinkShortener.Generators.SafeStringTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
alias LinkShortener.Generators.SafeString
|
||||||
|
|
||||||
|
@lengths [1, 2, 5, 10]
|
||||||
|
|
||||||
|
test "generate/1 returns random safe string with same length" do
|
||||||
|
for expected_length <- @lengths do
|
||||||
|
length = expected_length
|
||||||
|
|> SafeString.generate()
|
||||||
|
|> String.length()
|
||||||
|
|
||||||
|
assert length = expected_length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
defmodule LinkShortenerWeb.Api.V1.UserControllerTest do
|
||||||
|
use LinkShortenerWeb.ConnCase
|
||||||
|
|
||||||
|
import LinkShortener.AccountsFixtures
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts.User
|
||||||
|
|
||||||
|
@create_attrs %{
|
||||||
|
email: "some_email@mail.com",
|
||||||
|
password: "some password"
|
||||||
|
}
|
||||||
|
|
||||||
|
@update_attrs %{
|
||||||
|
email: "some updated email",
|
||||||
|
password: "some updated password"
|
||||||
|
}
|
||||||
|
|
||||||
|
@invalid_attrs %{
|
||||||
|
email: nil,
|
||||||
|
encrypted_password: nil
|
||||||
|
}
|
||||||
|
|
||||||
|
setup %{conn: conn} do
|
||||||
|
{:ok, conn: put_req_header(conn, "accept", "application/json")}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "create user" 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",
|
||||||
|
"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, :create), user: @invalid_attrs)
|
||||||
|
assert json_response(conn, 422)["errors"] != %{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp create_user(_) do
|
||||||
|
user = user_fixture()
|
||||||
|
%{user: user}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
defmodule LinkShortener.AccountsFixtures do
|
||||||
|
@moduledoc """
|
||||||
|
This module defines test helpers for creating
|
||||||
|
entities via the `LinkShortener.Accounts` context.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Generate a unique user email.
|
||||||
|
"""
|
||||||
|
def unique_user_email, do: "some email#{System.unique_integer([:positive])}"
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Generate a new user.
|
||||||
|
"""
|
||||||
|
def user_fixture(attrs \\ %{}) do
|
||||||
|
{:ok, user} =
|
||||||
|
attrs
|
||||||
|
|> Enum.into(%{
|
||||||
|
email: unique_user_email(),
|
||||||
|
password: "some encrypted_password"
|
||||||
|
})
|
||||||
|
|> LinkShortener.Accounts.create_user()
|
||||||
|
|
||||||
|
LinkShortener.Accounts.get_user!(user.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user