from django.core.exceptions import ValidationError from django.test import TestCase from core.tests_portal_access import make_employee from core.utils.validators import normalize_ukrainian_phone, validate_ukrainian_phone class UkrainianPhoneNormalizeTests(TestCase): def test_variants_become_canonical(self): expected = '+380501112233' for raw in ( '+380501112233', '0501112233', '050 111 22 33', '+38 (050) 111-22-33', '380501112233', '80501112233', '501112233', ): self.assertEqual(normalize_ukrainian_phone(raw), expected, raw) def test_empty_stays_empty(self): self.assertEqual(normalize_ukrainian_phone(''), '') self.assertEqual(normalize_ukrainian_phone(None), '') validate_ukrainian_phone('') def test_garbage_rejected(self): with self.assertRaises(ValidationError): validate_ukrainian_phone('немає') with self.assertRaises(ValidationError): validate_ukrainian_phone('12345') def test_employee_save_normalizes(self): employee = make_employee(phone='050 111 22 33') employee.refresh_from_db() self.assertEqual(employee.phone, '+380501112233') def test_full_clean_accepts_pretty(self): employee = make_employee(user_location_type='office') employee.set_unusable_password() employee.personal_phone = '+38 (067) 123-45-67' employee.clean_fields() employee.save(update_fields=['personal_phone']) employee.refresh_from_db() self.assertEqual(employee.personal_phone, '+380671234567') def test_full_clean_rejects_short(self): employee = make_employee(user_location_type='office') employee.set_unusable_password() employee.phone = '12345' with self.assertRaises(ValidationError): employee.clean_fields(exclude=['password'])