Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 4
0
def test_schedule_configuration_create_periodic_task_on_correct_post_request(
    db: MockFixture, ) -> None:
    configuration = ConfigurationFactory()
    User.objects.create_superuser(
        'superuser',
        '*****@*****.**',
        'superuser',
    )
    client = Client()
    client.login(username='******', password='******')  # noqa S106
    response = client.post(
        '/configuration/configuration/schedule_configuration/',
        data={
            'minute': '*',
            'hour': '*',
            'day_of_week': '*',
            'day_of_month': '*',
            'month_of_year': '*',
            'timezone': 'UTC',
            'configuration': configuration.id,
        },
    )
    assert response.status_code == 302
    assert response.url == '/configuration/configuration/'

    assert PeriodicTask.objects.filter(kwargs='{"configuration_id": ' +
                                       str(configuration.id) +
                                       '}', ).count() == 1
Esempio n. 5
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
Esempio n. 6
0
def test_run_configuration_in_api_sync_mode(db: MockFixture) -> None:
    configuration = ConfigurationFactory()
    user = UserFactory()
    client = APIClient()
    client.force_authenticate(user=user)
    response = client.post(
        f'/restapi/configurations/'
        f'{configuration.id}/run_configuration/', )
    assert response.json() == {'status': 'success'}
Esempio n. 7
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
Esempio n. 8
0
def test_skip_messages_filter(
    db: MockFixture,
    setup: tuple[User, User, list[InternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup

    configuration = ConfigurationFactory(users=(user1, user2), )

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

    assert messages_should_be_consumed == SkipKeywordsMessageFilter(
        **{'skip_keywords': ['skip_keyword1']}, )(
            fake_messages,
            configuration,
        )
Esempio n. 9
0
def test_schedule_configuration_use_case_creates_periodic_task(
    db: MockFixture,
) -> None:
    configuration = ConfigurationFactory()
    cleaned_data = {
        'minute': '*',
        'hour': '*',
        'day_of_week': '*',
        'day_of_month': '*',
        'month_of_year': '*',
        'timezone': pytz.UTC,
        'configuration': configuration,
    }
    ScheduleConfigurationUseCase(**cleaned_data).execute()

    assert PeriodicTask.objects.filter(
        task='configuration.tasks.run_configuration',
        kwargs=json.dumps({'configuration_id': configuration.id}),
    ).count() == 1
Esempio n. 10
0
def test_get_all_configurations_in_api(db: MockFixture) -> None:
    configurations = [ConfigurationFactory() for _ in range(2)]
    user = UserFactory()
    client = APIClient()
    client.force_authenticate(user=user)
    response = client.get('/restapi/configurations/')

    expected_configurations_json = [
        ConfigurationSerializer(configuration)
        for configuration in configurations
    ]
    for configuration in expected_configurations_json:
        configuration.context['request'] = response.wsgi_request

    expected_configurations_json = [
        configuration.data for configuration in expected_configurations_json
    ]

    actual_configurations_json = response.json()
    assert actual_configurations_json == expected_configurations_json
Esempio n. 11
0
def test_receiver_exists_filter(
    db: MockFixture,
    setup: tuple[User, User, list[InternalMessage]],
) -> None:
    user1, user2, messages_should_be_consumed = setup
    user_not_in_config = UserFactory(
        on_leave=False,
        working_time_start=datetime.time(8, 0, 0),
        working_time_end=datetime.time(17, 0, 0),
    )

    fake_messages = messages_should_be_consumed + [
        InternalMessage(user2, user_not_in_config,
                        'message to not in config user'),
    ]

    configuration = ConfigurationFactory(users=(user1, user2), )

    assert messages_should_be_consumed == ReceiverExistsMessageFilter()(
        fake_messages,
        configuration,
    )
Esempio n. 12
0
def test_schedule_configuration_returns_form_on_incorrect_post_request(
    db: MockFixture, ) -> None:
    configuration = ConfigurationFactory()
    User.objects.create_superuser(
        'superuser',
        '*****@*****.**',
        'superuser',
    )
    client = Client()
    client.login(username='******', password='******')  # noqa S106
    response = client.post(
        '/configuration/configuration/schedule_configuration/',
        data={
            'minute': '142',
            'hour': '42',
            'day_of_week': '42',
            'day_of_month': '42',
            'month_of_year': '42',
            'timezone': 'UTC',
            'configuration': configuration.id,
        },
    )
    assert response.status_code == 200
Esempio n. 13
0
def test_str_configuration(db: MockFixture) -> None:
    configuration = ConfigurationFactory()
    assert 'test_configuration' in str(configuration)