make blog slug tests content-independent
Commit 1709cb7 deleted the pinned hello-world post and ten tests
silently rotted with it - invisible until now because the suite could
not run in this clone (incomplete deps, then the broken frontmatter).
The slug, banner, og-meta and tag tests now derive slug and title from
the compiled posts at runtime (newest live post, newest draft), matching
the existing latest_post_with_links pattern, so content churn no longer
breaks them.
This commit is contained in:
@@ -1,6 +1,19 @@
|
||||
defmodule FirehoseWeb.BlogControllerTest do
|
||||
use FirehoseWeb.ConnCase, async: false
|
||||
|
||||
# Content churns; the behavior under test does not. Pick deterministically
|
||||
# from the compiled posts instead of pinning slugs - the pinned "hello-world"
|
||||
# post was deleted in 1709cb7 and ten tests silently rotted with it.
|
||||
defp a_draft_post do
|
||||
Enum.find(Firehose.EngineeringBlog.all_posts(), &(&1.published == false)) ||
|
||||
raise "no draft (published: false) engineering post to exercise status banners"
|
||||
end
|
||||
|
||||
defp a_live_post do
|
||||
Enum.find(Firehose.EngineeringBlog.all_posts(), &(Blogex.Post.visibility(&1) == :live)) ||
|
||||
raise "no live engineering post to exercise og meta tags"
|
||||
end
|
||||
|
||||
describe "GET /blog/:blog_id (index) - date filtering" do
|
||||
test "does not show future-dated posts", %{conn: conn} do
|
||||
html = conn |> get(~p"/blog/engineering") |> html_response(200)
|
||||
@@ -26,7 +39,8 @@ defmodule FirehoseWeb.BlogControllerTest do
|
||||
setup :register_and_log_in_user
|
||||
|
||||
test "authenticated user sees draft banner on draft post", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world")
|
||||
post = a_draft_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}")
|
||||
|
||||
assert html_response(response, 200) =~ "Draft"
|
||||
assert response.resp_body =~ "not published"
|
||||
@@ -49,9 +63,11 @@ defmodule FirehoseWeb.BlogControllerTest do
|
||||
|
||||
describe "GET /blog/:blog_id/:slug - no banners for unauthenticated" do
|
||||
test "unauthenticated user sees no banner on draft post", %{conn: conn} do
|
||||
post = a_draft_post()
|
||||
|
||||
response =
|
||||
conn
|
||||
|> get(~p"/blog/engineering/hello-world")
|
||||
|> get(~p"/blog/engineering/#{post.id}")
|
||||
|> html_response(200)
|
||||
|
||||
refute response =~ "post-status-banner"
|
||||
@@ -69,26 +85,30 @@ defmodule FirehoseWeb.BlogControllerTest do
|
||||
|
||||
describe "GET /blog/:blog_id/:slug - Open Graph meta tags" do
|
||||
test "meta tags include og_title", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)
|
||||
post = a_live_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}") |> html_response(200)
|
||||
|
||||
assert response =~ ~s(<meta property="og:title")
|
||||
assert response =~ "Hello World"
|
||||
assert response =~ post.title
|
||||
end
|
||||
|
||||
test "meta tags include og_description", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)
|
||||
post = a_live_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}") |> html_response(200)
|
||||
|
||||
assert response =~ ~s(<meta property="og:description")
|
||||
end
|
||||
|
||||
test "meta tags include og_type article", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)
|
||||
post = a_live_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}") |> html_response(200)
|
||||
|
||||
assert response =~ ~s(<meta property="og:type" content="article")
|
||||
end
|
||||
|
||||
test "meta tags include twitter:card", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)
|
||||
post = a_live_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}") |> html_response(200)
|
||||
|
||||
assert response =~ ~s(<meta name="twitter:card" content="summary_large_image")
|
||||
end
|
||||
@@ -101,10 +121,11 @@ defmodule FirehoseWeb.BlogControllerTest do
|
||||
end
|
||||
|
||||
test "meta tags have correct og_url", %{conn: conn} do
|
||||
response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)
|
||||
post = a_live_post()
|
||||
response = conn |> get(~p"/blog/engineering/#{post.id}") |> html_response(200)
|
||||
|
||||
assert response =~
|
||||
~s(<meta property="og:url" content="http://localhost:4002/blog/engineering/hello-world")
|
||||
~s(<meta property="og:url" content="http://localhost:4002/blog/engineering/#{post.id}")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,12 +1,25 @@
|
||||
defmodule FirehoseWeb.BlogTagsTest do
|
||||
use FirehoseWeb.ConnCase
|
||||
|
||||
# Content churns; the behavior under test does not. The newest live post
|
||||
# with a tag is a deterministic stand-in, and tag pages sort date-descending,
|
||||
# so it is on page 1 whatever else is published under that tag. (The pinned
|
||||
# "Hello World" post was deleted in 1709cb7 and this test silently rotted.)
|
||||
defp a_newest_live_post_with_tag do
|
||||
Enum.find(Firehose.EngineeringBlog.all_posts(), fn post ->
|
||||
Blogex.Post.visibility(post) == :live and post.tags != []
|
||||
end) || raise "no live tagged engineering post to exercise the tag page"
|
||||
end
|
||||
|
||||
describe "engineering blog tags" do
|
||||
test "GET /blog/engineering/tag/:tag shows tag page with filtered posts", %{conn: conn} do
|
||||
body = conn |> get("/blog/engineering/tag/elixir") |> html_response(200)
|
||||
assert body =~ ~s(tagged "elixir")
|
||||
post = a_newest_live_post_with_tag()
|
||||
tag = hd(post.tags)
|
||||
|
||||
body = conn |> get("/blog/engineering/tag/#{tag}") |> html_response(200)
|
||||
assert body =~ ~s(tagged "#{tag}")
|
||||
assert body =~ "Engineering Blog"
|
||||
assert body =~ "Hello World"
|
||||
assert body =~ post.title
|
||||
end
|
||||
|
||||
test "GET /blog/engineering/tag/:tag shows empty list for nonexistent tag", %{conn: conn} do
|
||||
|
||||
@@ -9,6 +9,14 @@ defmodule FirehoseWeb.BlogTest do
|
||||
body
|
||||
end
|
||||
|
||||
# Content churns; the behavior under test does not. The newest live post is
|
||||
# a deterministic stand-in - the pinned "hello-world" post was deleted in
|
||||
# 1709cb7 and the slug tests silently rotted with it.
|
||||
defp a_live_post do
|
||||
Enum.find(Firehose.EngineeringBlog.all_posts(), &(Blogex.Post.visibility(&1) == :live)) ||
|
||||
raise "no live engineering post to exercise the post pages"
|
||||
end
|
||||
|
||||
describe "engineering blog (HTML)" do
|
||||
test "GET /blog/engineering returns HTML index with layout", %{conn: conn} do
|
||||
body = visit_blog_page(conn, "engineering")
|
||||
@@ -17,8 +25,9 @@ defmodule FirehoseWeb.BlogTest do
|
||||
end
|
||||
|
||||
test "GET /blog/engineering/:slug returns HTML post with layout", %{conn: conn} do
|
||||
body = visit_blog_page(conn, "engineering", "/hello-world")
|
||||
assert body =~ "Hello World"
|
||||
post = a_live_post()
|
||||
body = visit_blog_page(conn, "engineering", "/#{post.id}")
|
||||
assert body =~ post.title
|
||||
end
|
||||
|
||||
test "GET /blog/engineering?page=2 shows the next page of older posts", %{conn: conn} do
|
||||
@@ -191,11 +200,16 @@ defmodule FirehoseWeb.BlogTest do
|
||||
end
|
||||
|
||||
test "GET /api/blog/engineering/:slug returns a post", %{conn: conn} do
|
||||
assert %{"id" => "hello-world", "title" => "Hello World"} =
|
||||
post = a_live_post()
|
||||
|
||||
assert %{"id" => id, "title" => title} =
|
||||
conn
|
||||
|> put_req_header("accept", "application/json")
|
||||
|> get("/api/blog/engineering/hello-world")
|
||||
|> get("/api/blog/engineering/#{post.id}")
|
||||
|> json_response(200)
|
||||
|
||||
assert id == post.id
|
||||
assert title == post.title
|
||||
end
|
||||
|
||||
test "GET /api/blog/engineering/:slug returns 404 for missing post", %{conn: conn} do
|
||||
|
||||
Reference in New Issue
Block a user