Merge lp:~stevenk/launchpad/shift-ap-creation into lp:launchpad

Proposed by Steve Kowalik
Status: Merged
Approved by: Steve Kowalik
Approved revision: no longer in the source branch.
Merged at revision: 15890
Proposed branch: lp:~stevenk/launchpad/shift-ap-creation
Merge into: lp:launchpad
Diff against target: 140 lines (+30/-30)
3 files modified
lib/lp/registry/model/product.py (+18/-20)
lib/lp/registry/tests/test_product.py (+9/-10)
lib/lp/testing/factory.py (+3/-0)
To merge this branch: bzr merge lp:~stevenk/launchpad/shift-ap-creation
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+121985@code.launchpad.net

Commit message

No longer create AccessPolicies in IProductSet.createProduct(), do it in IProduct.setB{ranch,ug}SharingPolicy().

Description of the change

Currently, when a new product is created, createProduct() will create PRIVATESECURITY and USERDATA AccessPolicies. This does not make sense if the product has its branch and bug sharing policies set to PROPRIETARY since the two that were created will just be cleaned up by a garbo job.

As such, stop creating them in createProduct(), and ensure they exist in setB{ranch,ug}SharingPolicy(). I have also refactored those two methods so that all of the heavy lifting is done by a generic method.

I have deleted a test that no longer makes sense, since creating a product itself won't create APs.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

+ def _check_and_set_sharing_policy(self, var, enum, kind, allowed_types):

It doesn't actually set the sharing policy. It just prepares for it.

27 + if var != enum.PUBLIC:
28 + if not self.has_current_commercial_subscription:

This can be a single if statement now.

208 + return self.makeProduct(skip_sharing_policy=True, **kwargs)

