コード例 #1
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def test_add_header():
    result = Message(to=["*****@*****.**"],
                     from_addresses=["*****@*****.**"],
                     date=datetime.now())
    result.headers["X-FileTrust-Tenant"] = "test"
    mime_result = result.as_mime()
    assert mime_result["X-FileTrust-Tenant"] == "test"
コード例 #2
0
def test_message_attach(mock_open):
    """
    tests a attachment attempt on a message
    Args:
        mock_open: dictionary

    Returns:
        boolean from assertions
    """
    # write a test file to try to attach
    with open("attachment.txt", "w") as test_attachment:
        test_attachment.write("TEXT")

    msg = Message(to=["*****@*****.**"],
                  from_addresses=["*****@*****.**"],
                  date=datetime.now())

    msg.attach("attachment.txt")

    expected = MIMEText("TEXT\n")
    expected.add_header("content-disposition",
                        "attachment",
                        filename="attachment.txt")

    result = msg.attachments[0]

    assert result.get_content_type() == expected.get_content_type()
    assert result.get_content_disposition() == \
        expected.get_content_disposition()
    assert result.get_payload() == expected.get_payload()
コード例 #3
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def test_create_message_unknown_headers():
    result = Message(to=["*****@*****.**"],
                     from_addresses=["*****@*****.**"],
                     date=datetime.now(),
                     unknown_header="test")
    mime_result = result.as_mime()
    assert mime_result["Unknown-Header"] == "test"
コード例 #4
0
def test_as_mime(mock_open):
    """
    creates and attaches a test file
    Args:
        mock_open: dictionary

    Returns:
        Runs assertions. Also prints out a result
    """
    # write a test file to try to attach
    with open("attachment.txt", "w") as test_attachment:
        test_attachment.write("TEXT")

    msg = Message(body="Hello, world!",
                  to=["*****@*****.**"],
                  from_addresses=["*****@*****.**"],
                  date=datetime.strptime("2019-11-12T15:24:28+00:00",
                                         "%Y-%m-%dT%H:%M:%S%z"))
    msg.attach("attachment.txt")

    # we need to set a boundary for the MIME message as it's not
    # very deterministic
    boundary = "===============8171060537750829823=="
    expected_str = f"""Content-Type: multipart/mixed; boundary="{boundary}"
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Date: Tue, 12 Nov 2019 15:24:28 +0000

--{boundary}
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello, world!
--{boundary}
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="attachment.txt"

TEXT

--{boundary}--
"""
    result = msg.as_mime()
    result.set_boundary(boundary)

    expected = email.message_from_string(expected_str)

    assert dict(result) == dict(expected)
    result_payload = result.get_payload()[0]
    expected_payload = expected.get_payload()[0]
    print(result)
    assert result_payload.get_payload() == \
        expected_payload.get_payload()
    assert result_payload.get_content_type() == \
        expected_payload.get_content_type()
    assert result_payload.get_content_disposition() == \
        expected_payload.get_content_disposition()
コード例 #5
0
ファイル: test_smtp.py プロジェクト: dmgolembiowski/sremail
def test_send_message(mock_smtp, mock_open, capsys):
    """

    Args:
        mock_smtp:
        mock_open:
        capsys:

    Returns:

    """
    # write a test file to try to attach
    with open("attachment.txt", "w") as test_attachment:
        test_attachment.write("TEXT")

    msg = Message(to=["*****@*****.**"],
                  from_addresses=["*****@*****.**"],
                  date=datetime.strptime("2019-11-12T16:48:25.773537",
                                         "%Y-%m-%dT%H:%M:%S.%f"))
    msg.attach("attachment.txt")

    # we need to set a boundary for the MIME message as it's not
    # very deterministic
    boundary = "===============8171060537750829823=="
    expected_str = f"""Content-Type: multipart/mixed; boundary="{boundary}"
MIME-Version: 1.0
Date: Tue, 12 Nov 2019 16:48:25 -0000
To: [email protected]
From: [email protected]

--{boundary}
Content-Type: text/plain
MIME-Version: 1.0
content-disposition: attachment; filename="attachment.txt"

TEXT

--{boundary}--
"""
    expected = email.message_from_string(expected_str)

    smtp.send(msg, "smtp.test.not_real.com:25")

    captured = capsys.readouterr()
    result_after_send = email.message_from_string(captured.out)
    result_after_send.set_boundary(boundary)

    assert dict(result_after_send) == dict(expected)
    result_payload = result_after_send.get_payload()[0]
    expected_payload = expected.get_payload()[0]
    assert result_payload.get_payload() == \
        expected_payload.get_payload()
    assert result_payload.get_content_type() == \
        expected_payload.get_content_type()
    assert result_payload.get_content_disposition() == \
        expected_payload.get_content_disposition()
