File: test_messages_client.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (158 lines) | stat: -rw-r--r-- 5,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import os
from typing import List
from devtools_testutils import recorded_by_proxy
from _decorators import MessagesPreparers
from azure.core.credentials import AccessToken
from azure.communication.messages import NotificationMessagesClient
from azure.communication.messages.models import (
    TextNotificationContent,
    ImageNotificationContent,
    TemplateNotificationContent,
    MessageReceipt,
    MessageTemplate,
    MessageTemplateText,
    MessageTemplateBindings,
    MessageTemplateValue,
    WhatsAppMessageTemplateBindings,
    WhatsAppMessageTemplateBindingsComponent,
)
from _shared.utils import get_http_logging_policy
from _messages_test_case import MessagesRecordedTestCase
from azure.communication.messages._shared.utils import parse_connection_str


class TestNotificationMessageClientForText(MessagesRecordedTestCase):

    @MessagesPreparers.messages_test_decorator
    @recorded_by_proxy
    def test_text_send_message(self):
        phone_number: str = "+14254360097"
        raised = False

        text_options = TextNotificationContent(
            channel_registration_id="75476a19-a68d-4e10-806c-3680f099e069",
            to=[phone_number],
            content="Thanks for your feedback Hello.",
        )

        message_response: MessageReceipt
        message_client: NotificationMessagesClient = self.create_notification_message_client()

        try:
            with message_client:
                message_responses = message_client.send(text_options)
                message_response = message_responses.receipts[0]
        except:
            raised = True
            raise
        assert raised is False
        assert message_response.message_id is not None
        assert message_response.to is not None

    @MessagesPreparers.messages_test_decorator
    @recorded_by_proxy
    def test_template_send_message(self):
        phone_number: str = "+14254360097"
        input_template: MessageTemplate = MessageTemplate(name="cpm_welcome", language="en_US")
        raised = False

        message_client: NotificationMessagesClient = self.create_notification_message_client()

        template_options = TemplateNotificationContent(
            channel_registration_id="75476a19-a68d-4e10-806c-3680f099e069", to=[phone_number], template=input_template
        )

        message_response: MessageReceipt

        try:
            with message_client:
                message_responses = message_client.send(template_options)
                message_response = message_responses.receipts[0]
        except:
            raised = True
            raise
        assert raised is False
        assert message_response.message_id is not None
        assert message_response.to is not None

    @MessagesPreparers.messages_test_decorator
    @recorded_by_proxy
    def test_template_with_parameters_send_message(self):

        phone_number: str = "+14254360097"
        parammeter1 = MessageTemplateText(name="first", text="2")

        input_template: MessageTemplate = MessageTemplate(
            name="sample_shipping_confirmation",
            language="en_us",
            template_values=[parammeter1],
            bindings=WhatsAppMessageTemplateBindings(
                body=[WhatsAppMessageTemplateBindingsComponent(ref_value="first")]
            ),
        )
        raised = False

        template_options = TemplateNotificationContent(
            channel_registration_id="75476a19-a68d-4e10-806c-3680f099e069", to=[phone_number], template=input_template
        )

        message_response: MessageReceipt
        message_client: NotificationMessagesClient = self.create_notification_message_client()

        try:
            with message_client:
                message_responses = message_client.send(template_options)
                message_response = message_responses.receipts[0]
        except:
            raised = True
            raise
        assert raised is False
        assert message_response.message_id is not None
        assert message_response.to is not None

    @MessagesPreparers.messages_test_decorator
    @recorded_by_proxy
    def test_image_send_message(self):
        phone_number: str = "+14254360097"
        input_media_uri: str = "https://aka.ms/acsicon1"
        raised = False

        template_options = ImageNotificationContent(
            channel_registration_id="75476a19-a68d-4e10-806c-3680f099e069", to=[phone_number], media_uri=input_media_uri
        )

        message_response: MessageReceipt
        message_client: NotificationMessagesClient = self.create_notification_message_client()

        try:
            with message_client:
                message_responses = message_client.send(template_options)
                message_response = message_responses.receipts[0]
        except:
            raised = True
            raise
        assert raised is False
        assert message_response.message_id is not None
        assert message_response.to is not None

    @MessagesPreparers.messages_test_decorator
    @recorded_by_proxy
    def test_download_media(self):
        phone_number: str = "+14254360097"
        input_media_id: str = "53a5dbc2-65ec-4b93-b652-b4bcc8830645"
        raised = False
        message_client: NotificationMessagesClient = self.create_notification_message_client()
        try:
            with message_client:
                media_stream = message_client.download_media(input_media_id)
        except:
            raised = True
            raise
        assert raised is False
        assert media_stream is not None