Esempio n. 1
0
def GetRequestObjects(headers=None,
                      path='/',
                      params=None,
                      payload=None,
                      user_info=None,
                      project=None,
                      method='GET',
                      perms=None,
                      services=None,
                      hotlist=None):
    """Make fake request and MonorailRequest objects for testing.

  Host param will override the 'Host' header, and has a default value of
  '127.0.0.1'.

  Args:
    headers: Dict of HTTP header strings.
    path: Path part of the URL in the request.
    params: Dict of query-string parameters.
    user_info: Dict of user attributes to set on a MonorailRequest object.
        For example, "user_id: 5" causes self.auth.user_id=5.
    project: optional Project object for the current request.
    method: 'GET' or 'POST'.
    perms: PermissionSet to use for this request.
    services: Connections to backends.
    hotlist: optional Hotlist object for the current request

  Returns:
    A tuple of (http Request, monorailrequest.MonorailRequest()).
  """
    headers = headers or {}
    params = params or {}

    headers.setdefault('Host', DEFAULT_HOST)
    post_items = None
    if method == 'POST' and payload:
        post_items = payload
    elif method == 'POST' and params:
        post_items = params

    if not services:
        services = service_manager.Services(project=fake.ProjectService(),
                                            user=fake.UserService(),
                                            usergroup=fake.UserGroupService(),
                                            features=fake.FeaturesService())
        services.project.TestAddProject('proj')
        services.features.TestAddHotlist('hotlist')

    request = webapp2.Request.blank(path, headers=headers, POST=post_items)
    mr = fake.MonorailRequest(user_info=user_info,
                              project=project,
                              perms=perms,
                              params=params,
                              hotlist=hotlist)
    mr.ParseRequest(request,
                    services,
                    profiler.Profiler(),
                    do_user_lookups=False)
    mr.auth.user_pb = user_pb2.MakeUser(0)
    return request, mr
Esempio n. 2
0
    def testClassifyIssue_spam(self):
        issue = fake.MakeTestIssue(project_id=789,
                                   local_id=1,
                                   reporter_id=111,
                                   owner_id=456,
                                   summary='sum',
                                   status='Live',
                                   issue_id=78901,
                                   is_spam=True)
        self.spam_service._predict = lambda body: 1.0

        # Prevent missing service inits to fail the test.
        self.spam_service.ml_engine = True

        comment_pb = tracker_pb2.IssueComment()
        comment_pb.content = "this is spam"
        reporter = user_pb2.MakeUser(111, email='*****@*****.**')
        res = self.spam_service.ClassifyIssue(issue, comment_pb, reporter,
                                              False)
        self.assertEqual(1.0, res['confidence_is_spam'])

        reporter.email = '*****@*****.**'
        res = self.spam_service.ClassifyIssue(issue, comment_pb, reporter,
                                              False)
        self.assertEqual(1.0, res['confidence_is_spam'])

        reporter.email = '*****@*****.**'
        res = self.spam_service.ClassifyIssue(issue, comment_pb, reporter,
                                              False)
        self.assertEqual(1.0, res['confidence_is_spam'])
Esempio n. 3
0
 def testUser_Everything(self):
     user = user_pb2.MakeUser(111,
                              email='*****@*****.**',
                              obscure_email=True)
     self.assertEqual(111, user.user_id)
     self.assertTrue(user.obscure_email)
     self.assertEqual('*****@*****.**', user.email)
Esempio n. 4
0
    def testClassifyComment_Whitelisted(self):
        self.spam_service._predict = assert_unreached

        # Prevent missing service inits to fail the test.
        self.spam_service.ml_engine = True

        commenter = user_pb2.MakeUser(111, email='*****@*****.**')
        res = self.spam_service.ClassifyComment('this is spam', commenter,
                                                False)
        self.assertEqual(0.0, res['confidence_is_spam'])

        commenter.email = '*****@*****.**'
        res = self.spam_service.ClassifyComment('this is spam', commenter,
                                                False)
        self.assertEqual(0.0, res['confidence_is_spam'])
