yak(pin-user-inspect-output-exactly-both-redacted-fields-shape-l0mg): pin User inspect output exactly (both redacted fields + shape)

This commit is contained in:
Willem van den Ende
2026-10-27 21:28:37 +00:00
parent a72d73f21c
commit a6a7b3a8c7
+51 -2
View File
@@ -390,8 +390,57 @@ defmodule Firehose.AccountsTest do
end
describe "inspect/2 for the User module" do
test "does not include password" do
refute inspect(%User{password: "123456"}) =~ "password: \"123456\""
# Rendering is `#Firehose.Accounts.User<__meta__: ..., id: 42, ...>`: a
# single line that ends with `, ...>`. The tests below pin the stable parts
# (module name, one line, redaction, field names and values) rather than
# the full string, so a toolchain bump that changes formatting does not
# fail them.
test "redacts every field the schema marks redacted, by name and by value" do
user = %User{
id: 42,
email: "shape-pin@example.com",
password: "plaintext-secret-value",
hashed_password: "hashed-secret-value"
}
output = inspect(user)
redacted = User.__schema__(:redact_fields)
# Guard against a vacuous loop if the schema key ever changes meaning.
# (`match?` rather than a comparison against `[]`: the type checker
# proves this schema's literal list non-empty and warns on the compare.)
assert match?([_ | _], redacted)
# The two secrets this schema declares; ties the loop to real fields.
assert :password in redacted
assert :hashed_password in redacted
for field <- redacted do
refute output =~ Atom.to_string(field)
refute output =~ Map.fetch!(user, field)
end
end
test "renders a single struct line with non-redacted fields and values" do
user = %User{
id: 42,
email: "shape-pin@example.com",
password: "plaintext-secret-value",
hashed_password: "hashed-secret-value"
}
output = inspect(user)
redacted = User.__schema__(:redact_fields)
assert String.starts_with?(output, "#Firehose.Accounts.User<")
assert String.ends_with?(output, ">")
refute output =~ "\n"
assert output =~ "id: 42"
assert output =~ ~s(email: "shape-pin@example.com")
for %{field: field} <- User.__info__(:struct), field not in redacted do
assert output =~ Atom.to_string(field)
end
end
end
end