def test_notify_users_raises_email_template_error(send_email):
    send_email.side_effect = EmailTemplateError('Error')
    with pytest.raises(EmailTemplateError):
        notify_users(
            NOTIFY_API_KEY, 'preview', {
                'id':
                100,
                'title':
                'My brief title',
                'lotSlug':
                'lot-slug',
                'frameworkSlug':
                'framework-slug',
                'users': [
                    {
                        'emailAddress': '*****@*****.**',
                        'active': True
                    },
                    {
                        'emailAddress': '*****@*****.**',
                        'active': False
                    },
                    {
                        'emailAddress': '*****@*****.**',
                        'active': True
                    },
                ],
            })
def test_notify_users(send_email):
    notify_users('KEY', {
        'id': 100,
        'title': 'My brief title',
        'lotSlug': 'lot-slug',
        'frameworkSlug': 'framework-slug',
        'users': [
            {'emailAddress': '*****@*****.**', 'active': True},
            {'emailAddress': '*****@*****.**', 'active': False},
            {'emailAddress': '*****@*****.**', 'active': True},
        ],
    })

    send_email.assert_called_once_with(
        ['*****@*****.**', '*****@*****.**'],
        FakeMail(
            'My brief title',
            '/buyers/frameworks/framework-slug/requirements/lot-slug/100/responses'
        ),
        'KEY',
        u'Next steps for your \u2018My brief title\u2019 requirements',
        '*****@*****.**',
        'Digital Marketplace Admin',
        ['requirements-closed'],
        logger=mock.ANY,
        metadata={'brief_id': 100}
    )
def test_notify_users(send_email):
    notify_users(
        NOTIFY_API_KEY, 'preview', {
            'id':
            100,
            'title':
            'My brief title',
            'lotSlug':
            'lot-slug',
            'frameworkSlug':
            'framework-slug',
            'users': [
                {
                    'emailAddress': '*****@*****.**',
                    'active': True
                },
                {
                    'emailAddress': '*****@*****.**',
                    'active': False
                },
                {
                    'emailAddress': '*****@*****.**',
                    'active': True
                },
            ],
        })

    brief_responses_url = \
        "https://www.preview.marketplace.team" \
        "/buyers/frameworks/framework-slug/requirements/lot-slug/100/responses"

    assert send_email.call_count == 2
    send_email.assert_any_call(
        mock.ANY,  # self
        '*****@*****.**',
        template_name_or_id=mock.ANY,
        personalisation={
            "brief_title": "My brief title",
            "brief_responses_url": brief_responses_url,
        },
        allow_resend=False,
    )
    send_email.assert_any_call(
        mock.ANY,  # self
        '*****@*****.**',
        template_name_or_id=mock.ANY,
        personalisation={
            "brief_title": "My brief title",
            "brief_responses_url": brief_responses_url,
        },
        allow_resend=False,
    )
def test_notify_users_returns_false_on_error(send_email):
    send_email.side_effect = EmailError('Error')
    assert not notify_users(
        NOTIFY_API_KEY, 'preview', {
            'id':
            100,
            'title':
            'My brief title',
            'lotSlug':
            'lot-slug',
            'frameworkSlug':
            'framework-slug',
            'users': [
                {
                    'emailAddress': '*****@*****.**',
                    'active': True
                },
                {
                    'emailAddress': '*****@*****.**',
                    'active': False
                },
                {
                    'emailAddress': '*****@*****.**',
                    'active': True
                },
            ],
        })
def test_notify_users_returns_false_on_error(send_email):
    send_email.side_effect = MandrillException('Error')
    assert not notify_users('KEY', {
        'id': 100,
        'title': 'My brief title',
        'lotSlug': 'lot-slug',
        'frameworkSlug': 'framework-slug',
        'users': [
            {'emailAddress': '*****@*****.**', 'active': True},
            {'emailAddress': '*****@*****.**', 'active': False},
            {'emailAddress': '*****@*****.**', 'active': True},
        ],
    })