~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
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for lp.bugs.model.Bug."""

__metaclass__ = type

from lazr.lifecycle.snapshot import Snapshot
from zope.component import getUtility
from zope.interface import providedBy
from zope.security.proxy import removeSecurityProxy

from canonical.testing.layers import DatabaseFunctionalLayer

from lp.bugs.enum import BugNotificationLevel
from lp.bugs.interfaces.bug import(
    CreateBugParams,
    IBugSet,
    )
from lp.bugs.interfaces.bugtask import (
    BugTaskImportance,
    BugTaskStatus,
    UserCannotEditBugTaskAssignee,
    UserCannotEditBugTaskImportance,
    UserCannotEditBugTaskMilestone,
    )
from lp.testing import (
    person_logged_in,
    StormStatementRecorder,
    TestCaseWithFactory,
    )


class TestBugSubscriptionMethods(TestCaseWithFactory):
    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestBugSubscriptionMethods, self).setUp()
        self.bug = self.factory.makeBug()
        self.person = self.factory.makePerson()

    def test_is_muted_returns_true_for_muted_users(self):
        # Bug.isMuted() will return True if the person passed to it is muted.
        with person_logged_in(self.person):
            self.bug.mute(self.person, self.person)
            self.assertEqual(True, self.bug.isMuted(self.person))

    def test_is_muted_returns_false_for_direct_subscribers(self):
        # Bug.isMuted() will return False if the user has a
        # regular subscription.
        with person_logged_in(self.person):
            self.bug.subscribe(
                self.person, self.person, level=BugNotificationLevel.METADATA)
            self.assertEqual(False, self.bug.isMuted(self.person))

    def test_is_muted_returns_false_for_non_subscribers(self):
        # Bug.isMuted() will return False if the user has no
        # subscription.
        with person_logged_in(self.person):
            self.assertEqual(False, self.bug.isMuted(self.person))

    def test_mute_team_fails(self):
        # Muting a subscription for an entire team doesn't work.
        with person_logged_in(self.person):
            team = self.factory.makeTeam(owner=self.person)
            self.assertRaises(AssertionError,
                              self.bug.mute, team, team)

    def test_mute_mutes_user(self):
        # Bug.mute() adds a BugMute record for the person passed to it.
        with person_logged_in(self.person):
            self.bug.mute(self.person, self.person)
            naked_bug = removeSecurityProxy(self.bug)
            bug_mute = naked_bug._getMutes(self.person).one()
            self.assertEqual(self.bug, bug_mute.bug)
            self.assertEqual(self.person, bug_mute.person)

    def test_mute_mutes_muter(self):
        # When exposed in the web API, the mute method regards the
        # first, `person` argument as optional, and the second
        # `muted_by` argument is supplied from the request.  In this
        # case, the person should be the muter.
        with person_logged_in(self.person):
            self.bug.mute(None, self.person)
            self.assertTrue(self.bug.isMuted(self.person))

    def test_mute_mutes_user_with_existing_subscription(self):
        # Bug.mute() will not touch the existing subscription.
        with person_logged_in(self.person):
            subscription = self.bug.subscribe(
                self.person, self.person,
                level=BugNotificationLevel.METADATA)
            self.bug.mute(self.person, self.person)
            self.assertTrue(self.bug.isMuted(self.person))
            self.assertEqual(
                BugNotificationLevel.METADATA,
                subscription.bug_notification_level)

    def test_unmute_unmutes_user(self):
        # Bug.unmute() will remove a muted subscription for the user
        # passed to it.
        with person_logged_in(self.person):
            self.bug.mute(self.person, self.person)
            self.assertTrue(self.bug.isMuted(self.person))
            self.bug.unmute(self.person, self.person)
            self.assertFalse(self.bug.isMuted(self.person))

    def test_unmute_returns_direct_subscription(self):
        # Bug.unmute() returns the previously muted direct subscription, if
        # any.
        with person_logged_in(self.person):
            self.bug.mute(self.person, self.person)
            self.assertEqual(True, self.bug.isMuted(self.person))
            self.assertEqual(None, self.bug.unmute(self.person, self.person))
            self.assertEqual(False, self.bug.isMuted(self.person))
            subscription = self.bug.subscribe(
                self.person, self.person,
                level=BugNotificationLevel.METADATA)
            self.bug.mute(self.person, self.person)
            self.assertEqual(True, self.bug.isMuted(self.person))
            self.assertEqual(
                subscription, self.bug.unmute(self.person, self.person))

    def test_unmute_mutes_unmuter(self):
        # When exposed in the web API, the unmute method regards the
        # first, `person` argument as optional, and the second
        # `unmuted_by` argument is supplied from the request.  In this
        # case, the person should be the muter.
        with person_logged_in(self.person):
            self.bug.mute(self.person, self.person)
            self.bug.unmute(None, self.person)
            self.assertFalse(self.bug.isMuted(self.person))


class TestBugSnapshotting(TestCaseWithFactory):
    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestBugSnapshotting, self).setUp()
        self.bug = self.factory.makeBug()
        self.person = self.factory.makePerson()

    def test_bug_snapshot_does_not_include_messages(self):
        # A snapshot of a bug does not include its messages or
        # attachments (which get the messages from the database).  If it
        # does, the webservice can become unusable if changes are made
        # to bugs with many comments, such as bug 1. See, for instance,
        # bug 744888.  This test is primarily to keep the problem from
        # slipping in again.  To do so, we resort to somewhat
        # extraordinary measures.  In addition to verifying that the
        # snapshot does not have the attributes that currently trigger
        # the problem, we also actually look at the SQL that is
        # generated by creating the snapshot.  With this, we can verify
        # that the Message table is not included.  This is ugly, but
        # this has a chance of fighting against future eager loading
        # optimizations that might trigger the problem again.
        with person_logged_in(self.person):
            with StormStatementRecorder() as recorder:
                Snapshot(self.bug, providing=providedBy(self.bug))
            sql_statements = recorder.statements
        # This uses "self" as a marker to show that the attribute does not
        # exist.  We do not use hasattr because it eats exceptions.
        #self.assertTrue(getattr(snapshot, 'messages', self) is self)
        #self.assertTrue(getattr(snapshot, 'attachments', self) is self)
        for sql in sql_statements:
            # We are going to be aggressive about looking for the problem in
            # the SQL.  We'll split the SQL up by whitespace, and then look
            # for strings that start with "message".  If that is too
            # aggressive in the future from some reason, please do adjust the
            # test appropriately.
            sql_tokens = sql.lower().split()
            self.assertEqual(
                [token for token in sql_tokens
                 if token.startswith('message')],
                [])
            self.assertEqual(
                [token for token in sql_tokens
                 if token.startswith('bugactivity')],
                [])


class TestBugCreation(TestCaseWithFactory):
    """Tests for bug creation."""

    layer = DatabaseFunctionalLayer

    def test_CreateBugParams_accepts_importance(self):
        # The importance of the initial bug task can be set using
        # CreateBugParams
        owner = self.factory.makePerson()
        target = self.factory.makeProduct(owner=owner)
        with person_logged_in(owner):
            params = CreateBugParams(
                owner=owner, title="A bug", comment="Nothing important.",
                importance=BugTaskImportance.HIGH)
            params.setBugTarget(product=target)
            bug = getUtility(IBugSet).createBug(params)
            self.assertEqual(
                bug.default_bugtask.importance, params.importance)

    def test_CreateBugParams_accepts_assignee(self):
        # The assignee of the initial bug task can be set using
        # CreateBugParams
        owner = self.factory.makePerson()
        target = self.factory.makeProduct(owner=owner)
        with person_logged_in(owner):
            params = CreateBugParams(
                owner=owner, title="A bug", comment="Nothing important.",
                assignee=owner)
            params.setBugTarget(product=target)
            bug = getUtility(IBugSet).createBug(params)
            self.assertEqual(
                bug.default_bugtask.assignee, params.assignee)

    def test_CreateBugParams_accepts_milestone(self):
        # The milestone of the initial bug task can be set using
        # CreateBugParams
        owner = self.factory.makePerson()
        target = self.factory.makeProduct(owner=owner)
        with person_logged_in(owner):
            params = CreateBugParams(
                owner=owner, title="A bug", comment="Nothing important.",
                milestone=self.factory.makeMilestone(product=target))
            params.setBugTarget(product=target)
            bug = getUtility(IBugSet).createBug(params)
            self.assertEqual(
                bug.default_bugtask.milestone, params.milestone)

    def test_CreateBugParams_accepts_status(self):
        # The status of the initial bug task can be set using
        # CreateBugParams
        owner = self.factory.makePerson()
        target = self.factory.makeProduct(owner=owner)
        with person_logged_in(owner):
            params = CreateBugParams(
                owner=owner, title="A bug", comment="Nothing important.",
                status=BugTaskStatus.TRIAGED)
            params.setBugTarget(product=target)
            bug = getUtility(IBugSet).createBug(params)
            self.assertEqual(
                bug.default_bugtask.status, params.status)

    def test_CreateBugParams_rejects_not_allowed_importance_changes(self):
        # createBug() will reject any importance value passed by users
        # who don't have the right to set the importance.
        person = self.factory.makePerson()
        target = self.factory.makeProduct()
        with person_logged_in(person):
            params = CreateBugParams(
                owner=person, title="A bug", comment="Nothing important.",
                importance=BugTaskImportance.HIGH)
            params.setBugTarget(product=target)
            self.assertRaises(
                UserCannotEditBugTaskImportance,
                getUtility(IBugSet).createBug, params)

    def test_CreateBugParams_rejects_not_allowed_assignee_changes(self):
        # createBug() will reject any importance value passed by users
        # who don't have the right to set the assignee.
        person = self.factory.makePerson()
        person_2 = self.factory.makePerson()
        target = self.factory.makeProduct()
        # Setting the target's bug supervisor means that
        # canTransitionToAssignee() will return False for `person` if
        # another Person is passed as `assignee`.
        with person_logged_in(target.owner):
            target.setBugSupervisor(target.owner, target.owner)
        with person_logged_in(person):
            params = CreateBugParams(
                owner=person, title="A bug", comment="Nothing important.",
                assignee=person_2)
            params.setBugTarget(product=target)
            self.assertRaises(
                UserCannotEditBugTaskAssignee,
                getUtility(IBugSet).createBug, params)

    def test_CreateBugParams_rejects_not_allowed_milestone_changes(self):
        # createBug() will reject any importance value passed by users
        # who don't have the right to set the milestone.
        person = self.factory.makePerson()
        target = self.factory.makeProduct()
        with person_logged_in(person):
            params = CreateBugParams(
                owner=person, title="A bug", comment="Nothing important.",
                milestone=self.factory.makeMilestone(product=target))
            params.setBugTarget(product=target)
            self.assertRaises(
                UserCannotEditBugTaskMilestone,
                getUtility(IBugSet).createBug, params)