Privacy relies on the APs existing, so you can't set skip_sharing_policy unconditionally here. I think it'd be better to have the one or two tests that need it remove the APs themselves, rather than preventing them from being created in the first place.

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/model/product.py'
2--- lib/lp/registry/model/product.py 2012-08-29 03:19:38 +0000
3+++ lib/lp/registry/model/product.py 2012-08-30 23:47:20 +0000
4@@ -107,6 +107,9 @@
5 )
6 from lp.code.enums import BranchType
7 from lp.code.interfaces.branch import DEFAULT_BRANCH_STATUS_IN_LISTING
8+from lp.code.model.branchnamespace import (
9+ POLICY_ALLOWED_TYPES as BRANCH_POLICY_ALLOWED_TYPES,
10+ )
11 from lp.code.model.branchvisibilitypolicy import BranchVisibilityPolicyMixin
12 from lp.code.model.hasbranches import (
13 HasBranchesMixin,
14@@ -119,6 +122,7 @@
15 BranchSharingPolicy,
16 BugSharingPolicy,
17 InformationType,
18+ PRIVATE_INFORMATION_TYPES,
19 )
20 from lp.registry.errors import CommercialSubscribersOnly
21 from lp.registry.interfaces.accesspolicy import (
22@@ -560,28 +564,26 @@
23 self.checkPrivateBugsTransitionAllowed(private_bugs, user)
24 self.private_bugs = private_bugs
25
26+ def _prepare_to_set_sharing_policy(self, var, enum, kind, allowed_types):
27+ if var != enum.PUBLIC and not self.has_current_commercial_subscription:
28+ raise CommercialSubscribersOnly(
29+ "A current commercial subscription is required to use "
30+ "proprietary %s." % kind)
31+ required_policies = set(allowed_types[var]).intersection(
32+ set(PRIVATE_INFORMATION_TYPES))
33+ self._ensurePolicies(required_policies)
34+
35 def setBranchSharingPolicy(self, branch_sharing_policy):
36 """See `IProductEditRestricted`."""
37- if branch_sharing_policy != BranchSharingPolicy.PUBLIC:
38- if not self.has_current_commercial_subscription:
39- raise CommercialSubscribersOnly(
40- "A current commercial subscription is required to use "
41- "proprietary branches.")
42- required_policies = [InformationType.PROPRIETARY]
43- if (branch_sharing_policy ==
44- BranchSharingPolicy.EMBARGOED_OR_PROPRIETARY):
45- required_policies.append(InformationType.EMBARGOED)
46- self._ensurePolicies(required_policies)
47+ self._prepare_to_set_sharing_policy(
48+ branch_sharing_policy, BranchSharingPolicy, 'branches',
49+ BRANCH_POLICY_ALLOWED_TYPES)
50 self.branch_sharing_policy = branch_sharing_policy
51
52 def setBugSharingPolicy(self, bug_sharing_policy):
53 """See `IProductEditRestricted`."""
54- if bug_sharing_policy != BugSharingPolicy.PUBLIC:
55- if not self.has_current_commercial_subscription:
56- raise CommercialSubscribersOnly(
57- "A current commercial subscription is required to use "
58- "proprietary bugs.")
59- self._ensurePolicies([InformationType.PROPRIETARY])
60+ self._prepare_to_set_sharing_policy(
61+ bug_sharing_policy, BugSharingPolicy, 'bugs', POLICY_ALLOWED_TYPES)
62 self.bug_sharing_policy = bug_sharing_policy
63
64 def getAllowedBugInformationTypes(self):
65@@ -1574,10 +1576,6 @@
66 'rather than a stable release branch. This is sometimes also '
67 'called MAIN or HEAD.'))
68 product.development_focus = trunk
69-
70- # Add default AccessPolicies.
71- product._ensurePolicies((InformationType.USERDATA,
72- InformationType.PRIVATESECURITY))
73 return product
74
75 def forReview(self, search_text=None, active=None,
76
77=== modified file 'lib/lp/registry/tests/test_product.py'
78--- lib/lp/registry/tests/test_product.py 2012-08-29 03:19:38 +0000
79+++ lib/lp/registry/tests/test_product.py 2012-08-30 23:47:20 +0000
80@@ -342,14 +342,6 @@
81 product.setPrivateBugs(True, bug_supervisor)
82 self.assertTrue(product.private_bugs)
83
84- def test_product_creation_creates_accesspolicies(self):
85- # Creating a new product also creates AccessPolicies for it.
86- product = self.factory.makeProduct()
87- ap = getUtility(IAccessPolicySource).findByPillar((product,))
88- expected = [
89- InformationType.USERDATA, InformationType.PRIVATESECURITY]
90- self.assertContentEqual(expected, [policy.type for policy in ap])
91-
92 def test_product_creation_grants_maintainer_access(self):
93 # Creating a new product creates an access grant for the maintainer
94 # for all default policies.
95@@ -364,8 +356,8 @@
96 self.assertEqual(expected_grantess, grantees)
97
98 def test_open_product_creation_sharing_policies(self):
99- # Creating a new open (non-proprietary) product sets the bug and branch
100- # sharing polices to public.
101+ # Creating a new open (non-proprietary) product sets the bug and
102+ # branch sharing polices to public, and creates policies if required.
103 owner = self.factory.makePerson()
104 with person_logged_in(owner):
105 product = getUtility(IProductSet).createProduct(
106@@ -374,6 +366,10 @@
107 self.assertEqual(BugSharingPolicy.PUBLIC, product.bug_sharing_policy)
108 self.assertEqual(
109 BranchSharingPolicy.PUBLIC, product.branch_sharing_policy)
110+ aps = getUtility(IAccessPolicySource).findByPillar([product])
111+ expected = [
112+ InformationType.USERDATA, InformationType.PRIVATESECURITY]
113+ self.assertContentEqual(expected, [policy.type for policy in aps])
114
115 def test_proprietary_product_creation_sharing_policies(self):
116 # Creating a new proprietary product sets the bug and branch sharing
117@@ -387,6 +383,9 @@
118 BugSharingPolicy.PROPRIETARY, product.bug_sharing_policy)
119 self.assertEqual(
120 BranchSharingPolicy.PROPRIETARY, product.branch_sharing_policy)
121+ aps = getUtility(IAccessPolicySource).findByPillar([product])
122+ expected = [InformationType.PROPRIETARY]
123+ self.assertContentEqual(expected, [policy.type for policy in aps])
124
125
126 class TestProductBugInformationTypes(TestCaseWithFactory):
127
128=== modified file 'lib/lp/testing/factory.py'
129--- lib/lp/testing/factory.py 2012-08-30 11:48:26 +0000
130+++ lib/lp/testing/factory.py 2012-08-30 23:47:20 +0000
131@@ -1017,6 +1017,9 @@
132 # migrated.
133 # XXX This method can be removed when branch visibility policy dies.
134 product = self.makeProduct(**kwargs)
135+ # Since createProduct() doesn't create PRIVATESECURITY/USERDATA.
136+ removeSecurityProxy(product)._ensurePolicies([
137+ InformationType.PRIVATESECURITY, InformationType.USERDATA])
138 removeSecurityProxy(product).bug_sharing_policy = None
139 removeSecurityProxy(product).branch_sharing_policy = None
140 return product