import json import pytest from app.services.remnawave_identity import load_legacy_user_ids, validate_cached_identities, deterministic_user_uuid LEGACY = 'a1111111-1111-4111-8111-111111111111' @pytest.mark.parametrize('entries', [[], {}, [{'id': 42, 'uuid': LEGACY}, {'id': 42, 'uuid': LEGACY}], [{'id': 42, 'uuid': LEGACY}, {'id': 43, 'uuid': LEGACY}], [{'id': True, 'uuid': LEGACY}]]) def test_legacy_map_rejects_ambiguous_or_empty_data(tmp_path, entries): path = tmp_path / 'map.json' path.write_text(json.dumps(entries)) with pytest.raises(ValueError): load_legacy_user_ids(path) def test_missing_legacy_cache_mapping_fails_closed(): with pytest.raises(ValueError, match='cache'): validate_cached_identities({}, [(42, LEGACY)]) def test_cache_allows_legacy_and_new_deterministic_identity_after_restart(): validate_cached_identities({42: LEGACY}, [(42, LEGACY), (99, deterministic_user_uuid(99))]) def test_deleted_history_requires_complete_snapshot_and_is_preserved(): assert validate_cached_identities({42: LEGACY}, [(7, 'b2222222-2222-4222-8222-222222222222')], remote_identities=[(42, LEGACY)]) == 1 def test_reused_remote_id_rejects_historical_uuid(): with pytest.raises(ValueError, match='cache'): validate_cached_identities({}, [(7, LEGACY)], remote_identities=[(7, deterministic_user_uuid(7))]) def test_deleted_cache_uuid_cannot_belong_to_another_legacy_id(): with pytest.raises(ValueError, match='another'): validate_cached_identities({42: LEGACY}, [(7, LEGACY)], remote_identities=[]) def test_deleted_cache_uuid_cannot_belong_to_another_live_id(): with pytest.raises(ValueError, match='another'): validate_cached_identities({}, [(7, LEGACY)], remote_identities=[(99, LEGACY)]) def test_matching_seed_does_not_override_conflicting_live_identity(): with pytest.raises(ValueError, match='conflicts'): validate_cached_identities({42: LEGACY}, [(42, LEGACY)], remote_identities=[(42, deterministic_user_uuid(42))]) def test_reused_seeded_id_cannot_hide_changed_short_uuid_behind_legacy_alias(): with pytest.raises(ValueError, match='short UUID'): validate_cached_identities({42: LEGACY}, [(42, LEGACY, 'old-short')], remote_identities=[(42, LEGACY, 'new-short')])