Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33fae4ece2 | ||
|
|
e89a8ea5db | ||
|
|
feebbe4f8a | ||
|
|
ecdce9e407 | ||
|
|
e3b202825b | ||
|
|
ebac60a2b0 | ||
|
|
c6236c0ef1 | ||
|
|
dd0e79c400 |
+52
@@ -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
|
||||
|
||||
@@ -34,4 +34,18 @@
|
||||
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,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
|
||||
@@ -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
|
||||
@@ -1,16 +1,16 @@
|
||||
defmodule LinkShortener.Links.Links do
|
||||
defmodule LinkShortener.Links do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
alias LinkShortener.Repo
|
||||
alias LinkShortener.Links.Link
|
||||
alias LinkShortener.Generators.SafeString
|
||||
alias LinkShortener.Generators.LinkWithRandomShorten, as: LinkGenerator
|
||||
|
||||
def new_one(), do: Link.changeset(%Link{})
|
||||
def new_one(), do: Link.changeset(%Link{}, %{})
|
||||
|
||||
def create_one(attrs, length \\ 10, generator \\ SafeString) do
|
||||
shorten = generator.generate(length)
|
||||
insert_one(Map.put(attrs, :shorten, shorten))
|
||||
def create_one(attrs, length \\ 10, generator \\ &LinkGenerator.generate_one/2) do
|
||||
generator.(attrs, length)
|
||||
|> insert_one()
|
||||
end
|
||||
|
||||
def insert_one(attrs) do
|
||||
@@ -19,25 +19,10 @@ defmodule LinkShortener.Links.Links do
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def edit_one(id) do
|
||||
get_one(id)
|
||||
|> Link.changeset()
|
||||
def get_one!(id) do
|
||||
Repo.get!(Link, id)
|
||||
end
|
||||
|
||||
def update_one(%Link{} = link, changes) do
|
||||
link
|
||||
|> Link.changeset(changes)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def insert_one(attrs) do
|
||||
%Link{}
|
||||
|> Link.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def delete_one(%Link{} = link), do: Repo.delete(link)
|
||||
|
||||
def get_one_by(attrs) do
|
||||
Repo.get_by(Link, attrs)
|
||||
end
|
||||
@@ -46,12 +31,26 @@ defmodule LinkShortener.Links.Links do
|
||||
get_one_by(%{shorten: shorten})
|
||||
end
|
||||
|
||||
def get_one(id) do
|
||||
Repo.get!(Link, id)
|
||||
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
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkController do
|
||||
use LinkShortenerWeb, :controller
|
||||
|
||||
alias LinkShortener.Links.Links
|
||||
alias LinkShortener.Links
|
||||
alias LinkShortener.Links.Link
|
||||
|
||||
action_fallback LinkShortenerWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
links = Links.get_all({})
|
||||
links = Links.get_all()
|
||||
render(conn, "index.json", links: links)
|
||||
end
|
||||
|
||||
@@ -21,12 +21,12 @@ defmodule LinkShortenerWeb.Api.V1.LinkController do
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
link = Links.get_one(id)
|
||||
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)
|
||||
link = Links.get_one!(id)
|
||||
|
||||
with {:ok, %Link{} = link} <- Links.update_one(link, link_params) do
|
||||
render(conn, "show.json", link: link)
|
||||
@@ -34,7 +34,7 @@ defmodule LinkShortenerWeb.Api.V1.LinkController do
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
link = Links.get_one(id)
|
||||
link = Links.get_one!(id)
|
||||
|
||||
with {:ok, %Link{}} <- Links.delete_one(link) do
|
||||
send_resp(conn, :no_content, "")
|
||||
|
||||
@@ -48,7 +48,10 @@ defmodule LinkShortener.MixProject do
|
||||
{:telemetry_poller, "~> 1.0"},
|
||||
{:gettext, "~> 0.18"},
|
||||
{:jason, "~> 1.2"},
|
||||
{:plug_cowboy, "~> 2.5"}
|
||||
{:plug_cowboy, "~> 2.5"},
|
||||
{:guardian, "~> 1.0"},
|
||||
{:comeonin, "~> 4.0"},
|
||||
{:bcrypt_elixir, "~> 1.0"}
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
@@ -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"},
|
||||
"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_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"},
|
||||
@@ -7,12 +9,15 @@
|
||||
"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_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"},
|
||||
"expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"},
|
||||
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
|
||||
"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"},
|
||||
"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"},
|
||||
"jose": {:hex, :jose, "1.11.6", "613fda82552128aa6fb804682e3a616f4bc15565a048dabd05b1ebd5827ed965", [:mix, :rebar3], [], "hexpm", "6275cb75504f9c1e60eeacb771adfeee4905a9e182103aa59b53fed651ff9738"},
|
||||
"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_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,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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,2 @@
|
||||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(LinkShortener.Repo, :manual)
|
||||
Reference in New Issue
Block a user