Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unittests for monorail.feature.activities.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 12 | try: |
| 13 | from mox3 import mox |
| 14 | except ImportError: |
| 15 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | |
| 17 | from features import activities |
| 18 | from framework import framework_views |
| 19 | from framework import profiler |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 20 | from mrproto import tracker_pb2 |
| 21 | from mrproto import user_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | from services import service_manager |
| 23 | from testing import fake |
| 24 | from testing import testing_helpers |
| 25 | |
| 26 | |
| 27 | class ActivitiesTest(unittest.TestCase): |
| 28 | |
| 29 | def setUp(self): |
| 30 | self.services = service_manager.Services( |
| 31 | config=fake.ConfigService(), |
| 32 | issue=fake.IssueService(), |
| 33 | user=fake.UserService(), |
| 34 | usergroup=fake.UserGroupService(), |
| 35 | project=fake.ProjectService(), |
| 36 | ) |
| 37 | |
| 38 | self.project_name = 'proj' |
| 39 | self.project_id = 987 |
| 40 | self.project = self.services.project.TestAddProject( |
| 41 | self.project_name, project_id=self.project_id, |
| 42 | process_inbound_email=True) |
| 43 | |
| 44 | self.issue_id = 11 |
| 45 | self.issue_local_id = 100 |
| 46 | self.issue = tracker_pb2.Issue() |
| 47 | self.issue.issue_id = self.issue_id |
| 48 | self.issue.project_id = self.project_id |
| 49 | self.issue.local_id = self.issue_local_id |
| 50 | self.services.issue.TestAddIssue(self.issue) |
| 51 | |
| 52 | self.comment_id = 123 |
| 53 | self.comment_timestamp = 120 |
| 54 | self.user = self.services.user.TestAddUser('testuser@example.com', 2) |
| 55 | self.user_id = self.user.user_id |
| 56 | self.mr_after = 1234 |
| 57 | |
| 58 | self.mox = mox.Mox() |
| 59 | |
| 60 | def tearDown(self): |
| 61 | self.mox.UnsetStubs() |
| 62 | self.mox.ResetAll() |
| 63 | |
| 64 | def testActivities_NoUpdates(self): |
| 65 | mr = testing_helpers.MakeMonorailRequest() |
| 66 | updates_data = activities.GatherUpdatesData( |
| 67 | self.services, mr, project_ids=[256], |
| 68 | user_ids=None, ending=None, updates_page_url=None, autolink=None, |
| 69 | highlight=None) |
| 70 | |
| 71 | self.assertIsNone(updates_data['pagination']) |
| 72 | self.assertIsNone(updates_data['no_stars']) |
| 73 | self.assertIsNone(updates_data['updates_data']) |
| 74 | self.assertEqual('yes', updates_data['no_activities']) |
| 75 | self.assertIsNone(updates_data['ending_type']) |
| 76 | |
| 77 | def createAndAssertUpdates(self, project_ids=None, user_ids=None, |
| 78 | ascending=True): |
| 79 | comment_1 = tracker_pb2.IssueComment( |
| 80 | id=self.comment_id, issue_id=self.issue_id, |
| 81 | project_id=self.project_id, user_id=self.user_id, |
| 82 | content='this is the 1st comment', |
| 83 | timestamp=self.comment_timestamp) |
| 84 | self.mox.StubOutWithMock(self.services.issue, 'GetIssueActivity') |
| 85 | |
| 86 | after = 0 |
| 87 | if ascending: |
| 88 | after = self.mr_after |
| 89 | self.services.issue.GetIssueActivity( |
| 90 | mox.IgnoreArg(), num=50, before=0, after=after, project_ids=project_ids, |
| 91 | user_ids=user_ids, ascending=ascending).AndReturn([comment_1]) |
| 92 | |
| 93 | self.mox.ReplayAll() |
| 94 | |
| 95 | mr = testing_helpers.MakeMonorailRequest() |
| 96 | if ascending: |
| 97 | mr.after = self.mr_after |
| 98 | |
| 99 | updates_page_url='testing/testing' |
| 100 | updates_data = activities.GatherUpdatesData( |
| 101 | self.services, mr, project_ids=project_ids, |
| 102 | user_ids=user_ids, ending=None, autolink=None, |
| 103 | highlight='highlightme', updates_page_url=updates_page_url) |
| 104 | self.mox.VerifyAll() |
| 105 | |
| 106 | if mr.after: |
| 107 | pagination = updates_data['pagination'] |
| 108 | self.assertIsNone(pagination.last) |
| 109 | self.assertEqual( |
| 110 | '%s?before=%d' % |
| 111 | (updates_page_url.split('/')[-1], self.comment_timestamp), |
| 112 | pagination.next_url) |
| 113 | self.assertEqual( |
| 114 | '%s?after=%d' % |
| 115 | (updates_page_url.split('/')[-1], self.comment_timestamp), |
| 116 | pagination.prev_url) |
| 117 | |
| 118 | activity_view = updates_data['updates_data'].older[0] |
| 119 | self.assertEqual( |
| 120 | '<a class="ot-issue-link"\n \n ' |
| 121 | 'href="/p//issues/detail?id=%s#c_ts%s"\n >' |
| 122 | 'issue %s</a>\n\n()\n\n\n\n\n \n commented on' % ( |
| 123 | self.issue_local_id, self.comment_timestamp, self.issue_local_id), |
| 124 | activity_view.escaped_title) |
| 125 | self.assertEqual( |
| 126 | '<span class="ot-issue-comment">\n this is the 1st comment\n</span>', |
| 127 | activity_view.escaped_body) |
| 128 | self.assertEqual('highlightme', activity_view.highlight) |
| 129 | self.assertEqual(self.project_name, activity_view.project_name) |
| 130 | |
| 131 | def testActivities_AscendingProjectUpdates(self): |
| 132 | self.createAndAssertUpdates(project_ids=[self.project_id], ascending=True) |
| 133 | |
| 134 | def testActivities_DescendingProjectUpdates(self): |
| 135 | self.createAndAssertUpdates(project_ids=[self.project_id], ascending=False) |
| 136 | |
| 137 | def testActivities_AscendingUserUpdates(self): |
| 138 | self.createAndAssertUpdates(user_ids=[self.user_id], ascending=True) |
| 139 | |
| 140 | def testActivities_DescendingUserUpdates(self): |
| 141 | self.createAndAssertUpdates(user_ids=[self.user_id], ascending=False) |
| 142 | |
| 143 | def testActivities_SpecifyProjectAndUser(self): |
| 144 | self.createAndAssertUpdates( |
| 145 | project_ids=[self.project_id], user_ids=[self.user_id], ascending=False) |