Esempio n. 5
0
    def testClassifyComment_spam(self):
        self.spam_service._predict = lambda body: 1.0

        # Prevent missing service inits to fail the test.
        self.spam_service.ml_engine = True

        commenter = user_pb2.MakeUser(111, email='*****@*****.**')
        res = self.spam_service.ClassifyComment('this is spam', commenter,
                                                False)
        self.assertEqual(1.0, res['confidence_is_spam'])

        commenter.email = '*****@*****.**'
        res = self.spam_service.ClassifyComment('this is spam', commenter,
                                                False)
        self.assertEqual(1.0, res['confidence_is_spam'])

        commenter.email = '*****@*****.**'
        res = self.spam_service.ClassifyComment('this is spam', commenter,
                                                False)
        self.assertEqual(1.0, res['confidence_is_spam'])
Esempio n. 6
0
    def GetUsersByIDs(self, cnxn, user_ids, use_cache=True):
        """Return a dictionary of retrieved User PBs.

    Args:
      cnxn: connection to SQL database.
      user_ids: list of user IDs to fetch.
      use_cache: set to False to ignore cache and force DB lookup.

    Returns:
      A dict {user_id: user_pb} for each specified user ID.  For any user ID
      that is not fount in the DB, a default User PB is created on-the-fly.
    """
        # Check the RAM cache and memcache, as appropriate.
        result_dict, missed_ids = self.user_2lc.GetAll(cnxn,
                                                       user_ids,
                                                       use_cache=use_cache)

        # Provide default values for any user ID that was not found.
        result_dict.update(
            (user_id, user_pb2.MakeUser(user_id)) for user_id in missed_ids)

        return result_dict
Esempio n. 7
0
 def testIsExempt_WhitelistedDomain(self):
     author = user_pb2.MakeUser(111, email='*****@*****.**')
     self.assertTrue(self.spam_service._IsExempt(author, False))
Esempio n. 8
0
 def __init__(self, user_id=0, email=None):
   self.user_id = user_id
   self.effective_ids = {user_id} if user_id else set()
   self.user_view = None
   self.user_pb = user_pb2.MakeUser(user_id)
   self.email = email
Esempio n. 9
0
 def testUser_Defaults(self):
     user = user_pb2.MakeUser(111)
     self.assertEqual(111, user.user_id)
     self.assertFalse(user.obscure_email)
     self.assertIsNone(user.email)
Esempio n. 10
0
 def __init__(self):
     self.user_id = 0
     self.effective_ids = set()
     self.user_view = None
     self.user_pb = user_pb2.MakeUser(0)
     self.email = None
