コード例 #1
0
    def test_regex_lru_cache(self):
        """ Test that the regex() method's output is cached
        """
        regex = Command.regex()

        with patch('re.compile') as mock_compile:
            mock_compile.return_value = False
            regex2 = Command.regex()

            self.assertEqual(regex, regex2)
            mock_compile.assert_not_called()
コード例 #2
0
    def test_from_email_invalid_sender(self):
        """ Test that an exception while retrieving the sender's email address
        returns None
        """
        cmd_str = ' '.join([Command.CMD_PREFIX, Command.VALID_ACTIONS[0]])
        mail = {'Subject': cmd_str}

        with patch('plexmonitor.lib.command.get_sender_email') as mock_sender:
            mock_sender.side_effect = [RuntimeError]
            cmd = Command.from_email(mail)
            self.assertIsNone(cmd)
コード例 #3
0
ファイル: email_task.py プロジェクト: jivid/plexmonitor
    def execute(self) -> None:
        last_unread = self.inbox.get_last_unread_mail_id()
        last_processed = self.last_mail_id

        if last_processed is not None and\
                int(last_unread) <= int(last_processed):
            self.logger.info("Nothing to fetch")
            return

        self.logger.info("Going to fetch mail ID {}".format(last_unread))
        mail = self.inbox.fetch(last_unread)  # type: email.message.Message
        self.last_mail_id = last_unread

        cmd = Command.from_email(mail)
        if not cmd:
            self.logger.info("No valid command")
            return

        self.logger.info("Got command {action} from {sender}".format(
            action=cmd.action, sender=cmd.context['sender']))
コード例 #4
0
    def test_from_email_valid_commands(self):
        """ Test that a valid command in the email's subject line yields a
        properly instantiated Command object with an action and context
        """
        fake_email = 'foo_email'

        def make_mail(action):
            return {
                'Subject': ' '.join([Command.CMD_PREFIX, action]),
                'From': fake_email
            }

        with patch('plexmonitor.lib.command.get_sender_email') as mock_sender:
            mock_sender.return_value = fake_email

            for action in Command.VALID_ACTIONS:
                mail = make_mail(action)
                cmd = Command.from_email(mail)
                self.assertIsInstance(cmd, Command)
                self.assertEqual(cmd.action, action)
                self.assertEqual(cmd.context['sender'], fake_email)
コード例 #5
0
 def test_regex(self):
     """ Test that the regex() method returns a pattern matcher
     """
     regex = Command.regex()
     self.assertIsInstance(regex, re._pattern_type)
コード例 #6
0
 def test_repr(self):
     cmd = Command(action='foo')
     self.assertEqual(repr(cmd), str(cmd))
コード例 #7
0
 def test_str(self):
     cmd = Command(action='foo')
     self.assertEqual(str(cmd), '<Command: foo>')
コード例 #8
0
 def test_from_email_invalid_command(self):
     """ Test that an invalid command in the subject line just returns None
     """
     mail = {'Subject': 'foo bar'}
     cmd = Command.from_email(mail)
     self.assertIsNone(cmd)
コード例 #9
0
 def test_from_email_no_subject(self):
     """ Test that a missing subject line just returns None
     """
     mail = {}
     cmd = Command.from_email(mail)
     self.assertIsNone(cmd)