|
| 1 | +import unittest |
| 2 | +import twiliofun as twilio |
| 3 | +#import twilio |
| 4 | +import re |
| 5 | + |
| 6 | +class TwilioTest(unittest.TestCase): |
| 7 | + def strip(self, xml): |
| 8 | + return re.sub(r'\n|\t', '', str(xml).strip()) |
| 9 | + |
| 10 | + def improperAppend(self, verb): |
| 11 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Say("")) |
| 12 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Gather()) |
| 13 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Play("")) |
| 14 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Record()) |
| 15 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Hangup()) |
| 16 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Redirect()) |
| 17 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Dial()) |
| 18 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Conference("")) |
| 19 | + self.assertRaises(twilio.TwilioException, verb.append, twilio.Sms("")) |
| 20 | + |
| 21 | +class TestResponse(TwilioTest): |
| 22 | + |
| 23 | + def testEmptyResponse(self): |
| 24 | + r = twilio.Response() |
| 25 | + self.assertEquals(self.strip(r), "<Response/>") |
| 26 | + |
| 27 | + def testResponseAddAttribute(self): |
| 28 | + r = twilio.Response(foo="bar") |
| 29 | + self.assertEquals(self.strip(r), '<Response foo="bar"/>') |
| 30 | + |
| 31 | +class TestSay(TwilioTest): |
| 32 | + |
| 33 | + def testEmptySay(self): |
| 34 | + """should be a say with no text""" |
| 35 | + r = twilio.Response() |
| 36 | + r.append(twilio.Say("")) |
| 37 | + self.assertEquals(self.strip(r), "<Response><Say/></Response>") |
| 38 | + |
| 39 | + def testSayHelloWorld(self): |
| 40 | + """should say hello monkey""" |
| 41 | + r = twilio.Response() |
| 42 | + r.append(twilio.Say("Hello World")) |
| 43 | + r = self.strip(r) |
| 44 | + self.assertEquals(r, "<Response><Say>Hello World</Say></Response>") |
| 45 | + |
| 46 | + def testSayLoop(self): |
| 47 | + """should say hello monkey and loop 3 times""" |
| 48 | + r = twilio.Response() |
| 49 | + r.append(twilio.Say("Hello Monkey", loop=3)) |
| 50 | + r = self.strip(r) |
| 51 | + self.assertEquals(r, '<Response><Say loop="3">Hello Monkey</Say></Response>') |
| 52 | + |
| 53 | + def testSayLoopWoman(self): |
| 54 | + """should say have a woman say hello monkey and loop 3 times""" |
| 55 | + r = twilio.Response() |
| 56 | + r.append(twilio.Say("Hello Monkey", loop=3, voice=twilio.Say.WOMAN)) |
| 57 | + r = self.strip(r) |
| 58 | + self.assertEquals(r, '<Response><Say loop="3" voice="woman">Hello Monkey</Say></Response>') |
| 59 | + |
| 60 | + def testSayConvienceMethod(self): |
| 61 | + """convenience method: should say have a woman say hello monkey and loop 3 times and be in french""" |
| 62 | + r = twilio.Response() |
| 63 | + r.addSay("Hello Monkey", loop=3, voice=twilio.Say.MAN, language=twilio.Say.FRENCH) |
| 64 | + r = self.strip(r) |
| 65 | + self.assertEquals(r, '<Response><Say language="fr" loop="3" voice="man">Hello Monkey</Say></Response>') |
| 66 | + |
| 67 | + def testSayAddAttribute(self): |
| 68 | + """add attribute""" |
| 69 | + r = twilio.Say("",foo="bar") |
| 70 | + r = self.strip(r) |
| 71 | + self.assertEquals(r, '<Say foo="bar"/>') |
| 72 | + |
| 73 | + def testSayBadAppend(self): |
| 74 | + """ should raise exceptions for wrong appending""" |
| 75 | + self.improperAppend(twilio.Say("")) |
| 76 | + |
| 77 | +class TestPlay(TwilioTest): |
| 78 | + |
| 79 | + def testEmptyPlay(self): |
| 80 | + """should play hello monkey""" |
| 81 | + r = twilio.Response() |
| 82 | + r.append(twilio.Play("")) |
| 83 | + r = self.strip(r) |
| 84 | + self.assertEqual(r,"<Response><Play/></Response>") |
| 85 | + |
| 86 | + def testPlayHello(self): |
| 87 | + """should play hello monkey""" |
| 88 | + r = twilio.Response() |
| 89 | + r.append(twilio.Play("http://hellomonkey.mp3")) |
| 90 | + r = self.strip(r) |
| 91 | + self.assertEqual(r, "<Response><Play>http://hellomonkey.mp3</Play></Response>") |
| 92 | + |
| 93 | + def testPlayHelloLoop(self): |
| 94 | + """should play hello monkey loop""" |
| 95 | + r = twilio.Response() |
| 96 | + r.append(twilio.Play("http://hellomonkey.mp3", loop=3)) |
| 97 | + r = self.strip(r) |
| 98 | + self.assertEqual(r, '<Response><Play loop="3">http://hellomonkey.mp3</Play></Response>') |
| 99 | + |
| 100 | + def testPlayConvienceMethod(self): |
| 101 | + """convenience method: should play hello monkey""" |
| 102 | + r = twilio.Response() |
| 103 | + r.addPlay("http://hellomonkey.mp3", loop=3) |
| 104 | + r = self.strip(r) |
| 105 | + self.assertEqual(r, '<Response><Play loop="3">http://hellomonkey.mp3</Play></Response>') |
| 106 | + |
| 107 | + def testPlayAddAttribute(self): |
| 108 | + """add attribute""" |
| 109 | + r = twilio.Play("",foo="bar") |
| 110 | + r = self.strip(r) |
| 111 | + self.assertEquals(r, '<Play foo="bar"/>') |
| 112 | + |
| 113 | + def testPlayBadAppend(self): |
| 114 | + """ should raise exceptions for wrong appending""" |
| 115 | + self.improperAppend(twilio.Play("")) |
| 116 | + |
| 117 | +class TestRecord(TwilioTest): |
| 118 | + |
| 119 | + def testRecordEmpty(self): |
| 120 | + """should record""" |
| 121 | + r = twilio.Response() |
| 122 | + r.append(twilio.Record()) |
| 123 | + r = self.strip(r) |
| 124 | + self.assertEquals(r, '<Response><Record/></Response>') |
| 125 | + |
| 126 | + def testRecordActionMethod(self): |
| 127 | + """should record with an action and a get method""" |
| 128 | + r = twilio.Response() |
| 129 | + r.append(twilio.Record(action="example.com", method="GET")) |
| 130 | + r = self.strip(r) |
| 131 | + self.assertEquals(r, '<Response><Record action="example.com" method="GET"/></Response>') |
| 132 | + |
| 133 | + def testRecordMaxlengthFinishTimeout(self): |
| 134 | + """should record with an maxlength, finishonkey, and timeout""" |
| 135 | + r = twilio.Response() |
| 136 | + r.append(twilio.Record(timeout=4,finishOnKey="#", maxLength=30)) |
| 137 | + r = self.strip(r) |
| 138 | + self.assertEquals(r, '<Response><Record finishOnKey="#" maxLength="30" timeout="4"/></Response>') |
| 139 | + |
| 140 | + def testRecordTranscribeCallback(self): |
| 141 | + """should record with a transcribe and transcribeCallback""" |
| 142 | + r = twilio.Response() |
| 143 | + r.append(twilio.Record(transcribeCallback="example.com")) |
| 144 | + r = self.strip(r) |
| 145 | + self.assertEquals(r, '<Response><Record transcribeCallback="example.com"/></Response>') |
| 146 | + |
| 147 | + def testRecordMaxlengthFinishTimeout(self): |
| 148 | + """should record with an maxlength, finishonkey, and timeout""" |
| 149 | + r = twilio.Response() |
| 150 | + r.addRecord(timeout=4,finishOnKey="#", maxLength=30) |
| 151 | + r = self.strip(r) |
| 152 | + self.assertEquals(r, '<Response><Record finishOnKey="#" maxLength="30" timeout="4"/></Response>') |
| 153 | + |
| 154 | + def testPlayAddAttribute(self): |
| 155 | + """add attribute""" |
| 156 | + r = twilio.Record("",foo="bar") |
| 157 | + r = self.strip(r) |
| 158 | + self.assertEquals(r, '<Record foo="bar"/>') |
| 159 | + |
| 160 | + def testPlayBadAppend(self): |
| 161 | + """ should raise exceptions for wrong appending""" |
| 162 | + self.improperAppend(twilio.Record()) |
| 163 | + |
| 164 | +class TestRedirect(TwilioTest): |
| 165 | + |
| 166 | + def testRedirectEmpty(self): |
| 167 | + r = twilio.Response() |
| 168 | + r.append(twilio.Redirect()) |
| 169 | + r = self.strip(r) |
| 170 | + self.assertEquals(r, '<Response><Redirect/></Response>') |
| 171 | + |
| 172 | + def testRedirectMethod(self): |
| 173 | + r = twilio.Response() |
| 174 | + r.append(twilio.Redirect(url="example.com", method="POST")) |
| 175 | + r = self.strip(r) |
| 176 | + self.assertEquals(r, '<Response><Redirect method="POST">example.com</Redirect></Response>') |
| 177 | + |
| 178 | + def testRedirectMethodGetParams(self): |
| 179 | + r = twilio.Response() |
| 180 | + r.append(twilio.Redirect(url="example.com?id=34&action=hey", method="POST")) |
| 181 | + r = self.strip(r) |
| 182 | + self.assertEquals(r, '<Response><Redirect method="POST">example.com?id=34&action=hey</Redirect></Response>') |
| 183 | + |
| 184 | + def testAddAttribute(self): |
| 185 | + """add attribute""" |
| 186 | + r = twilio.Redirect("",foo="bar") |
| 187 | + r = self.strip(r) |
| 188 | + self.assertEquals(r, '<Redirect foo="bar"/>') |
| 189 | + |
| 190 | + def testBadAppend(self): |
| 191 | + """ should raise exceptions for wrong appending""" |
| 192 | + self.improperAppend(twilio.Redirect()) |
| 193 | + |
| 194 | + |
| 195 | +class TestHangup(TwilioTest): |
| 196 | + |
| 197 | + def testHangup(self): |
| 198 | + """convenience: should Hangup to a url via POST""" |
| 199 | + r = twilio.Response() |
| 200 | + r.append(twilio.Hangup()) |
| 201 | + r = self.strip(r) |
| 202 | + self.assertEquals(r, '<Response><Hangup/></Response>') |
| 203 | + |
| 204 | + |
| 205 | + def testHangupConvience(self): |
| 206 | + """should raises exceptions for wrong appending""" |
| 207 | + r = twilio.Response() |
| 208 | + r.addHangup() |
| 209 | + r = self.strip(r) |
| 210 | + self.assertEquals(r, '<Response><Hangup/></Response>') |
| 211 | + |
| 212 | + def testBadAppend(self): |
| 213 | + """ should raise exceptions for wrong appending""" |
| 214 | + self.improperAppend(twilio.Hangup()) |
| 215 | + |
| 216 | +class TestSms(TwilioTest): |
| 217 | + |
| 218 | + def testEmpty(self): |
| 219 | + """Test empty sms verb""" |
| 220 | + r = twilio.Response() |
| 221 | + r.append(twilio.Sms("")) |
| 222 | + r = self.strip(r) |
| 223 | + self.assertEquals(r, '<Response><Sms/></Response>') |
| 224 | + |
| 225 | + def testBody(self): |
| 226 | + """Test hello world""" |
| 227 | + r = twilio.Response() |
| 228 | + r.append(twilio.Sms("Hello, World")) |
| 229 | + r = self.strip(r) |
| 230 | + self.assertEquals(r, '<Response><Sms>Hello, World</Sms></Response>') |
| 231 | + |
| 232 | + def testToFromAction(self): |
| 233 | + """ Test the to, from, and status callback""" |
| 234 | + r = twilio.Response() |
| 235 | + r.append(twilio.Sms("Hello, World", to=1231231234, sender=3453453456, |
| 236 | + statusCallback="example.com?id=34&action=hey")) |
| 237 | + r = self.strip(r) |
| 238 | + self.assertEquals(r, '<Response><Sms from="3453453456" statusCallback="example.com?id=34&action=hey" to="1231231234">Hello, World</Sms></Response>') |
| 239 | + |
| 240 | + def testActionMethod(self): |
| 241 | + """ Test the action and method parameters on Sms""" |
| 242 | + r = twilio.Response() |
| 243 | + r.append(twilio.Sms("Hello", method="POST", action="example.com?id=34&action=hey")) |
| 244 | + r = self.strip(r) |
| 245 | + self.assertEquals(r, '<Response><Sms action="example.com?id=34&action=hey" method="POST">Hello</Sms></Response>') |
| 246 | + |
| 247 | + def testConvience(self): |
| 248 | + """should raises exceptions for wrong appending""" |
| 249 | + r = twilio.Response() |
| 250 | + r.addSms("Hello") |
| 251 | + r = self.strip(r) |
| 252 | + self.assertEquals(r, '<Response><Sms>Hello</Sms></Response>') |
| 253 | + |
| 254 | + def testBadAppend(self): |
| 255 | + """ should raise exceptions for wrong appending""" |
| 256 | + self.improperAppend(twilio.Sms("Hello")) |
| 257 | + |
| 258 | +if __name__ == '__main__': |
| 259 | + unittest.main() |
| 260 | + |
0 commit comments