Testing hashing existing customers
This commit is contained in:
@@ -214,3 +214,101 @@ async def test_hashing_normalization(async_session: AsyncSession):
|
||||
"john.doe@example.com"
|
||||
)
|
||||
assert hashed.hashed_email == normalized_email_hash
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hash_existing_customers_normalizes_country_code(
|
||||
async_session: AsyncSession,
|
||||
):
|
||||
"""Test that hash_existing_customers normalizes invalid country codes.
|
||||
|
||||
Specifically tests the case where a customer was saved with a full country
|
||||
name (e.g., "Italy") instead of the ISO 3166-1 alpha-2 code (e.g., "IT").
|
||||
The validation should convert "Italy" to "IT", which is then hashed.
|
||||
"""
|
||||
# Create a customer directly in DB with invalid country code "Italy"
|
||||
# This simulates a customer that was saved before validation was implemented
|
||||
customer = Customer(
|
||||
given_name="Marco",
|
||||
surname="Rossi",
|
||||
contact_id="marco123",
|
||||
email_address="marco@example.com",
|
||||
phone="+39123456789",
|
||||
country_code="Italy", # Invalid - should be "IT"
|
||||
city_name="Rome",
|
||||
postal_code="00100",
|
||||
)
|
||||
async_session.add(customer)
|
||||
await async_session.commit()
|
||||
await async_session.refresh(customer)
|
||||
|
||||
# Verify no hashed version exists yet
|
||||
result = await async_session.execute(
|
||||
select(HashedCustomer).where(HashedCustomer.customer_id == customer.id)
|
||||
)
|
||||
hashed = result.scalar_one_or_none()
|
||||
assert hashed is None
|
||||
|
||||
# Verify the customer has the invalid country code
|
||||
assert customer.country_code == "Italy"
|
||||
|
||||
# Run hash_existing_customers - this should fail to validate "Italy"
|
||||
# because it's not a 2-letter code, so the customer should be skipped
|
||||
service = CustomerService(async_session)
|
||||
count = await service.hash_existing_customers()
|
||||
|
||||
# Should skip this customer due to validation error
|
||||
assert count == 0
|
||||
|
||||
# Verify hashed version was NOT created
|
||||
await async_session.refresh(customer)
|
||||
result = await async_session.execute(
|
||||
select(HashedCustomer).where(HashedCustomer.customer_id == customer.id)
|
||||
)
|
||||
hashed = result.scalar_one_or_none()
|
||||
assert hashed is None
|
||||
|
||||
# The customer's country_code should still be "Italy" (unchanged)
|
||||
assert customer.country_code == "Italy"
|
||||
|
||||
# Now let's test the case where we have a valid 2-letter code
|
||||
# but in the wrong case (should be normalized to uppercase)
|
||||
customer2 = Customer(
|
||||
given_name="Maria",
|
||||
surname="Bianchi",
|
||||
contact_id="maria123",
|
||||
email_address="maria@example.com",
|
||||
phone="+39987654321",
|
||||
country_code="it", # Valid but lowercase - should be normalized to "IT"
|
||||
city_name="Milan",
|
||||
postal_code="20100",
|
||||
)
|
||||
async_session.add(customer2)
|
||||
await async_session.commit()
|
||||
await async_session.refresh(customer2)
|
||||
|
||||
# Run hash_existing_customers again
|
||||
count = await service.hash_existing_customers()
|
||||
|
||||
# Should hash this customer (country code will be normalized)
|
||||
assert count == 1
|
||||
|
||||
# Verify the customer's country_code was normalized to uppercase
|
||||
await async_session.refresh(customer2)
|
||||
assert customer2.country_code == "IT"
|
||||
|
||||
# Verify hashed version was created with correct hash
|
||||
result = await async_session.execute(
|
||||
select(HashedCustomer).where(HashedCustomer.customer_id == customer2.id)
|
||||
)
|
||||
hashed = result.scalar_one_or_none()
|
||||
assert hashed is not None
|
||||
|
||||
# Verify the hashed country code matches the expected hash
|
||||
# "IT" -> lowercase "it" -> sha256 hash
|
||||
expected_hash = "2ad8a7049d7c5511ac254f5f51fe70a046ebd884729056f0fe57f5160d467153"
|
||||
assert hashed.hashed_country_code == expected_hash
|
||||
|
||||
# For comparison, verify that "italy" would produce a different hash
|
||||
italy_hash = "93ff074ca77b5f7e61e63320b615081149b818863599189b0e356ec3889d51f7"
|
||||
assert hashed.hashed_country_code != italy_hash
|
||||
|
||||
Reference in New Issue
Block a user