Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

2.3 KB
Raw Blame
defmodule PlausibleWeb.UnsubscribeControllerTest do
  use PlausibleWeb.ConnCase, async: true
  use Plausible.Repo

  setup {PlausibleWeb.FirstLaunchPlug.Test, :skip}

  describe "GET /sites/:domain/weekly-report/unsubscribe" do
    test "removes a recipient from the weekly report without them having to log in", %{conn: conn} do
      site = new_site()
      insert(:weekly_report, site: site, recipients: ["[email protected]"])

      conn =
        get(conn, "/sites/#{site.domain}/weekly-report/[email protected]")

      assert html_response(conn, 200) =~ "Unsubscribe successful"

      report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
      assert report.recipients == []
    end

    test "renders success if site or weekly report does not exist in the database", %{conn: conn} do
      conn =
        get(conn, "/sites/nonexistent.com/weekly-report/[email protected]")

      assert html_response(conn, 200) =~ "Unsubscribe successful"
    end

    test "renders failure if email parameter not provided", %{conn: conn} do
      conn =
        get(conn, "/sites/nonexistent.com/weekly-report/unsubscribe")

      assert html_response(conn, 400) =~ "Bad Request"
    end
  end

  describe "GET /sites/:domain/monthly-report/unsubscribe" do
    test "removes a recipient from the weekly report without them having to log in", %{conn: conn} do
      site = new_site()
      insert(:monthly_report, site: site, recipients: ["[email protected]"])

      conn =
        get(conn, "/sites/#{site.domain}/monthly-report/[email protected]")

      assert html_response(conn, 200) =~ "Unsubscribe successful"

      report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
      assert report.recipients == []
    end

    test "renders success if site or weekly report does not exist in the database", %{conn: conn} do
      conn =
        get(conn, "/sites/nonexistent.com/monthly-report/[email protected]")

      assert html_response(conn, 200) =~ "Unsubscribe successful"
    end

    test "renders failure if email parameter not provided", %{conn: conn} do
      conn =
        get(conn, "/sites/nonexistent.com/monthly-report/unsubscribe")

      assert html_response(conn, 400) =~ "Bad Request"
    end
  end
end