Delete test/plausible_web/plugins/api/controllers/shared_links_test.exs with huggingface_hub
Browse files
test/plausible_web/plugins/api/controllers/shared_links_test.exs
DELETED
|
@@ -1,304 +0,0 @@
|
|
| 1 |
-
defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinksTest do
|
| 2 |
-
use PlausibleWeb.PluginsAPICase, async: true
|
| 3 |
-
|
| 4 |
-
describe "examples" do
|
| 5 |
-
test "SharedLink" do
|
| 6 |
-
assert_schema(
|
| 7 |
-
Schemas.SharedLink.schema().example,
|
| 8 |
-
"SharedLink",
|
| 9 |
-
spec()
|
| 10 |
-
)
|
| 11 |
-
end
|
| 12 |
-
end
|
| 13 |
-
|
| 14 |
-
describe "unauthorized calls" do
|
| 15 |
-
for {method, url} <- [
|
| 16 |
-
{:get, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, 1)},
|
| 17 |
-
{:put, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)},
|
| 18 |
-
{:get, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index)}
|
| 19 |
-
] do
|
| 20 |
-
test "unauthorized call: #{method} #{url}", %{conn: conn} do
|
| 21 |
-
conn
|
| 22 |
-
|> unquote(method)(unquote(url))
|
| 23 |
-
|> json_response(401)
|
| 24 |
-
|> assert_schema("UnauthorizedError", spec())
|
| 25 |
-
end
|
| 26 |
-
end
|
| 27 |
-
end
|
| 28 |
-
|
| 29 |
-
describe "get /shared_links/:id" do
|
| 30 |
-
test "validates input out of the box", %{conn: conn, token: token, site: site} do
|
| 31 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, "hello")
|
| 32 |
-
|
| 33 |
-
resp =
|
| 34 |
-
conn
|
| 35 |
-
|> authenticate(site.domain, token)
|
| 36 |
-
|> get(url)
|
| 37 |
-
|> json_response(422)
|
| 38 |
-
|> assert_schema("UnprocessableEntityError", spec())
|
| 39 |
-
|
| 40 |
-
assert %{errors: [%{detail: "Invalid integer. Got: string"}]} = resp
|
| 41 |
-
end
|
| 42 |
-
|
| 43 |
-
test "retrieve shared link by ID", %{conn: conn, site: site, token: token} do
|
| 44 |
-
shared_link = insert(:shared_link, name: "Some Link Name", site: site)
|
| 45 |
-
|
| 46 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, shared_link.id)
|
| 47 |
-
|
| 48 |
-
resp =
|
| 49 |
-
conn
|
| 50 |
-
|> authenticate(site.domain, token)
|
| 51 |
-
|> get(url)
|
| 52 |
-
|> json_response(200)
|
| 53 |
-
|> assert_schema("SharedLink", spec())
|
| 54 |
-
|
| 55 |
-
assert resp.shared_link.href ==
|
| 56 |
-
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth=#{shared_link.slug}"
|
| 57 |
-
|
| 58 |
-
assert resp.shared_link.id == shared_link.id
|
| 59 |
-
assert resp.shared_link.password_protected == false
|
| 60 |
-
assert resp.shared_link.name == "Some Link Name"
|
| 61 |
-
end
|
| 62 |
-
|
| 63 |
-
test "fails to retrieve non-existing link", %{conn: conn, site: site, token: token} do
|
| 64 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, 666)
|
| 65 |
-
|
| 66 |
-
conn
|
| 67 |
-
|> authenticate(site.domain, token)
|
| 68 |
-
|> get(url)
|
| 69 |
-
|> json_response(404)
|
| 70 |
-
|> assert_schema("NotFoundError", spec())
|
| 71 |
-
end
|
| 72 |
-
|
| 73 |
-
test "fails to retrieve link from another site", %{conn: conn, site: site, token: token} do
|
| 74 |
-
shared_link = insert(:shared_link, name: "Some Link Name", site: build(:site))
|
| 75 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, shared_link.id)
|
| 76 |
-
|
| 77 |
-
conn
|
| 78 |
-
|> authenticate(site.domain, token)
|
| 79 |
-
|> get(url)
|
| 80 |
-
|> json_response(404)
|
| 81 |
-
|> assert_schema("NotFoundError", spec())
|
| 82 |
-
end
|
| 83 |
-
end
|
| 84 |
-
|
| 85 |
-
describe "put /shared_links" do
|
| 86 |
-
test "successfully creates a shared link with the location header", %{
|
| 87 |
-
conn: conn,
|
| 88 |
-
site: site,
|
| 89 |
-
token: token
|
| 90 |
-
} do
|
| 91 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
|
| 92 |
-
|
| 93 |
-
initial_conn = authenticate(conn, site.domain, token)
|
| 94 |
-
|
| 95 |
-
conn =
|
| 96 |
-
initial_conn
|
| 97 |
-
|> put_req_header("content-type", "application/json")
|
| 98 |
-
|> put(url, %{
|
| 99 |
-
shared_link: %{
|
| 100 |
-
name: "My Shared Link"
|
| 101 |
-
}
|
| 102 |
-
})
|
| 103 |
-
|
| 104 |
-
resp =
|
| 105 |
-
conn
|
| 106 |
-
|> json_response(201)
|
| 107 |
-
|> assert_schema("SharedLink", spec())
|
| 108 |
-
|
| 109 |
-
assert resp.shared_link.name == "My Shared Link"
|
| 110 |
-
|
| 111 |
-
assert resp.shared_link.href =~
|
| 112 |
-
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
|
| 113 |
-
|
| 114 |
-
[location] = get_resp_header(conn, "location")
|
| 115 |
-
|
| 116 |
-
assert location ==
|
| 117 |
-
Routes.plugins_api_shared_links_url(
|
| 118 |
-
PlausibleWeb.Endpoint,
|
| 119 |
-
:get,
|
| 120 |
-
resp.shared_link.id
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
assert ^resp =
|
| 124 |
-
initial_conn
|
| 125 |
-
|> get(location)
|
| 126 |
-
|> json_response(200)
|
| 127 |
-
|> assert_schema("SharedLink", spec())
|
| 128 |
-
end
|
| 129 |
-
|
| 130 |
-
test "create is idempotent", %{
|
| 131 |
-
conn: conn,
|
| 132 |
-
site: site,
|
| 133 |
-
token: token
|
| 134 |
-
} do
|
| 135 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
|
| 136 |
-
|
| 137 |
-
initial_conn = authenticate(conn, site.domain, token)
|
| 138 |
-
|
| 139 |
-
create = fn ->
|
| 140 |
-
initial_conn
|
| 141 |
-
|> put_req_header("content-type", "application/json")
|
| 142 |
-
|> put(url, %{
|
| 143 |
-
shared_link: %{
|
| 144 |
-
name: "My Shared Link"
|
| 145 |
-
}
|
| 146 |
-
})
|
| 147 |
-
end
|
| 148 |
-
|
| 149 |
-
conn = create.()
|
| 150 |
-
|
| 151 |
-
resp =
|
| 152 |
-
conn
|
| 153 |
-
|> json_response(201)
|
| 154 |
-
|> assert_schema("SharedLink", spec())
|
| 155 |
-
|
| 156 |
-
id = resp.shared_link.id
|
| 157 |
-
|
| 158 |
-
conn = create.()
|
| 159 |
-
|
| 160 |
-
assert ^id =
|
| 161 |
-
conn
|
| 162 |
-
|> json_response(201)
|
| 163 |
-
|> Map.fetch!("shared_link")
|
| 164 |
-
|> Map.fetch!("id")
|
| 165 |
-
end
|
| 166 |
-
|
| 167 |
-
test "validates input out of the box", %{conn: conn, token: token, site: site} do
|
| 168 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
|
| 169 |
-
|
| 170 |
-
resp =
|
| 171 |
-
conn
|
| 172 |
-
|> authenticate(site.domain, token)
|
| 173 |
-
|> put_req_header("content-type", "application/json")
|
| 174 |
-
|> put(url, %{})
|
| 175 |
-
|> json_response(422)
|
| 176 |
-
|> assert_schema("UnprocessableEntityError", spec())
|
| 177 |
-
|
| 178 |
-
assert %{errors: [%{detail: "Missing field: shared_link"}]} = resp
|
| 179 |
-
end
|
| 180 |
-
|
| 181 |
-
test "skips shared links feature access check", %{
|
| 182 |
-
conn: conn,
|
| 183 |
-
site: site,
|
| 184 |
-
token: token
|
| 185 |
-
} do
|
| 186 |
-
insert(:starter_subscription, team: site.team)
|
| 187 |
-
|
| 188 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
|
| 189 |
-
|
| 190 |
-
resp =
|
| 191 |
-
authenticate(conn, site.domain, token)
|
| 192 |
-
|> put_req_header("content-type", "application/json")
|
| 193 |
-
|> put(url, %{
|
| 194 |
-
shared_link: %{
|
| 195 |
-
name: "My Shared Link"
|
| 196 |
-
}
|
| 197 |
-
})
|
| 198 |
-
|> json_response(201)
|
| 199 |
-
|> assert_schema("SharedLink", spec())
|
| 200 |
-
|
| 201 |
-
assert resp.shared_link.name == "My Shared Link"
|
| 202 |
-
|
| 203 |
-
assert resp.shared_link.href =~
|
| 204 |
-
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
|
| 205 |
-
end
|
| 206 |
-
|
| 207 |
-
for special_name <- Plausible.Sites.shared_link_special_names() do
|
| 208 |
-
test "can create shared link with the reserved '#{special_name}' name", %{
|
| 209 |
-
conn: conn,
|
| 210 |
-
site: site,
|
| 211 |
-
token: token
|
| 212 |
-
} do
|
| 213 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
|
| 214 |
-
|
| 215 |
-
resp =
|
| 216 |
-
authenticate(conn, site.domain, token)
|
| 217 |
-
|> put_req_header("content-type", "application/json")
|
| 218 |
-
|> put(url, %{
|
| 219 |
-
shared_link: %{
|
| 220 |
-
name: unquote(special_name)
|
| 221 |
-
}
|
| 222 |
-
})
|
| 223 |
-
|> json_response(201)
|
| 224 |
-
|> assert_schema("SharedLink", spec())
|
| 225 |
-
|
| 226 |
-
assert resp.shared_link.name == unquote(special_name)
|
| 227 |
-
|
| 228 |
-
assert resp.shared_link.href =~
|
| 229 |
-
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
|
| 230 |
-
end
|
| 231 |
-
end
|
| 232 |
-
end
|
| 233 |
-
|
| 234 |
-
describe "get /shared_links" do
|
| 235 |
-
test "returns an empty shared link list if there's none", %{
|
| 236 |
-
conn: conn,
|
| 237 |
-
token: token,
|
| 238 |
-
site: site
|
| 239 |
-
} do
|
| 240 |
-
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index)
|
| 241 |
-
|
| 242 |
-
resp =
|
| 243 |
-
conn
|
| 244 |
-
|> authenticate(site.domain, token)
|
| 245 |
-
|> get(url)
|
| 246 |
-
|> json_response(200)
|
| 247 |
-
|> assert_schema("SharedLink.ListResponse", spec())
|
| 248 |
-
|
| 249 |
-
assert resp.shared_links == []
|
| 250 |
-
assert resp.meta.pagination.has_next_page == false
|
| 251 |
-
assert resp.meta.pagination.has_prev_page == false
|
| 252 |
-
assert resp.meta.pagination.links == %{}
|
| 253 |
-
end
|
| 254 |
-
|
| 255 |
-
test "returns a shared links list with pagination", %{
|
| 256 |
-
conn: conn,
|
| 257 |
-
token: token,
|
| 258 |
-
site: site
|
| 259 |
-
} do
|
| 260 |
-
for i <- 1..5 do
|
| 261 |
-
insert(:shared_link, site: site, name: "Shared Link #{i}")
|
| 262 |
-
end
|
| 263 |
-
|
| 264 |
-
url =
|
| 265 |
-
Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index, limit: 2)
|
| 266 |
-
|
| 267 |
-
initial_conn = authenticate(conn, site.domain, token)
|
| 268 |
-
|
| 269 |
-
page1 =
|
| 270 |
-
initial_conn
|
| 271 |
-
|> get(url)
|
| 272 |
-
|> json_response(200)
|
| 273 |
-
|> assert_schema("SharedLink.ListResponse", spec())
|
| 274 |
-
|
| 275 |
-
assert [%{shared_link: %{name: "Shared Link 5"}}, %{shared_link: %{name: "Shared Link 4"}}] =
|
| 276 |
-
page1.shared_links
|
| 277 |
-
|
| 278 |
-
assert page1.meta.pagination.has_next_page == true
|
| 279 |
-
assert page1.meta.pagination.has_prev_page == false
|
| 280 |
-
assert page1.meta.pagination.links.next
|
| 281 |
-
refute page1.meta.pagination.links[:prev]
|
| 282 |
-
|
| 283 |
-
page2 =
|
| 284 |
-
initial_conn
|
| 285 |
-
|> get(page1.meta.pagination.links.next.url)
|
| 286 |
-
|> json_response(200)
|
| 287 |
-
|> assert_schema("SharedLink.ListResponse", spec())
|
| 288 |
-
|
| 289 |
-
assert [%{shared_link: %{name: "Shared Link 3"}}, %{shared_link: %{name: "Shared Link 2"}}] =
|
| 290 |
-
page2.shared_links
|
| 291 |
-
|
| 292 |
-
assert page2.meta.pagination.has_next_page == true
|
| 293 |
-
assert page2.meta.pagination.has_prev_page == true
|
| 294 |
-
assert page2.meta.pagination.links.next
|
| 295 |
-
assert page2.meta.pagination.links.prev
|
| 296 |
-
|
| 297 |
-
assert ^page1 =
|
| 298 |
-
initial_conn
|
| 299 |
-
|> get(page2.meta.pagination.links.prev.url)
|
| 300 |
-
|> json_response(200)
|
| 301 |
-
|> assert_schema("SharedLink.ListResponse", spec())
|
| 302 |
-
end
|
| 303 |
-
end
|
| 304 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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