From f8c8c8e24f9cd578557e13a837e1f4d29e218149 Mon Sep 17 00:00:00 2001 From: vishnu Date: Sat, 14 Mar 2026 08:53:49 +0530 Subject: [PATCH] Fix Vote schema empty changeset by adding field validation The Vote changeset was casting no fields and validating nothing, violating Ecto best practices. Update to cast and require :dealt_card_id and :player_id, matching the ContinueVote pattern. Add corresponding unit tests. Closes #2557 Co-Authored-By: Claude Opus 4.6 (1M context) --- copi.owasp.org/lib/copi/cornucopia/vote.ex | 4 ++-- .../test/copi/cornucopia/vote_test.exs | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 copi.owasp.org/test/copi/cornucopia/vote_test.exs diff --git a/copi.owasp.org/lib/copi/cornucopia/vote.ex b/copi.owasp.org/lib/copi/cornucopia/vote.ex index 9cd451776..cb72deb18 100644 --- a/copi.owasp.org/lib/copi/cornucopia/vote.ex +++ b/copi.owasp.org/lib/copi/cornucopia/vote.ex @@ -12,7 +12,7 @@ defmodule Copi.Cornucopia.Vote do @doc false def changeset(vote, attrs) do vote - |> cast(attrs, []) - |> validate_required([]) + |> cast(attrs, [:dealt_card_id, :player_id]) + |> validate_required([:dealt_card_id, :player_id]) end end diff --git a/copi.owasp.org/test/copi/cornucopia/vote_test.exs b/copi.owasp.org/test/copi/cornucopia/vote_test.exs new file mode 100644 index 000000000..114dc4bed --- /dev/null +++ b/copi.owasp.org/test/copi/cornucopia/vote_test.exs @@ -0,0 +1,22 @@ +defmodule Copi.Cornucopia.VoteTest do + use Copi.DataCase + + alias Copi.Cornucopia.Vote + + describe "votes" do + @valid_attrs %{player_id: Ecto.ULID.generate(), dealt_card_id: 1} + @invalid_attrs %{player_id: nil, dealt_card_id: nil} + + test "changeset with valid attributes" do + changeset = Vote.changeset(%Vote{}, @valid_attrs) + assert changeset.valid? + end + + test "changeset with invalid attributes" do + changeset = Vote.changeset(%Vote{}, @invalid_attrs) + refute changeset.valid? + assert "can't be blank" in errors_on(changeset).player_id + assert "can't be blank" in errors_on(changeset).dealt_card_id + end + end +end