def test_create_ticket_from_email_without_message_id(self):

        """
        Ensure that a <Ticket> instance is created whenever an email is sent to a public queue.
        Also, make sure that the RFC 2822 field "message-id" is stored on the <Ticket.submitter_email_id>
        field.
        """

        msg = email.message.Message()
        submitter_email = '*****@*****.**'

        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email) 
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)

        object_from_message(str(msg), self.queue_public, quiet=True)

        ticket = Ticket.objects.get(title=self.ticket_data['title'], queue=self.queue_public, submitter_email=submitter_email)

        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As we have created an Ticket from an email, we notify the sender (+1) 
        # and the new and update queues (+2)
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))

        # Ensure that the submitter is notified
        self.assertIn(submitter_email,mail.outbox[0].to)
    def test_create_ticket_from_email_to_multiple_emails(self):

        """ 
        Ensure that an instance of <TicketCC> is created for every valid element of the
        "rfc_2822_cc" field when creating a <Ticket> instance.
        """

        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        to_list = [self.queue_public.email_address]
        cc_list = ['*****@*****.**', '*****@*****.**']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', ','.join(to_list + cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(msg), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As we have created an Ticket from an email, we notify the sender
        # and contacts on the cc_list (+1 as it's treated as a list),
        # the new and update queues (+2) 
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))

        # Ensure that the submitter is notified
        self.assertIn(submitter_email,mail.outbox[0].to)

        # Ensure that the queue's email was not subscribed to the event notifications.
        self.assertRaises(TicketCC.DoesNotExist, TicketCC.objects.get, ticket=ticket, email=to_list[0])
        
        for cc_email in cc_list:

            # Ensure that contacts on cc_list will be notified on the same email (index 0)
            self.assertIn(cc_email, mail.outbox[0].to)

            # Ensure that <TicketCC> exists
            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
    def test_create_ticket_from_email_to_a_notification_enabled_queue(self):

        """
            Ensure that when an email is sent to a Queue with notifications_enabled turned ON, 
        and a <Ticket> is created, all contacts n the TicketCC list are notified.
        """
        
        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Cc', ','.join(cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        object_from_message(str(msg), self.queue_public, quiet=True)
        

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As we have created a Ticket from an email, we notify the sender
        # and contacts on the cc_list (+1 as it's treated as a list),
        # the new and update queues (+2) 
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))

        # Ensure that the submitter is notified
        self.assertIn(submitter_email, mail.outbox[0].to)

        # Ensure that <TicketCC> exist
        for cc_email in cc_list:

            # Ensure that contacts on cc_list will be notified on the same email (index 0)
            self.assertIn(cc_email, mail.outbox[0].to)

            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
    def test_create_ticket_from_email_with_invalid_carbon_copy(self):

        """ 
        Ensure that no <TicketCC> instance is created if an invalid element of the
        "rfc_2822_cc" field is provided when creating a <Ticket> instance.
        """

        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['null@example', 'invalid@foobar']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Cc', ','.join(cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)

        object_from_message(str(msg), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As we have created an Ticket from an email, we notify the sender
        # and contacts on the cc_list (+1 as it's treated as a list),
        # the new and update queues (+2) 
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))

        # Ensure that the submitter is notified
        self.assertIn(submitter_email, mail.outbox[0].to)
        
        for cc_email in cc_list:

            # Ensure that contacts on cc_list will be notified on the same email (index 0)
            self.assertIn(cc_email, mail.outbox[0].to)

            # Ensure that <TicketCC> exists. Even if it's an invalid email.
            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
    def test_create_ticket_from_email_to_a_notification_disabled_queue(self):

        """
            Ensure that when an email is sent to a Queue with notifications_enabled turned OFF, only the 
        new_ticket_cc and updated_ticket_cc contacts (if they are set) are notified. No contact
        from the TicketCC list should be notified.
        """
        
        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Cc', ','.join(cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(msg), self.queue_public_with_notifications_disabled, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq2-%s" % ticket.id)

        # As we have created an Ticket from an email, we notify the sender (+1),
        # the new and update queues (+2) and that's all
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))

        # Ensure that <TicketCC> is created even if the Queue notifications are disabled 
        # so when staff members interact with the <Ticket>, they get notified
        for cc_email in cc_list:

            # Ensure that contacts on the cc_list are not notified
            self.assertNotIn(cc_email, mail.outbox[0].to)

            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
    def test_create_followup_from_email_with_valid_message_id_with_original_cc_list_included(self):

        """
        Ensure that if a message is received with an valid In-Reply-To ID, 
        the expected <TicketCC> instances are created even if the there were
        no <TicketCC>s so far.
        """

        ### Ticket and TicketCCs creation ###
        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(msg), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        ### end of the Ticket and TicketCCs creation ###

        # Reply message
        reply = email.message.Message()

        reply_message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        reply.__setitem__('Message-ID', reply_message_id)
        reply.__setitem__('In-Reply-To', message_id)
        reply.__setitem__('Subject', self.ticket_data['title'])
        reply.__setitem__('From', submitter_email)
        reply.__setitem__('To', self.queue_public.email_address)
        reply.__setitem__('Cc', ','.join(cc_list))
        reply.__setitem__('Content-Type', 'text/plain;')
        reply.set_payload(self.ticket_data['description'])

        object_from_message(str(reply), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # Ensure that <TicketCC> is created
        for cc_email in cc_list:
            # Even after 2 messages with the same cc_list, <get> MUST return only 
            # one object 
            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)

        # As we have created an Ticket from an email, we notify the sender (+1)
        # and the new and update queues (+2)
        expected_email_count = 1 + 2

        # As an update was made, we increase the expected_email_count with:
        # cc_list: +1
        # public_update_queue: +1
        expected_email_count += 1 + 1   
        self.assertEqual(expected_email_count, len(mail.outbox))
    def test_create_followup_from_email_to_a_notification_diabled_queue(self):

        """
            Ensure that when an email is sent to a Queue with notifications_enabled turned ON, 
        and a <FollowUp> is created, all contacts n the TicketCC list are notified.
        """

        ### Ticket and TicketCCs creation ###
        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public_with_notifications_disabled.email_address)
        msg.__setitem__('Cc', ','.join(cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(msg), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As we have created an Ticket from an email, we notify the sender
        # and contacts on the cc_list (+1 as it's treated as a list),
        # the new and update queues (+2) 
        expected_email_count = email_count + 1 + 2
        self.assertEqual(expected_email_count, len(mail.outbox))
        

        # Ensure that <TicketCC> is created
        for cc_email in cc_list:

            # Ensure that contacts on cc_list will be notified on the same email (index 0)
            self.assertIn(cc_email, mail.outbox[0].to)

            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
        ### end of the Ticket and TicketCCs creation ###

        # Reply message
        reply = email.message.Message()

        reply_message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'

        reply.__setitem__('Message-ID', reply_message_id)
        reply.__setitem__('In-Reply-To', message_id)
        reply.__setitem__('Subject', self.ticket_data['title'])
        reply.__setitem__('From', submitter_email)
        reply.__setitem__('To', self.queue_public_with_notifications_disabled.email_address)
        reply.__setitem__('Content-Type', 'text/plain;')
        reply.set_payload(self.ticket_data['description'])

        object_from_message(str(reply), self.queue_public_with_notifications_disabled, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)

        # As an update was made, we increase the expected_email_count with:
        # public_update_queue: +1
        expected_email_count += 1
        self.assertEqual(expected_email_count, len(mail.outbox))
    def test_create_followup_from_email_with_invalid_message_id(self):

        """
        Ensure that if a message is received with an invalid In-Reply-To ID and we
        can infer the original Ticket ID by the message's subject, the expected 
        <TicketCC> instances are created
        """
        
        ### Ticket and TicketCCs creation ###
        msg = email.message.Message()

        message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        msg.__setitem__('Message-ID', message_id)
        msg.__setitem__('Subject', self.ticket_data['title'])
        msg.__setitem__('From', submitter_email)
        msg.__setitem__('To', self.queue_public.email_address)
        msg.__setitem__('Cc', ','.join(cc_list))
        msg.__setitem__('Content-Type', 'text/plain;')
        msg.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(msg), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)

        # Ensure that <TicketCC> is created
        for cc_email in cc_list:
            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)
            self.assertTrue(ticket_cc.can_view, True)

        # As we have created an Ticket from an email, we notify the sender
        # and contacts on the cc_list (+1 as it's treated as a list),
        # the new and update queues (+2) 
        expected_email_count = 1 + 2
        self.assertEqual(expected_email_count, len(mail.outbox))

        # Ensure that the submitter is notified
        self.assertIn(submitter_email,mail.outbox[0].to)

        # Ensure that <TicketCC> is created
        for cc_email in cc_list:

            # Ensure that contacts on cc_list will be notified on the same email (index 0)
            self.assertIn(cc_email, mail.outbox[0].to)

            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)

        ### end of the Ticket and TicketCCs creation ###

        # Reply message
        reply = email.message.Message()

        reply_message_id = uuid.uuid4().hex
        submitter_email = '*****@*****.**'
        cc_list = ['*****@*****.**', '*****@*****.**']

        invalid_message_id = 'INVALID'
        reply_subject = 'Re: ' + self.ticket_data['title']

        reply.__setitem__('Message-ID', reply_message_id)
        reply.__setitem__('In-Reply-To', invalid_message_id)
        reply.__setitem__('Subject', reply_subject)
        reply.__setitem__('From', submitter_email)
        reply.__setitem__('To', self.queue_public.email_address)
        reply.__setitem__('Cc', ','.join(cc_list))
        reply.__setitem__('Content-Type', 'text/plain;')
        reply.set_payload(self.ticket_data['description'])

        email_count = len(mail.outbox)
        
        object_from_message(str(reply), self.queue_public, quiet=True)

        followup = FollowUp.objects.get(message_id=message_id)
        ticket = Ticket.objects.get(id=followup.ticket.id)
        self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)


        # Ensure that <TicketCC> is created
        for cc_email in cc_list:
            # Even after 2 messages with the same cc_list, <get> MUST return only 
            # one object 
            ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
            self.assertTrue(ticket_cc.ticket, ticket)
            self.assertTrue(ticket_cc.email, cc_email)

        # As we have created an Ticket from an email, we notify the sender (+1),
        # the new and update queues (+2) and contacts on the cc_list (+1 as it's 
        # treated as a list)
        self.assertEqual(email_count + 1 + 2, len(mail.outbox))