From 2e5557cb4a73aa99c0238e8b606241ecbdaf22f5 Mon Sep 17 00:00:00 2001 From: Amolith Date: Mon, 5 Jan 2026 14:28:13 -0700 Subject: [PATCH] fix: make JID lookups case-insensitive Centralizes JID normalization in QueryKey::JID.for and ensures Redis stores lowercase JIDs in create/change_jid methods. Fixes: https://todo.sr.ht/~singpolyma/soprani.ca/203 --- lib/customer_repo.rb | 18 +++++++++--------- test/test_customer_info_form.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index 16d3284f0a64dd5d8482a72ebed229edbcdc4188..5baef84cb2011bd0fad953525f0e32cc64575c7e 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -45,10 +45,11 @@ class CustomerRepo JID = Struct.new(:jid) do def self.for(jid) - if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/ + norm = jid.to_s.downcase + if norm =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/ ID.new($1) else - new(jid) + new(norm) end end @@ -96,9 +97,9 @@ class CustomerRepo @braintree.customer.create.then do |result| raise "Braintree customer create failed" unless result.success? - c = result.customer.id + c, norm = result.customer.id, jid.to_s.downcase @redis.msetnx( - "jmp_customer_id-#{jid}", c, "jmp_customer_jid-#{c}", jid + "jmp_customer_id-#{norm}", c, "jmp_customer_jid-#{c}", norm ).then do |redis_result| raise "Saving new customer to redis failed" unless redis_result == 1 @@ -132,11 +133,10 @@ class CustomerRepo end def change_jid(customer, new_jid) - @redis.set("jmp_customer_id-#{new_jid}", customer.customer_id).then { - @redis.set("jmp_customer_jid-#{customer.customer_id}", new_jid) - }.then { - SwapDefaultFwd.new.do(self, customer, new_jid) - }.then do + norm_jid = new_jid.to_s.downcase + @redis.set("jmp_customer_id-#{norm_jid}", customer.customer_id).then { + @redis.set("jmp_customer_jid-#{customer.customer_id}", norm_jid) + }.then { SwapDefaultFwd.new.do(self, customer, new_jid) }.then do @redis.del("jmp_customer_id-#{customer.jid}") end end diff --git a/test/test_customer_info_form.rb b/test/test_customer_info_form.rb index c8e8179d54d67b62be23a1dd660832f2a08833ba..c6741be5e30e62f73735eb1c81bf12d1ade74468 100644 --- a/test/test_customer_info_form.rb +++ b/test/test_customer_info_form.rb @@ -113,4 +113,13 @@ class CustomerInfoFormTest < Minitest::Test assert_nil(result) end em :test_route + + def test_find_real_jid_case_insensitive + result = @info_form.parse_something("TEST@EXAMPLE.COM").sync + assert_equal @customer_test, result + + result = @info_form.parse_something("Test@Example.Com").sync + assert_equal @customer_test, result + end + em :test_find_real_jid_case_insensitive end