Merge lp:~lifeless/launchpad/features into lp:launchpad

Proposed by Robert Collins
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 11863
Proposed branch: lp:~lifeless/launchpad/features
Merge into: lp:launchpad
Diff against target: 147 lines (+65/-4)
5 files modified
lib/lp/registry/interfaces/person.py (+5/-2)
lib/lp/registry/model/person.py (+3/-0)
lib/lp/registry/tests/test_person.py (+7/-0)
lib/lp/services/features/tests/test_webapp.py (+25/-2)
lib/lp/services/features/webapp.py (+25/-0)
To merge this branch: bzr merge lp:~lifeless/launchpad/features
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) code Approve
Review via email: mp+40049@code.launchpad.net

Commit message

Add a team: scope.

Description of the change

Not much to say. Team based scope. \o/. Drive by fix in inTeam to handle nonexistant teams more gracefully. (We could raise, but the existing handling for e.g. teams that are Persons is to return False, so I followed that style, for now).

To post a comment you must log in.
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

As discussed on IRC. Note that existing behaviour is for teams that are Persons is not actually to return False as opposed to raising an exception; it is to return False if the "team" is not the same person as self (which is the only circumstance under which there could be a membership).

Since you made a change in the interface where a mistake that would previously raise an error will now be quietly accepted, you're adding a test and documentation for the change.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/registry/interfaces/person.py'
2--- lib/lp/registry/interfaces/person.py 2010-11-04 00:18:34 +0000
3+++ lib/lp/registry/interfaces/person.py 2010-11-04 07:27:52 +0000
4@@ -1074,8 +1074,11 @@
5 since it inherits from `IPerson`) is a member of himself
6 (i.e. `person1.inTeam(person1)`).
7
8- :param team: An object providing `IPerson`, the name of a
9- team, or `None` (in which case `False` is returned).
10+ :param team: One of an object providing `IPerson`, the string name of a
11+ team or `None`. If a string was supplied the team is looked up.
12+ :return: A bool with the result of the membership lookup. When looking
13+ up the team from a string finds nothing or team was `None` then
14+ `False` is returned.
15 """
16
17 def clearInTeamCache():
18
19=== modified file 'lib/lp/registry/model/person.py'
20--- lib/lp/registry/model/person.py 2010-11-03 20:25:27 +0000
21+++ lib/lp/registry/model/person.py 2010-11-04 07:27:52 +0000
22@@ -1191,6 +1191,9 @@
23 # Translate the team name to an ITeam if we were passed a team.
24 if isinstance(team, (str, unicode)):
25 team = PersonSet().getByName(team)
26+ if team is None:
27+ # No team, no membership.
28+ return False
29
30 if self.id == team.id:
31 # A team is always a member of itself.
32
33=== modified file 'lib/lp/registry/tests/test_person.py'
34--- lib/lp/registry/tests/test_person.py 2010-11-03 13:58:34 +0000
35+++ lib/lp/registry/tests/test_person.py 2010-11-04 07:27:52 +0000
36@@ -220,6 +220,13 @@
37 {},
38 removeSecurityProxy(self.user)._inTeam_cache)
39
40+ def test_inTeam_person_string_missing_team(self):
41+ # If a check against a string is done, the team lookup is implicit:
42+ # treat a missing team as an empty team so that any pages that choose
43+ # to do this don't blow up unnecessarily. Similarly feature flags
44+ # team: scopes depend on this.
45+ self.assertFalse(self.user.inTeam('does-not-exist'))
46+
47
48 class TestPerson(TestCaseWithFactory):
49
50
51=== modified file 'lib/lp/services/features/tests/test_webapp.py'
52--- lib/lp/services/features/tests/test_webapp.py 2010-09-13 00:55:15 +0000
53+++ lib/lp/services/features/tests/test_webapp.py 2010-11-04 07:27:52 +0000
54@@ -7,7 +7,11 @@
55
56 from canonical.testing import layers
57 from lp.services.features import webapp
58-from lp.testing import TestCase
59+from lp.testing import (
60+ login_as,
61+ TestCase,
62+ TestCaseWithFactory,
63+ )
64 from canonical.launchpad.webapp.servers import LaunchpadTestRequest
65
66
67@@ -38,4 +42,23 @@
68 scopes = webapp.ScopesFromRequest(request)
69 self.assertTrue(scopes.lookup('pageid:'))
70 self.assertFalse(scopes.lookup('pageid:foo'))
71- self.assertFalse(scopes.lookup('pageid:foo'))
72+ self.assertFalse(scopes.lookup('pageid:foo:bar'))
73+
74+
75+class TestDBScopes(TestCaseWithFactory):
76+
77+ layer = layers.DatabaseFunctionalLayer
78+
79+ def test_team_scope_outside_team(self):
80+ request = LaunchpadTestRequest()
81+ scopes = webapp.ScopesFromRequest(request)
82+ self.factory.loginAsAnyone()
83+ self.assertFalse(scopes.lookup('team:nonexistent'))
84+
85+ def test_team_scope_in_team(self):
86+ request = LaunchpadTestRequest()
87+ scopes = webapp.ScopesFromRequest(request)
88+ member = self.factory.makePerson()
89+ team = self.factory.makeTeam(members=[member])
90+ login_as(member)
91+ self.assertTrue(scopes.lookup('team:%s' % team.name))
92
93=== modified file 'lib/lp/services/features/webapp.py'
94--- lib/lp/services/features/webapp.py 2010-10-25 17:29:07 +0000
95+++ lib/lp/services/features/webapp.py 2010-11-04 07:27:52 +0000
96@@ -7,7 +7,10 @@
97
98 __metaclass__ = type
99
100+from zope.component import getUtility
101+
102 import canonical.config
103+from canonical.launchpad.webapp.interfaces import ILaunchBag
104 from lp.services.features import per_thread
105 from lp.services.features.flags import FeatureController
106 from lp.services.features.rulesource import StormFeatureRuleSource
107@@ -34,11 +37,16 @@
108 - pageid:SomeType
109 - pageid:SomeType:+view
110 - pageid:SomeType:+view#subselector
111+ - team:
112+ This scope looks up a team. For instance
113+ - team:launchpad-beta-users
114 """
115 if scope_name == 'default':
116 return True
117 if scope_name.startswith('pageid:'):
118 return self._lookup_pageid(scope_name[len('pageid:'):])
119+ if scope_name.startswith('team:'):
120+ return self._lookup_team(scope_name[len('team:'):])
121 parts = scope_name.split('.')
122 if len(parts) == 2:
123 if parts[0] == 'server':
124@@ -65,6 +73,23 @@
125 return False
126 return True
127
128+ def _lookup_team(self, team_name):
129+ """Lookup a team membership as a scope.
130+
131+ This will do a two queries, so we probably want to keep the number of
132+ team based scopes in use to a small number. (Person.inTeam could be
133+ fixed to reduce this to one query).
134+
135+ teamid scopes are written as 'team:' + the team name to match.
136+
137+ E.g. the scope 'team:launchpad-beta-users' will match members of
138+ the team 'launchpad-beta-users'.
139+ """
140+ person = getUtility(ILaunchBag).user
141+ if person is None:
142+ return False
143+ return person.inTeam(team_name)
144+
145 def _pageid_to_namespace(self, pageid):
146 """Return a list of namespace elements for pageid."""
147 # Normalise delimiters.