Exemplo n.º 1
0
 def test_send(self):
     schedule_messages('text', recipients('fb', ''))
     send_scheduled_messages()
     assert_called_n(messenger_fb.lib.post)
     assert messenger_fb.lib.post.call_args[1]['proxies'] == {
         'https': '0.0.0.0'
     }
Exemplo n.º 2
0
 def test_send(self):
     schedule_messages('text', recipients('telegram', '1234567'))
     send_scheduled_messages()
     assert_called_n(messenger_telegram.lib.post, 2)
     assert messenger_telegram.lib.post.call_args[1]['proxies'] == {
         'https': 'socks5://user:pass@host:port'
     }
Exemplo n.º 3
0
def test_send_scheduled_messages_unknown_messenger():
    message = Message()
    message.save()
    dispatch = Dispatch(message=message, messenger='unknownname')
    dispatch.save()

    with pytest.raises(UnknownMessengerError):
        send_scheduled_messages()

    send_scheduled_messages(ignore_unknown_messengers=True)
Exemplo n.º 4
0
def test_send_scheduled_messages_unknown_messenger():
    message = Message()
    message.save()
    dispatch = Dispatch(message=message, messenger='unknownname')
    dispatch.save()

    with pytest.raises(UnknownMessengerError):
        send_scheduled_messages()

    send_scheduled_messages(ignore_unknown_messengers=True)
Exemplo n.º 5
0
def test_send_scheduled_messages_dynamic_context():
    msgr = get_registered_messenger_object('test_messenger')  # type: MessengerForTest
    msg_dyn = MessagePlainDynamicForTest('my_dyn_msg')
    schedule_messages(msg_dyn, recipients(msgr, ['three', 'four']))
    send_scheduled_messages()

    assert len(msgr.last_send['dispatch_models']) == 2
    assert msgr.last_send['message_model'].cls == 'testplain_dyn'
    assert msgr.last_send['message_cls'] == MessagePlainDynamicForTest
    assert msgr.last_send['dispatch_models'][0].message_cache == 'my_dyn_msg -- three%s' % WONDERLAND_DOMAIN
    assert msgr.last_send['dispatch_models'][1].message_cache == 'my_dyn_msg -- four%s' % WONDERLAND_DOMAIN
Exemplo n.º 6
0
def test_send_scheduled_messages_dynamic_context():
    msgr = get_registered_messenger_object('test_messenger')  # type: MessengerForTest
    msg_dyn = MessagePlainDynamicForTest('my_dyn_msg')
    schedule_messages(msg_dyn, recipients(msgr, ['three', 'four']))
    send_scheduled_messages()

    assert len(msgr.last_send['dispatch_models']) == 2
    assert msgr.last_send['message_model'].cls == 'testplain_dyn'
    assert msgr.last_send['message_cls'] == MessagePlainDynamicForTest
    assert msgr.last_send['dispatch_models'][0].message_cache == f'my_dyn_msg -- three{WONDERLAND_DOMAIN}'
    assert msgr.last_send['dispatch_models'][1].message_cache == f'my_dyn_msg -- four{WONDERLAND_DOMAIN}'
Exemplo n.º 7
0
def test_exception_propagation(monkeypatch):
    schedule_messages('text', recipients('telegram', ''))
    schedule_messages('text', recipients('telegram', ''))

    def new_method(*args, **kwargs):
        raise Exception('telegram beforesend failed')

    monkeypatch.setattr(messenger_telegram, 'before_send', new_method)
    send_scheduled_messages()

    errors = list(DispatchError.objects.all())
    assert len(errors) == 2
    assert errors[0].error_log == 'telegram beforesend failed'
    assert errors[1].error_log == 'telegram beforesend failed'
    def handle(self, *args, **options):
        priority = options.get('priority', None)
        priority_str = ''

        if priority is not None:
            priority_str = 'with priority %s ' % priority

        self.stdout.write('Sending scheduled messages %s...\n' % priority_str)
        try:
            send_scheduled_messages(priority=priority)
        except Exception as e:
            self.stderr.write(self.style.ERROR('Error on send: %s\n' % e))
        else:
            self.stdout.write('Sending done.\n')
Exemplo n.º 9
0
def test_send_scheduled_messages():
    # This one won't count, as won't fit into message priority filter.
    schedule_messages(
        MessagePlainDynamicForTest('my_dyn_msg'),
        recipients('test_messenger', ['three', 'four']))

    msgr = get_registered_messenger_object('test_messenger')  # type: MessengerForTest
    msg = MessagePlainForTest('my_message')
    schedule_messages(msg, recipients(msgr, ['one', 'two']))
    send_scheduled_messages(priority=MessagePlainForTest.priority)

    assert len(msgr.last_send['dispatch_models']) == 2
    assert msgr.last_send['message_model'].cls == 'testplain'
    assert msgr.last_send['message_cls'] == MessagePlainForTest
    assert msgr.last_send['dispatch_models'][0].message_cache == 'my_message'
    assert msgr.last_send['dispatch_models'][1].message_cache == 'my_message'
