def test_template_stored():
    def new_send(**kwargs):
        assert 'text' not in kwargs
        assert kwargs['subject'] == 'Some subject'
        assert kwargs['template'] == 'stored_template'
        assert kwargs['recipients'] == [{'address': {'email': '*****@*****.**'},
                                         'substitution_data': {'recipient_var': 'value'}}]
        assert kwargs['substitution_data'] == {
            'global_var': 'value'
        }

        return {
            'total_accepted_recipients': 0,
            'total_rejected_recipients': 0
        }

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = new_send
        email = EmailMessage(
            subject='Some subject', from_email="*****@*****.**",
            to=[{'address': {'email': '*****@*****.**'},
                 'substitution_data': {'recipient_var': 'value'}}]
        )
        email.sparkpost = {
            'template': "stored_template",
            'substitution_data': {
                'global_var': 'value'
            }
        }
        email.send()
def test_template_inline_missing():
    def new_send(**kwargs):
        assert kwargs['subject'] == 'Some subject'
        assert kwargs['text'] == 'template content'
        assert kwargs['recipients'] == [{'address': {'email': '*****@*****.**'},
                                         'substitution_data': {'recipient_var': 'value'}}]
        assert kwargs['substitution_data'] == {
            'global_var': 'value'
        }

        return {
            'total_accepted_recipients': 0,
            'total_rejected_recipients': 0
        }

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = new_send
        email = EmailMessage(
            subject='Some subject', from_email="*****@*****.**",
            to=[{'address': {'email': '*****@*****.**'},
                 'substitution_data': {'recipient_var': 'value'}}]
        )
        email.sparkpost = {
            'text': 'template content',
            'substitution_data': {
                'global_var': 'value'
            }
        }

        with pytest.raises(InvalidInlineTemplate):
            email.send()
def test_template_stored_invalid():
    mock_resp = mock.Mock()
    mock_resp.status_code = 422
    mock_resp.json = lambda: {
        'errors': [{
            "message": "Subresource not found",
            "description": "template 'invalid_stored_template' does not exist",
            "code": "1603"
        }]
    }

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = SparkPostAPIException(mock_resp)

        with pytest.raises(InvalidStoredTemplate):
            email = EmailMessage(subject='test subject',
                                 from_email="*****@*****.**",
                                 to=['*****@*****.**'])
            email.sparkpost = {'template_name': 'invalid_stored_template'}
            email.send()
def test_template_stored_invalid():
    mock_resp = mock.Mock()
    mock_resp.status_code = 422
    mock_resp.json = lambda: {
        'errors': [
            {
                "message": "Subresource not found",
                "description": "template 'invalid_stored_template' does not exist",
                "code": "1603"
            }
        ]
    }

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = SparkPostAPIException(mock_resp)

        with pytest.raises(InvalidStoredTemplate):
            email = EmailMessage(
                subject='test subject', from_email="*****@*****.**",
                to=['*****@*****.**']
            )
            email.sparkpost = {'template_name': 'invalid_stored_template'}
            email.send()
def test_template_inline_missing():
    def new_send(**kwargs):
        assert kwargs['subject'] == 'Some subject'
        assert kwargs['text'] == 'template content'
        assert kwargs['recipients'] == [{
            'address': {
                'email': '*****@*****.**'
            },
            'substitution_data': {
                'recipient_var': 'value'
            }
        }]
        assert kwargs['substitution_data'] == {'global_var': 'value'}

        return {'total_accepted_recipients': 0, 'total_rejected_recipients': 0}

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = new_send
        email = EmailMessage(subject='Some subject',
                             from_email="*****@*****.**",
                             to=[{
                                 'address': {
                                     'email': '*****@*****.**'
                                 },
                                 'substitution_data': {
                                     'recipient_var': 'value'
                                 }
                             }])
        email.sparkpost = {
            'text': 'template content',
            'substitution_data': {
                'global_var': 'value'
            }
        }

        with pytest.raises(InvalidInlineTemplate):
            email.send()
def test_template_stored():
    def new_send(**kwargs):
        assert 'text' not in kwargs
        assert kwargs['subject'] == 'Some subject'
        assert kwargs['template'] == 'stored_template'
        assert kwargs['recipients'] == [{
            'address': {
                'email': '*****@*****.**'
            },
            'substitution_data': {
                'recipient_var': 'value'
            }
        }]
        assert kwargs['substitution_data'] == {'global_var': 'value'}

        return {'total_accepted_recipients': 0, 'total_rejected_recipients': 0}

    with mock.patch.object(Transmissions, 'send') as mock_send:
        mock_send.side_effect = new_send
        email = EmailMessage(subject='Some subject',
                             from_email="*****@*****.**",
                             to=[{
                                 'address': {
                                     'email': '*****@*****.**'
                                 },
                                 'substitution_data': {
                                     'recipient_var': 'value'
                                 }
                             }])
        email.sparkpost = {
            'template': "stored_template",
            'substitution_data': {
                'global_var': 'value'
            }
        }
        email.send()