fix: make JID lookups case-insensitive

Amolith created

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

Change summary

lib/customer_repo.rb            | 18 +++++++++---------
test/test_customer_info_form.rb |  9 +++++++++
2 files changed, 18 insertions(+), 9 deletions(-)

Detailed changes

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

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