2.7 KiB · text 5af55d9
defmodule GitGudWeb.LfsHttpTest do
@moduledoc """
HTTP-level tests for the LFS endpoints exposed under
`/:owner/:name.git/info/lfs/*`. Exercises the smart-HTTP plug's new
LFS dispatch clauses + auth + body parsing.
"""
use GitGudWeb.ConnCase, async: false
import GitGud.AccountsFixtures
import GitGud.ForgeFixtures
setup do
user = user_fixture() |> GitGud.AccountsFixtures.set_password()
{_, repo} = repository_fixture(%{owner: user, visibility: "public"})
owner_handle = GitGud.Repositories.Storage.owner_handle(user)
auth =
"Basic " <> Base.encode64("#{user.email}:#{GitGud.AccountsFixtures.valid_user_password()}")
{:ok,
user: user,
repo: repo,
owner: owner_handle,
auth: auth}
end
test "POST batch (upload) without auth on a public repo prompts for auth", %{
repo: repo,
owner: owner
} do
payload = %{"operation" => "upload", "objects" => [%{"oid" => String.duplicate("a", 64), "size" => 1}]}
conn =
build_conn()
|> put_req_header("content-type", "application/vnd.git-lfs+json")
|> post("/#{owner}/#{repo.name}.git/info/lfs/objects/batch", Jason.encode!(payload))
assert conn.status == 401
end
test "POST batch (upload) with valid auth returns actions for new oid", %{
repo: repo,
owner: owner,
auth: auth
} do
oid = String.duplicate("c", 64)
payload = %{
"operation" => "upload",
"objects" => [%{"oid" => oid, "size" => 7}]
}
conn =
build_conn()
|> put_req_header("authorization", auth)
|> put_req_header("content-type", "application/vnd.git-lfs+json")
|> post(
"/#{owner}/#{repo.name}.git/info/lfs/objects/batch",
Jason.encode!(payload)
)
body = Jason.decode!(conn.resp_body)
assert conn.status == 200
assert body["transfer"] == "basic"
[obj] = body["objects"]
assert obj["oid"] == oid
assert obj["actions"]["upload"]["href"]
assert obj["actions"]["verify"]["href"]
end
test "POST batch (download) for an unknown oid returns per-object 404", %{
repo: repo,
owner: owner
} do
oid = String.duplicate("d", 64)
payload = %{
"operation" => "download",
"objects" => [%{"oid" => oid, "size" => 5}]
}
# Anonymous download is OK on a public repo (mirrors smart-HTTP).
conn =
build_conn()
|> put_req_header("content-type", "application/vnd.git-lfs+json")
|> post(
"/#{owner}/#{repo.name}.git/info/lfs/objects/batch",
Jason.encode!(payload)
)
body = Jason.decode!(conn.resp_body)
assert conn.status == 200
[obj] = body["objects"]
assert obj["error"]["code"] == 404
end
end