def test_receiver_post_send_signal(self) -> None: """Make sure the post_send signal is called.""" @receiver(post_send) def f(instance, **kwargs): self.body = instance.body self.state = True self.state = False self.body = None body = 'Here is the message' message = Message(body, '+12065550100', ['+441134960000']) message.send() self.assertTrue(self.state) self.assertEqual(body, self.body)
def test_custom_backend(self) -> None: """Test cutoms backend defined in this suite.""" connection = sms.get_connection('tests.custombackend.SmsBackend') self.assertTrue(hasattr(connection, 'test_outbox')) message = Message('Content', '0600000000', ['0600000000']) connection.send_messages([message]) # type: ignore self.assertEqual(len(connection.test_outbox), 1) # type: ignore
def message_from_bytes(s: bytes) -> Message: """Parse a bytes string into a Message object model.""" text = s.decode('ASCII', errors='surrogateescape') body = '' for line in text.splitlines(): if not header_RE.match(line): if body is not None: body += '\n' + line else: body = line from_result = header_from_RE.search(text) if from_result: from_phone = from_result.group(1) else: raise ValueError to_result = header_to_RE.search(text) if to_result: to = [to_result.group(1)] else: raise ValueError return Message(body, from_phone, to)
def test_dummy_backend(self) -> None: """ Make sure that dummy backends return correct number of sent text messages. """ connection = dummy.SmsBackend() message = Message() self.assertEqual(connection.send_messages([message, message, message]), 3)
def test_send_messages(self) -> None: """Test send_messages with the MessageBird backend.""" message = Message('Here is the message', '+12065550100', ['+441134960000']) connection = sms.get_connection() connection.client.message_create = MagicMock() # type: ignore connection.send_messages([message]) # type: ignore connection.client.message_create.assert_called_with( # type: ignore '+12065550100', ['+441134960000'], 'Here is the message')
def send_sms(body: str = '', originator: Optional[str] = None, recipients: Union[Optional[str], Optional[List[str]]] = None, fail_silently: bool = False, connection: Optional[Type['BaseSmsBackend']] = None) -> int: """ Easy wrapper for sending a single message to a recipient list. Allow to to be a string, to remain compatibility with older django-sms<=0.0.4. If originator is None, use DEFAULT_FROM_SMS setting. Note: The API for this method is frozen. New code wanting to extend the functionality should the the Message class directly. """ if isinstance(recipients, str): recipients = [recipients] msg = Message(body, originator, recipients, connection=connection) return msg.send(fail_silently=fail_silently)
def test_console_stream_kwarg(self) -> None: """ The console backend can be pointed at an arbitrary stream. """ stream = StringIO() connection = sms.get_connection('sms.backends.console.SmsBackend', stream=stream) message = Message('Content', '0600000000', ['0600000000']) connection.send_messages([message]) # type: ignore messages = stream.getvalue().split('\n' + ('-' * 79) + '\n') self.assertIn('from: ', messages[0])
def test_locmem_shared_messages(self) -> None: """ Make sure that the locmem backend populates the outbox. """ connections: List[Type[BaseSmsBackend]] = [ locmem.SmsBackend(), # type: ignore locmem.SmsBackend() # type: ignore ] message = Message('Content', '0600000000', ['0600000000']) for connection in connections: connection.send_messages([message]) # type: ignore self.assertEqual(len(sms.outbox), 2) # type: ignore
def test_file_sessions(self) -> None: """Make sure opening a connection creates a new file""" message = Message('Here is the message', '+12065550100', ['+441134960000']) connection = sms.get_connection() connection.send_messages([message]) # type: ignore self.assertEqual(len(os.listdir(self.tmp_dir)), 1) tmp_file = os.path.join(self.tmp_dir, os.listdir(self.tmp_dir)[0]) with open(tmp_file, 'rb') as fp: message = message_from_binary_file(fp) self.assertEqual(message.originator, '+12065550100') self.assertEqual(message.recipients, ['+441134960000'])