|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, Mock |
| 3 | +from http import HTTPStatus |
| 4 | +from adaptive_cards.card import AdaptiveCard |
| 5 | +from adaptive_cards.client import TeamsClient |
| 6 | +from adaptive_cards.elements import TextBlock |
| 7 | + |
| 8 | +# filepath: src/adaptive_cards/test_client.py |
| 9 | + |
| 10 | + |
| 11 | +class TestTeamsClient: |
| 12 | + @patch("adaptive_cards.client.requests.post") |
| 13 | + def test_send_single_card_success(self, mock_post): |
| 14 | + mock_response = Mock() |
| 15 | + mock_response.status_code = HTTPStatus.OK |
| 16 | + mock_post.return_value = mock_response |
| 17 | + |
| 18 | + client = TeamsClient("http://example.com/webhook") |
| 19 | + card = AdaptiveCard.new().add_item(TextBlock(text="Test Card")).create() |
| 20 | + response = client.send(card) |
| 21 | + |
| 22 | + assert response.status_code == HTTPStatus.OK |
| 23 | + mock_post.assert_called_once() |
| 24 | + |
| 25 | + @patch("adaptive_cards.client.requests.post") |
| 26 | + def test_send_multiple_cards_success(self, mock_post): |
| 27 | + mock_response = Mock() |
| 28 | + mock_response.status_code = HTTPStatus.ACCEPTED |
| 29 | + mock_post.return_value = mock_response |
| 30 | + |
| 31 | + client = TeamsClient("http://example.com/webhook") |
| 32 | + card1 = AdaptiveCard.new().add_item(TextBlock(text="Test Card")).create() |
| 33 | + card2 = AdaptiveCard.new().add_item(TextBlock(text="Test Card")).create() |
| 34 | + response = client.send(card1, card2) |
| 35 | + |
| 36 | + assert response.status_code == HTTPStatus.ACCEPTED |
| 37 | + mock_post.assert_called_once() |
| 38 | + |
| 39 | + def test_send_no_webhook_url(self): |
| 40 | + client = TeamsClient("") |
| 41 | + card = AdaptiveCard.new().add_item(TextBlock(text="Test Card")).create() |
| 42 | + |
| 43 | + with pytest.raises(ValueError, match="No webhook URL provided."): |
| 44 | + client.send(card) |
| 45 | + |
| 46 | + def test_send_no_cards_provided(self): |
| 47 | + client = TeamsClient("http://example.com/webhook") |
| 48 | + |
| 49 | + with pytest.raises(ValueError, match="No cards provided."): |
| 50 | + client.send() |
| 51 | + |
| 52 | + @patch("adaptive_cards.client.requests.post") |
| 53 | + def test_send_failed_request(self, mock_post): |
| 54 | + mock_response = Mock() |
| 55 | + mock_response.status_code = HTTPStatus.BAD_REQUEST |
| 56 | + mock_response.text = "Bad Request" |
| 57 | + mock_post.return_value = mock_response |
| 58 | + |
| 59 | + client = TeamsClient("http://example.com/webhook") |
| 60 | + card = AdaptiveCard.new().add_item(TextBlock(text="Test Card")).create() |
| 61 | + |
| 62 | + with pytest.raises(RuntimeError, match="Failed to send card: 400, Bad Request"): |
| 63 | + client.send(card) |
0 commit comments