File size: 2,334 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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: ["recipient@email.com"])

      conn =
        get(conn, "/sites/#{site.domain}/weekly-report/unsubscribe?email=recipient@email.com")

      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/unsubscribe?email=recipient@email.com")

      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: ["recipient@email.com"])

      conn =
        get(conn, "/sites/#{site.domain}/monthly-report/unsubscribe?email=recipient@email.com")

      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/unsubscribe?email=recipient@email.com")

      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