Refactor conn aliasing in controller tests to use pipe chains
Applied refactor_conn_aliasing.sh to eliminate conn shadowing. Show draft posts in test and dev
This commit is contained in:
parent
5d040b4062
commit
6a5269f30a
@ -61,7 +61,8 @@ config :logger, :default_formatter,
|
|||||||
config :phoenix, :json_library, Jason
|
config :phoenix, :json_library, Jason
|
||||||
|
|
||||||
config :blogex,
|
config :blogex,
|
||||||
blogs: [Firehose.EngineeringBlog, Firehose.ReleaseNotes]
|
blogs: [Firehose.EngineeringBlog, Firehose.ReleaseNotes],
|
||||||
|
show_drafts: true
|
||||||
|
|
||||||
# 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.
|
||||||
|
|||||||
@ -13,6 +13,9 @@ config :swoosh, api_client: Swoosh.ApiClient.Req
|
|||||||
# Disable Swoosh Local Memory Storage
|
# Disable Swoosh Local Memory Storage
|
||||||
config :swoosh, local: false
|
config :swoosh, local: false
|
||||||
|
|
||||||
|
# Hide draft blog posts in production
|
||||||
|
config :blogex, show_drafts: false
|
||||||
|
|
||||||
# Do not print debug messages in production
|
# Do not print debug messages in production
|
||||||
config :logger, level: :info
|
config :logger, level: :info
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,7 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
defp visit_engineering_page(conn, suffix \\ "") do
|
defp visit_engineering_page(conn, suffix \\ "") do
|
||||||
path = "/blog/engineering" <> suffix
|
path = "/blog/engineering" <> suffix
|
||||||
conn = get(conn, path)
|
body = conn |> get(path) |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Engineering Blog"
|
assert body =~ "Engineering Blog"
|
||||||
assert body =~ "firehose"
|
assert body =~ "firehose"
|
||||||
body
|
body
|
||||||
@ -14,8 +13,7 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
defp visit_engineering_path(conn, suffix) do
|
defp visit_engineering_path(conn, suffix) do
|
||||||
path = "/blog/engineering" <> suffix
|
path = "/blog/engineering" <> suffix
|
||||||
conn = get(conn, path)
|
body = conn |> get(path) |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "firehose"
|
assert body =~ "firehose"
|
||||||
body
|
body
|
||||||
end
|
end
|
||||||
@ -38,23 +36,19 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
describe "input validation" do
|
describe "input validation" do
|
||||||
test "GET /blog/nonexistent returns 404", %{conn: conn} do
|
test "GET /blog/nonexistent returns 404", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/nonexistent")
|
assert conn |> get("/blog/nonexistent") |> html_response(404)
|
||||||
assert html_response(conn, 404)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=abc falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=abc falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "")
|
assert conn |> get("/blog/engineering?page=abc") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=-1 falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=-1 falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "")
|
assert conn |> get("/blog/engineering?page=-1") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering?page=0 falls back to page 1", %{conn: conn} do
|
test "GET /blog/engineering?page=0 falls back to page 1", %{conn: conn} do
|
||||||
body = visit_engineering_page(conn, "?page=0")
|
assert conn |> get("/blog/engineering?page=0") |> html_response(200) =~ "Engineering Blog"
|
||||||
assert body =~ "Engineering Blog"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/engineering/nonexistent-post returns 404", %{conn: conn} do
|
test "GET /blog/engineering/nonexistent-post returns 404", %{conn: conn} do
|
||||||
@ -66,85 +60,99 @@ defmodule FirehoseWeb.BlogTest do
|
|||||||
|
|
||||||
describe "release notes blog (HTML)" do
|
describe "release notes blog (HTML)" do
|
||||||
test "GET /blog/releases returns HTML index", %{conn: conn} do
|
test "GET /blog/releases returns HTML index", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases")
|
body = conn |> get("/blog/releases") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Release Notes"
|
assert body =~ "Release Notes"
|
||||||
assert body =~ "v0.1.0 Released"
|
assert body =~ "v0.1.0 Released"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/releases/:slug returns HTML post", %{conn: conn} do
|
test "GET /blog/releases/:slug returns HTML post", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases/v0-1-0")
|
body = conn |> get("/blog/releases/v0-1-0") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "v0.1.0 Released"
|
assert body =~ "v0.1.0 Released"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /blog/releases/tag/:tag returns HTML tag page", %{conn: conn} do
|
test "GET /blog/releases/tag/:tag returns HTML tag page", %{conn: conn} do
|
||||||
conn = get(conn, "/blog/releases/tag/elixir")
|
body = conn |> get("/blog/releases/tag/elixir") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ ~s(tagged "elixir")
|
assert body =~ ~s(tagged "elixir")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "engineering blog (JSON API)" do
|
describe "engineering blog (JSON API)" do
|
||||||
test "GET /api/blog/engineering returns post index", %{conn: conn} do
|
test "GET /api/blog/engineering returns post index", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "engineering", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/engineering")
|
conn
|
||||||
assert %{"blog" => "engineering", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
refute Enum.empty?(posts)
|
refute Enum.empty?(posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"id" => "hello-world", "title" => "Hello World"} =
|
||||||
conn = get(conn, "/api/blog/engineering/hello-world")
|
conn
|
||||||
assert %{"id" => "hello-world", "title" => "Hello World"} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering/hello-world")
|
||||||
|
|> json_response(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert conn
|
||||||
conn = get(conn, "/api/blog/engineering/nonexistent")
|
|> put_req_header("accept", "application/json")
|
||||||
assert response(conn, 404)
|
|> get("/api/blog/engineering/nonexistent")
|
||||||
|
|> response(404)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/feed.xml returns RSS", %{conn: conn} do
|
test "GET /api/blog/engineering/feed.xml returns RSS", %{conn: conn} do
|
||||||
conn = get(conn, "/api/blog/engineering/feed.xml")
|
response = conn |> get("/api/blog/engineering/feed.xml")
|
||||||
assert response(conn, 200) =~ "<rss"
|
assert response(response, 200) =~ "<rss"
|
||||||
assert response_content_type(conn, :xml)
|
assert response_content_type(response, :xml)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/engineering/tag/:tag returns JSON with posts", %{conn: conn} do
|
test "GET /api/blog/engineering/tag/:tag returns JSON with posts", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "engineering", "tag" => "elixir", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/engineering/tag/elixir")
|
conn
|
||||||
assert %{"blog" => "engineering", "tag" => "elixir", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/engineering/tag/elixir")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "release notes blog (JSON API)" do
|
describe "release notes blog (JSON API)" do
|
||||||
test "GET /api/blog/releases returns post index", %{conn: conn} do
|
test "GET /api/blog/releases returns post index", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "release_notes", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/releases")
|
conn
|
||||||
assert %{"blog" => "release_notes", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
refute Enum.empty?(posts)
|
refute Enum.empty?(posts)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/:slug returns a post", %{conn: conn} do
|
test "GET /api/blog/releases/:slug returns a post", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"id" => "v0-1-0", "title" => "v0.1.0 Released"} =
|
||||||
conn = get(conn, "/api/blog/releases/v0-1-0")
|
conn
|
||||||
assert %{"id" => "v0-1-0", "title" => "v0.1.0 Released"} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases/v0-1-0")
|
||||||
|
|> json_response(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/feed.xml returns RSS", %{conn: conn} do
|
test "GET /api/blog/releases/feed.xml returns RSS", %{conn: conn} do
|
||||||
conn = get(conn, "/api/blog/releases/feed.xml")
|
response = conn |> get("/api/blog/releases/feed.xml")
|
||||||
assert response(conn, 200) =~ "<rss"
|
assert response(response, 200) =~ "<rss"
|
||||||
assert response_content_type(conn, :xml)
|
assert response_content_type(response, :xml)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/blog/releases/tag/:tag returns JSON with posts", %{conn: conn} do
|
test "GET /api/blog/releases/tag/:tag returns JSON with posts", %{conn: conn} do
|
||||||
conn = conn |> put_req_header("accept", "application/json")
|
assert %{"blog" => "release_notes", "tag" => "elixir", "posts" => posts} =
|
||||||
conn = get(conn, "/api/blog/releases/tag/elixir")
|
conn
|
||||||
assert %{"blog" => "release_notes", "tag" => "elixir", "posts" => posts} = json_response(conn, 200)
|
|> put_req_header("accept", "application/json")
|
||||||
|
|> get("/api/blog/releases/tag/elixir")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
assert is_list(posts)
|
assert is_list(posts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,8 +2,7 @@ defmodule FirehoseWeb.PageControllerTest do
|
|||||||
use FirehoseWeb.ConnCase
|
use FirehoseWeb.ConnCase
|
||||||
|
|
||||||
test "GET /", %{conn: conn} do
|
test "GET /", %{conn: conn} do
|
||||||
conn = get(conn, ~p"/")
|
body = conn |> get(~p"/") |> html_response(200)
|
||||||
body = html_response(conn, 200)
|
|
||||||
assert body =~ "Drinking from the firehose"
|
assert body =~ "Drinking from the firehose"
|
||||||
assert body =~ "Willem van den Ende"
|
assert body =~ "Willem van den Ende"
|
||||||
end
|
end
|
||||||
|
|||||||
@ -38,8 +38,8 @@ defmodule Firehose.DataCase do
|
|||||||
Sets up the sandbox based on the test tags.
|
Sets up the sandbox based on the test tags.
|
||||||
"""
|
"""
|
||||||
def setup_sandbox(tags) do
|
def setup_sandbox(tags) do
|
||||||
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Firehose.Repo, shared: not tags[:async])
|
pid = Sandbox.start_owner!(Firehose.Repo, shared: not tags[:async])
|
||||||
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
|
on_exit(fn -> Sandbox.stop_owner(pid) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|||||||
@ -110,6 +110,11 @@ defmodule Blogex do
|
|||||||
* `Blogex.Router` — mountable Plug router
|
* `Blogex.Router` — mountable Plug router
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@doc "Returns true if draft posts should be visible (dev/test environments)."
|
||||||
|
def show_drafts? do
|
||||||
|
Application.get_env(:blogex, :show_drafts, false)
|
||||||
|
end
|
||||||
|
|
||||||
defdelegate blogs, to: Blogex.Registry
|
defdelegate blogs, to: Blogex.Registry
|
||||||
defdelegate get_blog!(blog_id), to: Blogex.Registry
|
defdelegate get_blog!(blog_id), to: Blogex.Registry
|
||||||
defdelegate get_blog(blog_id), to: Blogex.Registry
|
defdelegate get_blog(blog_id), to: Blogex.Registry
|
||||||
|
|||||||
@ -73,8 +73,14 @@ defmodule Blogex.Blog do
|
|||||||
@doc "Returns the base URL path for this blog."
|
@doc "Returns the base URL path for this blog."
|
||||||
def base_path, do: @blog_base_path
|
def base_path, do: @blog_base_path
|
||||||
|
|
||||||
@doc "Returns all published posts, newest first."
|
@doc "Returns all visible posts, newest first. Drafts are included in dev/test."
|
||||||
def all_posts, do: Enum.filter(@posts, & &1.published)
|
def all_posts do
|
||||||
|
if Blogex.show_drafts?() do
|
||||||
|
@posts
|
||||||
|
else
|
||||||
|
Enum.filter(@posts, & &1.published)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc "Returns the N most recent published posts."
|
@doc "Returns the N most recent published posts."
|
||||||
def recent_posts(n \\ 5), do: Enum.take(all_posts(), n)
|
def recent_posts(n \\ 5), do: Enum.take(all_posts(), n)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user