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

95 lines
3.2 KiB
Python
Raw Normal View History

from __future__ import annotations
import pytest
from yuxi.channels.adapters.imessage.commands import (
get_command_help,
is_authorized_for_commands,
parse_command,
)
class TestParseCommand:
def test_valid_command_without_args(self):
result = parse_command("/status")
assert result == {"action": "status", "args": []}
def test_valid_command_with_args(self):
result = parse_command("/poll create Question Option1 Option2")
assert result["action"] == "poll"
assert result["args"] == ["create", "Question", "Option1", "Option2"]
def test_command_extra_spaces(self):
result = parse_command("/help ")
assert result == {"action": "help", "args": []}
def test_non_command_returns_none(self):
assert parse_command("Hello world") is None
def test_empty_string_returns_none(self):
assert parse_command("") is None
def test_slash_only_returns_none(self):
assert parse_command("/") is None
def test_slash_with_spaces_returns_none(self):
assert parse_command("/ ") is None
def test_command_uppercase_to_lowercase(self):
result = parse_command("/STATUS")
assert result["action"] == "status"
def test_command_mixed_case(self):
result = parse_command("/HeLp")
assert result["action"] == "help"
def test_command_with_quoted_args(self):
result = parse_command('/poll create "Question text" "Option A"')
assert result["action"] == "poll"
assert len(result["args"]) == 5
class TestIsAuthorizedForCommands:
def test_wildcard_allows_all(self):
assert is_authorized_for_commands("+8613800138000", ["*"]) is True
def test_exact_match(self):
assert is_authorized_for_commands("+8613800138000", ["+8613800138000"]) is True
def test_different_format_match(self):
assert is_authorized_for_commands("+8613800138000", ["8613800138000"]) is True
def test_handle_with_spaces(self):
assert is_authorized_for_commands(" +8613800138000 ", ["8613800138000"]) is True
def test_no_match(self):
assert is_authorized_for_commands("+8613800138000", ["+8613900139000"]) is False
def test_empty_allow_list(self):
assert is_authorized_for_commands("+8613800138000", []) is False
def test_email_handle(self):
assert is_authorized_for_commands("test@example.com", ["test@example.com"]) is True
def test_wildcard_in_list(self):
assert is_authorized_for_commands("anyone", ["user1", "*", "user2"]) is True
class TestGetCommandHelp:
def test_basic_help(self):
help_text = get_command_help()
assert "Available commands" in help_text
assert "/status" in help_text
assert "/help" in help_text
def test_include_owner_commands(self):
help_text = get_command_help(include_owner=True)
assert "/approve" in help_text
assert "/reject" in help_text
assert "/allowlist" in help_text
def test_exclude_owner_commands(self):
help_text = get_command_help(include_owner=False)
assert "/approve" not in help_text
assert "/reject" not in help_text
assert "/allowlist" not in help_text