def test_send_combination(self, mock_em_send):
        ''' | *brief*: Check that a message sent to a combination of known & unknown addresses
            |    returns a dictionary with appropriate statuses for each address.
        '''
        mock_em_send.return_value = 1
        known_addresses = [
            '*****@*****.**',  # Not yet confirmed
            '*****@*****.**',  # Confirmed
        ]
        # Make records to indicate the known addresses are known.
        yesterday = datetime.today() - timedelta(days=1)
        AddressConfirmation.objects.create(address=known_addresses[0],
                                           last_request_date=yesterday)
        AddressConfirmation.objects.create(address=known_addresses[1],
                                           confirmation_timestamp=yesterday)

        unknown_addresses = [
            '*****@*****.**',  # Address currently unknown
            '*****@*****.**',  # Address currently unknown
        ]
        sender_address = '*****@*****.**'

        recipient_addresses = known_addresses + unknown_addresses
        cem = ConfirmedEmailMessage(subject='Subject',
                                    body='message_body',
                                    from_email=sender_address,
                                    to=recipient_addresses)
        send_results = cem.send()

        # One call for each of the confirmation emails sent for unkown_addresses
        #    plus one call for the email sent to the confirmed address.
        self.assertEqual(mock_em_send.call_count, 3)
        self.assertEqual(set(send_results.keys()),
                         set(known_addresses + unknown_addresses))
        expected_results = {
            '*****@*****.**': 'queued',
            '*****@*****.**': 'sent',
            '*****@*****.**': 'queued',
            '*****@*****.**': 'queued',
        }
        self.assertEqual(send_results, expected_results)
    def test_send_known(self, mock_django_send, mock_send_unconfirmed):
        ''' | *brief*: Check that a message sent to a known address returns {<address>: 'sent'}
            | *author*: Jivan
            | *created*: 2016-02-25
        '''
        recipient_address = '*****@*****.**'
        sender_address = '*****@*****.**'
        message_subject = 'Greeting'
        message_content = 'Hi there'

        AddressConfirmation.objects.create(
            address=recipient_address, confirmation_timestamp=datetime.now())
        cem = ConfirmedEmailMessage(subject=message_subject,
                                    body=message_content,
                                    from_email=sender_address,
                                    to=[recipient_address])

        send_results = cem.send()
        expected_results = {recipient_address: 'sent'}
        self.assertEqual(send_results, expected_results)
        mock_send_unconfirmed.assert_not_called()
    def test_send_known(self, mock_django_send, mock_send_unconfirmed):
        ''' | *brief*: Check that a message sent to a known address returns {<address>: 'sent'}
            | *author*: Jivan
            | *created*: 2016-02-25
        '''
        recipient_address = '*****@*****.**'
        sender_address = '*****@*****.**'
        message_subject = 'Greeting'
        message_content = 'Hi there'

        AddressConfirmation.objects.create(
            address=recipient_address, confirmation_timestamp=datetime.now()
        )
        cem = ConfirmedEmailMessage(
            subject=message_subject, body=message_content, from_email=sender_address,
            to=[recipient_address]
        )

        send_results = cem.send()
        expected_results = {recipient_address: 'sent'}
        self.assertEqual(send_results, expected_results)
        mock_send_unconfirmed.assert_not_called()
    def test_send_unknown(self, mock_send_unconfirmed):  # , mock_emailmultialternatives_send):
        ''' | *brief*: Checks that a message sent to an unknown address
            |    returns {<address>: 'queued'}
            | *author*: Jivan
            | *created*: 2016-02-25
        '''
        recipient_address = '*****@*****.**'
        sender_address = '*****@*****.**'
        message_subject = 'Greeting'
        message_content = 'Hi there'

        cem = ConfirmedEmailMessage(
            subject=message_subject, body=message_content, from_email=sender_address,
            to=[recipient_address]
        )

        # Assume successful confirmation email sending
        mock_send_unconfirmed.return_value = 1
        send_results = cem.send()
        mock_send_unconfirmed.assert_called_once_with()
        expected_results = {recipient_address: 'queued'}
        self.assertEqual(send_results, expected_results)
    def test_attachment_serialization(self):
        to_address = '*****@*****.**'
        ac = AddressConfirmation.objects.create(address=to_address)

        binary_dirpath = os.path.normpath(os.path.join(os.path.abspath(__file__), os.path.pardir))
        binary_filepath = os.path.join(binary_dirpath, 'binary_testing_file')
        with open(binary_filepath, 'rb') as bf:
            attachment_data = bf.read()
        cem_before = ConfirmedEmailMessage(
            to=[to_address], from_email='*****@*****.**', subject='Test Email',
            body='This is the message body.'
        )
        cem_before.attach('attachment_file', attachment_data)
        qem = QueuedEmailMessage.objects.create(address_confirmation=ac)
        qem.email_contents = cem_before
        qem.save()

        cem_after = QueuedEmailMessage.objects.get(address_confirmation=ac).email_contents
        msg = 'before:\n{}\n'.format(cem_before)
        msg += '---\n'
        msg += 'after:\n{}'.format(cem_after)
        self.assertEqual(cem_after, cem_before, msg)
    def test_send_combination(self, mock_em_send):
        ''' | *brief*: Check that a message sent to a combination of known & unknown addresses
            |    returns a dictionary with appropriate statuses for each address.
        '''
        mock_em_send.return_value = 1
        known_addresses = [
            '*****@*****.**',  # Not yet confirmed
            '*****@*****.**',  # Confirmed
        ]
        # Make records to indicate the known addresses are known.
        yesterday = datetime.today() - timedelta(days=1)
        AddressConfirmation.objects.create(address=known_addresses[0], last_request_date=yesterday)
        AddressConfirmation.objects.create(address=known_addresses[1], confirmation_timestamp=yesterday)

        unknown_addresses = [
            '*****@*****.**',  # Address currently unknown
            '*****@*****.**',  # Address currently unknown
        ]
        sender_address = '*****@*****.**'

        recipient_addresses = known_addresses + unknown_addresses
        cem = ConfirmedEmailMessage(
            subject='Subject', body='message_body', from_email=sender_address,
            to=recipient_addresses
        )
        send_results = cem.send()

        # One call for each of the confirmation emails sent for unkown_addresses
        #    plus one call for the email sent to the confirmed address.
        self.assertEqual(mock_em_send.call_count, 3)
        self.assertEqual(set(send_results.keys()), set(known_addresses + unknown_addresses))
        expected_results = {
                '*****@*****.**': 'queued',
                '*****@*****.**': 'sent',
                '*****@*****.**': 'queued',
                '*****@*****.**': 'queued',
        }
        self.assertEqual(send_results, expected_results)
    def test_send_unknown(
            self,
            mock_send_unconfirmed):  # , mock_emailmultialternatives_send):
        ''' | *brief*: Checks that a message sent to an unknown address
            |    returns {<address>: 'queued'}
            | *author*: Jivan
            | *created*: 2016-02-25
        '''
        recipient_address = '*****@*****.**'
        sender_address = '*****@*****.**'
        message_subject = 'Greeting'
        message_content = 'Hi there'

        cem = ConfirmedEmailMessage(subject=message_subject,
                                    body=message_content,
                                    from_email=sender_address,
                                    to=[recipient_address])

        # Assume successful confirmation email sending
        mock_send_unconfirmed.return_value = 1
        send_results = cem.send()
        mock_send_unconfirmed.assert_called_once_with()
        expected_results = {recipient_address: 'queued'}
        self.assertEqual(send_results, expected_results)