def test_hw_failure_mail(self):
        """Tests hardware failure email is sent."""
        mailqueue = Mock(MailQueue, autospec=True)
        mailqueue.add = Mock(return_value=None, autospect=True)
        sensor1 = MockSensor(poll_results=[False, False],
                             error_results=[True, False])
        # noinspection PyTypeChecker
        eventloop = EventLoop(mailqueue, [sensor1],
                              poll_interval_in_seconds=.001,
                              loop_forever=False)

        # HW failure email sent.
        eventloop.run()
        self.assertEqual(
            Message('MockSensor has detected a hardware failure.',
                    'MockSensor has detected a hardware failure.'),
            mailqueue.add.call_args_list[0][0][0])
        self.assertEqual(1, mailqueue.send.call_count)

        # HW OK email sent.
        eventloop.run()
        self.assertEqual(
            Message('MockSensor hardware is OK.',
                    'MockSensor hardware is OK.'),
            mailqueue.add.call_args_list[1][0][0])
        self.assertEqual(2, mailqueue.send.call_count)
    def test_alarm_mail(self):
        """Tests alarm email is sent."""
        mailqueue = Mock(MailQueue, autospec=True)
        mailqueue.add = Mock(return_value=None, autospec=True)
        sensor1 = MockSensor(poll_results=[True, False])
        # noinspection PyTypeChecker
        eventloop = EventLoop(mailqueue, [sensor1],
                              poll_interval_in_seconds=.001,
                              loop_forever=False)

        # Alarm on email sent.
        eventloop.run()
        self.assertEqual(Message('MockSensor is on.', 'MockSensor is on.'),
                         mailqueue.add.call_args_list[0][0][0])
        self.assertEqual(1, mailqueue.send.call_count)

        # Alarm off email is sent.
        eventloop.run()
        self.assertEqual(Message('MockSensor is off.', 'MockSensor is off.'),
                         mailqueue.add.call_args_list[1][0][0])
        self.assertEqual(2, mailqueue.send.call_count)
    def test_second_two_messags(self):
        """Tests sending two messages, with a mock.

        The send method should be called twice, once for each message.
        """
        mail = Mock(Mail)
        check_internet_connection = CheckInternetConnectionMock()
        # noinspection PyTypeChecker
        mailqueue = MailQueue(mail, check_internet_connection)
        mailqueue.add(Message('One', 'BodyOne'))
        mailqueue.add(Message('Two', 'BodyTwo'))
        mailqueue.send()

        # Verify Main.send() was called twice with the correct arguments.
        self.assertEqual(mail.send.call_count, 2)
        self.assertEqual(mail.send.call_args_list[0][0], ('One', 'BodyOne'))
        self.assertEqual(mail.send.call_args_list[1][0], ('Two', 'BodyTwo'))

        # Send a second time.  The call count should remain at 2 because
        # the queue is cleared and Mail.send() is not called again.
        mailqueue.send()
        self.assertEqual(mail.send.call_count, 2)
    def test_fail_and_then_pass(self):
        """The first send fails, then the subsequent ones pass."""
        mail = MailMockFailPass()
        check_internet_connection = CheckInternetConnectionMock()
        mailqueue = MailQueue(mail, check_internet_connection)
        mailqueue.add(Message('One', 'BodyOne'))
        # The first time, send will raise an exception.
        mailqueue.send()

        # The second time, send will pass.
        mailqueue.send()

        # The third time, there is nothing in the queue.  So send should be called exactly twice.
        mailqueue.send()
        self.assertEqual(mail.send_call_count, 2)
 def test_fail_three_times(self, logs):
     """Tests failing to send the message three times."""
     mail = Mock(Mail)
     mail.send.side_effect = MailException
     check_internet_connection = CheckInternetConnectionMock()
     # noinspection PyTypeChecker
     mailqueue = MailQueue(mail, check_internet_connection, retries=3)
     mailqueue.add(Message('One', 'BodyOne'))
     for _ in range(0, 5):
         mailqueue.send()
     self.assertEqual(mail.send.call_count, 3,
                      'Mail.send() should be call exactly 3 times.')
     self.assertRegex(
         logs.output[0],
         'Failed to send message with subject "One" 3 times.  Giving up.')
Beispiel #6
0
def _send_test_mail(mailqueue):
    """Sends a test email.

    :param homemonitor.mailqueue.MailQueue mailqueue: Queue used to send out test email.
    """
    message = Message(
        'Test email from homemonitor.', 'Hi,\r\n\r\n'
        'This is a test email from the homemonitor on your Raspbery Pi.  '
        'Hopefully you received this email just fine and'
        ' your config file is setup correctly.\r\n\r\n'
        'Sincerely,\r\n'
        'HomeMonitor')
    mailqueue.add(message)
    print('Sending test email to {}...'.format(mailqueue.mail.receivers))
    mailqueue.send()
    print('Done.  Check your inbox.')
    def test_internet_down_and_up(self):
        """Tests the Internet being down for awhile, and then comes back up."""
        mail = Mock(Mail)
        check_internet_connection = CheckInternetConnectionMock(
            [False, False, False, False, False, True])
        # noinspection PyTypeChecker
        mailqueue = MailQueue(mail, check_internet_connection)
        mailqueue.add(Message('One', 'BodyOne'))

        # Send the message 5 times.  The actual mail.send() method should
        # not have been called because the Internet is down.
        for _ in range(0, 5):
            mailqueue.send()
        self.assertEqual(mail.send.call_count, 0)

        # On the 6th call, the Internet is back up and mail gets sent.
        mailqueue.send()
        self.assertEqual(mail.send.call_count, 1)