Exemplo n.º 1
0
def test_sitemessage_check_undelivered(capsys, command_run):

    message = Message(cls='test_message')
    message.save()

    Dispatch.create(message, recipients('test_messenger', 'someoner'))

    dispatch = Dispatch.objects.all()
    assert len(dispatch) == 1

    dispatch = dispatch[0]
    dispatch.dispatch_status = dispatch.DISPATCH_STATUS_FAILED
    dispatch.save()

    def run_command(count=1):

        command_run('sitemessage_check_undelivered')

        out, err = capsys.readouterr()

        assert f'Undelivered dispatches count: {count}' in out
        assert err == ''

    run_command()

    # Now let's check a fallback works.
    try:
        GET_DISPATCHES_ARGS[0] = lambda kwargs: None

        run_command(3)
        assert GET_DISPATCHES_ARGS[0] is _get_dispatches

    finally:
        GET_DISPATCHES_ARGS[0] = _get_dispatches_for_update
Exemplo n.º 2
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.º 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_create(self, user):
        m, _ = Message.create('some', {'abc': 'abc'}, sender=user, priority=22)
        assert m.cls == 'some'
        assert m.context == {'abc': 'abc'}
        assert m.sender == user
        assert m.priority == 22
        assert not m.dispatches_ready

        ctx = {'a': 'a', 'b': 'b'}
        m, _ = Message.create('some2', ctx)
        assert m.cls == 'some2'
        assert m.context == ctx
        assert m.sender is None
        assert not m.dispatches_ready
Exemplo n.º 5
0
    def test_create(self, user):
        m, _ = Message.create('some', {'abc': 'abc'}, sender=user, priority=22)
        assert m.cls == 'some'
        assert m.context == {'abc': 'abc'}
        assert m.sender == user
        assert m.priority == 22
        assert not m.dispatches_ready

        ctx = {'a': 'a', 'b': 'b'}
        m, _ = Message.create('some2', ctx)
        assert m.cls == 'some2'
        assert m.context == ctx
        assert m.sender is None
        assert not m.dispatches_ready
Exemplo n.º 6
0
    def test_get_unread(self):

        m = Message()
        m.save()

        d1 = Dispatch(message_id=m.id)
        d1.save()

        d2 = Dispatch(message_id=m.id)
        d2.save()
        assert Dispatch.get_unread().count() == 2

        d2.read_status = Dispatch.READ_STATUS_READ
        d2.save()
        assert Dispatch.get_unread().count() == 1
Exemplo n.º 7
0
    def test_get_unread(self):

        m = Message()
        m.save()

        d1 = Dispatch(message_id=m.id)
        d1.save()

        d2 = Dispatch(message_id=m.id)
        d2.save()
        assert Dispatch.get_unread().count() == 2

        d2.read_status = Dispatch.READ_STATUS_READ
        d2.save()
        assert Dispatch.get_unread().count() == 1
Exemplo n.º 8
0
    def test_log_dispatches_errors(self):

        assert DispatchError.objects.count() == 0

        m = Message()
        m.save()

        d1 = Dispatch(message_id=m.id)
        d1.save()

        d1.error_log = 'some_text'

        Dispatch.log_dispatches_errors([d1])
        errors = DispatchError.objects.all()
        assert len(errors) == 1
        assert errors[0].error_log == 'some_text'
Exemplo n.º 9
0
    def test_log_dispatches_errors(self):

        assert DispatchError.objects.count() == 0

        m = Message()
        m.save()

        d1 = Dispatch(message_id=m.id)
        d1.save()

        d1.error_log = 'some_text'

        Dispatch.log_dispatches_errors([d1])
        errors = DispatchError.objects.all()
        assert len(errors) == 1
        assert errors[0].error_log == 'some_text'
Exemplo n.º 10
0
    def test_set_dispatches_statuses(self):

        m = Message()
        m.save()

        d = Dispatch(message_id=m.id)
        d.save()

        Dispatch.set_dispatches_statuses(**{'sent': [d]})
        d_ = Dispatch.objects.get(pk=d.id)
        assert d_.dispatch_status == Dispatch.DISPATCH_STATUS_SENT
        assert d_.retry_count == 1

        Dispatch.set_dispatches_statuses(**{'error': [d]})
        d_ = Dispatch.objects.get(pk=d.id)
        assert d_.dispatch_status == Dispatch.DISPATCH_STATUS_ERROR
        assert d_.retry_count == 2
Exemplo n.º 11
0
    def test_set_dispatches_statuses(self):

        m = Message()
        m.save()

        d = Dispatch(message_id=m.id)
        d.save()

        Dispatch.set_dispatches_statuses(**{'sent': [d]})
        d_ = Dispatch.objects.get(pk=d.id)
        assert d_.dispatch_status == Dispatch.DISPATCH_STATUS_SENT
        assert d_.retry_count == 1

        Dispatch.set_dispatches_statuses(**{'error': [d]})
        d_ = Dispatch.objects.get(pk=d.id)
        assert d_.dispatch_status == Dispatch.DISPATCH_STATUS_ERROR
        assert d_.retry_count == 2
