def test_main(get_briefs_closed_on_date, notify_users):
    get_briefs_closed_on_date.return_value = [
        {
            'id': 100
        },
        {
            'id': 200
        },
        {
            'id': 300
        },
    ]

    notify_users.return_value = True

    with freeze_time('2016-01-02 03:04:05'):
        assert main('URL', 'API_KEY', NOTIFY_API_KEY, 'preview', '2016-01-02',
                    False)
    get_briefs_closed_on_date.assert_called_once_with(
        mock.ANY, datetime.date(2016, 1, 2))
    notify_users.assert_has_calls([
        mock.call(NOTIFY_API_KEY, 'preview', {'id': 100}),
        mock.call(NOTIFY_API_KEY, 'preview', {'id': 200}),
        mock.call(NOTIFY_API_KEY, 'preview', {'id': 300}),
    ])
def test_main_dry_run(get_closed_briefs, get_notified_briefs, notify_users):
    get_closed_briefs.return_value = [
        {'id': 100},
        {'id': 200},
    ]

    assert main('URL', 'API_KEY', 'KEY', None, True)
    notify_users.assert_not_called()
def test_main_fails_when_notify_users_fails(get_closed_briefs, get_notified_briefs, notify_users):
    get_closed_briefs.return_value = [
        {'id': 100},
        {'id': 200},
    ]

    notify_users.return_value = False

    assert not main('URL', 'API_KEY', 'KEY', None, False)
def test_main_doesnt_allow_old_date_closed(get_closed_briefs, get_notified_briefs, notify_users):
    get_closed_briefs.return_value = [
        {'id': 100},
        {'id': 200},
    ]

    with freeze_time('2016-01-12 03:04:05'):
        assert not main('URL', 'API_KEY', 'KEY', '2016-01-02', False)

    get_notified_briefs.assert_not_called()
    notify_users.assert_not_called()
def test_main_dry_run(get_briefs_closed_on_date, notify_users):
    get_briefs_closed_on_date.return_value = [
        {
            'id': 100
        },
        {
            'id': 200
        },
    ]

    assert main('URL', 'API_KEY', NOTIFY_API_KEY, 'preview', None, True)
    notify_users.assert_not_called()
def test_main_fails_when_notify_users_fails(get_briefs_closed_on_date,
                                            notify_users):
    get_briefs_closed_on_date.return_value = [
        {
            'id': 100
        },
        {
            'id': 200
        },
    ]

    notify_users.return_value = False

    assert not main('URL', 'API_KEY', NOTIFY_API_KEY, 'preview', None, False)
def test_main_doesnt_allow_old_date_closed(get_briefs_closed_on_date,
                                           notify_users):
    get_briefs_closed_on_date.return_value = [
        {
            'id': 100
        },
        {
            'id': 200
        },
    ]

    with freeze_time('2016-01-12 03:04:05'):
        assert not main('URL', 'API_KEY', NOTIFY_API_KEY, 'preview',
                        '2016-01-02', False)

    notify_users.assert_not_called()
def test_main(get_closed_briefs, get_notified_briefs, notify_users):
    get_notified_briefs.return_value = set([200])
    get_closed_briefs.return_value = [
        {'id': 100},
        {'id': 200},
        {'id': 300},
    ]

    notify_users.return_value = True

    with freeze_time('2016-01-02 03:04:05'):
        assert main('URL', 'API_KEY', 'KEY', '2016-01-02', False)
    get_closed_briefs.assert_called_once_with(mock.ANY, datetime.date(2016, 1, 2))
    get_notified_briefs.assert_called_once_with('KEY', datetime.date(2016, 1, 2))
    notify_users.assert_has_calls([
        mock.call('KEY', {'id': 100}),
        mock.call('KEY', {'id': 300}),
    ])
def test_main_with_no_briefs(get_briefs_closed_on_date, notify_users):
    get_briefs_closed_on_date.return_value = []

    assert main('URL', 'API_KEY', NOTIFY_API_KEY, 'preview', None, False)
    notify_users.assert_not_called()
def test_main_with_no_briefs(get_closed_briefs, get_notified_briefs, notify_users):
    get_closed_briefs.return_value = []

    assert main('URL', 'API_KEY', 'KEY', None, False)
    get_notified_briefs.assert_not_called()
    notify_users.assert_not_called()
Usage:
    notify-buyers-when-requirements-close.py <stage> --email-api-key=<email_api_key> [options]

    --date-closed=<date>  Notify about requirements that closed on the given date (date format: YYYY-MM-DD)
    --dry-run  List notifications that would be sent without sending the emails

"""

import sys

from docopt import docopt

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

if __name__ == "__main__":
    arguments = docopt(__doc__)
    ok = main(data_api_url=get_api_endpoint_from_stage(arguments['<stage>'],
                                                       'api'),
              data_api_access_token=get_auth_token('api',
                                                   arguments['<stage>']),
              email_api_key=arguments['--email-api-key'],
              stage=arguments['<stage>'],
              date_closed=arguments['--date-closed'],
              dry_run=arguments['--dry-run'])

    if not ok:
        sys.exit(1)
Usage:
    notify-buyers-when-requirements-close.py <stage> --api-token=<api_access_token>
                                                     --email-api-key=<email_api_key> [options]

    --date-closed=<date>  Notify about requirements that closed on the given date (date format: YYYY-MM-DD)
    --dry-run  List notifications that would be sent without sending the emails

"""

import sys

from docopt import docopt

sys.path.insert(0, '.')
from dmscripts.env import get_api_endpoint_from_stage
from dmscripts.notify_buyers_when_requirements_close import main


if __name__ == "__main__":
    arguments = docopt(__doc__)
    ok = main(
        data_api_url=get_api_endpoint_from_stage(arguments['<stage>'], 'api'),
        data_api_access_token=arguments['--api-token'],
        email_api_key=arguments['--email-api-key'],
        date_closed=arguments['--date-closed'],
        dry_run=arguments['--dry-run']
    )

    if not ok:
        sys.exit(1)