Skip to content

Commit

Permalink
Redirect to homepage after podcast unsubscribe
Browse files Browse the repository at this point in the history
This fixes the case where News unsubs don't realize
they got unsubbed because News doesn't display flash
  • Loading branch information
jerodsanto committed Aug 27, 2024
1 parent 713e0d2 commit d8296ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/changelog_web/controllers/home/home_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule ChangelogWeb.HomeController do

conn
|> put_flash(:success, "You're unsubscribed! Resubscribe any time 🤗")
|> redirect(to: ~p"/#{podcast.slug}")
|> redirect(to: ~p"/")
end

defp opt_out_setting(conn, person, setting) do
Expand Down
51 changes: 27 additions & 24 deletions test/changelog_web/controllers/home/home_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,41 @@ defmodule ChangelogWeb.HomeControllerTest do

@tag :as_user
test "renders the home page", %{conn: conn} do
conn = get(conn, Routes.home_path(conn, :show))
conn = get(conn, ~p"/~")
assert html_response(conn, 200)
end

@tag :as_user
test "renders the account form", %{conn: conn} do
conn = get(conn, Routes.home_path(conn, :account))
conn = get(conn, ~p"/~/account")
assert html_response(conn, 200) =~ "form"
end

@tag :as_user
test "renders the profile form", %{conn: conn} do
conn = get(conn, Routes.home_path(conn, :profile))
conn = get(conn, ~p"/~/profile")
assert html_response(conn, 200) =~ "form"
end

@tag :as_inserted_user
test "updates person and redirects to home page", %{conn: conn} do
conn =
put(conn, Routes.home_path(conn, :update), from: "account", person: %{name: "New Name"})
put(conn, ~p"/~", from: "account", person: %{name: "New Name"})

assert redirected_to(conn) == Routes.home_path(conn, :show)
assert redirected_to(conn) == ~p"/~"
end

@tag :as_inserted_user
test "does not update with invalid attributes", %{conn: conn} do
conn = put(conn, Routes.home_path(conn, :update), from: "profile", person: %{name: ""})
conn = put(conn, ~p"/~", from: "profile", person: %{name: ""})
assert html_response(conn, 200) =~ ~r/problem/
end

test "opting out of notifications", %{conn: conn} do
person = insert(:person)
{:ok, token} = Person.encoded_id(person)
conn = post(conn, Routes.home_path(conn, :opt_out, token, "setting", "email_on_authored_news"))
conn = post(conn, ~p"/~/nope/#{token}/setting/email_on_authored_news")

assert conn.status == 200
refute Repo.get(Person, person.id).settings.email_on_authored_news
end
Expand All @@ -50,41 +51,43 @@ defmodule ChangelogWeb.HomeControllerTest do
podcast = insert(:podcast)
sub = insert(:subscription_on_podcast, person: person, podcast: podcast)
{:ok, token} = Person.encoded_id(person)
conn = post(conn, Routes.home_path(conn, :opt_out, token, "podcast", podcast.slug))
assert redirected_to(conn) == Routes.podcast_path(conn, :show, podcast.slug)
conn = post(conn, ~p"/~/nope/#{token}/podcast/#{podcast.slug}")

assert redirected_to(conn) == ~p"/"
refute Subscription.is_subscribed(Repo.get(Subscription, sub.id))
end

test "opting out of a podcast subscription with an invalid token", %{conn: conn} do
podcast = insert(:podcast)
conn = post(conn, Routes.home_path(conn, :opt_out, "S6LDYS205P5OZ51JSATUT4C", "podcast", podcast.slug))
assert redirected_to(conn) == Routes.podcast_path(conn, :show, podcast.slug)
conn = post(conn, ~p"/~/nope/S6LDYS205P5OZ51JSATUT4C/podcast/#{podcast.slug}")

assert redirected_to(conn) == ~p"/"
end

test "opting out of a news subscription", %{conn: conn} do
person = insert(:person)
item = insert(:news_item)
sub = insert(:subscription_on_podcast, person: person, item: item)
{:ok, token} = Person.encoded_id(person)
conn = post(conn, Routes.home_path(conn, :opt_out, token, "news", item.id))
assert redirected_to(conn) == Routes.news_item_path(conn, :show, NewsItem.slug(item))
conn = post(conn, ~p"/~/nope/#{token}/news/#{item.id}")
assert redirected_to(conn) == ~p"/news/#{NewsItem.slug(item)}"
refute Subscription.is_subscribed(Repo.get(Subscription, sub.id))
end

