168 lines
5.4 KiB
Elixir
168 lines
5.4 KiB
Elixir
defmodule LabelmakerWeb.LabelControllerTest do
|
|
use LabelmakerWeb.ConnCase, async: false
|
|
|
|
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
|
|
|
|
setup do
|
|
# Clean up test images before each test
|
|
File.rm_rf!(@label_dir)
|
|
File.mkdir_p!(@label_dir)
|
|
:ok
|
|
end
|
|
|
|
describe "show/2" do
|
|
test "generates and returns PNG image for valid label", %{conn: conn} do
|
|
conn = get(conn, ~p"/TestLabel")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
assert byte_size(conn.resp_body) > 0
|
|
end
|
|
|
|
test "generates image with custom color", %{conn: conn} do
|
|
conn = get(conn, ~p"/ColorTest?color=red")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with custom font", %{conn: conn} do
|
|
conn = get(conn, ~p"/FontTest?font=Impact")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with font shortcut", %{conn: conn} do
|
|
conn = get(conn, ~p"/ShortcutTest?font=h")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with custom size", %{conn: conn} do
|
|
conn = get(conn, ~p"/SizeTest?size=96")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with outline", %{conn: conn} do
|
|
conn = get(conn, ~p"/OutlineTest?outline=blue")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image without outline", %{conn: conn} do
|
|
conn = get(conn, ~p"/NoOutline?outline=none")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with width and height", %{conn: conn} do
|
|
conn = get(conn, ~p"/FixedSize?width=400&height=300")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "generates image with alignment", %{conn: conn} do
|
|
conn = get(conn, ~p"/AlignTest?width=400&height=300&align=left")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "handles multiline labels with newlines", %{conn: conn} do
|
|
conn = get(conn, ~p"/Line1\nLine2\nLine3")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "handles URL-encoded labels", %{conn: conn} do
|
|
conn = get(conn, ~p"/Hello%20World")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "caches generated images", %{conn: conn} do
|
|
# First request generates the image
|
|
conn1 = get(conn, ~p"/CacheTest?color=blue")
|
|
assert conn1.status == 200
|
|
|
|
# Get the generated filename
|
|
files_before = File.ls!(@label_dir)
|
|
assert length(files_before) > 0
|
|
|
|
# Second request should use cached image
|
|
conn2 = get(conn, ~p"/CacheTest?color=blue")
|
|
assert conn2.status == 200
|
|
|
|
# Should still have same number of files (no duplicate generation)
|
|
files_after = File.ls!(@label_dir)
|
|
assert files_before == files_after
|
|
end
|
|
|
|
test "generates different files for different parameters", %{conn: conn} do
|
|
_conn1 = get(conn, ~p"/Test?color=red")
|
|
_conn2 = get(conn, ~p"/Test?color=blue")
|
|
|
|
files = File.ls!(@label_dir)
|
|
assert length(files) == 2
|
|
end
|
|
|
|
test "handles special characters in label", %{conn: conn} do
|
|
conn = get(conn, ~p"/Test!@#$%")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "handles emoji in label", %{conn: conn} do
|
|
conn = get(conn, ~p"/Hello🎉")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "ignores invalid color parameter and uses default", %{conn: conn} do
|
|
conn = get(conn, ~p"/Test?color=invalidcolor")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "ignores invalid font parameter and uses default", %{conn: conn} do
|
|
conn = get(conn, ~p"/Test?font=invalidfont")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "ignores invalid size parameter and uses default", %{conn: conn} do
|
|
conn = get(conn, ~p"/Test?size=999")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "handles combination of all valid parameters", %{conn: conn} do
|
|
conn = get(conn, ~p"/FullTest?color=yellow&font=Impact&outline=black&size=96")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
|
|
test "handles empty label", %{conn: conn} do
|
|
conn = get(conn, ~p"/ ")
|
|
|
|
assert conn.status == 200
|
|
assert get_resp_header(conn, "content-type") == ["image/png; charset=utf-8"]
|
|
end
|
|
end
|
|
end
|