def test_main_raises_email_template_error(
        data_api_client,
        get_live_briefs_with_new_questions_and_answers_between_two_dates,
        get_ids_of_interested_suppliers_for_briefs,
        get_supplier_email_addresses_by_supplier_id, send_supplier_emails,
        logger):
    get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value = [
        FILTERED_BRIEFS[0]
    ]
    get_ids_of_interested_suppliers_for_briefs.return_value = {
        3: [FILTERED_BRIEFS[0]["id"]],
        4: [FILTERED_BRIEFS[0]["id"]],
        5: [FILTERED_BRIEFS[0]["id"]],
    }
    get_supplier_email_addresses_by_supplier_id.side_effect = [[
        '*****@*****.**'
    ], ['*****@*****.**'], ['*****@*****.**']]
    send_supplier_emails.side_effect = EmailTemplateError

    with pytest.raises(EmailTemplateError):
        main('api_url', 'api_token', 'M', 'preview', dry_run=False)

    assert send_supplier_emails.call_args_list == [
        mock.call('M', ['*****@*****.**'], mock.ANY, mock.ANY),
    ]
def test_main_catches_and_logs_email_errors_returns_false(
        data_api_client,
        get_live_briefs_with_new_questions_and_answers_between_two_dates,
        get_ids_of_interested_suppliers_for_briefs,
        get_supplier_email_addresses_by_supplier_id, send_supplier_emails,
        logger):
    get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value = [
        FILTERED_BRIEFS[0]
    ]
    get_ids_of_interested_suppliers_for_briefs.return_value = {
        3: [FILTERED_BRIEFS[0]["id"]],
        4: [FILTERED_BRIEFS[0]["id"]],
        5: [FILTERED_BRIEFS[0]["id"]],
    }
    get_supplier_email_addresses_by_supplier_id.side_effect = [[
        '*****@*****.**'
    ], ['*****@*****.**'], ['*****@*****.**']]
    send_supplier_emails.side_effect = [EmailError, True, EmailError]

    result = main('api_url', 'api_token', 'M', 'preview', dry_run=False)

    assert result is False
    assert send_supplier_emails.call_args_list == [
        mock.call('M', ['*****@*****.**'], mock.ANY, mock.ANY),
        mock.call('M', ['*****@*****.**'], mock.ANY, mock.ANY),
        mock.call('M', ['*****@*****.**'], mock.ANY, mock.ANY),
    ]
    assert logger.error.call_args == mock.call(
        'Email sending failed for the following supplier IDs: {supplier_ids}',
        extra={"supplier_ids": "3,5"})
def test_main_can_restrict_to_custom_list_of_suppliers(
        data_api_client,
        get_live_briefs_with_new_questions_and_answers_between_two_dates,
        get_ids_of_interested_suppliers_for_briefs,
        get_supplier_email_addresses_by_supplier_id, send_supplier_emails):
    get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value = [
        FILTERED_BRIEFS[0]
    ]
    get_ids_of_interested_suppliers_for_briefs.return_value = {
        3: [FILTERED_BRIEFS[0]["id"]],
        4: [FILTERED_BRIEFS[0]["id"]],
    }
    get_supplier_email_addresses_by_supplier_id.return_value = [
        '*****@*****.**'
    ]

    with freeze_time('2017-04-19 08:00:00'):
        main('api_url',
             'api_token',
             NOTIFY_API_KEY,
             'preview',
             dry_run=False,
             supplier_ids=[4])

    assert data_api_client.call_args == mock.call('api_url', 'api_token')
    assert get_supplier_email_addresses_by_supplier_id.call_args_list == [
        mock.call(data_api_client.return_value, 4),
    ]
    assert send_supplier_emails.call_args_list == [
        mock.call(
            NOTIFY_API_KEY, ['*****@*****.**'], {
                'briefs': [{
                    'brief_title':
                    'Amazing Title',
                    'brief_link':
                    'https://www.preview.marketplace.team/'
                    'digital-outcomes-and-specialists/opportunities/3?utm_id=20170419qa'
                }]
            }, mock.ANY),
    ]
