bmc_hub/tests/test_email_sender_identity.py
Christian 1fb5b4a191 Add comprehensive tests for email and drift functionalities
- Introduced tests for drift operations UI and backend to ensure proper handling of ignore rules and UI elements.
- Added tests for email identity API to validate email linking and identity retrieval without updates.
- Implemented tests for email purchase link service to verify correct handling of purchase mail links and conflict scenarios.
- Created tests for email reply automation to ensure proper routing and processing of replies based on classification.
- Developed tests for email sender creation to validate suggestions and handling of existing contacts and companies.
- Added tests for email sender identity to ensure correct matching of contacts and domains.
- Implemented tests for work desk functionalities to validate task management, meeting registrations, and timer functionalities.
2026-11-03 01:16:12 +01:00

51 lines
2.3 KiB
Python

from app.services.email_sender_identity import find_sender_candidates
def test_shared_contact_and_domain_preserve_all_companies():
calls = []
def query(sql, params):
calls.append(params)
if len(calls) == 1:
return [dict(id=1, name='Hus A', kind='customer', source='contact_email', contact_name='Anne'),
dict(id=2, name='Hus B', kind='customer', source='contact_email', contact_name='Anne')]
return [dict(id=1, name='Hus A', kind='customer', source='exact_domain'),
dict(id=3, name='Leverandør', kind='vendor', source='exact_domain')]
result = find_sender_candidates('Anne <ANNE@example.dk>', query)
assert result['address'] == 'anne@example.dk'
assert result['ambiguous'] is True
assert [(r['kind'], r['id']) for r in result['candidates']] == [('customer', 1), ('customer', 2), ('vendor', 3)]
assert result['candidates'][0]['contact_name'] == 'Anne'
assert calls[0] == ('anne@example.dk',) * 4
def test_public_domain_can_match_exact_contact_but_never_domain():
calls = []
def query(sql, params):
calls.append(sql)
return [dict(id=2, name='Kunden', kind='customer', source='contact_email')]
result = find_sender_candidates('person@gmail.com', query, {'gmail.com'})
assert len(calls) == 1
assert not result['ambiguous']
def test_missing_address_does_not_query_database():
def forbidden(*args):
raise AssertionError('No DB query expected')
assert find_sender_candidates('', forbidden)['candidates'] == []
def test_supplier_contact_matches_person_before_supplier_domain():
def query(sql, params):
if 'JOIN vendor_contacts' in sql:
assert 'vc.contact_id = ct.id' in sql
assert 'v.id = vc.vendor_id' in sql
assert params == ('person@example.dk',) * 4
return [dict(id=8, name='Leverandør A', kind='vendor', source='contact_email',
contact_id=12, contact_name='Kontaktperson')]
return [dict(id=8, name='Leverandør A', kind='vendor', source='exact_domain')]
result = find_sender_candidates('Person <PERSON@example.dk>', query)
assert not result['ambiguous']
assert len(result['candidates']) == 1
assert result['candidates'][0]['source'] == 'contact_email'
assert result['candidates'][0]['contact_id'] == 12