def test_deactivate(self): # convert our test channel to be a Nexmo channel self.org.connect_nexmo("TEST_KEY", "TEST_SECRET", self.admin) channel = self.org.channels.all().first() channel.channel_type = "NX" channel.config = { Channel.CONFIG_NEXMO_APP_ID: "myappid", Channel.CONFIG_NEXMO_APP_PRIVATE_KEY: "secret" } channel.save(update_fields=("channel_type", "config")) # mock a 404 response from Nexmo during deactivation with self.settings(IS_PROD=True): with patch("nexmo.Client.delete_application" ) as mock_delete_application: mock_delete_application.side_effect = nexmo.ClientError( "404 response") # releasing shouldn't blow up on auth failures channel.release() channel.refresh_from_db() self.assertFalse(channel.is_active) mock_delete_application.assert_called_once_with( application_id="myappid")
def test_retry(self, mock_get_account_numbers): mock_get_account_numbers.side_effect = [ nexmo.ClientError("420 response from tests.com"), { "count": 2, "numbers": ["23463", "568658"] }, ] self.assertEqual(self.client.get_numbers(), ["23463", "568658"])
def send_sms(sender: str, to: str, message: str, client: nexmo.Client) -> dict: """Sends an SMS. ``sender`` and ``to`` must be in proper long form. """ # Nexmo is apparently picky about + being in the sender. sender = sender.strip("+") resp = client.send_message({"from": sender, "to": to, "text": message}) # Nexmo client incorrectly treats failed messages as successful error_text = resp["messages"][0].get("error-text") if error_text: raise nexmo.ClientError(error_text) return resp
def send_sms(sender: str, to: str, message: str, client: nexmo.Client) -> dict: """Sends an SMS. ``sender`` and ``to`` must be in proper long form. """ # This has to be removed at some point. Sleep a little to avoid hitting rate limits. time.sleep(0.3) # Nexmo is apparently picky about + being in the sender. sender = sender.strip("+") logging.info(f"Sending from {sender} to {to} message length {len(message)}") resp = client.send_message({"from": sender, "to": to, "text": message}) # Nexmo client incorrectly treats failed messages as successful error_text = resp["messages"][0].get("error-text") if error_text: raise nexmo.ClientError(error_text) return resp