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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
defmodule Plausible.Workers.TrafficChangeNotifierTest do
  use Plausible.DataCase, async: true
  use Bamboo.Test
  alias Plausible.Workers.TrafficChangeNotifier

  @view_dashboard_text "View dashboard"
  @review_installation_text "review your installation"

  describe "drops" do
    test "does not notify anyone if we've stopped accepting traffic for the owner" do
      user = new_user(team: [accept_traffic_until: Date.utc_today()])
      site = new_site(owner: user)

      insert(:drop_notification,
        site: site,
        threshold: 10,
        recipients: ["jerod@example.com", "uku@example.com"]
      )

      TrafficChangeNotifier.perform(nil)

      assert_no_emails_delivered()
    end

    test "does notify if threshold reached and we're accepting traffic" do
      user = new_user(team: [accept_traffic_until: Date.utc_today() |> Date.add(+1)])
      site = new_site(owner: user)

      insert(:drop_notification,
        site: site,
        threshold: 10,
        recipients: ["jerod@example.com"]
      )

      TrafficChangeNotifier.perform(nil)

      assert_email_delivered_with(
        subject: "Traffic drop on #{site.domain}",
        to: [nil: "jerod@example.com"]
      )
    end

    test "does not notify anyone if current visitors does not drop below notification threshold" do
      site = insert(:site)

      insert(:drop_notification,
        site: site,
        threshold: 2,
        recipients: ["jerod@example.com", "uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(2)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_no_emails_delivered()
    end

    test "notifies all recipients when traffic drops under configured threshold" do
      site = new_site()

      insert(:drop_notification,
        site: site,
        threshold: 10,
        recipients: ["jerod@example.com", "uku@example.com"]
      )

      TrafficChangeNotifier.perform(nil)

      assert_email_delivered_with(
        subject: "Traffic drop on #{site.domain}",
        to: [nil: "jerod@example.com"]
      )

      assert_email_delivered_with(
        subject: "Traffic drop on #{site.domain}",
        to: [nil: "uku@example.com"]
      )
    end

    test "includes dashboard and installation links only when recipient is guest or team member" do
      owner = new_user()
      {:ok, team} = Plausible.Teams.get_or_create(owner)
      site = new_site(team: team)
      team_member = add_member(team, role: :admin)
      viewer_guest = add_guest(site, role: :viewer, user: new_user())
      random_email = "random@example.com"

      insert(:drop_notification,
        site: site,
        threshold: 10,
        recipients: [owner.email, team_member.email, viewer_guest.email, random_email]
      )

      TrafficChangeNotifier.perform(nil)

      expected_subject = "Traffic drop on #{site.domain}"

      four_emails =
        for _ <- 1..4 do
          assert_delivered_email_matches(%{
            html_body: html_body,
            subject: ^expected_subject,
            to: [nil: email]
          })

          %{to: email, html_body: html_body}
        end

      {[random_recipient_email], site_member_emails} =
        Enum.split_with(four_emails, &(&1.to == random_email))

      refute random_recipient_email.html_body =~ @view_dashboard_text

      Enum.each(site_member_emails, fn email ->
        assert email.html_body =~ @view_dashboard_text
        assert email.html_body =~ @review_installation_text
      end)
    end

    on_ee do
      test "does not link to site installation in a consolidated view traffic drop notification" do
        %{email: user_email} = user = new_user()
        {:ok, team} = Plausible.Teams.get_or_create(user)
        new_site(team: team)
        new_site(team: team)

        consolidated_view = new_consolidated_view(team)

        insert(:drop_notification,
          site: consolidated_view,
          threshold: 10,
          recipients: [user.email]
        )

        TrafficChangeNotifier.perform(nil)

        assert_delivered_email_matches(%{
          html_body: html_body,
          subject: "Traffic drop on your sites",
          to: [nil: ^user_email]
        })

        assert html_body =~ "across all your sites"
        assert html_body =~ @view_dashboard_text
        refute html_body =~ @review_installation_text
      end

      test "does not notify traffic drop on consolidated view when ok_to_display? is false" do
        user = new_user()
        {:ok, team} = Plausible.Teams.get_or_create(user)
        site = new_site(team: team)
        new_site(team: team)

        consolidated_view = new_consolidated_view(team)

        Plausible.Repo.delete(site)

        refute Plausible.ConsolidatedView.ok_to_display?(team)

        insert(:drop_notification,
          site: consolidated_view,
          threshold: 10,
          recipients: [user.email]
        )

        TrafficChangeNotifier.perform(nil)

        assert_no_emails_delivered()
      end
    end

    test "does not send notifications more than once every 12 hours" do
      site = new_site()

      insert(:drop_notification,
        site: site,
        threshold: 1,
        recipients: ["uku@example.com"]
      )

      TrafficChangeNotifier.perform(nil, ~N[2021-01-01 00:00:00])

      assert_email_delivered_with(
        subject: "Traffic drop on #{site.domain}",
        to: [nil: "uku@example.com"]
      )

      TrafficChangeNotifier.perform(nil, ~N[2021-01-01 11:59:59])

      assert_no_emails_delivered()

      TrafficChangeNotifier.perform(nil, ~N[2021-01-01 12:00:01])

      assert_email_delivered_with(
        subject: "Traffic drop on #{site.domain}",
        to: [nil: "uku@example.com"]
      )
    end

    test "adds settings link if recipient has access to the site" do
      user = new_user(email: "robert@example.com")
      site = new_site(domain: "example.com", owner: user)

      insert(:drop_notification,
        site: site,
        threshold: 10,
        recipients: ["robert@example.com"]
      )

      TrafficChangeNotifier.perform(nil)

      assert_email_delivered_with(
        html_body: ~r|http://localhost:8000/example.com/installation\?flow=review|
      )
    end
  end

  describe "spikes" do
    test "does not notify anyone if current visitors does not exceed notification threshold" do
      site = insert(:site)

      insert(:spike_notification,
        site: site,
        threshold: 2,
        recipients: ["jerod@example.com", "uku@example.com"]
      )

      populate_stats(site, [build(:pageview, timestamp: minutes_ago(1))])

      TrafficChangeNotifier.perform(nil)

      assert_no_emails_delivered()
    end

    test "notifies all recipients when traffic is higher than configured threshold" do
      site = new_site()

      insert(:spike_notification,
        site: site,
        threshold: 2,
        recipients: ["jerod@example.com", "uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(3)),
        build(:pageview, timestamp: minutes_ago(2)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_email_delivered_with(
        subject: "Traffic spike on #{site.domain}",
        to: [nil: "jerod@example.com"]
      )

      assert_email_delivered_with(
        subject: "Traffic spike on #{site.domain}",
        to: [nil: "uku@example.com"]
      )
    end

    on_ee do
      test "notifies traffic spike on consolidated view" do
        {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
        site1 = new_site(team: team)
        site2 = new_site(team: team)

        consolidated_view = new_consolidated_view(team)

        insert(:spike_notification,
          site: consolidated_view,
          threshold: 2,
          recipients: ["uku@example.com"]
        )

        populate_stats(site1, [
          build(:pageview, referrer_source: "Google", pathname: "/b", timestamp: minutes_ago(1)),
          build(:pageview, referrer_source: "Google", pathname: "/a", timestamp: minutes_ago(2))
        ])

        populate_stats(site2, [
          build(:pageview, referrer_source: "Twitter", pathname: "/a", timestamp: minutes_ago(3))
        ])

        TrafficChangeNotifier.perform(nil)

        assert_delivered_email_matches(%{
          html_body: html_body,
          subject: "Traffic spike on your sites",
          to: [nil: "uku@example.com"]
        })

        assert html_body =~ "across all your sites"

        assert html_body =~ "The top sources for current visitors:"
        assert html_body =~ "<b>2</b> visitors from <b>Google</b>"
        assert html_body =~ "<b>1</b> visitor from <b>Twitter</b>"

        assert html_body =~ "Your top pages being visited:"
        assert html_body =~ "<b>2</b> visitors on <b>/a</b>"
        assert html_body =~ "<b>1</b> visitor on <b>/b</b>"
      end

      test "does not notify traffic spike on consolidated view when ok_to_display? is false" do
        user = new_user()
        {:ok, team} = Plausible.Teams.get_or_create(user)
        site1 = new_site(team: team)
        site2 = new_site(team: team)

        consolidated_view = new_consolidated_view(team)

        insert(:spike_notification,
          site: consolidated_view,
          threshold: 2,
          recipients: ["uku@example.com"]
        )

        populate_stats(site1, [
          build(:pageview, timestamp: minutes_ago(1)),
          build(:pageview, timestamp: minutes_ago(2)),
          build(:pageview, timestamp: minutes_ago(3))
        ])

        Plausible.Repo.delete(site2)

        refute Plausible.ConsolidatedView.ok_to_display?(team)

        insert(:drop_notification,
          site: consolidated_view,
          threshold: 10,
          recipients: [user.email]
        )

        TrafficChangeNotifier.perform(nil)

        assert_no_emails_delivered()
      end
    end

    test "ignores 'Direct / None' source" do
      site = new_site()

      insert(:spike_notification,
        site: site,
        threshold: 5,
        recipients: ["uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, referrer_source: "A", timestamp: minutes_ago(1)),
        build(:pageview, referrer_source: "A", timestamp: minutes_ago(1)),
        build(:pageview, referrer_source: "A", timestamp: minutes_ago(1)),
        build(:pageview, referrer_source: "B", timestamp: minutes_ago(1)),
        build(:pageview, referrer_source: "B", timestamp: minutes_ago(1)),
        build(:pageview, referrer_source: "C", timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_delivered_email_matches(%{
        to: [nil: "uku@example.com"],
        html_body: html_body
      })

      assert html_body =~ "The top sources for current visitors:"
      assert html_body =~ "<b>3</b> visitors from <b>A</b>"
      assert html_body =~ "<b>2</b> visitors from <b>B</b>"
      assert html_body =~ "<b>1</b> visitor from <b>C</b>"
      assert html_body =~ "There are currently <b>8</b>"
    end

    test "does not list sources at all when everything is 'Direct / None'" do
      site = new_site()

      insert(:spike_notification,
        site: site,
        threshold: 1,
        recipients: ["uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_delivered_email_matches(%{
        to: [nil: "uku@example.com"],
        html_body: html_body
      })

      refute html_body =~ "The top sources for current visitors:"
      assert html_body =~ "There are currently <b>2</b>"
    end

    test "includes top 3 pages" do
      site = new_site()

      insert(:spike_notification,
        site: site,
        threshold: 10,
        recipients: ["uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, pathname: "/one", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/one", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/one", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/one", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/two", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/two", timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/two", timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, pathname: "/not-this-one", timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_delivered_email_matches(%{
        to: [nil: "uku@example.com"],
        html_body: html_body
      })

      assert html_body =~ "There are currently <b>10</b>"

      assert html_body =~ "Your top pages being visited:"
      assert html_body =~ "<b>4</b> visitors on <b>/one</b>"
      assert html_body =~ "<b>3</b> visitors on <b>/two</b>"
      assert html_body =~ "<b>2</b> visitors on <b>/</b>"

      refute html_body =~ "/not-this-one"
    end

    test "does not check site if it is locked" do
      site = new_site()
      site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()

      insert(:spike_notification,
        site: site,
        threshold: 1,
        recipients: ["uku@example.com"]
      )

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_no_emails_delivered()
    end

    test "does not send notifications more than once every 12 hours" do
      site = new_site()
      insert(:spike_notification, site: site, threshold: 1, recipients: ["uku@example.com"])

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_email_delivered_with(
        subject: "Traffic spike on #{site.domain}",
        to: [nil: "uku@example.com"]
      )

      TrafficChangeNotifier.perform(nil)

      assert_no_emails_delivered()
    end

    test "adds a dashboard link if recipient has access to the site" do
      user = new_user(email: "robert@example.com")
      site = new_site(domain: "example.com", owner: user)
      insert(:spike_notification, site: site, threshold: 1, recipients: ["robert@example.com"])

      populate_stats(site, [
        build(:pageview, timestamp: minutes_ago(1)),
        build(:pageview, timestamp: minutes_ago(1))
      ])

      TrafficChangeNotifier.perform(nil)

      assert_delivered_email_matches(%{html_body: html_body})
      assert html_body =~ @view_dashboard_text
    end
  end

  def minutes_ago(min) do
    NaiveDateTime.utc_now(:second) |> NaiveDateTime.add(-min, :minute)
  end
end