Exemplo n.º 10
0
def test_send_scheduled_messages():
    # This one won't count, as won't fit into message priority filter.
    schedule_messages(
        MessagePlainDynamicForTest('my_dyn_msg'),
        recipients('test_messenger', ['three', 'four']))

    msgr = get_registered_messenger_object('test_messenger')  # type: MessengerForTest
    msg = MessagePlainForTest('my_message')
    schedule_messages(msg, recipients(msgr, ['one', 'two']))
    send_scheduled_messages(priority=MessagePlainForTest.priority)

    assert len(msgr.last_send['dispatch_models']) == 2
    assert msgr.last_send['message_model'].cls == 'testplain'
    assert msgr.last_send['message_cls'] == MessagePlainForTest
    assert msgr.last_send['dispatch_models'][0].message_cache == 'my_message'
    assert msgr.last_send['dispatch_models'][1].message_cache == 'my_message'
Exemplo n.º 11
0
    def test_send_fail(self):
        schedule_messages('text', recipients('xmppsleek', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('xmppsleek failed')

        old_method = messenger_xmpp.xmpp.send_message
        messenger_xmpp.xmpp.send_message = new_method
        messenger_xmpp._session_started = True
        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'xmppsleek failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_xmpp.xmpp.send_message = old_method
Exemplo n.º 12
0
    def test_send_fail(self):
        schedule_messages('text', recipients('twitter', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('tweet failed')

        old_method = messenger_twitter.api.statuses.update
        messenger_twitter.api.statuses.update = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'tweet failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_twitter.api.statuses.update = old_method
Exemplo n.º 13
0
    def test_send_fail(self):
        schedule_messages('text', recipients('smtp', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('smtp failed')

        old_method = messenger_smtp.smtp.sendmail
        messenger_smtp.smtp.sendmail = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'smtp failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_smtp.smtp.sendmail = old_method
Exemplo n.º 14
0
    def test_send_fail(self):
        schedule_messages('text', recipients('smtp', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('smtp failed')

        old_method = messenger_smtp.smtp.sendmail
        messenger_smtp.smtp.sendmail = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'smtp failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_smtp.smtp.sendmail = old_method
Exemplo n.º 15
0
    def test_send_fail(self):
        schedule_messages('text', recipients('vk', '12345'))

        def new_method(*args, **kwargs):
            raise Exception('vk failed')

        old_method = messenger_vk.lib.post
        messenger_vk.lib.post = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'vk failed'
            assert errors[0].dispatch.address == '12345'
        finally:
            messenger_vk.lib.post = old_method
Exemplo n.º 16
0
    def test_send_fail(self):
        schedule_messages('text', recipients('vk', '12345'))

        def new_method(*args, **kwargs):
            raise Exception('vk failed')

        old_method = messenger_vk.lib.post
        messenger_vk.lib.post = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'vk failed'
            assert errors[0].dispatch.address == '12345'
        finally:
            messenger_vk.lib.post = old_method
Exemplo n.º 17
0
    def handle(self, *args, **options):
        priority = options.get('priority', None)
        priority_str = ''

        if priority is not None:
            priority_str = 'with priority %s ' % priority

        self.stdout.write('Sending scheduled messages %s ...\n' % priority_str)

        try:
            send_scheduled_messages(priority=priority)

        except Exception as e:
            self.stderr.write(self.style.ERROR('Error on send: %s\n%s' % (e, format_exc())))

        else:
            self.stdout.write('Sending done.\n')
Exemplo n.º 18
0
    def test_send_fail(self):
        schedule_messages('text', recipients('xmppsleek', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('xmppsleek failed')

        old_method = messenger_xmpp.xmpp.send_message
        messenger_xmpp.xmpp.send_message = new_method
        messenger_xmpp._session_started = True
        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'xmppsleek failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_xmpp.xmpp.send_message = old_method
Exemplo n.º 19
0
    def test_send_fail(self):
        schedule_messages('text', recipients('twitter', 'someone'))

        def new_method(*args, **kwargs):
            raise Exception('tweet failed')

        old_method = messenger_twitter.api.statuses.update
        messenger_twitter.api.statuses.update = new_method

        try:
            send_scheduled_messages()
            errors = DispatchError.objects.all()
            assert len(errors) == 1
            assert errors[0].error_log == 'tweet failed'
            assert errors[0].dispatch.address == 'someone'
        finally:
            messenger_twitter.api.statuses.update = old_method
Exemplo n.º 20
0
    def handle(self, *args, **options):
        priority = options.get('priority', None)
        priority_str = ''

        if priority is not None:
            priority_str = f'with priority {priority} '

        self.stdout.write(f'Sending scheduled messages {priority_str} ...\n')

        try:
            send_scheduled_messages(priority=priority)

        except Exception as e:
            self.stderr.write(
                self.style.ERROR(f'Error on send: {e}\n{format_exc()}'))

        else:
            self.stdout.write('Sending done.\n')
Exemplo n.º 21
0
def send_messages_view(request, id):
    """
    Sends an internal email to the desired recipients.
    """
    # We extract the recipient's id.

    to_user = User.objects.get(pk=id)
    sender = request.user

    # Here we define the smtp recipients to inform them via email that
    # they have an unread notification.
    # my_smtp_recipients = recipients(SMTPMessenger, [to_user.email]),

    # Here we define the internal mail recipients
    # my_recipients = recipients(MyMessenger, [to_user])

    # schedule_email('There is hope.', [to_user, sender.username])
    #schedule_email('You have an unread notification in eduanalytics.',[my_smtp_recipients, sender]
    # schedule_messages('You are awesome VEER.', recipients('mymessenger', ['user1--address', user2]))

    schedule_messages(
        # You can pass one or several message objects:
        [
            # The first param of this Message Type is `subject`. The second a dictionary.
            EmailHtmlMessage('Congratulations Nancy for your baby', {
                'title': 'Message to my Sister',
                'entry': 'I will stand by you.'
            }),
        ],

        # The same applies to recipients: add one or many as required:
        recipients(MyMessenger, [
            to_user,
        ]),
        # It's useful sometimes to know message sender in terms of Django users:
        sender=request.user)

    # this function sends all scheduled messages.
    send_scheduled_messages(ignore_unknown_messengers=True,
                            ignore_unknown_message_types=True)
    HttpResponseRedirect(reverse('correo:inbox'))
Exemplo n.º 22
0
 def test_send(self):
     schedule_messages('text', recipients('xmppsleek', 'someone'))
     send_scheduled_messages()
     messenger_xmpp.xmpp.send_message.assert_called_once_with(
         mtype='chat', mbody='text', mfrom='somjid', mto='someone')
Exemplo n.º 23
0
 def test_send(self):
     schedule_messages('text', recipients('smtp', 'someone'))
     send_scheduled_messages()
     assert_called_n(messenger_smtp.smtp.sendmail)
Exemplo n.º 24
0
 def test_send(self):
     schedule_messages('text', recipients('vk', '12345'))
     send_scheduled_messages()
     assert_called_n(messenger_vk.lib.post)
     assert messenger_vk.lib.post.call_args[1]['data'][
         'owner_id'] == '12345'
Exemplo n.º 25
0
def task_send_messages(sig_num):
    """Отправка оповещений."""
    send_scheduled_messages(priority=1)
Exemplo n.º 26
0
 def test_send(self):
     schedule_messages('text', recipients('xmppsleek', 'someone'))
     send_scheduled_messages()
     messenger_xmpp.xmpp.send_message.assert_called_once_with(
         mtype='chat', mbody='text', mfrom='somjid', mto='someone'
     )
Exemplo n.º 27
0
 def test_send(self):
     schedule_messages('text', recipients('twitter', 'someone'))
     send_scheduled_messages()
     messenger_twitter.api.statuses.update.assert_called_with(status='@someone text')
Exemplo n.º 28
0
 def test_send(self):
     schedule_messages('text', recipients('telegram', '1234567'))
     send_scheduled_messages()
     assert_called_n(messenger_telegram.lib.post, 2)
Exemplo n.º 29
0
 def test_send(self):
     schedule_messages('text', recipients('fb', ''))
     send_scheduled_messages()
     assert_called_n(messenger_fb.lib.post)
Exemplo n.º 30
0
 def test_send(self):
     schedule_messages('text', recipients('twitter', 'someone'))
     send_scheduled_messages()
     messenger_twitter.api.statuses.update.assert_called_with(
         status='@someone text')
Exemplo n.º 31
0
 def test_send(self):
     schedule_messages('text', recipients('smtp', 'someone'))
     send_scheduled_messages()
     assert_called_n(messenger_smtp.smtp.sendmail)
Exemplo n.º 32
0
 def test_send(self):
     schedule_messages('text', recipients('telegram', '1234567'))
     send_scheduled_messages()
     assert_called_n(messenger_telegram.lib.post, 2)
Exemplo n.º 33
0
def task_digest_send(sig_num):
    """Рассылка еженедельного дайджеста.
    Пятница 13:00 Нск (9:00 Мск).

    """
    send_scheduled_messages(priority=7)
Exemplo n.º 34
0
 def test_send(self):
     schedule_messages('text', recipients('fb', ''))
     send_scheduled_messages()
     assert_called_n(messenger_fb.lib.post)
Exemplo n.º 35
0
 def test_send(self):
     schedule_messages('text', recipients('vk', '12345'))
     send_scheduled_messages()
     assert_called_n(messenger_vk.lib.post)
Exemplo n.º 36
0
 def test_send(self):
     schedule_messages('text', recipients('vk', '12345'))
     send_scheduled_messages()
     assert_called_n(messenger_vk.lib.post)