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>"))
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_with_no_date(self): envelope_str = ( b'1 (ENVELOPE ( ' b'NIL ' b'"subject" ' b'NIL ' b'NIL ' b'NIL ' b'NIL ' b'NIL ' b'NIL ' b'"<reply-to-id>" ' b'"<msg_id>"))' ) output = parse_fetch_response([envelope_str], normalise_times=False) self.assertSequenceEqual(output[1][b'ENVELOPE'], Envelope( None, b"subject", None, None, None, None, None, None, b"<reply-to-id>", b"<msg_id>" ) )
def make_fake_fetch_item(folder, uid, keys): body_text = fake.paragraphs(choice((1, 2, 3))) fake_data = { b'FLAGS': ['\\Seen'], b'BODYSTRUCTURE': (b'TEXT', b'PLAIN', None, None, None, b'UTF-8', 100), b'BODY[1]<0>': f'{body_text[0][:500]}', b'BODY[1]': '\n'.join(body_text), b'BODY[HEADER.FIELDS (REFERENCES CONTENT-TRANSFER-ENCODING)]': '', b'RFC822.SIZE': 100, } message_id_folder = choice(ALIAS_FOLDERS + [folder]) message_id = f'{message_id_folder}_{uid}' from_addresses = make_fake_addresses() subject = fake.text() fake_data[b'SEQ'] = uid fake_data[b'ENVELOPE'] = Envelope( datetime.utcnow(), subject, from_addresses, # from from_addresses, # sender from_addresses, # reply to None, # to None, # cc None, # bcc 'abc', # in reply to message_id, ) return {key: value for key, value in fake_data.items() if key in keys}
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_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_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 make_fake_fetch_data(folder, uid): body_text = fake.paragraphs(choice((2, 3, 4, 5, 6, 7, 8, 9))) headers = '' if choice(range(100)) > 60: reply_to_folder = choice(ALIAS_FOLDERS + [folder.name]) reply_to_uid = choice(FOLDER_NAME_TO_FAKE_FOLDER[reply_to_folder].uids) reply_to_message_id = f'{reply_to_folder}_{reply_to_uid}' headers = f'References: <{reply_to_message_id}>\r\n\r\n'.encode() fake_data = { b'FLAGS': ['\\Seen'], b'BODYSTRUCTURE': (b'TEXT', b'PLAIN', None, None, None, b'UTF-8', 100), b'BODY[1]<0>': f'{body_text[0][:500]}', b'BODY[1]': '\n\n'.join(body_text), b'BODY[HEADER.FIELDS (REFERENCES CONTENT-TRANSFER-ENCODING)]': headers, b'RFC822.SIZE': 100, } message_id = f'<{folder.name}_{uid}>' from_addresses = make_fake_addresses() subject = fake.sentence(choice((3, 4, 5, 6, 7, 8))) fake_data[b'SEQ'] = uid fake_data[b'ENVELOPE'] = Envelope( datetime.utcnow(), subject, from_addresses, # from from_addresses, # sender from_addresses, # reply to None, # to None, # cc None, # bcc None, # in reply to message_id, ) return fake_data