def test_should_send_newsletter(self): # Given from park.plugins.urls import DB_NAME bot = ChatRoomJabberBot( self.jid, self.password, debug=True, root=self.tempdir ) bar = '*****@*****.**' bot.users = {bar: 'bar'} url = 'http://muse-amuse.in' text = 'this is %s' % url db_path = join(bot.root, DB_NAME) bot.callback_message( None, xmpp.Message(frm=bar, typ='chat', body=text) ) self._wait_while(lambda: not exists(db_path)) extra_state = { 'last_newsletter': (datetime.now() - timedelta(10)).isoformat() } bot.save_state(extra_state=extra_state) # When with captured_stdout() as captured: self._run_bot(bot, lambda: captured.output) # Then message = message_from_string(captured.output) for payload in message.get_payload(): self.assertIn(url, payload.get_payload(decode=True))
def capture_output_from_hooks(): with captured_stdout() as captured: threads = [ self._run_hook_in_thread(hook, self, username, text) for hook in self._message_processors ] [thread.join() for thread in threads] self.message_queue.extend(captured.output.splitlines())
def wrapper(self, message, args): f_args = getargspec(function).args allowed_args = [bot, message, args] n = len(f_args) if n <= 3: args = allowed_args[3-n:] with captured_stdout() as captured: result = function(*args) if captured.output: self.message_queue.append(captured.output) else: result = None return result