Esempio n. 11
0
    def _DeserializeUsersByID(self, user_rows, actionlimit_rows,
                              dismissedcue_rows):
        """Convert database row tuples into User PBs.

    Args:
      user_rows: rows from the User DB table.
      actionlimit_rows: rows from the ActionLimit DB table.
      dismissedcue_rows: rows from the DismissedCues DB table.

    Returns:
      A dict {user_id: user_pb} for all the users referenced in user_rows.
    """
        result_dict = {}

        # Make one User PB for each row in user_rows.
        for row in user_rows:
            (user_id, email, is_site_admin, notify_issue_change,
             notify_starred_issue_change, email_compact_subject,
             email_view_widget, notify_starred_ping, banned,
             after_issue_update, keep_people_perms_open, preview_on_hover,
             ignore_action_limits, obscure_email, last_visit_timestamp,
             email_bounce_timestamp, vacation_message) = row
            user = user_pb2.MakeUser(user_id,
                                     email=email,
                                     obscure_email=obscure_email)
            user.is_site_admin = bool(is_site_admin)
            user.notify_issue_change = bool(notify_issue_change)
            user.notify_starred_issue_change = bool(
                notify_starred_issue_change)
            user.email_compact_subject = bool(email_compact_subject)
            user.email_view_widget = bool(email_view_widget)
            user.notify_starred_ping = bool(notify_starred_ping)
            if banned:
                user.banned = banned
            if after_issue_update:
                user.after_issue_update = user_pb2.IssueUpdateNav(
                    after_issue_update.upper())
            user.keep_people_perms_open = bool(keep_people_perms_open)
            user.preview_on_hover = bool(preview_on_hover)
            user.ignore_action_limits = bool(ignore_action_limits)
            user.last_visit_timestamp = last_visit_timestamp or 0
            user.email_bounce_timestamp = email_bounce_timestamp or 0
            if vacation_message:
                user.vacation_message = vacation_message
            result_dict[user_id] = user

        # Make an ActionLimit for each actionlimit row and attach it to a User PB.
        for row in actionlimit_rows:
            (user_id, action_type_name, recent_count, reset_timestamp,
             lifetime_count, lifetime_limit, period_soft_limit,
             period_hard_limit) = row
            if user_id not in result_dict:
                logging.error('Found action limits for missing user %r',
                              user_id)
                continue
            user = result_dict[user_id]
            action_type = actionlimit.ACTION_TYPE_NAMES[action_type_name]
            al = actionlimit.GetLimitPB(user, action_type)
            al.recent_count = recent_count
            al.reset_timestamp = reset_timestamp
            al.lifetime_count = lifetime_count
            al.lifetime_limit = lifetime_limit
            al.period_soft_limit = period_soft_limit
            al.period_hard_limit = period_hard_limit

        # Build up a list of dismissed "cue card" help items for the users.
        for user_id, cue in dismissedcue_rows:
            if user_id not in result_dict:
                logging.error('Found dismissed cues for missing user %r',
                              user_id)
                continue
            result_dict[user_id].dismissed_cues.append(cue)

        return result_dict
Esempio n. 12
0
 def __init__(self):
     self.user_pb = user_pb2.MakeUser(111)
     self.effective_ids = set([1, 2, 3])
     self.user_id = 111
     self.email = '*****@*****.**'
Esempio n. 13
0
def StuffUserView(user_id, email, obscure_email):
  """Construct a UserView with the given parameters for testing."""
  user = user_pb2.MakeUser(user_id, email=email, obscure_email=obscure_email)
  return UserView(user)
