File size: 9,248 Bytes
6778ee0 | 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 | defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinksTest do
use PlausibleWeb.PluginsAPICase, async: true
describe "examples" do
test "SharedLink" do
assert_schema(
Schemas.SharedLink.schema().example,
"SharedLink",
spec()
)
end
end
describe "unauthorized calls" do
for {method, url} <- [
{:get, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, 1)},
{:put, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)},
{:get, Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index)}
] do
test "unauthorized call: #{method} #{url}", %{conn: conn} do
conn
|> unquote(method)(unquote(url))
|> json_response(401)
|> assert_schema("UnauthorizedError", spec())
end
end
end
describe "get /shared_links/:id" do
test "validates input out of the box", %{conn: conn, token: token, site: site} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, "hello")
resp =
conn
|> authenticate(site.domain, token)
|> get(url)
|> json_response(422)
|> assert_schema("UnprocessableEntityError", spec())
assert %{errors: [%{detail: "Invalid integer. Got: string"}]} = resp
end
test "retrieve shared link by ID", %{conn: conn, site: site, token: token} do
shared_link = insert(:shared_link, name: "Some Link Name", site: site)
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, shared_link.id)
resp =
conn
|> authenticate(site.domain, token)
|> get(url)
|> json_response(200)
|> assert_schema("SharedLink", spec())
assert resp.shared_link.href ==
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth=#{shared_link.slug}"
assert resp.shared_link.id == shared_link.id
assert resp.shared_link.password_protected == false
assert resp.shared_link.name == "Some Link Name"
end
test "fails to retrieve non-existing link", %{conn: conn, site: site, token: token} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, 666)
conn
|> authenticate(site.domain, token)
|> get(url)
|> json_response(404)
|> assert_schema("NotFoundError", spec())
end
test "fails to retrieve link from another site", %{conn: conn, site: site, token: token} do
shared_link = insert(:shared_link, name: "Some Link Name", site: build(:site))
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :get, shared_link.id)
conn
|> authenticate(site.domain, token)
|> get(url)
|> json_response(404)
|> assert_schema("NotFoundError", spec())
end
end
describe "put /shared_links" do
test "successfully creates a shared link with the location header", %{
conn: conn,
site: site,
token: token
} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
initial_conn = authenticate(conn, site.domain, token)
conn =
initial_conn
|> put_req_header("content-type", "application/json")
|> put(url, %{
shared_link: %{
name: "My Shared Link"
}
})
resp =
conn
|> json_response(201)
|> assert_schema("SharedLink", spec())
assert resp.shared_link.name == "My Shared Link"
assert resp.shared_link.href =~
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
[location] = get_resp_header(conn, "location")
assert location ==
Routes.plugins_api_shared_links_url(
PlausibleWeb.Endpoint,
:get,
resp.shared_link.id
)
assert ^resp =
initial_conn
|> get(location)
|> json_response(200)
|> assert_schema("SharedLink", spec())
end
test "create is idempotent", %{
conn: conn,
site: site,
token: token
} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
initial_conn = authenticate(conn, site.domain, token)
create = fn ->
initial_conn
|> put_req_header("content-type", "application/json")
|> put(url, %{
shared_link: %{
name: "My Shared Link"
}
})
end
conn = create.()
resp =
conn
|> json_response(201)
|> assert_schema("SharedLink", spec())
id = resp.shared_link.id
conn = create.()
assert ^id =
conn
|> json_response(201)
|> Map.fetch!("shared_link")
|> Map.fetch!("id")
end
test "validates input out of the box", %{conn: conn, token: token, site: site} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
resp =
conn
|> authenticate(site.domain, token)
|> put_req_header("content-type", "application/json")
|> put(url, %{})
|> json_response(422)
|> assert_schema("UnprocessableEntityError", spec())
assert %{errors: [%{detail: "Missing field: shared_link"}]} = resp
end
test "skips shared links feature access check", %{
conn: conn,
site: site,
token: token
} do
insert(:starter_subscription, team: site.team)
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
resp =
authenticate(conn, site.domain, token)
|> put_req_header("content-type", "application/json")
|> put(url, %{
shared_link: %{
name: "My Shared Link"
}
})
|> json_response(201)
|> assert_schema("SharedLink", spec())
assert resp.shared_link.name == "My Shared Link"
assert resp.shared_link.href =~
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
end
for special_name <- Plausible.Sites.shared_link_special_names() do
test "can create shared link with the reserved '#{special_name}' name", %{
conn: conn,
site: site,
token: token
} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :create)
resp =
authenticate(conn, site.domain, token)
|> put_req_header("content-type", "application/json")
|> put(url, %{
shared_link: %{
name: unquote(special_name)
}
})
|> json_response(201)
|> assert_schema("SharedLink", spec())
assert resp.shared_link.name == unquote(special_name)
assert resp.shared_link.href =~
"http://localhost:8000/share/#{URI.encode_www_form(site.domain)}?auth="
end
end
end
describe "get /shared_links" do
test "returns an empty shared link list if there's none", %{
conn: conn,
token: token,
site: site
} do
url = Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index)
resp =
conn
|> authenticate(site.domain, token)
|> get(url)
|> json_response(200)
|> assert_schema("SharedLink.ListResponse", spec())
assert resp.shared_links == []
assert resp.meta.pagination.has_next_page == false
assert resp.meta.pagination.has_prev_page == false
assert resp.meta.pagination.links == %{}
end
test "returns a shared links list with pagination", %{
conn: conn,
token: token,
site: site
} do
for i <- 1..5 do
insert(:shared_link, site: site, name: "Shared Link #{i}")
end
url =
Routes.plugins_api_shared_links_url(PlausibleWeb.Endpoint, :index, limit: 2)
initial_conn = authenticate(conn, site.domain, token)
page1 =
initial_conn
|> get(url)
|> json_response(200)
|> assert_schema("SharedLink.ListResponse", spec())
assert [%{shared_link: %{name: "Shared Link 5"}}, %{shared_link: %{name: "Shared Link 4"}}] =
page1.shared_links
assert page1.meta.pagination.has_next_page == true
assert page1.meta.pagination.has_prev_page == false
assert page1.meta.pagination.links.next
refute page1.meta.pagination.links[:prev]
page2 =
initial_conn
|> get(page1.meta.pagination.links.next.url)
|> json_response(200)
|> assert_schema("SharedLink.ListResponse", spec())
assert [%{shared_link: %{name: "Shared Link 3"}}, %{shared_link: %{name: "Shared Link 2"}}] =
page2.shared_links
assert page2.meta.pagination.has_next_page == true
assert page2.meta.pagination.has_prev_page == true
assert page2.meta.pagination.links.next
assert page2.meta.pagination.links.prev
assert ^page1 =
initial_conn
|> get(page2.meta.pagination.links.prev.url)
|> json_response(200)
|> assert_schema("SharedLink.ListResponse", spec())
end
end
end
|