File: test_messaging_response.py

package info (click to toggle)
python-twilio 9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,756 kB
  • sloc: python: 8,281; makefile: 65
file content (128 lines) | stat: -rw-r--r-- 3,724 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
from tests.unit.twiml import TwilioTest
from twilio.twiml.messaging_response import MessagingResponse, Body, Media


class TestResponse(TwilioTest):
    def test_empty_response(self):
        r = MessagingResponse()
        assert self.strip(r) == '<?xml version="1.0" encoding="UTF-8"?><Response />'

    def test_response(self):
        r = MessagingResponse()
        r.message("Hello")
        r.redirect(url="example.com")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello</Message><Redirect>example.com</Redirect></Response>'
        )

    def test_response_chain(self):
        with MessagingResponse() as r:
            r.message("Hello")
            r.redirect(url="example.com")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello</Message><Redirect>example.com</Redirect></Response>'
        )

    def test_nested_verbs(self):
        with MessagingResponse() as r:
            with r.message("Hello") as m:
                m.media("example.com")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello<Media>example.com</Media></Message></Response>'
        )

    def test_child_node(self):
        with MessagingResponse() as r:
            with r.add_child("message", tag="global") as mod:
                mod.add_child("bold", "Hello")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><message tag="global"><bold>Hello</bold></message></Response>'
        )

    def test_mixed(self):
        r = MessagingResponse()

        r.append("before")
        r.add_child("Child").append("content")
        r.append("after")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response>before<Child>content</Child>after</Response>'
        )


class TestMessage(TwilioTest):
    def test_body(self):
        r = MessagingResponse()
        r.message("Hello")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello</Message></Response>'
        )

    def test_nested_body(self):
        b = Body("Hello World")

        r = MessagingResponse()
        r.append(b)

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Body>Hello World</Body></Response>'
        )

    def test_nested_body_media(self):
        b = Body("Hello World")
        m = Media("hey.jpg")

        r = MessagingResponse()
        r.append(b)
        r.append(m)

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Body>Hello World</Body><Media>hey.jpg</Media></Response>'
        )


class TestRedirect(TwilioTest):
    def test_redirect(self):
        r = MessagingResponse()
        r.redirect(url="example.com")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response><Redirect>example.com</Redirect></Response>'
        )


class TestText(TwilioTest):
    def test_text(self):
        r = MessagingResponse()
        r.append("No tags!")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response>No tags!</Response>'
        )

    def text_mixed(self):
        r = MessagingResponse()
        r.append("before")
        r.append(Body("Content"))
        r.append("after")

        assert (
            self.strip(r)
            == '<?xml version="1.0" encoding="UTF-8"?><Response>before<Body>Content</Body>after</Response>'
        )