File size: 10,180 Bytes
c9c49ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
defmodule Plausible.PropsTest do
  use Plausible.DataCase

  test "allow/2 returns error when user plan does not include props" do
    user = new_user() |> subscribe_to_growth_plan()
    site = new_site(owner: user)

    assert {:error, :upgrade_required} = Plausible.Props.allow(site, "my-prop-1")
    assert %Plausible.Site{allowed_event_props: nil} = Plausible.Repo.reload!(site)
  end

  test "allow/2 adds props to the array" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-2")

    assert %Plausible.Site{allowed_event_props: ["my-prop-2", "my-prop-1"]} =
             Plausible.Repo.reload!(site)
  end

  test "allow/2 takes a single prop or multiple" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert {:ok, site} = Plausible.Props.allow(site, ["my-prop-3", "my-prop-2"])

    assert %Plausible.Site{allowed_event_props: ["my-prop-3", "my-prop-2", "my-prop-1"]} =
             Plausible.Repo.reload!(site)
  end

  test "allow/2 trims trailing whitespaces" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "   my-prop-1 ")
    assert %Plausible.Site{allowed_event_props: ["my-prop-1"]} = Plausible.Repo.reload!(site)
  end

  test "allow/2 fails when prop list is too long" do
    site = new_site()
    props = for i <- 1..300, do: "my-prop-#{i}"

    assert {:ok, site} = Plausible.Props.allow(site, props)
    assert {:error, changeset} = Plausible.Props.allow(site, "my-prop-301")

    assert {"should have at most %{count} item(s)",
            [count: 300, validation: :length, kind: :max, type: :list]} ==
             changeset.errors[:allowed_event_props]
  end

  test "allow/2 fails when prop key is too long" do
    site = new_site()

    long_prop = String.duplicate("a", 301)
    assert {:error, changeset} = Plausible.Props.allow(site, long_prop)

    assert {"must be between 1 and 300 characters", [validation: :length, type: :string]} ==
             changeset.errors[:allowed_event_props]
  end

  test "allow/2 fails when prop key is empty" do
    site = new_site()

    assert {:error, changeset} = Plausible.Props.allow(site, "")

    assert {"must be between 1 and 300 characters", [validation: :length, type: :string]} ==
             changeset.errors[:allowed_event_props]

    assert {:error, changeset} = Plausible.Props.allow(site, " ")

    assert {"must be between 1 and 300 characters", [validation: :length, type: :string]} ==
             changeset.errors[:allowed_event_props]
  end

  test "allow/2 does not fail when prop key is already in the list" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert %Plausible.Site{allowed_event_props: ["my-prop-1"]} = Plausible.Repo.reload!(site)
  end

  test "disallow/2 removes the prop from the array" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-2")
    assert {:ok, site} = Plausible.Props.disallow(site, "my-prop-2")
    assert %Plausible.Site{allowed_event_props: ["my-prop-1"]} = Plausible.Repo.reload!(site)
  end

  test "disallow/2 does not fail when prop is not in the list" do
    site = new_site()

    assert {:ok, site} = Plausible.Props.allow(site, "my-prop-1")
    assert {:ok, site} = Plausible.Props.disallow(site, "my-prop-2")
    assert %Plausible.Site{allowed_event_props: ["my-prop-1"]} = Plausible.Repo.reload!(site)
  end

  test "allow_existing_props/2 returns error when user plan does not include props" do
    user = new_user() |> subscribe_to_growth_plan()
    site = new_site(owner: user)

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount"],
        "meta.value": ["500"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in"],
        "meta.value": ["100", "false"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer"],
        "meta.value": ["100", "false"]
      )
    ])

    assert {:error, :upgrade_required} = Plausible.Props.allow_existing_props(site)
    assert %Plausible.Site{allowed_event_props: nil} = Plausible.Repo.reload!(site)
  end

  test "allow_existing_props/1 saves the most frequent prop keys" do
    site = new_site()

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount"],
        "meta.value": ["500"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in"],
        "meta.value": ["100", "false"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer"],
        "meta.value": ["100", "false"]
      )
    ])

    {:ok, site} = Plausible.Props.allow_existing_props(site)

    assert %Plausible.Site{allowed_event_props: ["amount", "logged_in", "is_customer"]} =
             Plausible.Repo.reload!(site)
  end

  test "allow_existing_props/1 skips invalid keys" do
    site = new_site()

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount", String.duplicate("a", 301)],
        "meta.value": ["500", "true"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in"],
        "meta.value": ["100", "false"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer"],
        "meta.value": ["100", "false"]
      )
    ])

    {:ok, site} = Plausible.Props.allow_existing_props(site)

    assert %Plausible.Site{allowed_event_props: ["amount", "logged_in", "is_customer"]} =
             Plausible.Repo.reload!(site)
  end

  test "allow_existing_props/1 can be run multiple times" do
    site = new_site()

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount"],
        "meta.value": ["500"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in"],
        "meta.value": ["100", "false"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer"],
        "meta.value": ["100", "false"]
      )
    ])

    {:ok, %Plausible.Site{allowed_event_props: ["amount", "logged_in", "is_customer"]}} =
      Plausible.Props.allow_existing_props(site)

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "os"],
        "meta.value": ["500", "Windows"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in", "with_error"],
        "meta.value": ["100", "false", "true"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer", "first_time_customer"],
        "meta.value": ["100", "false", "true"]
      )
    ])

    {:ok,
     %Plausible.Site{
       allowed_event_props: [
         "amount",
         "logged_in",
         "is_customer",
         "os",
         "with_error",
         "first_time_customer"
       ]
     }} = Plausible.Props.allow_existing_props(site)
  end

  test "suggest_keys_to_allow/2 returns prop keys from events" do
    site = new_site()

    populate_stats(site, [
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "os"],
        "meta.value": ["500", "Windows"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "logged_in", "with_error"],
        "meta.value": ["100", "false", "true"]
      ),
      build(:event,
        name: "Payment",
        "meta.key": ["amount", "is_customer", "first_time_customer"],
        "meta.value": ["100", "false", "true"]
      )
    ])

    assert ["amount", "first_time_customer", "is_customer", "logged_in", "os", "with_error"] ==
             site |> Plausible.Props.suggest_keys_to_allow() |> Enum.sort()
  end

  on_ee do
    test "suggest_keys_to_allow/2 for consolidated view queries events across all included sites" 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)

      populate_stats(site1, [
        build(:pageview, "meta.key": ["a", "b"], "meta.value": ["A", "B"])
      ])

      populate_stats(site2, [
        build(:pageview, "meta.key": ["a", "c"], "meta.value": ["A", "C"])
      ])

      assert ["a", "b", "c"] ==
               Plausible.Props.suggest_keys_to_allow(consolidated_view) |> Enum.sort()
    end
  end

  test "suggest_keys_to_allow/2 does not return internal prop keys from special event types" do
    site = new_site()

    populate_stats(site, [
      build(:event,
        name: "File Download",
        "meta.key": ["url", "logged_in"],
        "meta.value": ["http://file.test", "false"]
      ),
      build(:event,
        name: "Outbound Link: Click",
        "meta.key": ["url", "first_time_customer"],
        "meta.value": ["http://link.test", "true"]
      ),
      build(:event,
        name: "404",
        "meta.key": ["path", "with_error"],
        "meta.value": ["/i-dont-exist", "true"]
      ),
      build(:event,
        name: "WP Search Queries",
        "meta.key": ["search_query", "result_count"],
        "meta.value": ["something", "12"]
      ),
      build(:event,
        name: "WP Form Completions",
        "meta.key": ["path"],
        "meta.value": ["/contact"]
      )
    ])

    assert ["first_time_customer", "logged_in", "result_count", "with_error"] ==
             site |> Plausible.Props.suggest_keys_to_allow() |> Enum.sort()
  end

  test "configured?/1 returns whether the site has allow at least one prop" do
    site = new_site()
    refute Plausible.Props.configured?(site)

    {:ok, site} = Plausible.Props.allow(site, "hello-world")
    assert Plausible.Props.configured?(site)

    {:ok, site} = Plausible.Props.disallow(site, "hello-world")
    refute Plausible.Props.configured?(site)
  end
end