def test_inject_message_id_hash(self): # When the injected message has a Message-ID header, the injected # message will also get an Message-ID-Hash header. inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertEqual(items[0].msg['message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K')
def test_inject_text_without_message_id(self): # inject_text() adds a Message-ID header if it's missing. filtered = self._remove_line('message-id') self.assertNotIn('Message-ID', filtered) inject_text(self.mlist, filtered) items = get_queue_messages('in', expected_count=1) self.assertIn('message-id', items[0].msg)
def test_inject_text_without_date(self): # inject_text() adds a Date header if it's missing. filtered = self._remove_line('date') self.assertNotIn('date', filtered) inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertIn('date', items[0].msg)
def test_inject_message_id_hash(self): # When the injected message has a Message-ID header, the injected # message will also get an X-Message-ID-Hash header. inject_text(self.mlist, self.text) items = get_queue_messages('in') self.assertEqual(items[0].msg['x-message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K')
def test_inject_text_without_date(self): # inject_text() adds a Date header if it's missing. filtered = self._remove_line('date') self.assertFalse('date' in filtered) inject_text(self.mlist, self.text) items = get_queue_messages('in') self.assertTrue('date' in items[0].msg)
def test_inject_text_without_message_id(self): # inject_text() adds a Message-ID header if it's missing. filtered = self._remove_line('message-id') self.assertFalse('Message-ID' in filtered) inject_text(self.mlist, filtered) items = get_queue_messages('in') self.assertTrue('message-id' in items[0].msg)
def test_inject_text_adds_original_size(self): # The metadata gets an original_size attribute that is the length of # the injected text. inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertEqual(items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def test_inject_message_id_hash_without_message_id(self): # When the injected message does not have a Message-ID header, a # Message-ID header will be added, and the injected message will also # get an Message-ID-Hash header. filtered = self._remove_line('message-id') self.assertNotIn('Message-ID', filtered) self.assertNotIn('Message-ID-Hash', filtered) inject_text(self.mlist, filtered) items = get_queue_messages('in', expected_count=1) self.assertIn('message-id', items[0].msg) self.assertIn('message-id-hash', items[0].msg)
def test_inject_message_id_hash_without_message_id(self): # When the injected message does not have a Message-ID header, a # Message-ID header will be added, and the injected message will also # get an X-Message-ID-Hash header. filtered = self._remove_line('message-id') self.assertFalse('Message-ID' in filtered) self.assertFalse('X-Message-ID-Hash' in filtered) inject_text(self.mlist, filtered) items = get_queue_messages('in') self.assertTrue('message-id' in items[0].msg) self.assertTrue('x-message-id-hash' in items[0].msg)
def test_inject_text_adds_original_size(self): # The metadata gets an original_size attribute that is the length of # the injected text. inject_text(self.mlist, self.text) items = get_queue_messages('in') self.assertEqual(items[0].msgdata['original_size'], # Add back the X-Message-ID-Header which was in the # message contributing to the original_size, but # wasn't in the original text. Don't forget the # newline! len(self.text) + 52)
def test_inject_text_adds_original_size(self): # The metadata gets an original_size attribute that is the length of # the injected text. inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertEqual( items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def test_inject_text_to_queue(self): # Explicitly use a different queue. inject_text(self.mlist, self.text, switchboard='virgin') get_queue_messages('in', expected_count=0) items = get_queue_messages('virgin', expected_count=1) # Remove the Message-ID-Hash header which isn't in the original text. del items[0].msg['message-id-hash'] del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual(items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def test_inject_text_to_queue(self): # Explicitly use a different queue. inject_text(self.mlist, self.text, switchboard='virgin') get_queue_messages('in', expected_count=0) items = get_queue_messages('virgin', expected_count=1) # Remove the Message-ID-Hash header which isn't in the original text. del items[0].msg['message-id-hash'] del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual( items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def test_inject_text_to_queue(self): # Explicitly use a different queue. inject_text(self.mlist, self.text, switchboard='virgin') items = get_queue_messages('in') self.assertEqual(len(items), 0) items = get_queue_messages('virgin') self.assertEqual(len(items), 1) # Remove the X-Message-ID-Hash header which isn't in the original text. del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual(items[0].msgdata['original_size'], # Add back the X-Message-ID-Header which was in the # message contributing to the original_size, but # wasn't in the original text. Don't forget the # newline! len(self.text) + 52)
def test_inject_text_to_queue(self): # Explicitly use a different queue. inject_text(self.mlist, self.text, switchboard='virgin') items = get_queue_messages('in') self.assertEqual(len(items), 0) items = get_queue_messages('virgin') self.assertEqual(len(items), 1) # Remove the X-Message-ID-Hash header which isn't in the original text. del items[0].msg['x-message-id-hash'] self.eq(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listname'], '*****@*****.**') self.assertEqual(items[0].msgdata['original_size'], # Add back the X-Message-ID-Header which was in the # message contributing to the original_size, but # wasn't in the original text. Don't forget the # newline! len(self.text) + 52)
def inject(ctx, queue, message_file, keywords, listspec): mlist = getUtility(IListManager).get(listspec) if mlist is None: ctx.fail(_('No such list: $listspec')) queue_name = ('in' if queue is None else queue) switchboard = config.switchboards.get(queue_name) if switchboard is None: ctx.fail(_('No such queue: $queue')) try: message_text = message_file.read() except KeyboardInterrupt: print('Interrupted') sys.exit(1) kws = {} for keyvalue in keywords: key, equals, value = keyvalue.partition('=') kws[key] = value inject_text(mlist, message_text, switchboard=queue, **kws)
def test_inject_text(self): # Test basic inject_text() call. inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertIsInstance(items[0].msg, Message) self.assertEqual(items[0].msg['message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K') # Delete these headers because they don't exist in the original text. del items[0].msg['message-id-hash'] del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual(items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def test_inject_text(self): # Test basic inject_text() call. inject_text(self.mlist, self.text) items = get_queue_messages('in') self.assertEqual(len(items), 1) self.assertTrue(isinstance(items[0].msg, Message)) self.assertEqual(items[0].msg['x-message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K') # Delete that header because it is not in the original text. del items[0].msg['x-message-id-hash'] self.eq(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listname'], '*****@*****.**') self.assertEqual(items[0].msgdata['original_size'], # Add back the X-Message-ID-Header which was in the # message contributing to the original_size, but # wasn't in the original text. Don't forget the # newline! len(self.text) + 52)
def test_inject_text(self): # Test basic inject_text() call. inject_text(self.mlist, self.text) items = get_queue_messages('in') self.assertEqual(len(items), 1) self.assertTrue(isinstance(items[0].msg, Message)) self.assertEqual(items[0].msg['x-message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K') # Delete that header because it is not in the original text. del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual(items[0].msgdata['original_size'], # Add back the X-Message-ID-Header which was in the # message contributing to the original_size, but # wasn't in the original text. Don't forget the # newline! len(self.text) + 52)
def test_inject_text(self): # Test basic inject_text() call. inject_text(self.mlist, self.text) items = get_queue_messages('in', expected_count=1) self.assertIsInstance(items[0].msg, Message) self.assertEqual(items[0].msg['message-id-hash'], 'GUXXQKNCHBFQAHGBFMGCME6HKZCUUH3K') # Delete these headers because they don't exist in the original text. del items[0].msg['message-id-hash'] del items[0].msg['x-message-id-hash'] self.assertMultiLineEqual(items[0].msg.as_string(), self.text) self.assertEqual(items[0].msgdata['listid'], 'test.example.com') self.assertEqual( items[0].msgdata['original_size'], # Add back the Message-ID-Hash and X-Message-ID-Hash # headers which wer in the message contributing to the # original_size, but weren't in the original text. # Don't forget the space, delimeter, and newline! len(self.text) + 50 + 52)
def process(self, args): """See `ICLISubCommand`.""" # Process --show first; if given, print output and exit, ignoring all # other command line switches. if args.show: print('Available queues:') for switchboard in sorted(config.switchboards): print(' ', switchboard) return # Could be None or sequence of length 0. if args.listname is None: self.parser.error(_('List name is required')) return assert len( args.listname) == 1, ('Unexpected positional arguments: %s' % args.listname) fqdn_listname = args.listname[0] mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: self.parser.error(_('No such list: $fqdn_listname')) return queue = ('in' if args.queue is None else args.queue) switchboard = config.switchboards.get(queue) if switchboard is None: self.parser.error(_('No such queue: $queue')) return if args.filename in (None, '-'): try: message_text = sys.stdin.read() except KeyboardInterrupt: print('Interrupted') sys.exit(1) else: with open(args.filename) as fp: message_text = fp.read() keywords = {} for keyvalue in args.keywords: key, equals, value = keyvalue.partition('=') keywords[key] = value inject_text(mlist, message_text, switchboard=queue, **keywords)
def process(self, args): """See `ICLISubCommand`.""" # Process --show first; if given, print output and exit, ignoring all # other command line switches. if args.show: print('Available queues:') for switchboard in sorted(config.switchboards): print(' ', switchboard) return # Could be None or sequence of length 0. if args.listname is None: self.parser.error(_('List name is required')) return assert len(args.listname) == 1, ( 'Unexpected positional arguments: %s' % args.listname) fqdn_listname = args.listname[0] mlist = getUtility(IListManager).get(fqdn_listname) if mlist is None: self.parser.error(_('No such list: $fqdn_listname')) return queue = ('in' if args.queue is None else args.queue) switchboard = config.switchboards.get(queue) if switchboard is None: self.parser.error(_('No such queue: $queue')) return if args.filename in (None, '-'): try: message_text = sys.stdin.read() except KeyboardInterrupt: print('Interrupted') sys.exit(1) else: with open(args.filename) as fp: message_text = fp.read() keywords = {} for keyvalue in args.keywords: key, equals, value = keyvalue.partition('=') keywords[key] = value inject_text(mlist, message_text, switchboard=queue, **keywords)
def on_post(self, request, response): """Inject a message into the queue.""" try: validator = Validator(list_id=str, text=str) values = validator(request) except ValueError as error: bad_request(response, str(error)) return list_id = values['list_id'] mlist = getUtility(IListManager).get_by_list_id(list_id) if mlist is None: bad_request(response, 'No such list: {}'.format(list_id)) return try: filebase = inject_text(mlist, values['text'], switchboard=self._name) except Exception as error: bad_request(response, str(error)) return else: location = path_to('queues/{}/{}'.format(self._name, filebase)) created(response, location)
def on_post(self, request, response): """Inject a message into the queue.""" try: validator = Validator(list_id=str, text=str) values = validator(request) except ValueError as error: bad_request(response, str(error)) return list_id = values['list_id'] mlist = getUtility(IListManager).get_by_list_id(list_id) if mlist is None: bad_request(response, 'No such list: {}'.format(list_id)) return try: filebase = inject_text( mlist, values['text'], switchboard=self._name) except Exception as error: bad_request(response, str(error)) return else: location = path_to('queues/{}/{}'.format(self._name, filebase)) created(response, location)
def test_inject_text_with_keywords(self): # Keyword arguments are copied into the metadata. inject_text(self.mlist, self.text, foo='yes', bar='no') items = get_queue_messages('in', expected_count=1) self.assertEqual(items[0].msgdata['foo'], 'yes') self.assertEqual(items[0].msgdata['bar'], 'no')
def test_inject_text_with_recipients(self): # Explicit recipients end up in the metadata. recipients = ['*****@*****.**', '*****@*****.**'] inject_text(self.mlist, self.text, recipients) items = get_queue_messages('in') self.assertEqual(items[0].msgdata['recipients'], recipients)
def test_inject_text_with_keywords(self): # Keyword arguments are copied into the metadata. inject_text(self.mlist, self.text, foo='yes', bar='no') items = get_queue_messages('in') self.assertEqual(items[0].msgdata['foo'], 'yes') self.assertEqual(items[0].msgdata['bar'], 'no')
def test_inject_text_with_recipients(self): # Explicit recipients end up in the metadata. recipients = ['*****@*****.**', '*****@*****.**'] inject_text(self.mlist, self.text, recipients) items = get_queue_messages('in', expected_count=1) self.assertEqual(items[0].msgdata['recipients'], recipients)