File size: 5,646 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
defmodule Plausible.Workers.AcceptTrafficUntilTest do
  use Plausible.DataCase, async: true
  use Bamboo.Test

  alias Plausible.Workers.AcceptTrafficUntil

  @moduletag :ee_only

  test "does not send any notifications when sites have no stats" do
    today = Date.utc_today()
    next_week = today |> Date.add(+7)
    tomorrow = today |> Date.add(+1)

    user1 = new_user(team: [accept_traffic_until: next_week])

    user2 = new_user(team: [accept_traffic_until: tomorrow])

    _site1 = new_site(owner: user1)

    _site2 = new_site(owner: user2)

    {:ok, 2} = AcceptTrafficUntil.perform(nil)

    refute_notifications(user1.email)
    refute_notifications(user2.email)
  end

  test "does not send any notifications when site has stats older than 2d" do
    today = Date.utc_today()
    next_week = today |> Date.add(+7)

    user = new_user(team: [accept_traffic_until: next_week])

    new_site(owner: user)
    |> populate_stats([build(:pageview, timestamp: Date.add(today, -3))])

    {:ok, 1} = AcceptTrafficUntil.perform(nil)

    refute_notifications(user.email)
  end

  test "does send notification when last stat is 2d old" do
    today = Date.utc_today()
    next_week = today |> Date.add(+7)

    user = new_user(team: [accept_traffic_until: next_week])

    new_site(owner: user)
    |> populate_stats([build(:pageview, timestamp: Date.add(today, -2))])

    {:ok, 1} = AcceptTrafficUntil.perform(nil)

    assert_weekly_notification(user.email)
  end

  test "tomorrow: sends one e-mail" do
    tomorrow = Date.utc_today() |> Date.add(+1)
    user = new_user(team: [accept_traffic_until: tomorrow])

    new_site(owner: user) |> populate_stats([build(:pageview)])

    {:ok, 1} = AcceptTrafficUntil.perform(nil)
    assert_final_notification(user.email)
  end

  test "next week: sends one e-mail" do
    next_week = Date.utc_today() |> Date.add(+7)
    user = new_user(team: [accept_traffic_until: next_week])

    new_site(owner: user) |> populate_stats([build(:pageview)])

    {:ok, 1} = AcceptTrafficUntil.perform(nil)
    assert_weekly_notification(user.email)
  end

  test "sends combined warnings in one shot" do
    today = Date.utc_today()
    next_week = today |> Date.add(+7)
    tomorrow = today |> Date.add(+1)
    in_8_days = today |> Date.add(+8)

    user1 = new_user(team: [accept_traffic_until: next_week])
    user2 = new_user(team: [accept_traffic_until: tomorrow])
    user3 = new_user(team: [accept_traffic_until: in_8_days], email: "nope@example.com")
    user4 = new_user(team: [accept_traffic_until: today], email: "nope2@example.com")

    new_site(owner: user1) |> populate_stats([build(:pageview)])
    new_site(owner: user2) |> populate_stats([build(:pageview)])
    new_site(owner: user3) |> populate_stats([build(:pageview)])
    new_site(owner: user4) |> populate_stats([build(:pageview)])

    {:ok, 2} = AcceptTrafficUntil.perform(nil)
    assert_weekly_notification(user1.email)
    assert_final_notification(user2.email)
    refute_notifications("nope@example.com")
    refute_notifications("nope2@example.com")
  end

  test "sends multiple notifications per recipient as the time passes" do
    today = Date.utc_today()
    email = "cycle@example.com"

    user =
      new_user(
        email: email,
        trial_expiry_date: Plausible.Teams.Team.trial_expiry()
      )

    team = team_of(user)

    site = new_site(owner: user)

    populate_stats(site, [
      build(:pageview, timestamp: today),
      build(:pageview, timestamp: Date.add(team.trial_expiry_date, 7)),
      build(:pageview, timestamp: Date.add(team.trial_expiry_date, 8)),
      build(:pageview, timestamp: Date.add(team.trial_expiry_date, 13))
    ])

    # today's worker is no-op
    {:ok, 0} = AcceptTrafficUntil.perform(nil, today)
    refute_notifications(email)

    # trial_expiry + 7 days worker => we'll stop counting next week
    {:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 7))
    assert_weekly_notification(email)

    # another call on the same day == no-op, e-mail already sent
    {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 7))
    refute_notifications(email)

    # another call on another day == no-op, no notification type applies
    {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 8))
    refute_notifications(email)

    # day before accept_stats_until (trial_expiry_date + 14 - 1)
    {:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 13))
    assert_final_notification(email)

    # another one on the day before accept_stats_until (trial_expiry_date + 14 - 1) == no-op
    {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(team.trial_expiry_date, 13))
    refute_notifications(email)
  end

  defp assert_weekly_notification(email) when is_binary(email) do
    assert_email_delivered_with(
      html_body: ~r/Hey Jane,/,
      to: [nil: email],
      subject:
        PlausibleWeb.Email.approaching_accept_traffic_until(%{
          name: "",
          email: email,
          team: build(:team, identifier: Ecto.UUID.generate())
        }).subject
    )
  end

  defp assert_final_notification(email) when is_binary(email) do
    assert_email_delivered_with(
      html_body: ~r/Hey Jane,/,
      to: [nil: email],
      subject:
        PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{
          name: "",
          email: email,
          team: build(:team, identifier: Ecto.UUID.generate())
        }).subject
    )
  end

  defp refute_notifications(email) when is_binary(email) do
    refute_email_delivered_with(to: [nil: email])
  end
end