ForcePilot/backend/test/unit/channels/test_nextcloudtalk_normalize.py

151 lines
4.8 KiB
Python
Raw Normal View History

from __future__ import annotations
import pytest
from yuxi.channels.adapters.nextcloudtalk.normalize import (
is_pure_token,
looks_like_nc_target,
normalize_nc_target,
normalize_room_target,
normalize_user_target,
resolve_target_with_hint,
strip_nc_prefix,
)
class TestIsPureToken:
def test_valid_token(self):
assert is_pure_token("abc123def456abcdef1234") is True
def test_min_length_token(self):
assert is_pure_token("a" * 20) is True
def test_max_length_token(self):
assert is_pure_token("a" * 64) is True
def test_too_short_token(self):
assert is_pure_token("abc123def456") is False
def test_too_long_token(self):
assert is_pure_token("a" * 65) is False
def test_token_with_special_chars(self):
assert is_pure_token("abc-def") is False
def test_token_with_uppercase(self):
assert is_pure_token("ABC123") is False
def test_token_with_whitespace(self):
assert is_pure_token(" " + "a" * 20 + " ") is True
def test_empty_string(self):
assert is_pure_token("") is False
class TestResolveTargetWithHint:
def test_pure_token_gets_direct_token_hint(self):
target, hints = resolve_target_with_hint("a" * 30)
assert hints["resolver_hint"] == "direct_token"
assert target == f"nextcloud-talk:{'a' * 30}"
def test_room_prefix(self):
target, hints = resolve_target_with_hint("room:myroom")
assert hints["resolver_hint"] == "room_token"
assert target.startswith("nextcloud-talk:")
def test_user_prefix(self):
target, hints = resolve_target_with_hint("user:johndoe")
assert hints["resolver_hint"] == "user_id"
assert target.startswith("nextcloud-talk:")
def test_already_prefixed(self):
target, hints = resolve_target_with_hint("nextcloud-talk:room:abc")
assert target == "nextcloud-talk:room:abc"
def test_whitespace_stripped(self):
target, _ = resolve_target_with_hint(" user:johndoe ")
assert target == "nextcloud-talk:user:johndoe"
class TestNormalizeNcTarget:
def test_bare_target(self):
assert normalize_nc_target("room:abc") == "nextcloud-talk:room:abc"
def test_nextcloud_talk_colon_prefix(self):
assert normalize_nc_target("nextcloud-talk:room:abc") == "nextcloud-talk:room:abc"
def test_nextcloudtalk_colon_prefix(self):
assert normalize_nc_target("nextcloudtalk:room:abc") == "nextcloud-talk:room:abc"
def test_nc_talk_colon_prefix(self):
assert normalize_nc_target("nc-talk:room:abc") == "nextcloud-talk:room:abc"
def test_nc_colon_prefix(self):
assert normalize_nc_target("nc:johndoe") == "nextcloud-talk:johndoe"
def test_already_normalized(self):
result = normalize_nc_target("nextcloud-talk:johndoe")
assert result == "nextcloud-talk:johndoe"
def test_lowercase_conversion(self):
result = normalize_nc_target("ROOM:ABC")
assert result == "nextcloud-talk:room:abc"
class TestStripNcPrefix:
def test_strips_nextcloud_talk_prefix(self):
assert strip_nc_prefix("nextcloud-talk:room:abc") == "room:abc"
def test_strips_nextcloudtalk(self):
assert strip_nc_prefix("nextcloudtalk:room:abc") == "room:abc"
def test_strips_nc_talk(self):
assert strip_nc_prefix("nc-talk:room:abc") == "room:abc"
def test_strips_nc(self):
assert strip_nc_prefix("nc:johndoe") == "johndoe"
def test_no_prefix_returns_original(self):
assert strip_nc_prefix("johndoe") == "johndoe"
def test_strips_whitespace_and_lowers(self):
assert strip_nc_prefix(" NC:JohnDoe ") == "johndoe"
class TestLooksLikeNcTarget:
def test_matches_nextcloud_talk(self):
assert looks_like_nc_target("nextcloud-talk:abc") is True
def test_matches_nextcloudtalk(self):
assert looks_like_nc_target("nextcloudtalk:abc") is True
def test_matches_nc_talk(self):
assert looks_like_nc_target("nc-talk:abc") is True
def test_matches_nc(self):
assert looks_like_nc_target("nc:abc") is True
def test_no_match(self):
assert looks_like_nc_target("random:abc") is False
def test_case_insensitive(self):
assert looks_like_nc_target("NC-TALK:ABC") is True
def test_empty_string(self):
assert looks_like_nc_target("") is False
class TestNormalizeRoomTarget:
def test_basic(self):
assert normalize_room_target("abc123") == "nextcloud-talk:room:abc123"
def test_with_special_chars(self):
assert normalize_room_target("room_1") == "nextcloud-talk:room:room_1"
class TestNormalizeUserTarget:
def test_basic(self):
assert normalize_user_target("johndoe") == "nextcloud-talk:johndoe"
def test_with_email(self):
assert normalize_user_target("john@example.com") == "nextcloud-talk:john@example.com"