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
|
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for bug-branch linking from the bugs side."""
__metaclass__ = type
from zope.component import getUtility
from zope.security.interfaces import Unauthorized
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.bugs.interfaces.bugbranch import (
IBugBranch,
IBugBranchSet,
)
from lp.bugs.model.bugbranch import (
BugBranch,
BugBranchSet,
)
from lp.testing import (
anonymous_logged_in,
celebrity_logged_in,
TestCaseWithFactory,
)
class TestBugBranchSet(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_bugbranchset_provides_IBugBranchSet(self):
# BugBranchSet objects provide IBugBranchSet.
self.assertProvides(BugBranchSet(), IBugBranchSet)
def test_getBranchesWithVisibleBugs_no_branches(self):
bug_branches = getUtility(IBugBranchSet)
links = bug_branches.getBranchesWithVisibleBugs(
[], self.factory.makePerson())
self.assertEqual([], list(links))
def test_getBranchesWithVisibleBugs_finds_branches_with_public_bugs(self):
# IBugBranchSet.getBranchesWithVisibleBugs returns all of the
# Branch ids associated with the given branches that have bugs
# visible to the current user. Those trivially include ones
# for non-private bugs.
branch_1 = self.factory.makeBranch()
branch_2 = self.factory.makeBranch()
bug_a = self.factory.makeBug()
bug_b = self.factory.makeBug()
self.factory.loginAsAnyone()
bug_a.linkBranch(branch_1, self.factory.makePerson())
bug_a.linkBranch(branch_2, self.factory.makePerson())
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[branch_1.id, branch_2.id],
utility.getBranchesWithVisibleBugs(
[branch_1, branch_2], self.factory.makePerson()))
def test_getBranchesWithVisibleBugs_shows_public_bugs_to_anon(self):
# getBranchesWithVisibleBugs shows public bugs to anyone,
# including anonymous users.
branch = self.factory.makeBranch()
bug = self.factory.makeBug(private=False)
with celebrity_logged_in('admin'):
bug.linkBranch(branch, self.factory.makePerson())
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[branch.id], utility.getBranchesWithVisibleBugs([branch], None))
def test_getBranchesWithVisibleBugs_ignores_duplicate_bugbranches(self):
# getBranchesWithVisibleBugs reports a branch only once even if
# it's linked to the same bug multiple times.
branch = self.factory.makeBranch()
user = self.factory.makePerson()
bug = self.factory.makeBug()
self.factory.loginAsAnyone()
bug.linkBranch(branch, user)
bug.linkBranch(branch, user)
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[branch.id], utility.getBranchesWithVisibleBugs([branch], user))
def test_getBranchesWithVisibleBugs_ignores_extra_bugs(self):
# getBranchesWithVisibleBugs reports a branch only once even if
# it's liked to multiple bugs.
branch = self.factory.makeBranch()
user = self.factory.makePerson()
with celebrity_logged_in('admin'):
self.factory.makeBug().linkBranch(branch, user)
self.factory.makeBug().linkBranch(branch, user)
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[branch.id], utility.getBranchesWithVisibleBugs([branch], user))
def test_getBranchesWithVisibleBugs_hides_private_bugs_from_anon(self):
# getBranchesWithVisibleBugs does not show private bugs to users
# who aren't logged in.
branch = self.factory.makeBranch()
bug = self.factory.makeBug(private=True)
with celebrity_logged_in('admin'):
bug.linkBranch(branch, self.factory.makePerson())
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[], utility.getBranchesWithVisibleBugs([branch], None))
def test_getBranchesWithVisibleBugs_hides_private_bugs_from_joe(self):
# getBranchesWithVisibleBugs does not show private bugs to
# arbitrary logged-in users (such as Average Joe, or J. Random
# Hacker).
branch = self.factory.makeBranch()
bug = self.factory.makeBug(private=True)
with celebrity_logged_in('admin'):
bug.linkBranch(branch, self.factory.makePerson())
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[],
utility.getBranchesWithVisibleBugs(
[branch], self.factory.makePerson()))
def test_getBranchesWithVisibleBugs_shows_private_bugs_to_sub(self):
# getBranchesWithVisibleBugs will show private bugs to their
# subscribers.
branch = self.factory.makeBranch()
bug = self.factory.makeBug(private=True)
user = self.factory.makePerson()
with celebrity_logged_in('admin'):
bug.subscribe(user, self.factory.makePerson())
bug.linkBranch(branch, self.factory.makePerson())
utility = getUtility(IBugBranchSet)
self.assertContentEqual(
[branch.id], utility.getBranchesWithVisibleBugs([branch], user))
def test_getBugBranchesForBugTasks(self):
# IBugBranchSet.getBugBranchesForBugTasks returns all of the BugBranch
# objects associated with the given bug tasks.
bug_a = self.factory.makeBug()
bug_b = self.factory.makeBug()
bugtasks = bug_a.bugtasks + bug_b.bugtasks
branch = self.factory.makeBranch()
self.factory.loginAsAnyone()
link_1 = bug_a.linkBranch(branch, self.factory.makePerson())
link_2 = bug_b.linkBranch(branch, self.factory.makePerson())
found_links = getUtility(IBugBranchSet).getBugBranchesForBugTasks(
bugtasks)
self.assertEqual(set([link_1, link_2]), set(found_links))
class TestBugBranch(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestBugBranch, self).setUp()
# Bug branch linking is generally available to any logged in user.
self.factory.loginAsAnyone()
def test_bugbranch_provides_IBugBranch(self):
# BugBranch objects provide IBugBranch.
bug_branch = BugBranch(
branch=self.factory.makeBranch(), bug=self.factory.makeBug(),
registrant=self.factory.makePerson())
self.assertProvides(bug_branch, IBugBranch)
def test_linkBranch_returns_IBugBranch(self):
# Bug.linkBranch returns an IBugBranch linking the bug to the branch.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
registrant = self.factory.makePerson()
bug_branch = bug.linkBranch(branch, registrant)
self.assertEqual(branch, bug_branch.branch)
self.assertEqual(bug, bug_branch.bug)
self.assertEqual(registrant, bug_branch.registrant)
def test_bug_start_with_no_linked_branches(self):
# Bugs have a linked_branches attribute which is initially an empty
# collection.
bug = self.factory.makeBug()
self.assertEqual([], list(bug.linked_branches))
def test_linkBranch_adds_to_linked_branches(self):
# Bug.linkBranch populates the Bug.linked_branches with the created
# BugBranch object.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug_branch = bug.linkBranch(branch, self.factory.makePerson())
self.assertEqual([bug_branch], list(bug.linked_branches))
def test_linking_branch_twice_returns_same_IBugBranch(self):
# Calling Bug.linkBranch twice with the same parameters returns the
# same object.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug_branch = bug.linkBranch(branch, self.factory.makePerson())
bug_branch_2 = bug.linkBranch(branch, self.factory.makePerson())
self.assertEqual(bug_branch, bug_branch_2)
def test_linking_branch_twice_different_registrants(self):
# Calling Bug.linkBranch twice with the branch but different
# registrants returns the existing bug branch object rather than
# creating a new one.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug_branch = bug.linkBranch(branch, self.factory.makePerson())
bug_branch_2 = bug.linkBranch(branch, self.factory.makePerson())
self.assertEqual(bug_branch, bug_branch_2)
def test_bug_has_no_branches(self):
# Bug.hasBranch returns False for any branch that it is not linked to.
bug = self.factory.makeBug()
self.assertFalse(bug.hasBranch(self.factory.makeBranch()))
def test_bug_has_branch(self):
# Bug.hasBranch returns False for any branch that it is linked to.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug.linkBranch(branch, self.factory.makePerson())
self.assertTrue(bug.hasBranch(branch))
def test_unlink_branch(self):
# Bug.unlinkBranch removes the bug<->branch link.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug.linkBranch(branch, self.factory.makePerson())
bug.unlinkBranch(branch, self.factory.makePerson())
self.assertEqual([], list(bug.linked_branches))
self.assertFalse(bug.hasBranch(branch))
def test_unlink_not_linked_branch(self):
# When unlinkBranch is called with a branch that isn't already linked,
# nothing discernable happens.
bug = self.factory.makeBug()
branch = self.factory.makeBranch()
bug.unlinkBranch(branch, self.factory.makePerson())
self.assertEqual([], list(bug.linked_branches))
self.assertFalse(bug.hasBranch(branch))
def test_the_unwashed_cannot_link_branch_to_private_bug(self):
# Those who cannot see a bug are forbidden to link a branch to it.
bug = self.factory.makeBug(private=True)
self.assertRaises(Unauthorized, getattr, bug, 'linkBranch')
def test_the_unwashed_cannot_unlink_branch_from_private_bug(self):
# Those who cannot see a bug are forbidden to unlink branches from it.
bug = self.factory.makeBug(private=True)
self.assertRaises(Unauthorized, getattr, bug, 'unlinkBranch')
def test_anonymous_users_cannot_link_branches(self):
# Anonymous users cannot link branches to bugs, even public bugs.
bug = self.factory.makeBug()
with anonymous_logged_in():
self.assertRaises(Unauthorized, getattr, bug, 'linkBranch')
def test_anonymous_users_cannot_unlink_branches(self):
# Anonymous users cannot unlink branches from bugs, even public bugs.
bug = self.factory.makeBug()
with anonymous_logged_in():
self.assertRaises(Unauthorized, getattr, bug, 'unlinkBranch')
def test_adding_branch_changes_date_last_updated(self):
# Adding a branch to a bug changes IBug.date_last_updated.
bug = self.factory.makeBug()
last_updated = bug.date_last_updated
branch = self.factory.makeBranch()
self.factory.loginAsAnyone()
bug.linkBranch(branch, self.factory.makePerson())
self.assertTrue(bug.date_last_updated > last_updated)
|