feat(auth): несколько эталонных mail-доменов для верификации регистрации
Настройка «Эталон mail-домена» теперь хранит список доменов вместо
одного — email при регистрации принимается, если совпадает с любым из
них. Старое значение в БД ({"domain": str|None}) читается прозрачно
(обратная совместимость без Alembic-миграции) и переписывается в новую
форму ({"domains": [...]}) при первом же сохранении настроек. В админке
добавление/удаление доменов — списком чипов; на экране регистрации
подсказка о несовпадении домена перечисляет все эталонные варианты.
This commit is contained in:
@@ -134,7 +134,7 @@ async def test_ensure_bootstrapped_imports_yaml_defaults(
|
||||
assert cfg.display_timezone == "Europe/Moscow"
|
||||
assert cfg.registration_team_choice is False
|
||||
assert cfg.registration_email_domain_enabled is False
|
||||
assert cfg.registration_email_domain is None
|
||||
assert cfg.registration_email_domains == []
|
||||
assert cfg.contact_email_enabled is False
|
||||
assert cfg.contact_email is None
|
||||
|
||||
@@ -326,7 +326,7 @@ async def test_registration_team_choice_toggle(
|
||||
async def test_registration_email_domain_enable_without_domain_rejected(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""Включение верификации без домена (ни в патче, ни ранее сохранённого) → 400."""
|
||||
"""Включение верификации без доменов (ни в патче, ни ранее сохранённых) → 400."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
|
||||
@@ -335,7 +335,24 @@ async def test_registration_email_domain_enable_without_domain_rejected(
|
||||
|
||||
cfg = await service.get()
|
||||
assert cfg.registration_email_domain_enabled is False
|
||||
assert cfg.registration_email_domain is None
|
||||
assert cfg.registration_email_domains == []
|
||||
|
||||
|
||||
async def test_registration_email_domain_enable_with_empty_list_rejected(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""Пустой список доменов при включении верификации — та же ошибка, что и
|
||||
отсутствие поля (не молчаливое отключение проверки)."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
|
||||
with pytest.raises(InvalidEmailDomainError):
|
||||
await service.update(
|
||||
SettingsUpdateIn(registration_email_domain_enabled=True, registration_email_domains=[])
|
||||
)
|
||||
|
||||
cfg = await service.get()
|
||||
assert cfg.registration_email_domain_enabled is False
|
||||
|
||||
|
||||
async def test_registration_email_domain_rejects_invalid_pattern(
|
||||
@@ -345,48 +362,106 @@ async def test_registration_email_domain_rejects_invalid_pattern(
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
|
||||
with pytest.raises(InvalidEmailDomainError):
|
||||
await service.update(SettingsUpdateIn(registration_email_domain="not a domain"))
|
||||
await service.update(SettingsUpdateIn(registration_email_domains=["not a domain"]))
|
||||
|
||||
cfg = await service.get()
|
||||
assert cfg.registration_email_domain is None
|
||||
assert cfg.registration_email_domains == []
|
||||
|
||||
|
||||
async def test_registration_email_domain_normalizes_input(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""`@Corp.RU ` нормализуется в `corp.ru` (strip, убрать «@», lower)."""
|
||||
"""`@Corp.RU ` нормализуется в `corp.ru` (strip, убрать «@», lower); пустые строки
|
||||
отбрасываются, дубликаты схлопываются."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
|
||||
cfg = await service.update(
|
||||
SettingsUpdateIn(
|
||||
registration_email_domain_enabled=True, registration_email_domain="@Corp.RU "
|
||||
registration_email_domain_enabled=True,
|
||||
registration_email_domains=["@Corp.RU ", "", "corp.ru", "Acme.IO"],
|
||||
)
|
||||
)
|
||||
|
||||
assert cfg.registration_email_domain_enabled is True
|
||||
assert cfg.registration_email_domain == "corp.ru"
|
||||
assert cfg.registration_email_domains == ["corp.ru", "acme.io"]
|
||||
|
||||
reloaded = await service.get()
|
||||
assert reloaded.registration_email_domain == "corp.ru"
|
||||
assert reloaded.registration_email_domains == ["corp.ru", "acme.io"]
|
||||
|
||||
|
||||
async def test_registration_email_domain_can_be_disabled_keeping_stored_domain(
|
||||
async def test_registration_email_domain_can_be_disabled_keeping_stored_domains(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""Выключение верификации без передачи домена не требует домена и не роняет валидацию."""
|
||||
"""Выключение верификации без передачи доменов не требует их и не роняет валидацию."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
await service.update(
|
||||
SettingsUpdateIn(
|
||||
registration_email_domain_enabled=True, registration_email_domain="acme.io"
|
||||
registration_email_domain_enabled=True, registration_email_domains=["acme.io"]
|
||||
)
|
||||
)
|
||||
|
||||
cfg = await service.update(SettingsUpdateIn(registration_email_domain_enabled=False))
|
||||
|
||||
assert cfg.registration_email_domain_enabled is False
|
||||
assert cfg.registration_email_domain == "acme.io"
|
||||
assert cfg.registration_email_domains == ["acme.io"]
|
||||
|
||||
|
||||
async def test_registration_email_domain_reads_legacy_single_domain_shape(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""До многодоменной поддержки в БД хранилась форма `{"domain": str|None}` (один
|
||||
домен, без миграции на проде) — чтение должно разворачивать её в список из
|
||||
одного элемента (см. `_extract_email_domains`)."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
stmt = (
|
||||
pg_insert(InstanceSetting)
|
||||
.values(
|
||||
key="registration_email_domain",
|
||||
value={"enabled": True, "domain": "legacy.example"},
|
||||
)
|
||||
.on_conflict_do_update(
|
||||
index_elements=["key"],
|
||||
set_={"value": {"enabled": True, "domain": "legacy.example"}},
|
||||
)
|
||||
)
|
||||
await db_session.execute(stmt)
|
||||
await db_session.commit()
|
||||
|
||||
cfg = await service.get()
|
||||
|
||||
assert cfg.registration_email_domain_enabled is True
|
||||
assert cfg.registration_email_domains == ["legacy.example"]
|
||||
|
||||
|
||||
async def test_registration_email_domain_rewrites_legacy_shape_on_update(
|
||||
db_session: AsyncSession, clean_instance_settings: None
|
||||
) -> None:
|
||||
"""Первое же сохранение после чтения старой формы переписывает строку в
|
||||
новую (`domains: [...]`), а не оставляет legacy `domain` рядом."""
|
||||
service = InstanceSettingsService(db_session)
|
||||
await service.ensure_bootstrapped(PLUGINS_YAML)
|
||||
stmt = (
|
||||
pg_insert(InstanceSetting)
|
||||
.values(
|
||||
key="registration_email_domain",
|
||||
value={"enabled": True, "domain": "legacy.example"},
|
||||
)
|
||||
.on_conflict_do_update(
|
||||
index_elements=["key"],
|
||||
set_={"value": {"enabled": True, "domain": "legacy.example"}},
|
||||
)
|
||||
)
|
||||
await db_session.execute(stmt)
|
||||
await db_session.commit()
|
||||
|
||||
await service.update(SettingsUpdateIn(registration_email_domains=["new.example"]))
|
||||
|
||||
row = await db_session.get(InstanceSetting, "registration_email_domain")
|
||||
assert row is not None
|
||||
assert row.value == {"enabled": True, "domains": ["new.example"]}
|
||||
|
||||
|
||||
async def test_contact_email_enable_without_email_rejected(
|
||||
|
||||
Reference in New Issue
Block a user