test "muting a news discussion", %{conn: conn} do
person = insert(:person)
item = insert(:news_item)
{:ok, token} = Person.encoded_id(person)
conn = post(conn, Routes.home_path(conn, :opt_out, token, "news", item.id))
assert redirected_to(conn) == Routes.news_item_path(conn, :show, NewsItem.slug(item))
conn = post(conn, ~p"/~/nope/#{token}/news/#{item.id}")
assert redirected_to(conn) == ~p"/news/#{NewsItem.slug(item)}"
assert Subscription.is_unsubscribed(person, item)
end

@tag :as_inserted_user
test "subscribing to Changelog Nightly", %{conn: conn} do
with_mock(Craisin.Subscriber, subscribe: fn _, _ -> true end) do
nightly_id = Newsletters.nightly().id
conn = post(conn, Routes.home_path(conn, :subscribe, id: nightly_id))
conn = post(conn, ~p"/~/subscribe?id=#{nightly_id}")
assert called(Craisin.Subscriber.subscribe(nightly_id, :_))
assert conn.status == 302
assert count(Subscription.subscribed()) == 0
Expand All @@ -94,7 +97,7 @@ defmodule ChangelogWeb.HomeControllerTest do
@tag :as_inserted_user
test "subscribing to a podcast", %{conn: conn} do
podcast = insert(:podcast)
conn = post(conn, Routes.home_path(conn, :subscribe, slug: podcast.slug))
conn = post(conn, ~p"/~/subscribe?slug=#{podcast.slug}")
assert conn.status == 200
assert count(Subscription.subscribed()) == 1
end
Expand All @@ -104,7 +107,7 @@ defmodule ChangelogWeb.HomeControllerTest do
podcast = insert(:podcast)
insert(:subscription_on_podcast, podcast: podcast, person: conn.assigns.current_user)
assert count(Subscription.subscribed()) == 1
conn = post(conn, Routes.home_path(conn, :unsubscribe, slug: podcast.slug))
conn = post(conn, ~p"/~/unsubscribe?slug=#{podcast.slug}")
assert conn.status == 200
assert count(Subscription.subscribed()) == 0
end
Expand All @@ -113,7 +116,7 @@ defmodule ChangelogWeb.HomeControllerTest do
test "unsubscribing from Changelog Nightly", %{conn: conn} do
with_mock(Craisin.Subscriber, unsubscribe: fn _, _ -> true end) do
nightly_id = Newsletters.nightly().id
conn = post(conn, Routes.home_path(conn, :unsubscribe, id: nightly_id))
conn = post(conn, ~p"/~/unsubscribe?id=#{nightly_id}")
assert called(Craisin.Subscriber.unsubscribe(nightly_id, :_))
assert conn.status == 302
end
Expand All @@ -125,7 +128,7 @@ defmodule ChangelogWeb.HomeControllerTest do
{:ok, token} = Person.encoded_id(person)

conn =
post(conn, Routes.home_path(conn, :opt_out, token, "setting", "email_on_submitted_news"))
post(conn, ~p"/~/nope/#{token}/setting/email_on_submitted_news")

assert conn.status == 200
refute Repo.get(Person, person.id).settings.email_on_submitted_news
Expand All @@ -134,10 +137,10 @@ defmodule ChangelogWeb.HomeControllerTest do
test "requires user on all actions except email links", %{conn: conn} do
Enum.each(
[
get(conn, Routes.home_path(conn, :show)),
get(conn, Routes.home_path(conn, :profile)),
get(conn, Routes.home_path(conn, :account)),
put(conn, Routes.home_path(conn, :update), person: %{})
get(conn, ~p"/~"),
get(conn, ~p"/~/profile"),
get(conn, ~p"/~/account"),
put(conn, ~p"/~", person: %{})
],
fn conn ->
assert html_response(conn, 302)
Expand Down

0 comments on commit d8296ed

Please sign in to comment.