Leon4gr45 commited on
Commit
9463246
·
verified ·
1 Parent(s): ff73aca

Delete test/plausible_web/plugs/authorize_site_access_test.exs with huggingface_hub

Browse files
test/plausible_web/plugs/authorize_site_access_test.exs DELETED
@@ -1,393 +0,0 @@
1
- defmodule PlausibleWeb.Plugs.AuthorizeSiteAccessTest do
2
- use PlausibleWeb.ConnCase, async: false
3
- alias PlausibleWeb.Plugs.AuthorizeSiteAccess
4
-
5
- setup [:create_user, :log_in, :create_site]
6
-
7
- test "init rejects invalid role names" do
8
- assert_raise ArgumentError, fn ->
9
- AuthorizeSiteAccess.init(_allowed_roles = [:admin, :invalid])
10
- end
11
- end
12
-
13
- for init_argument <- [
14
- [],
15
- :all_roles,
16
- {:all_roles, nil},
17
- {[:public, :viewer, :admin, :editor, :super_admin, :owner, :billing], nil}
18
- ] do
19
- test "init resolves to expected options with argument #{inspect(init_argument)}" do
20
- assert {[:public, :viewer, :admin, :editor, :super_admin, :owner, :billing], nil} ==
21
- AuthorizeSiteAccess.init(unquote(init_argument))
22
- end
23
- end
24
-
25
- for invalid_site_param <- [[], 1, :invalid] do
26
- test "init rejects invalid site_param #{inspect(invalid_site_param)}" do
27
- assert_raise ArgumentError, fn ->
28
- AuthorizeSiteAccess.init({[:super_admin], unquote(invalid_site_param)})
29
- end
30
- end
31
- end
32
-
33
- test "returns 404 on non-existent site", %{conn: conn} do
34
- opts = AuthorizeSiteAccess.init(:all_roles)
35
-
36
- conn =
37
- conn
38
- |> bypass_through(PlausibleWeb.Router)
39
- |> get("/plug-tests/invalid.domain/with-domain")
40
- |> AuthorizeSiteAccess.call(opts)
41
-
42
- assert conn.halted
43
- assert html_response(conn, 404)
44
- end
45
-
46
- test "returns 404 on hacky :domain input", %{conn: conn} do
47
- opts = AuthorizeSiteAccess.init(:all_roles)
48
-
49
- conn =
50
- conn
51
- |> bypass_through(PlausibleWeb.Router)
52
- |> get("/plug-tests/%c0%ae/with-domain")
53
- |> AuthorizeSiteAccess.call(opts)
54
-
55
- assert conn.halted
56
- assert html_response(conn, 404)
57
- end
58
-
59
- test "rejects user completely unrelated to the site", %{conn: conn} do
60
- opts = AuthorizeSiteAccess.init(:all_roles)
61
-
62
- site = new_site()
63
-
64
- conn =
65
- conn
66
- |> bypass_through(PlausibleWeb.Router)
67
- |> get("/plug-tests/#{site.domain}/with-domain")
68
- |> AuthorizeSiteAccess.call(opts)
69
-
70
- assert conn.halted
71
- assert html_response(conn, 404)
72
- end
73
-
74
- test "can be configured to expect site domain at conn.params['some_key'], fails when this is not met",
75
- %{
76
- conn: conn,
77
- site: site
78
- } do
79
- opts =
80
- AuthorizeSiteAccess.init({:all_roles, "some_key"})
81
-
82
- conn =
83
- conn
84
- |> bypass_through(PlausibleWeb.Router)
85
- |> get("/plug-tests/basic", %{"wrong_key" => site.domain})
86
- |> AuthorizeSiteAccess.call(opts)
87
-
88
- assert conn.halted
89
- assert conn.status == 404
90
- end
91
-
92
- test "can be configured to expect site domain at conn.params['some_key'], succeeds when it is met",
93
- %{
94
- conn: conn,
95
- site: site
96
- } do
97
- opts =
98
- AuthorizeSiteAccess.init({:all_roles, "some_key"})
99
-
100
- conn =
101
- conn
102
- |> bypass_through(PlausibleWeb.Router)
103
- |> get("/plug-tests/basic", %{"some_key" => site.domain})
104
- |> AuthorizeSiteAccess.call(opts)
105
-
106
- refute conn.halted
107
- assert conn.assigns.site.id == site.id
108
- end
109
-
110
- test "doesn't allow bypassing :domain in path with :domain in query param", %{
111
- conn: conn,
112
- site: site
113
- } do
114
- other_site = new_site()
115
-
116
- opts = AuthorizeSiteAccess.init([:admin, :owner])
117
-
118
- conn =
119
- conn
120
- |> bypass_through(PlausibleWeb.Router)
121
- |> get("/plug-tests/#{other_site.domain}/with-domain", %{
122
- "domain" => site.domain
123
- })
124
- |> AuthorizeSiteAccess.call(opts)
125
-
126
- assert conn.halted
127
- assert conn.status == 404
128
- assert conn.path_params == %{"domain" => other_site.domain}
129
- end
130
-
131
- test "returns 404 with custom error message for failed API routes", %{conn: conn, user: user} do
132
- site = new_site()
133
- add_guest(site, user: user, role: :viewer)
134
-
135
- opts = AuthorizeSiteAccess.init([:admin, :owner, :super_admin])
136
-
137
- conn =
138
- conn
139
- |> bypass_through(PlausibleWeb.Router)
140
- |> get("/plug-tests/#{site.domain}/api-with-domain")
141
- |> AuthorizeSiteAccess.call(opts)
142
-
143
- assert conn.halted
144
-
145
- assert json_response(conn, 404) == %{
146
- "error" => "Site does not exist or user does not have sufficient access."
147
- }
148
- end
149
-
150
- on_ee do
151
- test "returns 404 for consolidated views by default", %{conn: conn, user: user} do
152
- {:ok, team} = Plausible.Teams.get_or_create(user)
153
- new_site(team: team)
154
- consolidated_view = new_consolidated_view(team)
155
-
156
- opts = AuthorizeSiteAccess.init([:super_admin, :admin, :owner])
157
-
158
- conn =
159
- conn
160
- |> bypass_through(PlausibleWeb.Router)
161
- |> get("/plug-tests/#{consolidated_view.domain}/with-domain")
162
- |> AuthorizeSiteAccess.call(opts)
163
-
164
- assert conn.halted
165
- assert conn.status == 404
166
- end
167
- end
168
-
169
- test "rejects unrelated shared link slug even if user is permitted for site", %{
170
- conn: conn,
171
- site: site
172
- } do
173
- shared_link_other_site = insert(:shared_link, site: new_site())
174
-
175
- params = %{"shared_link" => %{"name" => "some name"}}
176
-
177
- opts = AuthorizeSiteAccess.init([:super_admin, :admin, :owner])
178
-
179
- conn =
180
- conn
181
- |> bypass_through(PlausibleWeb.Router)
182
- |> get("/plug-tests/#{site.domain}/shared-link/#{shared_link_other_site.slug}", params)
183
- |> AuthorizeSiteAccess.call(opts)
184
-
185
- assert conn.halted
186
- assert conn.status == 404
187
- end
188
-
189
- test "rejects user not permitted to site trying to use unrelated shared link for another one",
190
- %{
191
- conn: conn,
192
- user: user,
193
- site: site
194
- } do
195
- shared_link = insert(:shared_link, site: site)
196
- other_site = new_site(owner: user)
197
-
198
- opts = AuthorizeSiteAccess.init([:super_admin, :admin, :owner])
199
-
200
- conn =
201
- conn
202
- |> bypass_through(PlausibleWeb.Router)
203
- |> get("/plug-tests/#{other_site.domain}/with-domain", %{"auth" => shared_link.slug})
204
- |> AuthorizeSiteAccess.call(opts)
205
-
206
- assert conn.halted
207
- assert conn.status == 404
208
- end
209
-
210
- test "doesn't allow bypassing :slug in path with :slug or :auth in query param", %{
211
- conn: conn,
212
- site: site
213
- } do
214
- shared_link = insert(:shared_link, site: site)
215
- shared_link_other_site = insert(:shared_link, site: build(:site))
216
-
217
- params = %{
218
- "shared_link" => %{"name" => "some name"},
219
- "slug" => shared_link.slug,
220
- "auth" => shared_link.slug
221
- }
222
-
223
- opts = AuthorizeSiteAccess.init([:super_admin, :admin, :owner])
224
-
225
- conn =
226
- conn
227
- |> bypass_through(PlausibleWeb.Router)
228
- |> get("/plug-tests/#{site.domain}/shared-link/#{shared_link_other_site.slug}", params)
229
- |> AuthorizeSiteAccess.call(opts)
230
-
231
- assert conn.halted
232
- assert conn.status == 404
233
- end
234
-
235
- test "rejects user on mismatched membership role", %{conn: conn, user: user} do
236
- site = new_site()
237
- add_guest(site, user: user, role: :editor)
238
-
239
- opts = AuthorizeSiteAccess.init([:owner])
240
-
241
- conn =
242
- conn
243
- |> bypass_through(PlausibleWeb.Router)
244
- |> get("/plug-tests/#{site.domain}/with-domain")
245
- |> AuthorizeSiteAccess.call(opts)
246
-
247
- assert conn.halted
248
- assert html_response(conn, 404)
249
- end
250
-
251
- test "allows user based on ownership", %{conn: conn, user: user} do
252
- site = new_site(owner: user)
253
-
254
- opts = AuthorizeSiteAccess.init([:owner])
255
-
256
- conn =
257
- conn
258
- |> bypass_through(PlausibleWeb.Router)
259
- |> get("/plug-tests/#{site.domain}/with-domain")
260
- |> AuthorizeSiteAccess.call(opts)
261
-
262
- refute conn.halted
263
- assert conn.assigns.site.id == site.id
264
- assert conn.assigns.site_role == :owner
265
- end
266
-
267
- for role <- [:viewer, :editor] do
268
- test "allows user based on their #{role} guest membership", %{conn: conn, user: user} do
269
- site = new_site()
270
- add_guest(site, user: user, role: unquote(role))
271
-
272
- opts = AuthorizeSiteAccess.init([unquote(role)])
273
-
274
- conn =
275
- conn
276
- |> bypass_through(PlausibleWeb.Router)
277
- |> get("/plug-tests/#{site.domain}/with-domain")
278
- |> AuthorizeSiteAccess.call(opts)
279
-
280
- # Does not set current team for guest membership
281
- refute get_session(conn, "current_team_id") == site.team.identifier
282
- refute conn.assigns.current_team.id == site.team.id
283
-
284
- refute conn.halted
285
- assert conn.assigns.site.id == site.id
286
- assert conn.assigns.site_role == unquote(role)
287
- end
288
- end
289
-
290
- for role <- [:viewer, :editor, :admin, :billing] do
291
- test "allows user based on their #{role} team membership", %{conn: conn, user: user} do
292
- site = new_site()
293
- add_member(site.team, user: user, role: unquote(role))
294
-
295
- opts = AuthorizeSiteAccess.init([unquote(role)])
296
-
297
- conn =
298
- conn
299
- |> bypass_through(PlausibleWeb.Router)
300
- |> get("/plug-tests/#{site.domain}/with-domain")
301
- |> AuthorizeSiteAccess.call(opts)
302
-
303
- # Sets current team for team membership
304
- assert get_session(conn, "current_team_id") == site.team.identifier
305
- assert conn.assigns.current_team.id == site.team.id
306
-
307
- refute conn.halted
308
- assert conn.assigns.site.id == site.id
309
- assert conn.assigns.site_role == unquote(role)
310
- end
311
- end
312
-
313
- @tag :ee_only
314
- test "allows user based on their superadmin status", %{conn: conn, user: user} do
315
- site = new_site()
316
-
317
- patch_env(:super_admin_user_ids, [user.id])
318
-
319
- opts = AuthorizeSiteAccess.init([:super_admin])
320
-
321
- conn =
322
- conn
323
- |> bypass_through(PlausibleWeb.Router)
324
- |> get("/plug-tests/#{site.domain}/with-domain")
325
- |> AuthorizeSiteAccess.call(opts)
326
-
327
- refute conn.halted
328
- assert conn.assigns.site.id == site.id
329
- assert conn.assigns.site_role == :super_admin
330
- end
331
-
332
- test "allows user based on website visibility (authenticated user)", %{conn: conn} do
333
- site = new_site(public: true)
334
-
335
- opts = AuthorizeSiteAccess.init([:public])
336
-
337
- conn =
338
- conn
339
- |> bypass_through(PlausibleWeb.Router)
340
- |> get("/plug-tests/#{site.domain}/with-domain")
341
- |> AuthorizeSiteAccess.call(opts)
342
-
343
- refute conn.halted
344
- assert conn.assigns.site.id == site.id
345
- end
346
-
347
- test "allows user based on website visibility (anonymous request)" do
348
- site = new_site(public: true)
349
-
350
- opts = AuthorizeSiteAccess.init([:public])
351
-
352
- conn =
353
- build_conn()
354
- |> bypass_through(PlausibleWeb.Router)
355
- |> get("/plug-tests/#{site.domain}/with-domain")
356
- |> AuthorizeSiteAccess.call(opts)
357
-
358
- refute conn.halted
359
- assert conn.assigns.site.id == site.id
360
- end
361
-
362
- test "allows user based on shared link auth (authenticated user)", %{conn: conn} do
363
- site = new_site()
364
- shared_link = insert(:shared_link, site: site)
365
-
366
- opts = AuthorizeSiteAccess.init([:public])
367
-
368
- conn =
369
- conn
370
- |> bypass_through(PlausibleWeb.Router)
371
- |> get("/plug-tests/#{site.domain}/with-domain", %{"auth" => shared_link.slug})
372
- |> AuthorizeSiteAccess.call(opts)
373
-
374
- refute conn.halted
375
- assert conn.assigns.site.id == site.id
376
- end
377
-
378
- test "allows user based on shared link auth (anonymous request)" do
379
- site = new_site()
380
- shared_link = insert(:shared_link, site: site)
381
-
382
- opts = AuthorizeSiteAccess.init([:public])
383
-
384
- conn =
385
- build_conn()
386
- |> bypass_through(PlausibleWeb.Router)
387
- |> get("/plug-tests/#{site.domain}/with-domain", %{"auth" => shared_link.slug})
388
- |> AuthorizeSiteAccess.call(opts)
389
-
390
- refute conn.halted
391
- assert conn.assigns.site.id == site.id
392
- end
393
- end