def test_ENVELOPE(self): envelope_str = ( b'1 (ENVELOPE ( ' b'"Sun, 24 Mar 2013 22:06:10 +0200" ' b'"subject" ' b'(("name" NIL "address1" "domain1.com")) ' # from (name and address) b'((NIL NIL "address2" "domain2.com")) ' # sender (just address) b'(("name" NIL "address3" "domain3.com") NIL) ' # reply to b'NIL' # to (no address) b'((NIL NIL "address4" "domain4.com") ' # cc b'("person" NIL "address4b" "domain4b.com")) ' b'NIL ' # bcc b'"<reply-to-id>" ' b'"<msg_id>"))') output = parse_fetch_response([envelope_str], normalise_times=False) self.assertSequenceEqual( output[1][b'ENVELOPE'], Envelope(datetime(2013, 3, 24, 22, 6, 10, tzinfo=FixedOffset(120)), b"subject", (Address(b"name", None, b"address1", b"domain1.com"), ), (Address(None, None, b"address2", b"domain2.com"), ), (Address(b"name", None, b"address3", b"domain3.com"), ), None, (Address(None, None, b"address4", b"domain4.com"), Address(b"person", None, b"address4b", b"domain4b.com")), None, b"<reply-to-id>", b"<msg_id>"))
def test_ENVELOPE(self): envelope_str = ('1 (ENVELOPE ( ' '"Sun, 24 Mar 2013 22:06:10 +0200" ' '"subject" ' '(("name" NIL "address1" "domain1.com")) ' '((NIL NIL "address2" "domain2.com")) ' '(("name" NIL "address3" "domain3.com")) ' 'NIL' '((NIL NIL "address4" "domain4.com") ' '("person" NIL "address4b" "domain4b.com")) ' 'NIL "<reply-to-id>" "<msg_id>"))') output = parse_fetch_response([envelope_str], normalise_times=False) self.assertSequenceEqual( output[1]['ENVELOPE'], Envelope(datetime(2013, 3, 24, 22, 6, 10, tzinfo=FixedOffset(120)), "subject", (Address("name", None, "address1", "domain1.com"), ), (Address(None, None, "address2", "domain2.com"), ), (Address("name", None, "address3", "domain3.com"), ), None, (Address(None, None, "address4", "domain4.com"), Address("person", None, "address4b", "domain4b.com")), None, "<reply-to-id>", "<msg_id>"))
def test_Address_str(self): self.assertEqual(str(Address(b"Mary Jane", None, b"mary", b"jane.org")), "Mary Jane <*****@*****.**>") self.assertEqual(str(Address("Mary Jane", None, "mary", "jane.org")), "Mary Jane <*****@*****.**>") self.assertEqual(str(Address("Anonymous", None, "undisclosed-recipients", None)), "Anonymous <undisclosed-recipients>") self.assertEqual(str(Address(None, None, None, "undisclosed-recipients")), "undisclosed-recipients")
def test_fetch(self): # Generate a fresh message-id each time because Gmail is # clever and will treat appends of messages with # previously seen message-ids as the same message. This # breaks our tests when the test message is updated. msg_id_header = make_msgid() msg = ("Message-ID: %s\r\n" % msg_id_header) + MULTIPART_MESSAGE self.client.select_folder(self.base_folder) self.append_msg(msg) self.client.normalise_times = False fields = ["RFC822", b"FLAGS", "INTERNALDATE", "ENVELOPE"] msg_id = self.client.search()[0] resp = self.client.fetch(msg_id, fields) self.assertEqual(len(resp), 1) msginfo = resp[msg_id] extra_fields = [b"SEQ"] if self.condstore_enabled: extra_fields.append(b"MODSEQ") self.assertSetEqual( set(msginfo.keys()), set([to_bytes(f) for f in fields] + extra_fields), ) self.assertEqual(msginfo[b"SEQ"], 1) self.assertEqual(msginfo[b"RFC822"], to_bytes(msg)) self.assertIsInstance(msginfo[b"INTERNALDATE"], datetime) self.assertIsInstance(msginfo[b"FLAGS"], tuple) self.assertSequenceEqual( msginfo[b"ENVELOPE"], Envelope( datetime(2010, 3, 16, 16, 45, 32, tzinfo=FixedOffset(0)), b"A multipart message", (Address(b"Bob Smith", None, b"bob", b"smith.com"), ), (Address(b"Bob Smith", None, b"bob", b"smith.com"), ), (Address(b"Bob Smith", None, b"bob", b"smith.com"), ), ( Address(b"Some One", None, b"some", b"one.com"), Address(None, None, b"foo", b"foo.com"), ), None, None, None, to_bytes(msg_id_header), ), )
def test_Address_str_ignores_encoding_error(self, mock_logger): self.assertEqual( str(Address(b'Russian \xc2\xeb\xe0\xe4\xe8\xec\xe8\xf0', None, b"g\xe9rard", "domain.org")), "Russian <*****@*****.**>" ) # Ensure warning has been triggered twice, for name and mailbox bytes self.assertEqual(mock_logger.warning.call_count, 2)
def make_fake_address(): email = choice((fake.ascii_company_email(), fake.ascii_email(), fake.ascii_free_email())) mailbox, host = email.split('@', 1) name = choice((fake.name(), fake.name(), fake.name(), fake.company(), fake.company(), None)) if name: name = name.encode() return Address(name, None, mailbox.encode(), host.encode())
def make_fake_address(): name = fake.name() bits = name.split() first_name = bits[0] last_name = bits[1] if len(bits) > 1 else bits[0] return Address(name.encode(), None, first_name.encode(), last_name.encode())
def test_fetch(self): # Generate a fresh message-id each time because Gmail is # clever and will treat appends of messages with # previously seen message-ids as the same message. This # breaks our tests when the test message is updated. msg_id_header = make_msgid() msg = ('Message-ID: %s\r\n' % msg_id_header) + MULTIPART_MESSAGE self.client.select_folder(self.base_folder) self.append_msg(msg) self.client.normalise_times = False fields = ['RFC822', b'FLAGS', 'INTERNALDATE', 'ENVELOPE'] msg_id = self.client.search()[0] resp = self.client.fetch(msg_id, fields) self.assertEqual(len(resp), 1) msginfo = resp[msg_id] extra_fields = [b'SEQ'] if self.condstore_enabled: extra_fields.append(b'MODSEQ') self.assertSetEqual( set(msginfo.keys()), set([to_bytes(f) for f in fields] + extra_fields), ) self.assertEqual(msginfo[b'SEQ'], 1) self.assertEqual(msginfo[b'RFC822'], to_bytes(msg)) self.assertIsInstance(msginfo[b'INTERNALDATE'], datetime) self.assertIsInstance(msginfo[b'FLAGS'], tuple) self.assertSequenceEqual( msginfo[b'ENVELOPE'], Envelope( datetime(2010, 3, 16, 16, 45, 32, tzinfo=FixedOffset(0)), b'A multipart message', (Address(b'Bob Smith', None, b'bob', b'smith.com'), ), (Address(b'Bob Smith', None, b'bob', b'smith.com'), ), (Address(b'Bob Smith', None, b'bob', b'smith.com'), ), (Address(b'Some One', None, b'some', b'one.com'), Address(None, None, b'foo', b'foo.com')), None, None, None, to_bytes(msg_id_header)))
def test_ENVELOPE_with_empty_addresses(self): envelope_str = ( b'1 (ENVELOPE ( ' b'NIL ' b'"subject" ' b'(("name" NIL "address1" "domain1.com") NIL) ' b'(NIL (NIL NIL "address2" "domain2.com")) ' b'(("name" NIL "address3" "domain3.com") NIL ("name" NIL "address3b" "domain3b.com")) ' b'NIL' b'((NIL NIL "address4" "domain4.com") ' b'("person" NIL "address4b" "domain4b.com")) ' b'NIL "<reply-to-id>" "<msg_id>"))') output = parse_fetch_response([envelope_str], normalise_times=False) self.assertSequenceEqual( output[1][b'ENVELOPE'], Envelope(None, b"subject", (Address(b"name", None, b"address1", b"domain1.com"), ), (Address(None, None, b"address2", b"domain2.com"), ), (Address(b"name", None, b"address3", b"domain3.com"), Address(b"name", None, b"address3b", b"domain3b.com")), None, (Address(None, None, b"address4", b"domain4.com"), Address(b"person", None, b"address4b", b"domain4b.com")), None, b"<reply-to-id>", b"<msg_id>"))