def setUp(self): raw_message = read_file( f"{settings.BASE_DIR}/apps/restricted/fixtures/emails/valid.txt") parser = EmailParser(raw_message, "*****@*****.**", ParserMessageType.STRING) self.message = parser.parse() self.processor = MessageProcessor("*****@*****.**", self.message, {})
def test_parse_message_no_token(self): """Parsing a message with no token has no effect, the function returns None""" raw_message = read_file( f'{settings.BASE_DIR}/apps/restricted/fixtures/emails/no_token.txt' ) parser = EmailParser(raw_message, '*****@*****.**', ParserMessageType.STRING) message = parser.parse() token = get_mail_token(message) self.assertIsNone(token)
def test_parse_valid_email(self): """Try to parse a valid message, make sure we add the correct properties to the message.""" raw_message = read_file( f"{settings.BASE_DIR}/apps/restricted/fixtures/emails/valid.txt" ) parser = EmailParser(raw_message, "*****@*****.**", ParserMessageType.STRING) message = parser.parse() self.assertIsInstance(message, Message) self.assertEquals(message.original_size, len(raw_message)) self.assertEquals(message["X-MailFrom"], "*****@*****.**") self.assertEquals(message["To"], "*****@*****.**")
def test_rewrite_message(self): """Remove headers that may cause problems. Only keep a few required headers.""" raw_message = read_file( f'{settings.BASE_DIR}/apps/restricted/fixtures/emails/clean_headers.txt' ) parser = EmailParser(raw_message, '*****@*****.**', ParserMessageType.STRING) message = parser.parse() new_message = self.processor.rewrite_message(message, '*****@*****.**') self.assertCountEqual( ['From', 'Subject', 'Content-Type', 'MIME-Version', 'Sender'], new_message.keys())
def test_parse_valid_message(self): """Try to parse a valid message and make sure ve remove the token payload""" raw_message = read_file( f'{settings.BASE_DIR}/apps/restricted/fixtures/emails/valid.txt') parser = EmailParser(raw_message, '*****@*****.**', ParserMessageType.STRING) message = parser.parse() payloads = len(message.get_payload()) token = get_mail_token(message) self.assertEquals('test_token', token) self.assertEquals(len(message.get_payload()), payloads - 1)
def test_send(self): """Test the outbox and make sure we have correct headers in the messages.""" raw_message = read_file( f'{settings.BASE_DIR}/apps/restricted/fixtures/emails/clean_headers.txt' ) parser = EmailParser(raw_message, '*****@*****.**', ParserMessageType.STRING) message = parser.parse() new_message = self.processor.rewrite_message(message, '*****@*****.**') messages = self.processor.send(['*****@*****.**', '*****@*****.**'], '*****@*****.**', new_message) self.assertEquals(2, messages) outbox = mail.outbox first_message = outbox[0].message() self.assertSequenceEqual([ 'Subject', 'Content-Type', 'MIME-Version', 'Sender', 'From', 'To' ], first_message.keys())
def test_send(self): """Test the outbox and make sure we have correct headers in the messages.""" raw_message = read_file( f"{settings.BASE_DIR}/apps/restricted/fixtures/emails/clean_headers.txt" ) parser = EmailParser(raw_message, "*****@*****.**", ParserMessageType.STRING) message = parser.parse() new_message = self.processor.rewrite_message(message, "*****@*****.**") messages = self.processor.send(["*****@*****.**", "*****@*****.**"], "*****@*****.**", new_message) self.assertEquals(2, messages) outbox = mail.outbox first_message = outbox[0].message() self.assertSequenceEqual( [ "Subject", "Content-Type", "MIME-Version", "Sender", "From", "To" ], first_message.keys(), )