~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for the bugcomment module."""

__metaclass__ = type

from datetime import (
    datetime,
    timedelta,
    )
from itertools import count

from pytz import utc
from soupmatchers import (
    HTMLContains,
    Tag,
    )
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.bugs.browser.bugcomment import group_comments_with_activity
from lp.coop.answersbugs.visibility import (
    TestHideMessageControlMixin,
    TestMessageVisibilityMixin,
    )
from lp.services.features.testing import FeatureFixture
from lp.testing import (
    BrowserTestCase,
    celebrity_logged_in,
    login_person,
    person_logged_in,
    TestCase,
    )
from lp.testing.layers import DatabaseFunctionalLayer
from lp.testing.pages import find_tag_by_id


class BugActivityStub:

    def __init__(self, datechanged, owner=None):
        self.datechanged = datechanged
        if owner is None:
            owner = PersonStub()
        self.person = owner

    def __repr__(self):
        return "BugActivityStub(%r, %r)" % (
            self.datechanged.strftime('%Y-%m-%d--%H%M'), self.person)


class BugCommentStub:

    def __init__(self, datecreated, index, owner=None):
        self.datecreated = datecreated
        if owner is None:
            owner = PersonStub()
        self.owner = owner
        self.activity = []
        self.index = index

    def __repr__(self):
        return "BugCommentStub(%r, %d, %r)" % (
            self.datecreated.strftime('%Y-%m-%d--%H%M'),
            self.index, self.owner)


class PersonStub:

    ids = count(1)

    def __init__(self):
        self.id = next(self.ids)

    def __repr__(self):
        return "PersonStub#%d" % self.id


class TestGroupCommentsWithActivities(TestCase):
    """Tests for `group_comments_with_activities`."""

    def setUp(self):
        super(TestGroupCommentsWithActivities, self).setUp()
        self.now = datetime.now(utc)
        self.time_index = (
            (self.now + timedelta(minutes=counter), counter)
            for counter in count(1))

    def group(self, comments, activities):
        return list(
            group_comments_with_activity(
                comments=comments, activities=activities))

    def test_empty(self):
        # Given no comments or activities the result is also empty.
        self.assertEqual(
            [], self.group(comments=[], activities=[]))

    def test_activity_empty_no_common_actor(self):
        # When no activities are passed in, and the comments passed in don't
        # have any common actors, no grouping is possible.
        comments = [
            BugCommentStub(*next(self.time_index))
            for number in xrange(5)]
        self.assertEqual(
            comments, self.group(comments=comments, activities=[]))

    def test_comments_empty_no_common_actor(self):
        # When no comments are passed in, and the activities passed in don't
        # have any common actors, no grouping is possible.
        activities = [
            BugActivityStub(next(self.time_index)[0])
            for number in xrange(5)]
        self.assertEqual(
            [[activity] for activity in activities], self.group(
                comments=[], activities=activities))

    def test_no_common_actor(self):
        # When each activities and comment given has a different actor then no
        # grouping is possible.
        activity1 = BugActivityStub(next(self.time_index)[0])
        comment1 = BugCommentStub(*next(self.time_index))
        activity2 = BugActivityStub(next(self.time_index)[0])
        comment2 = BugCommentStub(*next(self.time_index))

        activities = set([activity1, activity2])
        comments = list([comment1, comment2])

        self.assertEqual(
            [[activity1], comment1, [activity2], comment2],
            self.group(comments=comments, activities=activities))

    def test_comment_then_activity_close_by_common_actor(self):
        # An activity shortly after a comment by the same person is grouped
        # into the comment.
        actor = PersonStub()
        comment = BugCommentStub(*next(self.time_index), owner=actor)
        activity = BugActivityStub(next(self.time_index)[0], owner=actor)
        grouped = self.group(comments=[comment], activities=[activity])
        self.assertEqual([comment], grouped)
        self.assertEqual([activity], comment.activity)

    def test_activity_then_comment_close_by_common_actor(self):
        # An activity shortly before a comment by the same person is grouped
        # into the comment.
        actor = PersonStub()
        activity = BugActivityStub(next(self.time_index)[0], owner=actor)
        comment = BugCommentStub(*next(self.time_index), owner=actor)
        grouped = self.group(comments=[comment], activities=[activity])
        self.assertEqual([comment], grouped)
        self.assertEqual([activity], comment.activity)

    def test_interleaved_activity_with_comment_by_common_actor(self):
        # Activities shortly before and after a comment are grouped into the
        # comment's activity.
        actor = PersonStub()
        activity1 = BugActivityStub(next(self.time_index)[0], owner=actor)
        comment = BugCommentStub(*next(self.time_index), owner=actor)
        activity2 = BugActivityStub(next(self.time_index)[0], owner=actor)
        grouped = self.group(
            comments=[comment], activities=[activity1, activity2])
        self.assertEqual([comment], grouped)
        self.assertEqual([activity1, activity2], comment.activity)

    def test_common_actor_over_a_prolonged_time(self):
        # There is a timeframe for grouping events, 5 minutes by default.
        # Anything outside of that window is considered separate.
        actor = PersonStub()
        activities = [
            BugActivityStub(next(self.time_index)[0], owner=actor)
            for count in xrange(8)]
        grouped = self.group(comments=[], activities=activities)
        self.assertEqual(2, len(grouped))
        self.assertEqual(activities[:5], grouped[0])
        self.assertEqual(activities[5:], grouped[1])

    def test_two_comments_by_common_actor(self):
        # Only one comment will ever appear in a group.
        actor = PersonStub()
        comment1 = BugCommentStub(*next(self.time_index), owner=actor)
        comment2 = BugCommentStub(*next(self.time_index), owner=actor)
        grouped = self.group(comments=[comment1, comment2], activities=[])
        self.assertEqual([comment1, comment2], grouped)

    def test_two_comments_with_activity_by_common_actor(self):
        # Activity gets associated with earlier comment when all other factors
        # are unchanging.
        actor = PersonStub()
        activity1 = BugActivityStub(next(self.time_index)[0], owner=actor)
        comment1 = BugCommentStub(*next(self.time_index), owner=actor)
        activity2 = BugActivityStub(next(self.time_index)[0], owner=actor)
        comment2 = BugCommentStub(*next(self.time_index), owner=actor)
        activity3 = BugActivityStub(next(self.time_index)[0], owner=actor)
        grouped = self.group(
            comments=[comment1, comment2],
            activities=[activity1, activity2, activity3])
        self.assertEqual([comment1, comment2], grouped)
        self.assertEqual([activity1, activity2], comment1.activity)
        self.assertEqual([activity3], comment2.activity)


class TestBugCommentVisibility(
        BrowserTestCase, TestMessageVisibilityMixin):

    layer = DatabaseFunctionalLayer

    def makeHiddenMessage(self):
        """Required by the mixin."""
        with celebrity_logged_in('admin'):
            bug = self.factory.makeBug()
            comment = self.factory.makeBugComment(
                    bug=bug, body=self.comment_text)
            comment.visible = False
        return bug

    def getView(self, context, user=None, no_login=False):
        """Required by the mixin."""
        view = self.getViewBrowser(
            context=context.default_bugtask,
            user=user,
            no_login=no_login)
        return view


class TestBugHideCommentControls(
        BrowserTestCase, TestHideMessageControlMixin):

    layer = DatabaseFunctionalLayer

    feature_flag = {'disclosure.users_hide_own_bug_comments.enabled': 'on'}

    def getContext(self, comment_owner=None):
        """Required by the mixin."""
        administrator = getUtility(ILaunchpadCelebrities).admin.teamowner
        bug = self.factory.makeBug()
        with person_logged_in(administrator):
            self.factory.makeBugComment(bug=bug, owner=comment_owner)
        return bug

    def getView(self, context, user=None, no_login=False):
        """Required by the mixin."""
        view = self.getViewBrowser(
            context=context.default_bugtask,
            user=user,
            no_login=no_login)
        return view

    def _test_hide_link_visible(self, context, user):
        view = self.getView(context=context, user=user)
        hide_link = find_tag_by_id(view.contents, self.control_text)
        self.assertIs(None, hide_link)
        with FeatureFixture(self.feature_flag):
            view = self.getView(context=context, user=user)
            hide_link = find_tag_by_id(view.contents, self.control_text)
            self.assertIsNot(None, hide_link)

    def test_comment_owner_sees_hide_control(self):
        # The comment owner sees the hide control.
        owner = self.factory.makePerson()
        context = self.getContext(comment_owner=owner)
        self._test_hide_link_visible(context, owner)

    def test_pillar_owner_sees_hide_control(self):
        # The pillar owner sees the hide control.
        person = self.factory.makePerson()
        context = self.getContext()
        naked_bugtask = removeSecurityProxy(context.default_bugtask)
        removeSecurityProxy(naked_bugtask.pillar).owner = person
        self._test_hide_link_visible(context, person)

    def test_pillar_driver_sees_hide_control(self):
        # The pillar driver sees the hide control.
        person = self.factory.makePerson()
        context = self.getContext()
        naked_bugtask = removeSecurityProxy(context.default_bugtask)
        removeSecurityProxy(naked_bugtask.pillar).driver = person
        self._test_hide_link_visible(context, person)

    def test_pillar_bug_supervisor_sees_hide_control(self):
        # The pillar bug supervisor sees the hide control.
        person = self.factory.makePerson()
        context = self.getContext()
        naked_bugtask = removeSecurityProxy(context.default_bugtask)
        removeSecurityProxy(naked_bugtask.pillar).bug_supervisor = person
        self._test_hide_link_visible(context, person)

    def test_pillar_security_contact_sees_hide_control(self):
        # The pillar security contact sees the hide control.
        person = self.factory.makePerson()
        context = self.getContext()
        naked_bugtask = removeSecurityProxy(context.default_bugtask)
        removeSecurityProxy(naked_bugtask.pillar).security_contact = person
        self._test_hide_link_visible(context, person)


class TestBugCommentMicroformats(BrowserTestCase):

    layer = DatabaseFunctionalLayer

    def test_bug_comment_metadata(self):
        owner = self.factory.makePerson()
        login_person(owner)
        bug_comment = self.factory.makeBugComment()
        browser = self.getViewBrowser(bug_comment)
        iso_date = bug_comment.datecreated.isoformat()
        self.assertThat(
            browser.contents,
            HTMLContains(Tag(
                'comment time tag',
                'time',
                attrs=dict(
                    itemprop='commentTime',
                    title=True,
                    datetime=iso_date))))