File: test_messaging_response.py

package info (click to toggle)
python-twilio 7.7.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,976 kB
  • sloc: python: 148,884; makefile: 50; sh: 2
file content (109 lines) | stat: -rw-r--r-- 3,534 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
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>'