Leon4gr45 commited on
Commit
d771370
·
verified ·
1 Parent(s): 74a3a02

Delete test/plausible_web/controllers/api/stats_controller/authorization_test.exs with huggingface_hub

Browse files
test/plausible_web/controllers/api/stats_controller/authorization_test.exs DELETED
@@ -1,224 +0,0 @@
1
- defmodule PlausibleWeb.Api.StatsController.AuthorizationTest do
2
- use PlausibleWeb.ConnCase, async: true
3
-
4
- defp query_visitors(conn, site, params) do
5
- params =
6
- [date_range: "day", metrics: ["visitors"], filters: []]
7
- |> Keyword.merge(params)
8
-
9
- post(conn, Routes.stats_path(conn, :query, site.domain), params)
10
- end
11
-
12
- describe "API authorization - as anonymous user" do
13
- test "returns 404 for a site that doesn't exist", %{conn: conn} do
14
- conn = init_session(conn)
15
-
16
- conn =
17
- post(conn, "/api/stats/fake-site.com/query", %{})
18
-
19
- assert json_response(conn, 404) == %{
20
- "error" => "Site does not exist or user does not have sufficient access."
21
- }
22
- end
23
-
24
- test "returns 404 for private site", %{conn: conn} do
25
- conn = init_session(conn)
26
- site = insert(:site, public: false)
27
-
28
- conn =
29
- post(conn, "/api/stats/#{site.domain}/query", %{})
30
-
31
- assert json_response(conn, 404) == %{
32
- "error" => "Site does not exist or user does not have sufficient access."
33
- }
34
- end
35
-
36
- test "returns stats for public site", %{conn: conn} do
37
- conn = init_session(conn)
38
- site = insert(:site, public: true)
39
-
40
- conn =
41
- query_visitors(conn, site, [])
42
-
43
- assert %{"results" => _} = json_response(conn, 200)
44
- end
45
- end
46
-
47
- describe "API authorization for shared links - as anonymous user" do
48
- test "returns 404 for non-existent shared link", %{conn: conn} do
49
- site = new_site()
50
-
51
- conn = query_visitors(conn, site, auth: "does-not-exist")
52
-
53
- assert json_response(conn, 404) == %{
54
- "error" => "Site does not exist or user does not have sufficient access."
55
- }
56
- end
57
-
58
- test "returns 200 for unlisted shared link without cookie", %{conn: conn} do
59
- site = new_site()
60
- link = insert(:shared_link, site: site)
61
-
62
- conn = query_visitors(conn, site, auth: link.slug)
63
-
64
- assert %{"results" => _any} = json_response(conn, 200)
65
- end
66
-
67
- test "returns 200 for password-protected link with valid cookie", %{conn: conn} do
68
- site = new_site()
69
-
70
- link =
71
- insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
72
-
73
- token = Plausible.Auth.Token.sign_shared_link(link.slug)
74
- cookie_name = "shared-link-" <> link.slug
75
-
76
- conn =
77
- conn
78
- |> put_req_cookie(cookie_name, token)
79
- |> query_visitors(site, auth: link.slug)
80
-
81
- assert %{"results" => _any} = json_response(conn, 200)
82
- end
83
-
84
- test "returns 404 for password-protected link with invalid cookie value", %{conn: conn} do
85
- site = new_site()
86
-
87
- link =
88
- insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
89
-
90
- other_link =
91
- insert(:shared_link,
92
- name: "other link",
93
- site: site,
94
- password_hash: Plausible.Auth.Password.hash("password")
95
- )
96
-
97
- other_link_token = Plausible.Auth.Token.sign_shared_link(other_link.slug)
98
- cookie_name = "shared-link-" <> link.slug
99
-
100
- conn =
101
- conn
102
- |> put_req_cookie(cookie_name, other_link_token)
103
- |> query_visitors(site, auth: link.slug)
104
-
105
- assert json_response(conn, 404) == %{
106
- "error" => "Site does not exist or user does not have sufficient access."
107
- }
108
- end
109
-
110
- test "returns 404 for password-protected link without cookie", %{conn: conn} do
111
- site = new_site()
112
-
113
- link =
114
- insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
115
-
116
- conn = query_visitors(conn, site, auth: link.slug)
117
-
118
- assert json_response(conn, 404) == %{
119
- "error" => "Site does not exist or user does not have sufficient access."
120
- }
121
- end
122
- end
123
-
124
- describe "Filter validation for limited view shared links" do
125
- test "returns 400 for limited view shared link if there are no filters", %{conn: conn} do
126
- site = new_site()
127
-
128
- segment = insert(:segment, site: site, type: :site, name: "Scandinavia")
129
-
130
- link =
131
- insert(:shared_link, site: site, segment: segment)
132
-
133
- conn = query_visitors(conn, site, auth: link.slug)
134
-
135
- assert json_response(conn, 400) == %{
136
- "error" => "The first filter must be for the segment with id #{segment.id}"
137
- }
138
- end
139
-
140
- test "returns 400 for limited view shared link if the segment filter is not for the right segment",
141
- %{conn: conn} do
142
- site = new_site()
143
-
144
- segment = insert(:segment, site: site, type: :site, name: "Scandinavia")
145
-
146
- link =
147
- insert(:shared_link, site: site, segment: segment)
148
-
149
- conn =
150
- query_visitors(conn, site,
151
- auth: link.slug,
152
- filters: [["is", "segment", [segment.id + 1]]]
153
- )
154
-
155
- assert json_response(conn, 400) == %{
156
- "error" => "The first filter must be for the segment with id #{segment.id}"
157
- }
158
- end
159
-
160
- test "returns 200 for limited view shared link if the required segment is present, permits other filters to be applied",
161
- %{conn: conn} do
162
- site = new_site()
163
-
164
- segment = insert(:segment, site: site, type: :site, name: "Scandinavia")
165
-
166
- link =
167
- insert(:shared_link, site: site, segment: segment)
168
-
169
- conn =
170
- query_visitors(conn, site,
171
- auth: link.slug,
172
- filters: [
173
- ["is", "segment", [segment.id]],
174
- ["is", "event:page", ["/docs"]]
175
- ]
176
- )
177
-
178
- assert json_response(conn, 200)
179
- end
180
- end
181
-
182
- describe "API authorization - as logged in user" do
183
- setup [:create_user, :log_in]
184
-
185
- test "returns 404 for a site that doesn't exist", %{conn: conn} do
186
- conn = init_session(conn)
187
-
188
- conn =
189
- post(conn, "/api/stats/fake-site.com/query", %{})
190
-
191
- assert json_response(conn, 404) == %{
192
- "error" => "Site does not exist or user does not have sufficient access."
193
- }
194
- end
195
-
196
- test "returns 404 when user does not have access to site", %{conn: conn} do
197
- site = new_site()
198
-
199
- conn =
200
- post(conn, "/api/stats/#{site.domain}/query", %{})
201
-
202
- assert json_response(conn, 404) == %{
203
- "error" => "Site does not exist or user does not have sufficient access."
204
- }
205
- end
206
-
207
- test "returns stats for public site", %{conn: conn} do
208
- site = new_site(public: true)
209
-
210
- conn =
211
- query_visitors(conn, site, [])
212
-
213
- assert %{"results" => _} = json_response(conn, 200)
214
- end
215
-
216
- test "returns stats for a private site that the user owns", %{conn: conn, user: user} do
217
- site = new_site(public: false, owner: user)
218
-
219
- conn = query_visitors(conn, site, [])
220
-
221
- assert %{"results" => _} = json_response(conn, 200)
222
- end
223
- end
224
- end