Exemplo n.º 1
0
 def test_base64(self):
     hello = "aGVsbG8gd29ybGQ="
     self.assertEqual(Envelope.load(f"\n{hello}").message(), hello)
     self.assertEqual(
         Envelope.load(
             f"Content-Transfer-Encoding: base64\n\n{hello}").message(),
         "hello world")
Exemplo n.º 2
0
    def test_internationalized(self):
        self.assertEqual("Žluťoučký kůň",
                         Envelope.load(self.internationalized).subject())

        # when using preview, we do not want to end up with "Subject: =?utf-8?b?xb1sdcWlb3XEjWvDvSBrxa/FiA==?="
        # which could appear even when .subject() shows decoded version
        self.assertIn(
            "Subject: Žluťoučký kůň",
            Envelope.load(self.internationalized).preview().splitlines())
Exemplo n.º 3
0
 def test_alternative_and_related(self):
     e = Envelope.load(path=self.inline_image)
     self.assertEqual("Hi <img src='cid:image.gif'/>", e.message())
     self.assertEqual("Inline image message", e.subject())
     self.assertEqual("Plain alternative", e.message(alternative=PLAIN))
     self.assertEqual(self.image_file.read_bytes(),
                      bytes(e.attachments()[0]))
Exemplo n.º 4
0
 def test_addresses(self):
     e = Envelope.load(path=self.eml)
     self.assertEqual(1, len(e.to()))
     contact = e.to()[0]
     self.assertEqual("Person <*****@*****.**>", contact)
     self.assertEqual("*****@*****.**", contact)
     self.assertEqual("*****@*****.**", contact.address)
     self.assertEqual("Person", contact.name)
Exemplo n.º 5
0
    def test_load_file(self):
        e = Envelope.load(self.eml.read_text())
        self.assertEqual(e.subject(), "Hello world subject")

        # multiple headers returned as list and in the same order
        self.assertEqual(len(e.header("Received")), 2)
        self.assertEqual(
            e.header("Received")[1][:26], "from receiver2.example.com")
Exemplo n.º 6
0
    def test_encoded_headers(self):
        e = Envelope.load(path=str(self.utf_header))
        self.assertEqual(e.subject(), "Re: text")
        self.assertEqual(e.from_(), "Jiří <*****@*****.**>")

        # Test header case-sensitive parsing in .header().
        # We have to type the value to `str` due to this strange fact:
        # `key = "subject"; email["Subject"] = policy.header_store_parse(key, "hello")[1];`
        #   would force `str(email)` output 'subject: hello' (small 's'!)
        # Interestingly, setting `key = "anything else";` would output correct 'Subject: hello'
        # val = str(policy.header_store_parse(k, val)[1])
        self.assertIn("Subject: Re: text", str(e))
Exemplo n.º 7
0
    def test_encrypted_gpg_subject(self):
        body = "just a body text"
        subject = "This is an encrypted subject"
        encrypted_eml = (
            Envelope(body).gpg("tests/gpg_ring/").to(
                "*****@*****.**").from_(
                    "*****@*****.**").subject(
                        subject).encryption()
            # .attach(path=self.text_attachment)
            # .attach(self.image_file, inline=True)
            .as_message().as_string())

        # subject has been encrypted
        self.assertIn("Subject: Encrypted message", encrypted_eml)
        self.assertNotIn(subject, encrypted_eml)

        # subject has been decrypted
        e = Envelope.load(encrypted_eml)
        self.assertEqual(body, e.message())
        self.assertEqual(subject, e.subject())
Exemplo n.º 8
0
    def test_smime_decrypt_attachments(self):
        body = "an encrypted message with the attachments"  # note that the inline image is not referenced in the text
        encrypted_envelope = (Envelope(body).smime().reply_to(
            "*****@*****.**").subject("my message").encryption(
                Path(
                    self.smime_cert)).attach(path=self.text_attachment).attach(
                        self.image_file, inline=True).as_message().as_string())

        e = Envelope.load(encrypted_envelope,
                          key=self.smime_key,
                          cert=self.smime_cert)

        # body stayed the same
        self.assertEqual(body, e.message())

        # attachments are as expected
        self.assertEqual(2, len(e.attachments()))
        self.assertEqual(1, len(e.attachments(inline=True)))
        self.assertEqual(
            e.attachments(inline=True)[0].data, self.image_file.read_bytes())
        self.assertEqual(
            Path(self.text_attachment).read_bytes(),
            e.attachments("generic.txt").data)
Exemplo n.º 9
0
 def test_load(self):
     self.assertEqual(
         Envelope.load("Subject: testing message").subject(),
         "testing message")
Exemplo n.º 10
0
 def e():
     return Envelope.load(path=self.eml).cc(contact)
Exemplo n.º 11
0
 def test_quoted_printable(self):
     """ Envelope is able to load the text that is already quoted. """
     self._quoted_message(
         Envelope.load(
             f"Content-Transfer-Encoding: quoted-printable\n\n{self.quoted}"
         ))
Exemplo n.º 12
0
 def test_encrypted_signed_gpg(self):
     e = Envelope.load(path="tests/eml/test_encrypted_signed_gpg.eml")
     self.assertEqual("dumb encrypted and signed message", e.message())
Exemplo n.º 13
0
 def test_signed_gpg(self):
     # XX we might test signature verification
     e = Envelope.load(path="tests/eml/test_signed_gpg.eml")
     self.assertEqual("dumb message", e.message())
Exemplo n.º 14
0
 def test_smime_key_cert_together(self):
     # XX verify signature
     e = Envelope.load(path="tests/eml/smime_key_cert_together.eml",
                       key=self.key_cert_together)
     self.assertEqual("dumb message", e.message())
Exemplo n.º 15
0
 def test_smime_decrypt(self):
     e = Envelope.load(path="tests/eml/smime_encrypt.eml",
                       key=self.smime_key,
                       cert=self.smime_cert)
     self.assertEqual("dumb message", e.message())
Exemplo n.º 16
0
 def test_another_charset(self):
     self.assertEqual("Dobrý den", Envelope.load(self.charset).message())