def test_backend_arg(self) -> None: """Test backend argument of sms.get_connection().""" self.assertIsInstance( sms.get_connection('sms.backends.dummy.SmsBackend'), dummy.SmsBackend ) with tempfile.TemporaryDirectory() as tmp_dir: self.assertIsInstance( sms.get_connection( 'sms.backends.filebased.SmsBackend', file_path=tmp_dir ), filebased.SmsBackend ) if sys.platform == 'win32': msg = ('_getfullpathname: path should be string, bytes or ' 'os.PathLike, not object') else: msg = 'expected str, bytes or os.PathLike object, not object' with self.assertRaisesMessage(TypeError, msg): sms.get_connection( 'sms.backends.filebased.SmsBackend', file_path=object() ) self.assertIsInstance(sms.get_connection(), locmem.SmsBackend)
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 get_connection( self, fail_silently: bool = False ) -> Type['BaseSmsBackend']: from sms import get_connection if not self.connection: self.connection = get_connection(fail_silently=fail_silently) return self.connection
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 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_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'])
def get_connection(self, fail_silently=False): from sms import get_connection if not self.connection: self.connection = get_connection(fail_silently=fail_silently) return self.connection