Add transaction QR Code generator

Add tests and require that address and transaction hashes are valid
pull/232/head
Tim Mecklem 7 years ago
parent b0fd53bc06
commit 32f29f8cea
  1. 8
      apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex
  2. 15
      apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex
  3. 1
      apps/explorer_web/lib/explorer_web/router.ex
  4. 19
      apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs
  5. 19
      apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs

@ -1,7 +1,15 @@
defmodule ExplorerWeb.AddressQRCodeController do
use ExplorerWeb, :controller
alias Explorer.Chain.Hash.Truncated
def index(conn, %{"address_id" => id}) do
case Truncated.cast(id) do
{:ok, _} ->
send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png")
_ ->
send_resp(conn, :not_found, "")
end
end
end

@ -0,0 +1,15 @@
defmodule ExplorerWeb.TransactionQRCodeController do
use ExplorerWeb, :controller
alias Explorer.Chain.Hash.Full
def index(conn, %{"transaction_id" => id}) do
case Full.cast(id) do
{:ok, _} ->
send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png")
_ ->
send_resp(conn, :not_found, "")
end
end
end

@ -62,6 +62,7 @@ defmodule ExplorerWeb.Router do
)
resources("/logs", TransactionLogController, only: [:index], as: :log)
resources("/qrcode.png", TransactionQRCodeController, only: [:index], as: :qr_code)
end
resources "/addresses", AddressController, only: [:show] do

@ -0,0 +1,19 @@
defmodule ExplorerWeb.AddressQRCodeControllerTest do
use ExplorerWeb.ConnCase
import ExplorerWeb.Router.Helpers, only: [address_qr_code_path: 4]
describe "GET index/3" do
test "with valid address hash returns a QR code", %{conn: conn} do
conn = get(conn, address_qr_code_path(conn, :index, :en, address_hash()))
assert response(conn, 200)
end
test "with invalid address hash returns 404", %{conn: conn} do
conn = get(conn, address_qr_code_path(conn, :index, :en, "0xhaha"))
assert response(conn, 404)
end
end
end

@ -0,0 +1,19 @@
defmodule ExplorerWeb.TransactionQRCodeControllerTest do
use ExplorerWeb.ConnCase
import ExplorerWeb.Router.Helpers, only: [transaction_qr_code_path: 4]
describe "GET index/3" do
test "with valid `Hash.Full` returns a QR code", %{conn: conn} do
conn = get(conn, transaction_qr_code_path(conn, :index, :en, transaction_hash()))
assert response(conn, 200)
end
test "with invalid address hash returns 404", %{conn: conn} do
conn = get(conn, transaction_qr_code_path(conn, :index, :en, "0xhaha"))
assert response(conn, 404)
end
end
end
Loading…
Cancel
Save