def test_find_mentions_one_per_user(self):
        """Test finding of people mentions."""
        comment_text = (u"<a href=\"mailto:[email protected]\"></a>"
                        u"<a href=\"mailto:[email protected]\"></a>"
                        u"<a href=\"mailto:[email protected]\"></a>")
        comment = people_mentions.CommentData(
            author="*****@*****.**",
            created_at=datetime.datetime(2017, 1, 10, 7, 31, 42),
            comment_text=comment_text,
        )
        with mock.patch("ggrc.utils.user_generator.find_user",
                        return_value=True):
            # pylint: disable=protected-access
            email_mentions = people_mentions._find_email_mentions([comment])
        self.assertEquals(len(email_mentions), 2)
        self.assertTrue(u"*****@*****.**" in email_mentions)
        self.assertTrue(u"*****@*****.**" in email_mentions)
        self.assertEquals(len(email_mentions[u"*****@*****.**"]), 1)
        self.assertEquals(len(email_mentions[u"*****@*****.**"]), 1)

        first_email_elem = next(iter(email_mentions[u"*****@*****.**"]))
        self.assertEquals(first_email_elem, comment)

        second_email_elem = next(iter(email_mentions[u"*****@*****.**"]))
        self.assertEquals(second_email_elem, comment)
 def test_generate_mention_email(self, comment_text):
     """Test email generation."""
     comments_data = set()
     comments_data.add(
         people_mentions.CommentData(
             author="*****@*****.**",
             created_at=datetime.datetime(2017, 07, 10, 7, 31, 42),
             comment_text=comment_text,
         ))
 def test_find_mentions_in_comments(self):
     """Test find people mentions in several comments."""
     comment_text = (u"<a href=\"mailto:[email protected]\">one</a>"
                     u"<a href=\"mailto:[email protected]\">two</a>"
                     u"<a href=\"mailto:[email protected]\">three</a>")
     first_comment = people_mentions.CommentData(
         author="*****@*****.**",
         created_at=datetime.datetime(2017, 1, 10, 7, 31, 42),
         comment_text=comment_text,
     )
     second_comment = people_mentions.CommentData(
         author="*****@*****.**",
         created_at=datetime.datetime(2017, 1, 10, 7, 31, 42),
         comment_text=comment_text,
     )
     with mock.patch("ggrc.utils.user_generator.find_user",
                     return_value=True):
         # pylint: disable=protected-access
         email_mentions = people_mentions._find_email_mentions(
             [first_comment, second_comment])
     self.assertEquals(len(email_mentions), 2)
     self.assertEquals(len(email_mentions[u"*****@*****.**"]), 2)
     self.assertEquals(len(email_mentions[u"*****@*****.**"]), 2)
 def test_send_mentions(self, send_email_mock):
     """Test sending mentions emails."""
     comments_data = set()
     comments_data.add(
         people_mentions.CommentData(
             author="*****@*****.**",
             created_at=datetime.datetime(2018, 1, 10, 7, 31, 42),
             comment_text=u"One <a href=\"mailto:[email protected]\"></a>",
         ))
     comments_data.add(
         people_mentions.CommentData(
             author="*****@*****.**",
             created_at=datetime.datetime(2018, 1, 10, 7, 31, 50),
             comment_text=u"Two <a href=\"mailto:[email protected]\"></a>",
         ))
     with mock.patch("ggrc.utils.user_generator.find_user",
                     return_value=True):
         people_mentions.send_mentions(object_name="Object",
                                       href="api/objects/1",
                                       comments_data=comments_data)
     expected_title = (u"[email protected] mentioned you "
                       u"on a comment within Object")
     body = settings.EMAIL_MENTIONED_PERSON.render(
         person_mention={
             "comments": [
                 (u"[email protected] mentioned you on a comment within Object "
                  u"at 01/09/2018 23:31:42 PST:\n"
                  u"One <a href=\"mailto:[email protected]\"></a>\n"),
                 (u"[email protected] mentioned you on a comment within Object "
                  u"at 01/09/2018 23:31:50 PST:\n"
                  u"Two <a href=\"mailto:[email protected]\"></a>\n"),
             ],
             "url":
             "api/objects/1",
         })
     send_email_mock.assert_called_once_with(u"*****@*****.**",
                                             expected_title, body)
 def test_find_email_mentions_mult(self):
     """Test finding several mentions in comments."""
     first_comment = people_mentions.CommentData(
         author="*****@*****.**",
         created_at=datetime.datetime(2018, 1, 10, 7, 31, 42),
         comment_text=u"One <a href=\"mailto:[email protected]\"></a>",
     )
     second_comment = people_mentions.CommentData(
         author="*****@*****.**",
         created_at=datetime.datetime(2018, 1, 10, 7, 31, 50),
         comment_text=u"Two <a href=\"mailto:[email protected]\"></a>",
     )
     comments = [first_comment, second_comment]
     with mock.patch("ggrc.utils.user_generator.find_user",
                     return_value=True):
         # pylint: disable=protected-access
         email_mentions = people_mentions._find_email_mentions(comments)
     self.assertEquals(len(email_mentions), 1)
     self.assertTrue(u"*****@*****.**" in email_mentions)
     self.assertEquals(len(email_mentions[u"*****@*****.**"]), 2)
     related_comments = sorted(list(email_mentions[u"*****@*****.**"]))
     comments = sorted(comments)
     self.assertEquals(related_comments[0], comments[0])
     self.assertEquals(related_comments[1], comments[1])
 def test_find_email_mentions_regex(self):
     """Test email regex in find mention function."""
     comment_text = (u"<a href=\"mail:{[email protected]}\"></a>"
                     u"<a href=\"mailto:{[email protected]}\"></a>"
                     u"<a url=\"mailto{[email protected]}\"></a>"
                     u"<a href=\"mailto:{user4@[email protected]}\"></a>")
     comment = people_mentions.CommentData(
         author="*****@*****.**",
         created_at=datetime.datetime(2018, 1, 10, 7, 31, 42),
         comment_text=comment_text,
     )
     with mock.patch("ggrc.utils.user_generator.find_user",
                     return_value=True):
         # pylint: disable=protected-access
         email_mentions = people_mentions._find_email_mentions([comment])
     self.assertEquals(len(email_mentions), 0)