Browse Source

added tests

pull/7/head
Victor Roest 2 years ago
parent
commit
66da3ca55b
Signed by: 0x76 GPG Key ID: A3923C699D1A3BDA
  1. 40
      .drone.yaml
  2. 2
      config/test.exs
  3. 2
      lib/dps_web/controllers/author_controller.ex
  4. 2
      lib/dps_web/telemetry.ex
  5. 62
      test/dps_web/controllers/author_controller_test.exs
  6. 13
      test/dps_web/controllers/page_controller_test.exs
  7. 72
      test/dps_web/controllers/poem_controller_test.exs

40
.drone.yaml

@ -3,16 +3,30 @@ kind: pipeline
type: kubernetes
name: default
steps:
- name: kaniko
image: harbor.xirion.net/finitum/drone-kaniko:0.7.0
when:
branch: [main]
settings:
cache: true
username:
from_secret: REGISTRY_USER
password:
from_secret: REGISTRY_PASSWORD
registry: harbor.xirion.net
repo: library/dps
tags: latest
- name: test
image: harbor.xirion.net/hub/library/elixir
commands:
- mix local.rebar --force
- mix local.hex --force
- mix test
- name: kaniko
image: harbor.xirion.net/finitum/drone-kaniko:0.7.0
when:
branch: [main]
settings:
cache: true
username:
from_secret: REGISTRY_USER
password:
from_secret: REGISTRY_PASSWORD
registry: harbor.xirion.net
repo: library/dps
tags: latest
services:
- name: postgres
image: harbor.xirion.net/hub/library/postgres:13-alpine
environment:
POSTGRES_DB: dps_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

2
config/test.exs

@ -20,3 +20,5 @@ config :dps, DpsWeb.Endpoint,
# Print only warnings and errors during test
config :logger, level: :warn
config :dps, :basic_auth, username: "user", password: "secret"

2
lib/dps_web/controllers/author_controller.ex

@ -24,7 +24,7 @@ defmodule DpsWeb.AuthorController do
end
def create(conn, params) do
author = get_in(params, ["author"])
author = get_in(params, ["author"]) || %{}
with {:ok, %Author{id: id}} <- Author.Query.create_author(author) do
redirect(conn, to: Routes.poem_path(conn, :new, %{"author" => id}))

2
lib/dps_web/telemetry.ex

@ -13,7 +13,7 @@ defmodule DpsWeb.Telemetry do
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000},
# Add reporters as children of your supervision tree
{TelemetryMetricsPrometheus, [metrics: metrics()]}
{TelemetryMetricsPrometheus, [metrics: custom_metrics()]}
# {Telemetry.Metrics.ConsoleReporter, metrics: custom_metrics()},
]

62
test/dps_web/controllers/author_controller_test.exs

@ -0,0 +1,62 @@
defmodule DpsWeb.AuthorControllerTest do
use DpsWeb.ConnCase
@create_attrs %{name: "Some Author"}
@invalid_attrs %{name: nil}
describe "index" do
test "GET /authors", %{conn: conn} do
conn = get(conn, Routes.author_path(conn, :index))
assert html_response(conn, 200) =~ "Authors"
end
end
describe "new author" do
test "is blocked with auth", %{conn: conn} do
conn = get(conn, Routes.author_path(conn, :new))
assert response(conn, 401) =~ "Unauthorized"
end
test "GET /authors/new", %{conn: conn} do
conn =
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn = get(conn, Routes.author_path(conn, :new))
assert html_response(conn, 200) =~ "Add Author"
end
end
describe "create author" do
test "is blocked with auth", %{conn: conn} do
conn = post(conn, Routes.author_path(conn, :create), author: @create_attrs)
assert response(conn, 401) =~ "Unauthorized"
end
test "redirects to poem when data is valid", %{conn: conn} do
conn =
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn = post(conn, Routes.author_path(conn, :create), author: @create_attrs)
# Assert that we got redirected to create a new poem
poem_path = Routes.poem_path(conn, :new, %{author: 0}) |> String.slice(0..-2)
assert redirected_to(conn) |> String.starts_with?(poem_path)
id = redirected_to(conn) |> String.trim(poem_path)
assert redirected_to(conn) == Routes.poem_path(conn, :new, %{author: id})
# Assert get on author shows the newly created author
conn = get(conn, Routes.author_path(conn, :show, id))
assert html_response(conn, 200) =~ @create_attrs.name
end
test "renders errors when data is invalid", %{conn: conn} do
conn =
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn = post(conn, Routes.author_path(conn, :create), author: @invalid_attrs)
assert html_response(conn, 200) =~ "Add Author"
end
end
end

13
test/dps_web/controllers/page_controller_test.exs

@ -1,13 +0,0 @@
defmodule DpsWeb.PageControllerTest do
use DpsWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Poems"
end
test "GET /poems", %{conn: conn} do
conn = get(conn, "/poems")
assert html_response(conn, 200) =~ "Poems"
end
end

72
test/dps_web/controllers/poem_controller_test.exs

@ -0,0 +1,72 @@
defmodule DpsWeb.PoemControllerTest do
use DpsWeb.ConnCase
@create_attrs %{
title: "Some Title",
epigraph: "Some Epi",
content: "Some Content",
author_id: 1
}
@invalid_attrs %{title: nil, epigraph: nil, content: nil, author_id: nil}
defp random_string(length \\ 32) do
:crypto.strong_rand_bytes(length) |> Base.encode64() |> binary_part(0, length)
end
describe "index" do
test "GET /poems", %{conn: conn} do
conn = get(conn, Routes.poem_path(conn, :index))
assert html_response(conn, 200) =~ "poems"
end
end
describe "new poem" do
test "is blocked with auth", %{conn: conn} do
conn = get(conn, Routes.poem_path(conn, :new))
assert response(conn, 401) =~ "Unauthorized"
end
test "GET /authors/new", %{conn: conn} do
conn =
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn = get(conn, Routes.poem_path(conn, :new))
assert html_response(conn, 200) =~ "Add Poem"
end
end
describe "create poem" do
test "renders errors when data is invalid", %{conn: conn} do
conn = put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn = post(conn, Routes.poem_path(conn, :create), poem: @invalid_attrs)
assert html_response(conn, 200) =~ "Add Poem"
end
test "redirects to show when data is valid", %{conn: conn} do
{:ok, new_author} = Dps.Author.Query.create_author(%{name: random_string()})
conn =
put_req_header(conn, "authorization", Plug.BasicAuth.encode_basic_auth("user", "secret"))
conn =
post(conn, Routes.poem_path(conn, :create),
poem: %{@create_attrs | author_id: new_author.id}
)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.poem_path(conn, :show, id)
conn = get(conn, Routes.poem_path(conn, :show, id))
html = html_response(conn, 200)
Map.to_list(@create_attrs)
|> Enum.all?(fn
{_, attr} when is_binary(attr) -> html =~ attr
_ -> true
end)
|> assert
end
end
end
Loading…
Cancel
Save