Esempio n. 14
0
    def createAndAssertUpdates(self,
                               project_ids=None,
                               user_ids=None,
                               ascending=True):
        user = user_pb2.MakeUser(self.user_id)
        comment_1 = tracker_pb2.IssueComment(id=self.comment_id,
                                             issue_id=self.issue_id,
                                             project_id=self.project_id,
                                             user_id=self.user_id,
                                             content='this is the 1st comment',
                                             timestamp=self.comment_timestamp)
        self.mox.StubOutWithMock(self.services.issue, 'GetComments')

        created_order = 'created'
        field = 'project_id' if project_ids else 'commenter_id'
        where_clauses = [('Issue.id = Comment.issue_id', [])]
        if project_ids:
            where_clauses.append(('Comment.project_id IN (%s)', project_ids))
        if user_ids:
            where_clauses.append(('Comment.commenter_id IN (%s)', user_ids))
        if ascending:
            where_clauses.append(('created > %s', [self.mr_after]))
        else:
            created_order += ' DESC'
        self.services.issue.GetComments(
            mox.IgnoreArg(),
            deleted_by=None,
            joins=[('Issue', [])],
            limit=activities.UPDATES_PER_PAGE + 1,
            order_by=[(created_order, [])],
            use_clause='USE INDEX (%s) USE INDEX FOR ORDER BY (%s)' %
            (field, field),
            where=where_clauses).AndReturn([comment_1])

        self.mox.StubOutWithMock(framework_views, 'MakeAllUserViews')
        framework_views.MakeAllUserViews(mox.IgnoreArg(), self.services.user,
                                         [self.user_id],
                                         []).AndReturn({self.user_id: user})

        self.mox.ReplayAll()

        mr = testing_helpers.MakeMonorailRequest()
        if ascending:
            mr.after = self.mr_after

        updates_page_url = 'testing/testing'
        updates_data = activities.GatherUpdatesData(
            self.services,
            mr,
            profiler.Profiler(),
            project_ids=project_ids,
            user_ids=user_ids,
            ending=None,
            autolink=None,
            highlight='highlightme',
            updates_page_url=updates_page_url)
        self.mox.VerifyAll()

        if mr.after:
            pagination = updates_data['pagination']
            self.assertIsNone(pagination.last)
            self.assertEquals(
                '%s?before=%d' %
                (updates_page_url.split('/')[-1], self.comment_timestamp),
                pagination.next_url)
            self.assertEquals(
                '%s?after=%d' %
                (updates_page_url.split('/')[-1], self.comment_timestamp),
                pagination.prev_url)

        activity_view = updates_data['updates_data'].older[0]
        self.assertEqual(
            '<a class="ot-issue-link"\n \n '
            'href="/p//issues/detail?id=%s#c_ts%s"\n >'
            'issue %s</a>\n\n()\n\n\n\n\n \n commented on' %
            (self.issue_local_id, self.comment_timestamp, self.issue_local_id),
            activity_view.escaped_title)
        self.assertEqual(
            '<span class="ot-issue-comment">\n this is the 1st comment\n</span>',
            activity_view.escaped_body)
        self.assertEqual('highlightme', activity_view.highlight)
        self.assertEqual(self.project_name, activity_view.project_name)
Esempio n. 15
0
 def __init__(self):
     self.user_pb = user_pb2.MakeUser(111)
     self.effective_ids = set([1, 2, 3])
     self.user_id = 111
Esempio n. 16
0
    def _DeserializeUsersByID(self, user_rows, dismissedcue_rows,
                              linkedaccount_rows):
        """Convert database row tuples into User PBs.

    Args:
      user_rows: rows from the User DB table.
      dismissedcue_rows: rows from the DismissedCues DB table.
      linkedaccount_rows: rows from the LinkedAccount DB table.

    Returns:
      A dict {user_id: user_pb} for all the users referenced in user_rows.
    """
        result_dict = {}

        # Make one User PB for each row in user_rows.
        for row in user_rows:
            (user_id, email, is_site_admin, notify_issue_change,
             notify_starred_issue_change, email_compact_subject,
             email_view_widget, notify_starred_ping, banned,
             after_issue_update, keep_people_perms_open, preview_on_hover,
             obscure_email, last_visit_timestamp, email_bounce_timestamp,
             vacation_message) = row
            user = user_pb2.MakeUser(user_id,
                                     email=email,
                                     obscure_email=obscure_email)
            user.is_site_admin = bool(is_site_admin)
            user.notify_issue_change = bool(notify_issue_change)
            user.notify_starred_issue_change = bool(
                notify_starred_issue_change)
            user.email_compact_subject = bool(email_compact_subject)
            user.email_view_widget = bool(email_view_widget)
            user.notify_starred_ping = bool(notify_starred_ping)
            if banned:
                user.banned = banned
            if after_issue_update:
                user.after_issue_update = user_pb2.IssueUpdateNav(
                    after_issue_update.upper())
            user.keep_people_perms_open = bool(keep_people_perms_open)
            user.preview_on_hover = bool(preview_on_hover)
            user.last_visit_timestamp = last_visit_timestamp or 0
            user.email_bounce_timestamp = email_bounce_timestamp or 0
            if vacation_message:
                user.vacation_message = vacation_message
            result_dict[user_id] = user

        # Build up a list of dismissed "cue card" help items for the users.
        for user_id, cue in dismissedcue_rows:
            if user_id not in result_dict:
                logging.error('Found dismissed cues for missing user %r',
                              user_id)
                continue
            result_dict[user_id].dismissed_cues.append(cue)

        # Put in any linked accounts.
        for parent_id, child_id in linkedaccount_rows:
            if parent_id in result_dict:
                result_dict[parent_id].linked_child_ids.append(child_id)
            if child_id in result_dict:
                result_dict[child_id].linked_parent_id = parent_id

        return result_dict
