defmodule CartosnapApp.Storage.S3Test do
# Not async: swaps the backend's config in app env.
use ExUnit.Case, async: false
alias CartosnapApp.Storage.S3
setup do
previous = Application.get_env(:cartosnap_app, S3)
Application.put_env(:cartosnap_app, S3,
endpoint: "https://s3.us-west-004.backblazeb2.com",
region: "us-west-004",
access_key_id: "key-id",
secret_access_key: "secret",
req_options: [plug: {Req.Test, __MODULE__}, retry: false]
)
on_exit(fn ->
if previous,
do: Application.put_env(:cartosnap_app, S3, previous),
else: Application.delete_env(:cartosnap_app, S3)
end)
end
test "puts path-style, signed for the B2 region" do
test_pid = self()
Req.Test.stub(__MODULE__, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
send(test_pid, {:request, conn.method, conn.request_path, conn.req_headers, body})
Plug.Conn.send_resp(conn, 200, "")
end)
assert :ok = S3.put("u/p/orig.jpg", "bytes", "image/jpeg")
assert_receive {:request, "PUT", "/cartosnap-test/u/p/orig.jpg", headers, "bytes"}
headers = Map.new(headers)
assert headers["content-type"] == "image/jpeg"
assert headers["authorization"] =~ "Credential=key-id/"
assert headers["authorization"] =~ "/us-west-004/s3/aws4_request"
end
test "gets the raw body, and reports failures" do
Req.Test.stub(__MODULE__, fn
%{request_path: "/cartosnap-test/u/p/thumb.jpg"} = conn ->
conn
|> Plug.Conn.put_resp_content_type("image/jpeg")
|> Plug.Conn.send_resp(200, <<1, 2, 3>>)
conn ->
Plug.Conn.send_resp(conn, 404, "<Error><Code>NoSuchKey</Code></Error>")
end)
assert {:ok, <<1, 2, 3>>} = S3.get("u/p/thumb.jpg")
assert {:error, {:http, 404, _}} = S3.get("u/p/missing.jpg")
end
test "deletes" do
Req.Test.stub(__MODULE__, fn %{method: "DELETE"} = conn ->
Plug.Conn.send_resp(conn, 204, "")
end)
assert :ok = S3.delete("u/p/full.jpg")
end
test "public URLs are the bucket's virtual host unless configured" do
assert S3.url("u/p/thumb.jpg") ==
"https://cartosnap-test.s3.us-west-004.backblazeb2.com/u/p/thumb.jpg"
Application.put_env(
:cartosnap_app,
S3,
Keyword.put(Application.get_env(:cartosnap_app, S3), :public_url, "https://cdn.example/")
)
assert S3.url("u/p/thumb.jpg") == "https://cdn.example/u/p/thumb.jpg"
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
⭐
Log in to mark this repository.
2.4 KiB · text
History
e569b6d