Static blog with front page summary
Goal: have a personal blog, and try out another point in the 'modular app design with elixir' space. Designing OTP systems with elixir had some interesting ideas.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
defmodule FirehoseWeb.BlogTest do
|
||||
use FirehoseWeb.ConnCase
|
||||
|
||||
describe "engineering blog (HTML)" do
|
||||
test "GET /blog/engineering returns HTML index with layout", %{conn: conn} do
|
||||
conn = get(conn, "/blog/engineering")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ "Engineering Blog"
|
||||
assert body =~ "Hello World"
|
||||
# Verify app layout is present (navbar)
|
||||
assert body =~ "firehose"
|
||||
end
|
||||
|
||||
test "GET /blog/engineering/:slug returns HTML post with layout", %{conn: conn} do
|
||||
conn = get(conn, "/blog/engineering/hello-world")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ "Hello World"
|
||||
assert body =~ "firehose"
|
||||
end
|
||||
|
||||
test "GET /blog/engineering/tag/:tag returns HTML tag page", %{conn: conn} do
|
||||
conn = get(conn, "/blog/engineering/tag/elixir")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ ~s(tagged "elixir")
|
||||
assert body =~ "Hello World"
|
||||
end
|
||||
end
|
||||
|
||||
describe "release notes blog (HTML)" do
|
||||
test "GET /blog/releases returns HTML index", %{conn: conn} do
|
||||
conn = get(conn, "/blog/releases")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ "Release Notes"
|
||||
assert body =~ "v0.1.0 Released"
|
||||
end
|
||||
|
||||
test "GET /blog/releases/:slug returns HTML post", %{conn: conn} do
|
||||
conn = get(conn, "/blog/releases/v0-1-0")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ "v0.1.0 Released"
|
||||
end
|
||||
end
|
||||
|
||||
describe "engineering blog (JSON API)" do
|
||||
test "GET /api/blog/engineering returns post index", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/engineering")
|
||||
assert %{"blog" => "engineering", "posts" => posts} = json_response(conn, 200)
|
||||
assert is_list(posts)
|
||||
assert length(posts) > 0
|
||||
end
|
||||
|
||||
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/engineering/hello-world")
|
||||
assert %{"id" => "hello-world", "title" => "Hello World"} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/engineering/nonexistent")
|
||||
assert response(conn, 404)
|
||||
end
|
||||
|
||||
test "GET /api/blog/engineering/feed.xml returns RSS", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/engineering/feed.xml")
|
||||
assert response_content_type(conn, :xml)
|
||||
assert response(conn, 200) =~ "<rss"
|
||||
end
|
||||
end
|
||||
|
||||
describe "release notes blog (JSON API)" do
|
||||
test "GET /api/blog/releases returns post index", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/releases")
|
||||
assert %{"blog" => "release_notes", "posts" => posts} = json_response(conn, 200)
|
||||
assert is_list(posts)
|
||||
assert length(posts) > 0
|
||||
end
|
||||
|
||||
test "GET /api/blog/releases/:slug returns a post", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/releases/v0-1-0")
|
||||
assert %{"id" => "v0-1-0", "title" => "v0.1.0 Released"} = json_response(conn, 200)
|
||||
end
|
||||
|
||||
test "GET /api/blog/releases/feed.xml returns RSS", %{conn: conn} do
|
||||
conn = get(conn, "/api/blog/releases/feed.xml")
|
||||
assert response_content_type(conn, :xml)
|
||||
assert response(conn, 200) =~ "<rss"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
defmodule FirehoseWeb.ErrorHTMLTest do
|
||||
use FirehoseWeb.ConnCase, async: true
|
||||
|
||||
# Bring render_to_string/4 for testing custom views
|
||||
import Phoenix.Template, only: [render_to_string: 4]
|
||||
|
||||
test "renders 404.html" do
|
||||
assert render_to_string(FirehoseWeb.ErrorHTML, "404", "html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(FirehoseWeb.ErrorHTML, "500", "html", []) == "Internal Server Error"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
defmodule FirehoseWeb.ErrorJSONTest do
|
||||
use FirehoseWeb.ConnCase, async: true
|
||||
|
||||
test "renders 404" do
|
||||
assert FirehoseWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
|
||||
end
|
||||
|
||||
test "renders 500" do
|
||||
assert FirehoseWeb.ErrorJSON.render("500.json", %{}) ==
|
||||
%{errors: %{detail: "Internal Server Error"}}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
defmodule FirehoseWeb.PageControllerTest do
|
||||
use FirehoseWeb.ConnCase
|
||||
|
||||
test "GET /", %{conn: conn} do
|
||||
conn = get(conn, ~p"/")
|
||||
body = html_response(conn, 200)
|
||||
assert body =~ "Drinking from the firehose"
|
||||
assert body =~ "Willem van den Ende"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
defmodule FirehoseWeb.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 FirehoseWeb.ConnCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
# The default endpoint for testing
|
||||
@endpoint FirehoseWeb.Endpoint
|
||||
|
||||
use FirehoseWeb, :verified_routes
|
||||
|
||||
# Import conveniences for testing with connections
|
||||
import Plug.Conn
|
||||
import Phoenix.ConnTest
|
||||
import FirehoseWeb.ConnCase
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
Firehose.DataCase.setup_sandbox(tags)
|
||||
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
defmodule Firehose.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 Firehose.DataCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
alias Firehose.Repo
|
||||
|
||||
import Ecto
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
import Firehose.DataCase
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
Firehose.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!(Firehose.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,2 @@
|
||||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(Firehose.Repo, :manual)
|
||||
Reference in New Issue
Block a user