Exemplo n.º 1
0
def test_rent_number_none_available():
    client = mock.create_autospec(nexmo.Client)
    client.application_id = "appid"

    client.get_available_numbers.return_value = {"numbers": []}

    with pytest.raises(RuntimeError, match="No numbers available"):
        lowlevel.rent_number(sms_callback_url="example.com/sms", client=client)
Exemplo n.º 2
0
def test_rent_number_buy_error_is_okay():
    client = mock.create_autospec(nexmo.Client)
    client.application_id = "appid"

    client.get_available_numbers.return_value = {
        "numbers": [
            {
                "country": "US",
                "msisdn": "+1123456789"
            },
            {
                "country": "US",
                "msisdn": "+1987654321"
            },
        ]
    }

    # Return an error when trying to buy the first number, so that the method
    # ends up buying the second number.
    client.buy_number.side_effect = [nexmo.Error(), None]

    result = lowlevel.rent_number(sms_callback_url="example.com/sms",
                                  client=client)

    assert result == {"country": "US", "msisdn": "+1987654321"}
    assert client.buy_number.call_count == 2
Exemplo n.º 3
0
def test_rent_number(country, expected):
    client = mock.create_autospec(nexmo.Client)
    client.application_id = "appid"

    client.get_available_numbers.return_value = {
        "numbers": [
            # Should always grab the first one.
            {
                "country": country,
                "msisdn": expected
            },
            {
                "country": "US",
                "msisdn": "19876543210"
            },
        ]
    }

    result = lowlevel.rent_number(sms_callback_url="example.com/sms",
                                  country_code=country,
                                  client=client)

    assert result == {"country": country, "msisdn": f"+{expected}"}

    client.get_available_numbers.assert_called_once_with(country, mock.ANY)
    client.buy_number.assert_called_once_with({
        "country": country,
        "msisdn": expected
    })
Exemplo n.º 4
0
def test_rent_number():
    client = mock.create_autospec(nexmo.Client)
    client.application_id = "appid"

    client.get_available_numbers.return_value = {
        "numbers": [
            {
                "country": "US",
                "msisdn": "123456789"
            },
            {
                "country": "US",
                "msisdn": "987654321"
            },
        ]
    }

    result = lowlevel.rent_number(sms_callback_url="example.com/sms",
                                  client=client)

    assert result == {"country": "US", "msisdn": "+1123456789"}

    client.get_available_numbers.assert_called_once()
    client.buy_number.assert_called_once_with({
        "country": "US",
        "msisdn": "123456789"
    })