예제 #1
0
def test_configuration_with_multiplie_producers_and_consumers(
    db: MockFixture,
    mocker: MockFixture,
    setup: tuple[User, User, list[ExternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup

    fake_messages = messages_should_be_consumed + [
        ExternalMessage('unknown_user', user1.username,
                        'message from unknown user'),
    ]

    mocker.patch.object(SampleProducer,
                        'produce_external_messages',
                        return_value=fake_messages)
    consume_messages_spy = mocker.spy(SampleConsumer, 'consume_messages')

    configuration = ConfigurationFactory(
        consumers=(ConsumerModelFactory(), ConsumerModelFactory()),
        producers=(ProducerModelFactory(), ProducerModelFactory()),
        users=(user1, user2),
    )
    configuration.run()

    assert consume_messages_spy.call_count == 2
    assert consume_messages_spy.call_args.args[
        1] == messages_should_be_consumed * 2
예제 #2
0
def test_run_configuration_should_filter_message_where_receiver_is_not_working(
    db: MockFixture,
    mocker: MockFixture,
    setup: tuple[User, User, list[ExternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup
    user_not_working = UserFactory(
        on_leave=True,
        working_time_start=datetime.time(8, 0, 0),
        working_time_end=datetime.time(17, 0, 0),
    )

    fake_messages = messages_should_be_consumed + [
        ExternalMessage(user2.username, user_not_working.username,
                        'message to not working user'),
    ]

    mocker.patch.object(SampleProducer,
                        'produce_external_messages',
                        return_value=fake_messages)

    consume_messages_spy = mocker.spy(SampleConsumer, 'consume_messages')

    configuration = ConfigurationFactory(
        consumers=(ConsumerModelFactory(), ),
        producers=(ProducerModelFactory(), ),
        users=(user1, user2, user_not_working),
        message_filters=(MessageFilterModelFactory(
            object_type=ReceiverWorkingMessageFilter.__name__), ),
    )

    configuration.run()

    assert consume_messages_spy.call_args.args[
        1] == messages_should_be_consumed
예제 #3
0
def test_run_configuration_should_filter_message_with_skip_keywords(
    db: MockFixture,
    mocker: MockFixture,
    setup: tuple[User, User, list[ExternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup

    fake_messages = messages_should_be_consumed + [
        ExternalMessage(user2.username, user1.username,
                        'message with skip_keyword1'),
    ]

    mocker.patch.object(SampleProducer,
                        'produce_external_messages',
                        return_value=fake_messages)

    consume_messages_spy = mocker.spy(SampleConsumer, 'consume_messages')

    configuration = ConfigurationFactory(
        consumers=(ConsumerModelFactory(), ),
        producers=(ProducerModelFactory(), ),
        users=(user1, user2),
        message_filters=(MessageFilterModelFactory(
            object_type=SkipKeywordsMessageFilter.__name__,
            parameters={'skip_keywords': ['skip_keyword1']},
        ), ),
    )

    configuration.run()

    assert consume_messages_spy.call_args.args[
        1] == messages_should_be_consumed
예제 #4
0
def test_configuration_without_consumers(
    db: MockFixture,
    setup: tuple[User, User, list[ExternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup
    configuration = ConfigurationFactory(
        consumers=(ConsumerModelFactory(), ),
        users=(user1, user2),
    )
    with capture_logs() as cap_logs:
        configuration.run()
        assert {
            'configuration_id': configuration.id,
            'event': 'No producers or consumers, skipping...',
            'log_level': 'info',
        } in cap_logs
예제 #5
0
def test_run_configuration_should_filter_message_where_receiver_doesnt_have_consumer_username(
    db: MockFixture,
    mocker: MockFixture,
    setup: tuple[User, User, list[ExternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup
    user_without_consumer_username = UserFactory(
        on_leave=False,
        working_time_start=datetime.time(8, 0, 0),
        working_time_end=datetime.time(17, 0, 0),
    )
    user_without_consumer_username.additional_info.pop(
        SampleConsumer.USERNAME_KEY)
    user_without_consumer_username.save()

    messages_should_be_consumed = [
        ExternalMessage(user1.username, user2.username, 'message1'),
        ExternalMessage(user2.username, user1.username, 'message2'),
    ]

    fake_messages = messages_should_be_consumed + [
        ExternalMessage(
            user1.username,
            user_without_consumer_username.username,
            'message to user without consumer username',
        ),
    ]

    mocker.patch.object(SampleProducer,
                        'produce_external_messages',
                        return_value=fake_messages)

    consume_messages_spy = mocker.spy(SampleConsumer, 'consume_messages')

    configuration = ConfigurationFactory(
        consumers=(ConsumerModelFactory(), ),
        producers=(ProducerModelFactory(), ),
        users=(user1, user2, user_without_consumer_username),
    )

    configuration.run()

    assert consume_messages_spy.call_args.args[
        1] == messages_should_be_consumed
예제 #6
0
def test_create_configuration(
    db: MockFixture,
    client: Client,
) -> None:
    producers = [ProducerModelFactory() for _ in range(2)]
    consumers = [ConsumerModelFactory() for _ in range(2)]
    users = [UserFactory() for _ in range(2)]
    response = graphql_query(
        """
        mutation {
            configuration(
                input: {
                    name: "fuck_muasdltiplit"
                    Consumers: [""" + ','.join(str(consumer.id) for consumer in consumers) + """]
                    Producers: [""" + ','.join(str(producer.id) for producer in producers) + """]
                    users: [""" + ','.join(str(user.id) for user in users) + """]
                }
            ) {
            configuration {
                id
                name
                Producers {
                    id
                }
                Consumers {
                    id
                }
                users {
                    id
                }
                MessageFilters {
                    id
                }
            }
            }
        }
        """,
        client=client,
    )
    content = json.loads(response.content)

    assert 'errors' not in content