Compare commits

...
16 Commits
Author SHA1 Message Date
KKlochko cde8970b46 Add User model.
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build was killed
2023-08-05 21:15:40 +03:00
KKlochko 33fae4ece2 Add dependencies for API Authentication.
continuous-integration/drone/tag Build was killed
2023-08-05 21:13:59 +03:00
KKlochko e89a8ea5db Update Links.create_one to use a function.
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build was killed
2023-08-04 21:10:36 +03:00
KKlochko feebbe4f8a Add a generator for Link with random shorten. 2023-08-04 21:10:08 +03:00
KKlochko ecdce9e407 Add the test config.
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build was killed
2023-08-03 14:36:07 +03:00
KKlochko e3b202825b Add the CI/CD configuration. 2023-08-03 14:35:14 +03:00
KKlochko ebac60a2b0 Add tests for the Links module. 2023-08-02 21:26:56 +03:00
KKlochko c6236c0ef1 Refactor the Links module. 2023-08-02 21:20:01 +03:00
KKlochko dd0e79c400 Add tests for the API. 2023-08-01 18:09:01 +03:00
KKlochko e6e344745b Add the API to update the Link model. 2023-08-01 18:02:14 +03:00
KKlochko 62e855e1b6 Fix the route path in LinkController.create/2. 2023-08-01 17:59:34 +03:00
KKlochko 8f6ec31366 Fix Links.update_one/2. 2023-08-01 17:58:42 +03:00
KKlochko 9c273a473e Update the controller and the view for updated Link model. 2023-07-31 19:01:58 +03:00
KKlochko 9e7e052249 Refactor to split the Links to a Link model and a Links logic. 2023-07-31 18:56:57 +03:00
KKlochko fb4f0025d1 Move Links to the Links context. 2023-07-31 18:56:57 +03:00
KKlochko 6799e5b00b Rename the controller and the view names to "Link". 2023-07-31 18:51:35 +03:00
29 changed files with 844 additions and 130 deletions
+52
View File
@@ -0,0 +1,52 @@
kind: pipeline
type: docker
name: default
environment:
DATABASE_NAME: link_shortener_dev
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: postgres
DATABASE_PORT: 5432
SECRET_KEY_BASE: TestSecretKeycRZ1H45gfdAsdf8DIb45df4664mRCruzwVEF68wosddfg324ost
steps:
- name: install dependencies
image: elixir:1.14.0-slim
volumes:
- name: mix
path: /root/.mix
commands:
- mix local.hex --force
- mix local.rebar --force
- mix deps.get
- mix compile
- name: run migrations
image: elixir:1.14.0-slim
volumes:
- name: mix
path: /root/.mix
commands:
- mix ecto.setup
- name: run tests
image: elixir:1.14.0-slim
volumes:
- name: mix
path: /root/.mix
commands:
- mix test
volumes:
- name: mix
temp: {}
services:
- name: postgres
image: postgres:15-alpine
environment:
POSTGRES_DB: link_shortener_dev
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
+27
View File
@@ -23,4 +23,31 @@
Add the API to create the Links model. Add the API to create the Links model.
** 0.4.2 <2023-07-30 Sun> ** 0.4.2 <2023-07-30 Sun>
Add the API to delete the Links model. Add the API to delete the Links model.
** 0.4.3 <2023-07-31 Mon>
Rename the controller and the view names to "Link".
Move Links to the Links context.
Refactor to split the Links to a Link model and a Links logic.
Update the controller and the view for updated Link model.
** 0.4.4 <2023-08-01 Tue>
Fix Links.update_one/2.
** 0.4.5 <2023-08-01 Tue>
Fix the route path in LinkController.create/2.
** 0.4.6 <2023-08-01 Tue>
Add the API to update the Link model.
** 0.4.7 <2023-08-01 Tue>
Add tests for the API.
** 0.4.8 <2023-08-02 Wed>
Refactor the Links module.
** 0.4.9 <2023-08-02 Wed>
Add tests for the Links module.
** 0.4.10 <2023-08-03 Thu>
Add the CI/CD configuration.
Add the test config.
** 0.5.1 <2023-08-04 Fri>
Add a generator for Link with random shorten.
Update Links.create_one to use a function.
** 0.5.2 <2023-08-05 Sat>
Add dependencies for API Authentication.
** 0.6.0 <2023-08-05 Sat>
Add User model.
+30
View File
@@ -0,0 +1,30 @@
import Config
# Configure your database
#
# The MIX_TEST_PARTITION environment variable can be used
# to provide built-in test partitioning in CI environment.
# Run `mix help test` for more information.
config :link_shortener, LinkShortener.Repo,
username: System.get_env("DATABASE_USERNAME"),
password: System.get_env("DATABASE_PASSWORD"),
hostname: System.get_env("DATABASE_HOST"),
database: "link_shortener_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: 10
# We don't run a server during test. If one is required,
# you can enable the server option below.
config :link_shortener, LinkShortenerWeb.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4002],
secret_key_base: System.get_env("SECRET_KEY_BASE"),
server: false
# In test we don't send emails.
config :link_shortener, LinkShortener.Mailer, adapter: Swoosh.Adapters.Test
# Print only warnings and errors during test
config :logger, level: :warn
# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime
+104
View File
@@ -0,0 +1,104 @@
defmodule LinkShortener.Accounts do
@moduledoc """
The Accounts context.
"""
import Ecto.Query, warn: false
alias LinkShortener.Repo
alias LinkShortener.Accounts.User
@doc """
Returns the list of users.
## Examples
iex> list_users()
[%User{}, ...]
"""
def list_users do
Repo.all(User)
end
@doc """
Gets a single user.
Raises `Ecto.NoResultsError` if the User does not exist.
## Examples
iex> get_user!(123)
%User{}
iex> get_user!(456)
** (Ecto.NoResultsError)
"""
def get_user!(id), do: Repo.get!(User, id)
@doc """
Creates a user.
## Examples
iex> create_user(%{field: value})
{:ok, %User{}}
iex> create_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a user.
## Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a user.
## Examples
iex> delete_user(user)
{:ok, %User{}}
iex> delete_user(user)
{:error, %Ecto.Changeset{}}
"""
def delete_user(%User{} = user) do
Repo.delete(user)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.
## Examples
iex> change_user(user)
%Ecto.Changeset{data: %User{}}
"""
def change_user(%User{} = user, attrs \\ %{}) do
User.changeset(user, attrs)
end
end
+31
View File
@@ -0,0 +1,31 @@
defmodule LinkShortener.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :email, :string
field :encrypted_password, :string
field :password, :string, virtual: true
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
|> unique_constraint(:email)
|> put_hashed_password()
end
defp put_hashed_password(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}}
->
put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password))
_ ->
changeset
end
end
end
@@ -0,0 +1,14 @@
defmodule LinkShortener.Generators.LinkWithRandomShorten do
@moduledoc """
This module provides a generator for Link.
"""
alias LinkShortener.Generators.SafeString
@doc """
Generate a Link with random shorten with the length.
"""
def generate_one(attrs, length \\ 10, generator \\ &SafeString.generate/1) do
Map.put(attrs, :shorten, generator.(length))
end
end
-73
View File
@@ -1,73 +0,0 @@
defmodule LinkShortener.Links do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias LinkShortener.Repo
alias LinkShortener.Links
alias LinkShortener.Generators.SafeString
schema "links" do
field :name, :string
field :url, :string
field :shorten, :string
timestamps()
end
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:name, :url, :shorten])
|> validate_required([:url, :shorten])
|> unique_constraint(:shorten)
end
def new_one(), do: Links.changeset(%Links{})
def create_one(attrs, length \\ 10, generator \\ SafeString) do
shorten = generator.generate(length)
insert_one(Map.put(attrs, :shorten, shorten))
end
def insert_one(attrs) do
%Links{}
|> Links.changeset(attrs)
|> Repo.insert()
end
def edit_one(id) do
get_one(id)
|> Links.changeset()
end
def update_one(%Links{} = Links, changes) do
Links
|> Links.changeset(changes)
|> Repo.update()
end
def insert_one(attrs) do
%Links{}
|> Links.changeset(attrs)
|> Repo.insert()
end
def delete_one(%Links{} = Links), do: Repo.delete(Links)
def get_one_by(attrs) do
Repo.get_by(Links, attrs)
end
def get_one_by_shorten(shorten) do
get_one_by(%{shorten: shorten})
end
def get_one(id) do
Repo.get!(Links, id)
end
def get_all(opts) do
from(Links)
|> Repo.all()
end
end
+21
View File
@@ -0,0 +1,21 @@
defmodule LinkShortener.Links.Link do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
schema "links" do
field :name, :string
field :url, :string
field :shorten, :string
timestamps()
end
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:name, :url, :shorten])
|> validate_required([:url, :shorten])
|> unique_constraint(:shorten)
end
end
+56
View File
@@ -0,0 +1,56 @@
defmodule LinkShortener.Links do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias LinkShortener.Repo
alias LinkShortener.Links.Link
alias LinkShortener.Generators.LinkWithRandomShorten, as: LinkGenerator
def new_one(), do: Link.changeset(%Link{}, %{})
def create_one(attrs, length \\ 10, generator \\ &LinkGenerator.generate_one/2) do
generator.(attrs, length)
|> insert_one()
end
def insert_one(attrs) do
%Link{}
|> Link.changeset(attrs)
|> Repo.insert()
end
def get_one!(id) do
Repo.get!(Link, id)
end
def get_one_by(attrs) do
Repo.get_by(Link, attrs)
end
def get_one_by_shorten(shorten) do
get_one_by(%{shorten: shorten})
end
def get_all() do
from(Link)
|> Repo.all()
end
def get_all(opts) do
from(Link)
|> Repo.all()
end
def edit_one(%Link{} = link) do
link
|> Link.changeset(%{})
end
def update_one(%Link{} = link, changes) do
link
|> Link.changeset(changes)
|> Repo.update()
end
def delete_one(%Link{} = link), do: Repo.delete(link)
end
@@ -0,0 +1,43 @@
defmodule LinkShortenerWeb.Api.V1.LinkController do
use LinkShortenerWeb, :controller
alias LinkShortener.Links
alias LinkShortener.Links.Link
action_fallback LinkShortenerWeb.FallbackController
def index(conn, _params) do
links = Links.get_all()
render(conn, "index.json", links: links)
end
def create(conn, %{"link" => link_params}) do
with {:ok, %Link{} = link} <- Links.insert_one(link_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.v1_link_path(conn, :show, link))
|> render("show.json", link: link)
end
end
def show(conn, %{"id" => id}) do
link = Links.get_one!(id)
render(conn, "show.json", link: link)
end
def update(conn, %{"id" => id, "link" => link_params}) do
link = Links.get_one!(id)
with {:ok, %Link{} = link} <- Links.update_one(link, link_params) do
render(conn, "show.json", link: link)
end
end
def delete(conn, %{"id" => id}) do
link = Links.get_one!(id)
with {:ok, %Link{}} <- Links.delete_one(link) do
send_resp(conn, :no_content, "")
end
end
end
@@ -1,34 +0,0 @@
defmodule LinkShortenerWeb.Api.V1.LinksController do
use LinkShortenerWeb, :controller
alias LinkShortener.Links
action_fallback LinkShortenerWeb.FallbackController
def index(conn, _params) do
links = Links.get_all({})
render(conn, "index.json", links: links)
end
def create(conn, %{"link" => link_params}) do
with {:ok, %Links{} = link} <- Links.insert_one(link_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.links_path(conn, :show, link))
|> render("show.json", links: link)
end
end
def show(conn, %{"id" => id}) do
link = Links.get_one(id)
render(conn, "show.json", links: link)
end
def delete(conn, %{"id" => id}) do
link = Links.get_one(id)
with {:ok, %Link{}} <- Links.delete_one(link) do
send_resp(conn, :no_content, "")
end
end
end
@@ -0,0 +1,43 @@
defmodule LinkShortenerWeb.Api.V1.UserController do
use LinkShortenerWeb, :controller
alias LinkShortener.Accounts
alias LinkShortener.Accounts.User
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
with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.v1_user_path(conn, :show, user))
|> render("show.json", user: user)
end
end
def show(conn, %{"id" => id}) do
user = Accounts.get_user!(id)
render(conn, "show.json", user: user)
end
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
+1 -1
View File
@@ -26,7 +26,7 @@ defmodule LinkShortenerWeb.Router do
pipe_through :api pipe_through :api
scope "/v1", Api.V1, as: :v1 do scope "/v1", Api.V1, as: :v1 do
resources "/links", LinksController resources "/links", LinkController
end end
end end
@@ -0,0 +1,21 @@
defmodule LinkShortenerWeb.Api.V1.LinkView do
use LinkShortenerWeb, :view
alias LinkShortenerWeb.Api.V1.LinkView
def render("index.json", %{links: links}) do
%{data: render_many(links, LinkView, "link.json")}
end
def render("show.json", %{link: link}) do
%{data: render_one(link, LinkView, "link.json")}
end
def render("link.json", %{link: link}) do
%{
id: link.id,
name: link.name,
url: link.url,
shorten: link.shorten
}
end
end
@@ -1,21 +0,0 @@
defmodule LinkShortenerWeb.Api.V1.LinksView do
use LinkShortenerWeb, :view
alias LinkShortenerWeb.Api.V1.LinksView
def render("index.json", %{links: links}) do
%{data: render_many(links, LinksView, "link.json")}
end
def render("show.json", %{links: link}) do
%{data: render_one(link, LinksView, "link.json")}
end
def render("link.json", %{links: link}) do
%{
id: link.id,
name: link.name,
url: link.url,
shorten: link.shorten
}
end
end
@@ -0,0 +1,20 @@
defmodule LinkShortenerWeb.Api.V1.UserView do
use LinkShortenerWeb, :view
alias LinkShortenerWeb.UserView
def render("index.json", %{users: users}) do
%{data: render_many(users, UserView, "user.json")}
end
def render("show.json", %{user: user}) do
%{data: render_one(user, UserView, "user.json")}
end
def render("user.json", %{user: user}) do
%{
id: user.id,
email: user.email,
encrypted_password: user.encrypted_password
}
end
end
+4 -1
View File
@@ -48,7 +48,10 @@ defmodule LinkShortener.MixProject do
{:telemetry_poller, "~> 1.0"}, {:telemetry_poller, "~> 1.0"},
{:gettext, "~> 0.18"}, {:gettext, "~> 0.18"},
{:jason, "~> 1.2"}, {:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"} {:plug_cowboy, "~> 2.5"},
{:guardian, "~> 1.0"},
{:comeonin, "~> 4.0"},
{:bcrypt_elixir, "~> 1.0"}
] ]
end end
+5
View File
@@ -1,5 +1,7 @@
%{ %{
"bcrypt_elixir": {:hex, :bcrypt_elixir, "1.1.1", "6b5560e47a02196ce5f0ab3f1d8265db79a23868c137e973b27afef928ed8006", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "10f658be786bd2daaadcd45cc5b598da01d5bbc313da4d0e3efb2d6a511d896d"},
"castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"}, "castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"},
"comeonin": {:hex, :comeonin, "4.1.2", "3eb5620fd8e35508991664b4c2b04dd41e52f1620b36957be837c1d7784b7592", [:mix], [{:argon2_elixir, "~> 1.2", [hex: :argon2_elixir, repo: "hexpm", optional: true]}, {:bcrypt_elixir, "~> 0.12.1 or ~> 1.0", [hex: :bcrypt_elixir, repo: "hexpm", optional: true]}, {:pbkdf2_elixir, "~> 0.12", [hex: :pbkdf2_elixir, repo: "hexpm", optional: true]}], "hexpm", "d8700a0ca4dbb616c22c9b3f6dd539d88deaafec3efe66869d6370c9a559b3e9"},
"cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"}, "cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"}, "cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"},
@@ -7,12 +9,15 @@
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"ecto": {:hex, :ecto, "3.10.2", "6b887160281a61aa16843e47735b8a266caa437f80588c3ab80a8a960e6abe37", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6a895778f0d7648a4b34b486af59a1c8009041fbdf2b17f1ac215eb829c60235"}, "ecto": {:hex, :ecto, "3.10.2", "6b887160281a61aa16843e47735b8a266caa437f80588c3ab80a8a960e6abe37", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6a895778f0d7648a4b34b486af59a1c8009041fbdf2b17f1ac215eb829c60235"},
"ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"}, "ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"},
"elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"},
"esbuild": {:hex, :esbuild, "0.7.1", "fa0947e8c3c3c2f86c9bf7e791a0a385007ccd42b86885e8e893bdb6631f5169", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "66661cdf70b1378ee4dc16573fcee67750b59761b2605a0207c267ab9d19f13c"}, "esbuild": {:hex, :esbuild, "0.7.1", "fa0947e8c3c3c2f86c9bf7e791a0a385007ccd42b86885e8e893bdb6631f5169", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "66661cdf70b1378ee4dc16573fcee67750b59761b2605a0207c267ab9d19f13c"},
"expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"}, "expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.34.3", "5e2dcaec5d7c228ce5b1d3501502e308b2d79eb655e4191751a1fe491c37feac", [:mix], [], "hexpm", "9577440eea5b97924b4bf3c7ea55f7b8b6dce589f9b28b096cc294a8dc342341"}, "floki": {:hex, :floki, "0.34.3", "5e2dcaec5d7c228ce5b1d3501502e308b2d79eb655e4191751a1fe491c37feac", [:mix], [], "hexpm", "9577440eea5b97924b4bf3c7ea55f7b8b6dce589f9b28b096cc294a8dc342341"},
"gettext": {:hex, :gettext, "0.22.3", "c8273e78db4a0bb6fba7e9f0fd881112f349a3117f7f7c598fa18c66c888e524", [:mix], [{:expo, "~> 0.4.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "935f23447713954a6866f1bb28c3a878c4c011e802bcd68a726f5e558e4b64bd"}, "gettext": {:hex, :gettext, "0.22.3", "c8273e78db4a0bb6fba7e9f0fd881112f349a3117f7f7c598fa18c66c888e524", [:mix], [{:expo, "~> 0.4.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "935f23447713954a6866f1bb28c3a878c4c011e802bcd68a726f5e558e4b64bd"},
"guardian": {:hex, :guardian, "1.2.1", "bdc8dd3dbf0fb7216cb6f91c11831faa1a64d39cdaed9a611e37f2413e584983", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "723fc404edfb7bd5cba4cd83329b352037f102aa97468f44e58ac7f47c136a98"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"phoenix": {:hex, :phoenix, "1.6.16", "e5bdd18c7a06da5852a25c7befb72246de4ddc289182285f8685a40b7b5f5451", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e15989ff34f670a96b95ef6d1d25bad0d9c50df5df40b671d8f4a669e050ac39"}, "phoenix": {:hex, :phoenix, "1.6.16", "e5bdd18c7a06da5852a25c7befb72246de4ddc289182285f8685a40b7b5f5451", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e15989ff34f670a96b95ef6d1d25bad0d9c50df5df40b671d8f4a669e050ac39"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.4.2", "b21bd01fdeffcfe2fab49e4942aa938b6d3e89e93a480d4aee58085560a0bc0d", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "70242edd4601d50b69273b057ecf7b684644c19ee750989fd555625ae4ce8f5d"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.2", "b21bd01fdeffcfe2fab49e4942aa938b6d3e89e93a480d4aee58085560a0bc0d", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "70242edd4601d50b69273b057ecf7b684644c19ee750989fd555625ae4ce8f5d"},
@@ -0,0 +1,14 @@
defmodule LinkShortener.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :email, :string
add :encrypted_password, :string
timestamps()
end
create unique_index(:users, [:email])
end
end
+106
View File
@@ -0,0 +1,106 @@
defmodule LinkShortener.LinksTest do
use LinkShortener.DataCase
alias LinkShortener.Links
@create_attrs %{
name: "some link name",
url: "https://gitlab.com/KKlochko/link_shortener",
shorten: "git_repo",
}
@create_generated_attrs %{
name: "some link name",
url: "https://gitlab.com/KKlochko/link_shortener",
}
@update_attrs %{
name: "some updated link name",
url: "https://gitlab.com/KKlochko/link_shortener2",
shorten: "new_git_repo",
}
@invalid_attrs %{
name: nil,
url: nil,
shorten: nil,
}
describe "links" do
alias LinkShortener.Links.Link
import LinkShortener.LinksFixtures
test "new_one/1 returns the changeset" do
assert %Ecto.Changeset{} = Links.new_one()
end
test "create_one/1 with valid data creates a link" do
assert {:ok, %Link{} = link} = Links.create_one(@create_generated_attrs)
assert link.name == "some link name"
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
assert String.length(link.shorten) == 10
end
test "create_one/2 with valid data creates a link" do
assert {:ok, %Link{} = link} = Links.create_one(@create_generated_attrs, 5)
assert link.name == "some link name"
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
assert String.length(link.shorten) == 5
end
test "insert_one/1 with valid data creates a link" do
assert {:ok, %Link{} = link} = Links.insert_one(@create_attrs)
assert link.name == "some link name"
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
assert link.shorten == "git_repo"
end
test "insert_one/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Links.insert_one(@invalid_attrs)
end
test "get_one!/1 returns the link with given id" do
link = link_fixture()
assert Links.get_one!(link.id) == link
end
test "get_one_by!/1 returns the link with given shorten" do
link = link_fixture()
assert Links.get_one_by(%{shorten: link.shorten}) == link
end
test "get_one_by_shorten!/1 returns the link with given shorten" do
link = link_fixture()
assert Links.get_one_by_shorten(link.shorten) == link
end
test "get_all/0 returns all links" do
link = link_fixture()
assert Links.get_all() == [link]
end
test "edit_one/1 with valid data returns the changeset" do
link = link_fixture()
assert %Ecto.Changeset{} = Links.edit_one(link)
end
test "update_one/2 with valid data updates the link" do
link = link_fixture()
assert {:ok, %Link{} = link} = Links.update_one(link, @update_attrs)
assert link.name == "some updated link name"
assert link.url == "https://gitlab.com/KKlochko/link_shortener2"
assert link.shorten == "new_git_repo"
end
test "update_link/2 with invalid data returns error changeset" do
link = link_fixture()
assert {:error, %Ecto.Changeset{}} = Links.update_one(link, @invalid_attrs)
assert link == Links.get_one!(link.id)
end
test "delete_one/1 deletes the link" do
link = link_fixture()
assert {:ok, %Link{}} = Links.delete_one(link)
assert_raise Ecto.NoResultsError, fn -> Links.get_one!(link.id) end
end
end
end
@@ -0,0 +1,97 @@
defmodule LinkShortenerWeb.Api.V1.LinkControllerTest do
use LinkShortenerWeb.ConnCase
import LinkShortener.LinksFixtures
alias LinkShortener.Links.Link
alias LinkShortener.Links
@create_attrs %{
name: "some link name",
url: "https://gitlab.com/KKlochko/link_shortener",
shorten: "git_repo",
}
@update_attrs %{
name: "some updated link name",
url: "https://gitlab.com/KKlochko/link_shortener2",
shorten: "new_git_repo",
}
@invalid_attrs %{
name: nil,
url: nil,
shorten: nil,
}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all links", %{conn: conn} do
conn = get(conn, Routes.v1_link_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end
end
describe "create link" do
test "renders link when data is valid", %{conn: conn} do
conn = post(conn, Routes.v1_link_path(conn, :create), link: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.v1_link_path(conn, :show, id))
assert %{
"id" => ^id,
"name" => "some link name",
"url" => "https://gitlab.com/KKlochko/link_shortener",
"shorten" => "git_repo",
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.v1_link_path(conn, :create), link: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update link" do
setup [:create_link]
test "renders link when data is valid", %{conn: conn, link: %Link{id: id} = link} do
conn = put(conn, Routes.v1_link_path(conn, :update, link), link: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.v1_link_path(conn, :show, id))
assert %{
"id" => ^id,
"name" => "some updated link name",
"url" => "https://gitlab.com/KKlochko/link_shortener2",
"shorten" => "new_git_repo",
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, link: link} do
conn = put(conn, Routes.v1_link_path(conn, :update, link), link: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete link" do
setup [:create_link]
test "deletes chosen link", %{conn: conn, link: link} do
conn = delete(conn, Routes.v1_link_path(conn, :delete, link))
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, Routes.v1_link_path(conn, :show, link))
end
end
end
defp create_link(_) do
link = link_fixture()
%{link: link}
end
end
@@ -0,0 +1,8 @@
defmodule LinkShortenerWeb.PageControllerTest do
use LinkShortenerWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Usage"
end
end
@@ -0,0 +1,14 @@
defmodule LinkShortenerWeb.ErrorViewTest do
use LinkShortenerWeb.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "renders 404.html" do
assert render_to_string(LinkShortenerWeb.ErrorView, "404.html", []) == "Not Found"
end
test "renders 500.html" do
assert render_to_string(LinkShortenerWeb.ErrorView, "500.html", []) == "Internal Server Error"
end
end
@@ -0,0 +1,8 @@
defmodule LinkShortenerWeb.LayoutViewTest do
use LinkShortenerWeb.ConnCase, async: true
# When testing helpers, you may want to import Phoenix.HTML and
# use functions such as safe_to_string() to convert the helper
# result into an HTML string.
# import Phoenix.HTML
end
@@ -0,0 +1,3 @@
defmodule LinkShortenerWeb.PageViewTest do
use LinkShortenerWeb.ConnCase, async: true
end
+38
View File
@@ -0,0 +1,38 @@
defmodule LinkShortenerWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use LinkShortenerWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with connections
import Plug.Conn
import Phoenix.ConnTest
import LinkShortenerWeb.ConnCase
alias LinkShortenerWeb.Router.Helpers, as: Routes
# The default endpoint for testing
@endpoint LinkShortenerWeb.Endpoint
end
end
setup tags do
LinkShortener.DataCase.setup_sandbox(tags)
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
+58
View File
@@ -0,0 +1,58 @@
defmodule LinkShortener.DataCase do
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.
You may define functions here to be used as helpers in
your tests.
Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use LinkShortener.DataCase, async: true`, although
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate
using do
quote do
alias LinkShortener.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import LinkShortener.DataCase
end
end
setup tags do
LinkShortener.DataCase.setup_sandbox(tags)
:ok
end
@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(LinkShortener.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end
@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)
"""
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
+24
View File
@@ -0,0 +1,24 @@
defmodule LinkShortener.LinksFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `LinkShortener.Links` context.
"""
alias LinkShortener.Links
@doc """
Generate a link.
"""
def link_fixture(attrs \\ %{}) do
{:ok, link} =
attrs
|> Enum.into(%{
name: "some name",
url: "https://gitlab.com/KKlochko/link_shortener",
shorten: "api-article",
})
|> Links.create_one()
link
end
end
+2
View File
@@ -0,0 +1,2 @@
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(LinkShortener.Repo, :manual)