コード例 #6
0
def test_create_message_unknown_headers():
    """
    creates a message with unknown or unrecognised headers
    Returns:
        boolean
    """
    result = Message(to=["*****@*****.**"],
                     from_addresses=["*****@*****.**"],
                     date=datetime.now(),
                     unknown_header="test")
    mime_result = result.as_mime()
    assert mime_result["Unknown-Header"] == "test"
コード例 #7
0
def test_add_header():
    """
    Adds a header
    Returns:
        boolean on assertion that the header gets added successfully
    """
    result = Message(to=["*****@*****.**"],
                     from_addresses=["*****@*****.**"],
                     date=datetime.now())
    result.headers["X-FileTrust-Tenant"] = "test"
    mime_result = result.as_mime()
    assert mime_result["X-FileTrust-Tenant"] == "test"
コード例 #8
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def create_message(body: str, headers: Dict[str, object],
                   attachments: List[MIMEApplication]) -> Message:
    msg = Message.__new__(Message)
    msg.body = body
    msg.headers = headers
    msg.attachments = attachments
    return msg
コード例 #9
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def test_message_attach_stream_unknown_mime():
    byte_stream = io.BytesIO(b"testing testing 123")

    msg = Message(to=["*****@*****.**"],
                  from_addresses=["*****@*****.**"],
                  date=datetime.now())
    msg.attach_stream(byte_stream, "test.coff")

    expected = MIMEApplication(b"testing testing 123")
    expected.add_header("content-disposition",
                        "attachment",
                        filename="test.coff")

    result = msg.attachments[0]

    assert result.get_content_type() == expected.get_content_type()
    assert result.get_content_disposition() == \
        expected.get_content_disposition()
    assert result.get_payload() == expected.get_payload()
コード例 #10
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def test_with_headers():
    result = Message.with_headers({
        "To": ["*****@*****.**"],
        "Date": "Mon, 25 Nov 2019 14:59:32 UTC +0000",
        "From": ["*****@*****.**"]
    })
    assert result.headers == {
        "To": ["*****@*****.**"],
        "Date": "Mon, 25 Nov 2019 14:59:32 UTC +0000",
        "From": ["*****@*****.**"]
    }
コード例 #11
0
def test_create_message(body, headers, expected, raises):
    """

    Args:
        body: message body
        headers: message headers
        expected: expected result
        raises: error check

    Returns:
        boolean
    """
    with raises:
        result = Message(body, **headers)
        print(result.headers)
        assert result == expected
コード例 #12
0
def test_with_headers():
    """
    compares two input string for equality
    Returns:
        None
    """
    result = Message.with_headers({
        "To": ["*****@*****.**"],
        "Date": "Mon, 25 Nov 2019 14:59:32 UTC +0000",
        "From": ["*****@*****.**"]
    })
    assert result.headers == {
        "To": ["*****@*****.**"],
        "Date": "Mon, 25 Nov 2019 14:59:32 UTC +0000",
        "From": ["*****@*****.**"]
    }
コード例 #13
0
def create_message(body: str, headers: Dict[str, object],
                   attachments: List[MIMEApplication]) -> Message:
    """

    Args:
        body: message body
        headers: message headers
        attachments:email file attachment

    Returns: formatted msg

    """
    msg = Message.__new__(Message)
    msg.body = body
    msg.headers = headers
    msg.attachments = attachments
    return msg
コード例 #14
0
ファイル: test_message.py プロジェクト: Figglewatts/sremail
def test_create_message(body, headers, expected, raises):
    with raises:
        result = Message(body, **headers)
        print(result.headers)
        assert result == expected
コード例 #15
0
ファイル: spec.py プロジェクト: Figglewatts/victoria_smoke
 def as_message(self) -> Message:
     msg = Message(**self.headers)
     for attachment in self.attach:
         msg.attach(attachment)
     return msg