Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (70 sloc) 3.026 kb
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
from __future__ import with_statement
import sys
if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest
from mock import Mock
from twilio import TwilioException
from twilio.rest.resources import AvailablePhoneNumber
from twilio.rest.resources import AvailablePhoneNumbers
from twilio.rest.resources import PhoneNumbers


class AvailablePhoneNumberTest(unittest.TestCase):

    def setUp(self):
        self.parent = Mock()
        self.instance = AvailablePhoneNumber(self.parent)

    def test_init(self):
        self.assertEquals(self.instance.name, "")

    def test_purchase(self):
        self.instance.phone_number = "+123"
        self.instance.purchase(voice_url="http://www.google.com")

        self.parent.purchase.assert_called_with(
            voice_url="http://www.google.com",
            phone_number="+123")


class AvailabePhoneNumbersTest(unittest.TestCase):

    def setUp(self):
        self.resource = AvailablePhoneNumbers("http://api.twilio.com",
                                              ("user", "pass"), Mock())

    def test_get(self):
        with self.assertRaises(TwilioException):
            self.resource.get("PN123")

    def test_list(self):
        request = Mock()
        request.return_value = (Mock(), {"available_phone_numbers": []})
        self.resource.request = request

        self.resource.list()

        uri = "http://api.twilio.com/AvailablePhoneNumbers/US/Local"
        request.assert_called_with("GET", uri, params={})

    def test_load_instance(self):
        instance = self.resource.load_instance({"hey": "you"})
        self.assertIsInstance(instance.parent, Mock)
        self.assertEquals(instance.hey, "you")

    def test_purchase_status_callback(self):
        request = Mock()
        request.return_value = (Mock(), {"available_phone_numbers": []})
        self.resource.request = request

        self.resource.list()

        uri = "http://api.twilio.com/AvailablePhoneNumbers/US/Local"
        request.assert_called_with("GET", uri, params={})


class PhoneNumbersTest(unittest.TestCase):

    def setUp(self):
        self.resource = PhoneNumbers("http://api.twilio.com",
                                     ("user", "pass"))

    def test_reference(self):
        self.assertEquals(self.resource.available_phone_numbers.phone_numbers,
                          self.resource)

    def test_purchase_status_callback(self):
        request = Mock()
        response = Mock()
        response.status_code = 201
        request.return_value = (response, {"sid": ""})
        self.resource.request = request

        self.resource.purchase(area_code="530", status_callback_url="http://",
                               status_callback_method="POST")

        uri = "http://api.twilio.com/IncomingPhoneNumbers"

        data = {
            "AreaCode": "530",
            "StatusCallback": "http://",
            "StatusCallbackMethod": "POST",
            }

        request.assert_called_with("POST", uri, data=data)
Something went wrong with that request. Please try again.