You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB
Python

"""Tests for KakaoTalk client."""
from unittest.mock import MagicMock, patch
import pytest
from gimme_job.runtime.kakao import KakaoTalkClient, MAX_TEXT_LENGTH
def test_not_configured_without_tokens():
client = KakaoTalkClient(rest_api_key="", access_token="", refresh_token="")
assert not client.is_configured()
def test_configured_with_tokens():
client = KakaoTalkClient(rest_api_key="key123", access_token="token123")
assert client.is_configured()
def test_send_returns_false_when_not_configured():
client = KakaoTalkClient(rest_api_key="", access_token="")
result = client.send_self_memo("test message")
assert result is False
def test_text_truncation():
"""Messages over MAX_TEXT_LENGTH are truncated before sending."""
long_text = "x" * (MAX_TEXT_LENGTH + 1000)
client = KakaoTalkClient(rest_api_key="key", access_token="token")
with patch.object(client, "_do_send", return_value=True) as mock_send:
client.send_self_memo(long_text)
sent_text = mock_send.call_args[0][0]
assert len(sent_text) <= MAX_TEXT_LENGTH
assert "[...truncated]" in sent_text
@patch("httpx.post")
def test_send_self_memo_success(mock_post):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"result_code": 0}
mock_post.return_value = mock_response
client = KakaoTalkClient(rest_api_key="key", access_token="token")
result = client._do_send("Hello KakaoTalk!")
assert result is True
@patch("httpx.post")
def test_send_self_memo_auth_failure(mock_post):
mock_response = MagicMock()
mock_response.status_code = 401
mock_response.text = "Unauthorized"
mock_post.return_value = mock_response
client = KakaoTalkClient(rest_api_key="key", access_token="expired_token")
# tenacity wraps the exception in RetryError after exhausting attempts
with pytest.raises(Exception):
client._do_send("test")