Files
sure/test/controllers/family_merchants_controller_test.rb
Erkan Doğan 71106f63b0 fix(merchants): preserve user-selected merchant colors on save (#2760) (#2802)
* fix(merchants): preserve user-selected merchant colors on save (#2760)

Fixes #2760.

## Problem

FamilyMerchant#set_default_color callback ran on before_validation and unconditionally executed self.color = COLORS.sample.

As a result:
- Any user-selected color or color passed via API/import was discarded and replaced with a random palette color.
- Renaming or updating any merchant field re-assigned a new random color on every save.

## Fix

Guard set_default_color so it only assigns a random palette color when color is blank (self.color = COLORS.sample if color.blank?).

## Test

- Added test/models/family_merchant_test.rb testing color preservation on creation and update, as well as default color fallback when blank.
- Updated test/controllers/family_merchants_controller_test.rb to assert persisted color on create and update.
- Verified in dev container: 9 runs, 22 assertions, 0 failures, 0 errors.
- RuboCop: 0 offenses.

* fix(merchants): validate hex format and fallback to default color if invalid

Add hex format validation (/\A#[0-9A-Fa-f]{6}\z/) to FamilyMerchant#color (matching Category and Tag models) and ensure set_default_color replaces invalid hex values (e.g. from API/CSV imports) with a default palette color before saving.

Responds to Codex review feedback on PR #2802.

---------

Co-authored-by: erkdgn <erkdgn@users.noreply.github.com>
2026-07-26 01:38:31 +02:00

51 lines
1.4 KiB
Ruby

require "test_helper"
class FamilyMerchantsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@merchant = merchants(:netflix)
end
test "index" do
get family_merchants_path
assert_response :success
end
test "new" do
get new_family_merchant_path
assert_response :success
end
test "should create merchant" do
assert_difference("FamilyMerchant.count") do
post family_merchants_url, params: { family_merchant: { name: "new merchant", color: "#000000" } }
end
assert_redirected_to family_merchants_path
created_merchant = FamilyMerchant.find_by(name: "new merchant")
assert_equal "#000000", created_merchant.color
end
test "should update merchant" do
patch family_merchant_url(@merchant), params: { family_merchant: { name: "new name", color: "#000000" } }
assert_redirected_to family_merchants_path
assert_equal "#000000", @merchant.reload.color
end
test "should destroy merchant" do
assert_difference("FamilyMerchant.count", -1) do
delete family_merchant_url(@merchant)
end
assert_redirected_to family_merchants_path
end
test "enhance enqueues job and redirects" do
assert_enqueued_with(job: EnhanceProviderMerchantsJob) do
post enhance_family_merchants_path
end
assert_redirected_to family_merchants_path
end
end