Exemplo n.º 1
0
def test_email(mock_server):
    # Given
    imposter = smtp_imposter()

    to_email = random_email()
    from_email = random_email()
    body_text = random_string()

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        server = smtplib.SMTP()
        server.connect(host=imposter.host, port=imposter.port)
        try:
            server.sendmail(
                to_email,
                [from_email],
                message(to_email=to_email, from_email=from_email, body_text=body_text).as_string(),
            )
        finally:
            server.quit()

        # Then
        assert_that(s, email_sent(body_text=body_text))
Exemplo n.º 2
0
def test_email_sent():
    # Given
    server = MagicMock()
    request = SentEmailBuilder().with_text("sausages").build()
    server.get_actual_requests.return_value = [request, request]

    # When

    # Then
    assert_that(server, email_sent(body_text="sausages"))
    assert_that(server, not_(email_sent(body_text="chips")))
    assert_that(email_sent(body_text="sausages"), has_string("email with body text: 'sausages'"))
    assert_that(
        email_sent(body_text="chips"),
        mismatches_with(
            server,
            all_of(
                contains_string("found <0> matching requests: <[]>. All requests: <["),
                contains_string("text='sausages'"),
            ),
        ),
    )
Exemplo n.º 3
0
def test_email_sent():
    # Given
    server = MagicMock()
    request = {"envelopeFrom": "", "text": "sausages"}
    server.get_actual_requests.return_value = {"someport": [request, request]}

    # When

    # Then
    assert_that(server, email_sent(body_text="sausages"))
    assert_that(server, not_(email_sent(body_text="chips")))
    assert_that(email_sent(body_text="sausages"), has_string("email with body text: 'sausages'"))
    assert_that(
        email_sent(body_text="chips"),
        mismatches_with(
            server,
            all_of(
                contains_string("found <0> matching requests: <[]>. All requests: <[{"),
                contains_string("'text': 'sausages'"),
            ),
        ),
    )
Exemplo n.º 4
0
def test_email(mock_server):
    # Given
    imposter = smtp_imposter()

    # TODO - make brunns-builders more realistic, so we don't have to do this here.'
    to_email = email.utils.formataddr(
        ((" ".join(a_string() for _ in range(randint(1, 3)))), (EmailBuilder().build()))
    )
    from_email = ", ".join(
        email.utils.formataddr(
            ((" ".join(a_string() for _ in range(randint(1, 3)))), EmailBuilder().build())
        )
        for _ in range(randint(1, 5))
    )
    body_text = a_string()
    message = (
        EmailMessageBuilder()
        .with_to_email(to_email)
        .with_from_email(from_email)
        .with_body_text(body_text)
        .build()
        .as_string()
    )

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        server = smtplib.SMTP()
        server.connect(host=imposter.host, port=imposter.port)
        try:
            server.sendmail(to_email, [from_email], message)
        finally:
            server.quit()

        # Then
        assert_that(imposter, email_sent(body_text=body_text))
Exemplo n.º 5
0
def test_email(mock_server):
    # Given
    imposter = smtp_imposter()

    from_email = EmailAddressBuilder().build()
    to_email = EmailAddressBuilder().build()
    body_text = a_string()

    message = (EmailMessageBuilder().with_to(to_email).and_from(
        from_email).and_body_text(body_text).build().as_string())

    with mock_server(imposter) as s:
        logger.debug("server: %s", s)

        # When
        server = smtplib.SMTP()
        server.connect(host=imposter.host, port=imposter.port)
        try:
            server.sendmail(to_email, [from_email], message)
        finally:
            server.quit()

        # Then
        assert_that(imposter, email_sent(body_text=body_text))