Exemplo n.º 12
0
    def test_create(self, user):

        message = Message(cls='test_message')
        message.save()

        recipients_ = recipients('test_messenger', [user])
        recipients_ += recipients('buggy', 'idle')

        dispatches = Dispatch.create(message, recipients_)
        assert isinstance(dispatches[0], Dispatch)
        assert  isinstance(dispatches[1], Dispatch)
        assert dispatches[0].message_id == message.id
        assert dispatches[1].message_id == message.id
        assert dispatches[0].messenger == 'test_messenger'
        assert dispatches[1].messenger == 'buggy'

        dispatches = Dispatch.create(message, Recipient('msgr', None, 'address'))
        assert len(dispatches) == 1
Exemplo n.º 13
0
def test_prepare_undispatched():
    m, d = Message.create('testplain', {MessageBase.SIMPLE_TEXT_ID: 'abc'})

    Subscription.create('fred', 'testplain', 'test_messenger')
    Subscription.create('colon', 'testplain', 'test_messenger')

    dispatches = prepare_dispatches()
    assert len(dispatches) == 2
    assert dispatches[0].address == 'fred'
    assert dispatches[1].address == 'colon'
Exemplo n.º 14
0
    def test_create(self, user):

        message = Message(cls='test_message')
        message.save()

        recipients_ = recipients('test_messenger', [user])
        recipients_ += recipients('buggy', 'idle')

        dispatches = Dispatch.create(message, recipients_)
        assert isinstance(dispatches[0], Dispatch)
        assert isinstance(dispatches[1], Dispatch)
        assert dispatches[0].message_id == message.id
        assert dispatches[1].message_id == message.id
        assert dispatches[0].messenger == 'test_messenger'
        assert dispatches[1].messenger == 'buggy'

        dispatches = Dispatch.create(message,
                                     Recipient('msgr', None, 'address'))
        assert len(dispatches) == 1
Exemplo n.º 15
0
def test_prepare_undispatched():
    m, d = Message.create('testplain', {MessageBase.SIMPLE_TEXT_ID: 'abc'})

    Subscription.create('fred', 'testplain', 'test_messenger')
    Subscription.create('colon', 'testplain', 'test_messenger')

    dispatches = prepare_dispatches()
    assert len(dispatches) == 2
    assert dispatches[0].address == 'fred'
    assert dispatches[1].address == 'colon'
Exemplo n.º 16
0
    def create_message(dispatches_count):

        message = Message(cls='test_message')
        message.save()

        users = []

        for _ in range(dispatches_count):
            users.append(user_create())

        Dispatch.create(message, recipients('test_messenger', users))

        dispatches = Dispatch.objects.filter(
            dispatch_status=Dispatch.DISPATCH_STATUS_PENDING)

        for dispatch in dispatches:
            dispatch.dispatch_status = dispatch.DISPATCH_STATUS_SENT
            dispatch.save()

        return message, dispatches
Exemplo n.º 17
0
def prepare_dispatches():
    """Automatically creates dispatches for messages without them.

    :return: list of Dispatch
    :rtype: list
    """
    dispatches = []
    target_messages = Message.get_without_dispatches()

    cache = {}

    for message_model in target_messages:

        if message_model.cls not in cache:
            message_cls = get_registered_message_type(message_model.cls)
            subscribers = message_cls.get_subscribers()
            cache[message_model.cls] = (message_cls, subscribers)
        else:
            message_cls, subscribers = cache[message_model.cls]

        dispatches.extend(message_cls.prepare_dispatches(message_model))

    return dispatches
Exemplo n.º 18
0
    def test_deserialize_context(self):
        m = Message(cls='some_cls', context={'a': 'a', 'b': 'b', 'c': 'c'})
        m.save()

        m2 = Message.objects.get(pk=m.pk)
        assert m2.context == {'a': 'a', 'b': 'b', 'c': 'c'}
Exemplo n.º 19
0
    def test_get_type(self):
        m = Message(cls='test_message')

        assert m.get_type() is MessageForTest
Exemplo n.º 20
0
    def test_str(self):
        m = Message()
        m.cls = 'aaa'

        assert str(m) == 'aaa'
Exemplo n.º 21
0
    def test_str(self):
        m = Message()
        m.cls = 'aaa'

        assert str(m) == 'aaa'
Exemplo n.º 22
0
    def test_get_type(self):
        m = Message(cls='test_message')

        assert m.get_type() is MessageForTest
Exemplo n.º 23
0
    def test_deserialize_context(self):
        m = Message(cls='some_cls', context={'a': 'a', 'b': 'b', 'c': 'c'})
        m.save()

        m2 = Message.objects.get(pk=m.pk)
        assert m2.context == {'a': 'a', 'b': 'b', 'c': 'c'}