Esempio n. 17
0
 def testGroupMemberView(self):
     user = user_pb2.MakeUser(1, email='*****@*****.**')
     gmv = group_helpers.GroupMemberView(user, 888, 'member')
     self.assertEqual(888, gmv.group_id)
     self.assertEqual('member', gmv.role)
Esempio n. 18
0
 def testIsExempt_RegularUser(self):
     author = user_pb2.MakeUser(111, email='*****@*****.**')
     self.assertFalse(self.spam_service._IsExempt(author, False))
     author = user_pb2.MakeUser(111, email='*****@*****.**')
     self.assertFalse(self.spam_service._IsExempt(author, False))
Esempio n. 19
0
 def testIsExempt_ProjectMember(self):
     author = user_pb2.MakeUser(111, email='*****@*****.**')
     self.assertTrue(self.spam_service._IsExempt(author, True))
Esempio n. 20
0
    def createAndAssertUpdates(self,
                               project_ids=None,
                               user_ids=None,
                               ascending=True):
        user = user_pb2.MakeUser(self.user_id)
        comment_1 = tracker_pb2.IssueComment(id=self.comment_id,
                                             issue_id=self.issue_id,
                                             project_id=self.project_id,
                                             user_id=self.user_id,
                                             content='this is the 1st comment',
                                             timestamp=self.comment_timestamp)
        self.mox.StubOutWithMock(self.services.issue, 'GetIssueActivity')

        after = 0
        if ascending:
            after = self.mr_after
        self.services.issue.GetIssueActivity(mox.IgnoreArg(),
                                             num=50,
                                             before=0,
                                             after=after,
                                             project_ids=project_ids,
                                             user_ids=user_ids,
                                             ascending=ascending).AndReturn(
                                                 [comment_1])

        self.mox.StubOutWithMock(framework_views, 'MakeAllUserViews')
        framework_views.MakeAllUserViews(mox.IgnoreArg(), self.services.user,
                                         [self.user_id],
                                         []).AndReturn({self.user_id: user})

        self.mox.ReplayAll()

        mr = testing_helpers.MakeMonorailRequest()
        if ascending:
            mr.after = self.mr_after

        updates_page_url = 'testing/testing'
        updates_data = activities.GatherUpdatesData(
            self.services,
            mr,
            project_ids=project_ids,
            user_ids=user_ids,
            ending=None,
            autolink=None,
            highlight='highlightme',
            updates_page_url=updates_page_url)
        self.mox.VerifyAll()

        if mr.after:
            pagination = updates_data['pagination']
            self.assertIsNone(pagination.last)
            self.assertEquals(
                '%s?before=%d' %
                (updates_page_url.split('/')[-1], self.comment_timestamp),
                pagination.next_url)
            self.assertEquals(
                '%s?after=%d' %
                (updates_page_url.split('/')[-1], self.comment_timestamp),
                pagination.prev_url)

        activity_view = updates_data['updates_data'].older[0]
        self.assertEqual(
            '<a class="ot-issue-link"\n \n '
            'href="/p//issues/detail?id=%s#c_ts%s"\n >'
            'issue %s</a>\n\n()\n\n\n\n\n \n commented on' %
            (self.issue_local_id, self.comment_timestamp, self.issue_local_id),
            activity_view.escaped_title)
        self.assertEqual(
            '<span class="ot-issue-comment">\n this is the 1st comment\n</span>',
            activity_view.escaped_body)
        self.assertEqual('highlightme', activity_view.highlight)
        self.assertEqual(self.project_name, activity_view.project_name)