From 32f29f8ceafc4b21d38210da97dd5cfb3df009e3 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Wed, 30 May 2018 15:57:42 -0400 Subject: [PATCH] Add transaction QR Code generator Add tests and require that address and transaction hashes are valid --- .../controllers/address_qr_code_controller.ex | 10 +++++++++- .../transaction_qr_code_controller.ex | 15 +++++++++++++++ apps/explorer_web/lib/explorer_web/router.ex | 1 + .../address_qr_code_controller_test.exs | 19 +++++++++++++++++++ .../transaction_qr_code_controller_test.exs | 19 +++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex create mode 100644 apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs create mode 100644 apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs diff --git a/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex index 73331d7abb..5366f39c5c 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/address_qr_code_controller.ex @@ -1,7 +1,15 @@ defmodule ExplorerWeb.AddressQRCodeController do use ExplorerWeb, :controller + alias Explorer.Chain.Hash.Truncated + def index(conn, %{"address_id" => id}) do - send_download(conn, {:binary, QRCode.to_png(id)}, "content-type": "image/png", filename: "#{id}.png") + 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 diff --git a/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex new file mode 100644 index 0000000000..0e2cb4255f --- /dev/null +++ b/apps/explorer_web/lib/explorer_web/controllers/transaction_qr_code_controller.ex @@ -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 diff --git a/apps/explorer_web/lib/explorer_web/router.ex b/apps/explorer_web/lib/explorer_web/router.ex index 9f528c9822..e187b7a0e2 100644 --- a/apps/explorer_web/lib/explorer_web/router.ex +++ b/apps/explorer_web/lib/explorer_web/router.ex @@ -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 diff --git a/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs new file mode 100644 index 0000000000..e57a393e84 --- /dev/null +++ b/apps/explorer_web/test/explorer_web/controllers/address_qr_code_controller_test.exs @@ -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 diff --git a/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs new file mode 100644 index 0000000000..1984846234 --- /dev/null +++ b/apps/explorer_web/test/explorer_web/controllers/transaction_qr_code_controller_test.exs @@ -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