Ejemplo n.º 1
0
def test_queue_mail_message_and_send_via_mandrill(settings):
    settings.EMAIL_BACKEND = "djrill.mail.backends.djrill.DjrillBackend"
    with mock.patch('django.core.mail.EmailMultiAlternatives') as mock_mail:
        mock_mail.return_value.mandrill_response = [{
            "email": "*****@*****.**",
            "status": "sent",
            "_id": "123",
            "reject_reason": None,
        }]
        email_address = get_env_variable('TEST_EMAIL_ADDRESS_2')
        enquiry = make_enquiry(
            email_address,
            "Welcome",
            'Can I join your club?',
        )
        template = _create_goodbye_mandrill_template()
        content_data = {
            email_address: {
                "name": "Fred",
                "title": "SpaceX",
                "question": enquiry.description
            }
        }
        queue_mail_template(enquiry, template.slug, content_data)
        m = _mail(enquiry)
        assert m.sent is None
        assert m.sent_response_code is None
        assert m.message.subject == 'Goodbye *|name|*'
        # test the send facility using djrill mail backend
        # temp_email_backend = settings.EMAIL_BACKEND
        send_mail()
        m = _mail(enquiry)
        assert m.sent is not None
        assert m.sent_response_code is not None
Ejemplo n.º 2
0
def test_queue_mail():
    enquiry = _queue_enquiry()
    message = Message.objects.get(subject='Farming')
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_1')
    mail = Mail.objects.get(email=email_address)
    assert message == mail.message
    assert enquiry == message.content_object
Ejemplo n.º 3
0
def create_enquiry():
    email = get_env_variable('TEST_EMAIL_ADDRESS_1')
    e = Enquiry(**dict(
        email = email,
        subject = "Membership Options",
        description = (
            "Dear Sirs,\n\n"
            "I've just got a job as a legal secretary.  "
            "Can you please outline details of membership "
            "options available to me\n\n"
            "Kind regards,\n\n"
            "G. Paltrow"
        )
    ))
    e.save()
    return e
Ejemplo n.º 4
0
def test_queue_no_email():
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_1')
    enquiry = make_enquiry(
        email_address,
        "Farming",
        'How many cows in the field?',
    )
    with pytest.raises(MailError) as e:
        queue_mail_message(
            enquiry,
            [],
            enquiry.subject,
            enquiry.description,
        )
    expect = "Cannot 'queue_mail_message' without 'email_addresses'"
    assert expect in str(e.value)
Ejemplo n.º 5
0
def _queue_enquiry(attachments=None):
    if not attachments:
        attachments = []
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_1')
    enquiry = make_enquiry(
        email_address,
        "Farming",
        'How many cows in the field?',
    )
    queue_mail_message(
        enquiry,
        [enquiry.email, ],
        enquiry.subject,
        enquiry.description,
        attachments=attachments,
    )
    return enquiry
Ejemplo n.º 6
0
def test_queue_mail_with_attachment():
    file_name = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        'data',
        'sample.odt',
    )
    enquiry = _queue_enquiry([file_name])
    message = Message.objects.get(subject='Farming')
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_1')
    mail = Mail.objects.get(email=email_address)
    assert message == mail.message
    assert enquiry == message.content_object
    assert 1 == message.attachments().count()
    assert filecmp.cmp(
        file_name,
        message.attachments().first().document.file.name,
        shallow=False
    ) is True
Ejemplo n.º 7
0
def queue_enquiry_ack_with_copy(enq=None):
    copy_email = get_env_variable('TEST_EMAIL_ADDRESS_2')
    if not enq:
        enq = create_enquiry()
    context = {
        enq.email: {
            "SUBJECT": "Re: " + enq.subject,
            "BODY": enq.description,
            "DATE": enq.created.strftime("%d-%b-%Y %H:%M:%S")
        },
        copy_email: {
            "SUBJECT": "Copy: " + enq.subject,
            "BODY": "<h1>Copy of Message sent to '" + 
            enq.email + "':</h1>" + enq.description,
            "DATE": enq.created.strftime("%d-%b-%Y %H:%M:%S")
        }
    }
    queue_mail_template(
        enq,
        'enquiry_acknowledgement',
        context,
    )
Ejemplo n.º 8
0
def test_queue_mail_message():
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_2')
    if not email_address:
        raise MailError("Cannot test without a 'TEST_EMAIL_ADDRESS_2'")
    enquiry = make_enquiry(
        email_address,
        "Welcome",
        'Can I join your club?',
    )
    template = _create_welcome_template()
    content_data = {
        email_address: {
            "name": "Fred",
            "title": "SpaceX",
            "question": enquiry.description
        }
    }
    queue_mail_template(enquiry, template.slug, content_data)
    message = Message.objects.get(subject='Welcome {{name}}')
    mail_item = Mail.objects.get(email=email_address)
    assert message == mail_item.message
    assert enquiry == mail_item.message.content_object
Ejemplo n.º 9
0
def test_queue_mail_message():
    email_address = get_env_variable('TEST_EMAIL_ADDRESS_2')
    if not email_address:
        raise MailError("Cannot test without a 'TEST_EMAIL_ADDRESS_2'")
    enquiry = make_enquiry(
        email_address,
        "Welcome",
        'Can I join your club?',
    )
    template = _create_welcome_template()
    content_data = {
        email_address: {
            "name": "Fred",
            "title": "SpaceX",
            "question": enquiry.description,
            "dict": {'age': 52, 'colour': 'blue'},
            "list": [1, 3, 9],
        }
    }
    queue_mail_template(enquiry, template.slug, content_data)
    message = Message.objects.get(subject='Welcome {{name}}')
    mail_item = Mail.objects.get(email=email_address)
    assert message == mail_item.message
    assert enquiry == mail_item.message.content_object
    # name
    obj = MailField.objects.get(key='name')
    assert 'Fred' == obj.value
    assert obj.is_json is False
    # dict
    obj = MailField.objects.get(key='dict')
    assert obj.is_json is True
    assert {'age': 52, 'colour': 'blue'} == json.loads(obj.value)
    # list
    obj = MailField.objects.get(key='list')
    assert obj.is_json is True
    assert [1, 3, 9] == json.loads(obj.value)