def test_main_does_not_try_to_send_email_if_no_active_users_for_supplier(
        data_api_client,
        get_live_briefs_with_new_questions_and_answers_between_two_dates,
        get_ids_of_interested_suppliers_for_briefs,
        get_supplier_email_addresses_by_supplier_id, send_supplier_emails,
        logger):
    get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value = [
        FILTERED_BRIEFS[0]
    ]
    get_ids_of_interested_suppliers_for_briefs.return_value = {
        3: [FILTERED_BRIEFS[0]["id"]]
    }
    get_supplier_email_addresses_by_supplier_id.return_value = []

    result = main('api_url', 'api_token', 'M', 'preview', dry_run=False)

    assert result is True
    assert not send_supplier_emails.called
    assert logger.info.call_args == mock.call(
        'Email not sent for the following supplier ID due to no active users: {supplier_id}',
        extra={
            "supplier_id": 3,
            "brief_ids_list": "3"
        })
def test_main_calls_functions(
        data_api_client,
        get_live_briefs_with_new_questions_and_answers_between_two_dates,
        get_ids_of_interested_suppliers_for_briefs,
        get_supplier_email_addresses_by_supplier_id, send_supplier_emails):
    brief0, brief1, brief2 = FILTERED_BRIEFS[0], FILTERED_BRIEFS[
        1], FILTERED_BRIEFS[2]
    get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value = [
        brief0, brief1, brief2
    ]
    get_ids_of_interested_suppliers_for_briefs.return_value = {
        3: [brief0['id'], brief1['id']],
        4: [brief2['id']]
    }
    get_supplier_email_addresses_by_supplier_id.side_effect = [[
        '*****@*****.**', '*****@*****.**'
    ], ['*****@*****.**']]

    with freeze_time('2017-04-19 08:00:00'):
        result = main('api_url',
                      'api_token',
                      NOTIFY_API_KEY,
                      'preview',
                      dry_run=False)

    assert result
    assert data_api_client.call_args == mock.call('api_url', 'api_token')
    assert get_live_briefs_with_new_questions_and_answers_between_two_dates.call_args_list == [
        mock.call(data_api_client.return_value, datetime(2017, 4, 18, hour=8),
                  datetime(2017, 4, 19, hour=8))
    ]
    assert get_ids_of_interested_suppliers_for_briefs.call_args == \
        mock.call(
            data_api_client.return_value,
            get_live_briefs_with_new_questions_and_answers_between_two_dates.return_value
        )
    assert get_supplier_email_addresses_by_supplier_id.call_args_list == [
        mock.call(data_api_client.return_value, 3),
        mock.call(data_api_client.return_value, 4),
    ]
    assert send_supplier_emails.call_args_list == [
        mock.call(
            NOTIFY_API_KEY, ['*****@*****.**', '*****@*****.**'], {
                'briefs': [{
                    'brief_title':
                    'Amazing Title',
                    'brief_link':
                    'https://www.preview.marketplace.team/'
                    'digital-outcomes-and-specialists/opportunities/3?utm_id=20170419qa'
                }, {
                    'brief_title':
                    'Brilliant Title',
                    'brief_link':
                    'https://www.preview.marketplace.team/'
                    'digital-outcomes-and-specialists/opportunities/4?utm_id=20170419qa'
                }]
            }, mock.ANY),
        mock.call(
            NOTIFY_API_KEY, ['*****@*****.**'], {
                'briefs': [{
                    'brief_title':
                    'Confounded Title',
                    'brief_link':
                    'https://www.preview.marketplace.team/'
                    'digital-outcomes-and-specialists/opportunities/5?utm_id=20170419qa'
                }]
            }, mock.ANY),
    ]
Ejemplo n.º 6
0
    ./scripts/notify-suppliers-of-new-questions-answers.py preview notify-token --dry-run --supplier-ids=2,3,4
"""

import sys

from docopt import docopt

sys.path.insert(0, '.')
from dmscripts.helpers.auth_helpers import get_auth_token
from dmscripts.notify_suppliers_of_new_questions_answers import main
from dmutils.env_helpers import get_api_endpoint_from_stage

if __name__ == "__main__":
    arguments = docopt(__doc__)

    list_of_supplier_ids = []
    if arguments['--supplier-ids']:
        list_of_supplier_ids = list(
            map(int, arguments['--supplier-ids'].split(',')))

    ok = main(data_api_url=get_api_endpoint_from_stage(arguments['<stage>'],
                                                       'api'),
              data_api_token=get_auth_token('api', arguments['<stage>']),
              email_api_key=arguments['<notify_api_key>'],
              stage=arguments['<stage>'],
              dry_run=arguments['--dry-run'],
              supplier_ids=list_of_supplier_ids)

    if not ok:
        sys.exit(1)