Groups2009 wikipage: Diary and plan update
- Changes from yesterday were committed.
Groups2009 code: group manager is created in the request initialization
Toggle line numbers
1 # HG changeset patch
2 # User Dmitrijs Milajevs <dimazest@gmail.com>
3 # Date 1243718077 -7200
4 # Node ID 6d6a6983079a96e717dd2e70e0a012c8fab60efa
5 # Parent c3e904a9c00bd4f31a14cba78267671b2fdc5e3e
6 Groups 2009: Moin.groups.GroupManager is created with a request initialization. This avoids repeated creation of the GroupManager.
7
8 --- a/MoinMoin/_tests/wikiconfig.py
9 +++ b/MoinMoin/_tests/wikiconfig.py
10 @@ -25,7 +25,7 @@
11 data_dir = os.path.join(_base_dir, "data")
12 data_underlay_dir = os.path.join(_base_dir, "underlay")
13
14 - group_manager = lambda self, request: GroupManager([])
15 + group_manager_init = lambda self, request: GroupManager([])
16
17 #show_hosts = 1
18
19 --- a/MoinMoin/conftest.py
20 +++ b/MoinMoin/conftest.py
21 @@ -31,7 +31,7 @@
22
23 from MoinMoin.support.python_compatibility import set
24 from MoinMoin.web.request import TestRequest, Client
25 -from MoinMoin.wsgiapp import Application, init
26 +from MoinMoin.wsgiapp import Application, init, init_group_manager
27 from MoinMoin._tests import maketestwiki, wikiconfig
28
29 coverage_modules = set()
30 @@ -72,6 +72,7 @@
31 request = TestRequest()
32 request.given_config = given_config
33 request = init(request)
34 + init_group_manager(request)
35 return request
36
37
38 @@ -115,4 +116,3 @@
39 if coverage is not None:
40 coverage_modules.update(getattr(self.obj, 'coverage_modules', []))
41 return super(Module, self).run(*args, **kwargs)
42 -
43 --- a/MoinMoin/groups/_tests/test_group_manager.py
44 +++ b/MoinMoin/groups/_tests/test_group_manager.py
45 @@ -12,6 +12,7 @@
46
47 from MoinMoin.groups import BackendManager, GroupManager
48
49 +
50 class TestGroupManagerAPI(object):
51 """
52 Performs test of the API of GroupManager.
53 @@ -19,41 +20,38 @@
54
55 from MoinMoin._tests import wikiconfig
56 class Config(wikiconfig.Config):
57 - pass
58 + admin_group = frozenset([u'Admin', u'JohnDoe'])
59 + editor_group = frozenset([u'MainEditor', u'JohnDoe'])
60 + fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
61
62 - def setup_class(self):
63 - self.admin_group = frozenset([u'Admin', u'JohnDoe'])
64 - self.editor_group = frozenset([u'MainEditor', u'JohnDoe'])
65 - self.fruit_group = frozenset([u'Apple', u'Banana', u'Cherry'])
66 + first_backend_groups = {u'AdminGroup': admin_group,
67 + u'EditorGroup': editor_group,
68 + u'FruitGroup': fruit_group}
69
70 - first_backend_groups = {u'AdminGroup': self.admin_group,
71 - u'EditorGroup': self.editor_group,
72 - u'FruitGroup': self.fruit_group}
73 -
74 - self.user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
75 - self.city_group = frozenset([u'Bolzano', u'Riga', u'London'])
76 + user_group = frozenset([u'JohnDoe', u'Bob', u'Joe'])
77 + city_group = frozenset([u'Bolzano', u'Riga', u'London'])
78 # Suppose, someone hacked second backend
79 # and added himself to AdminGroup
80 - self.second_admin_group = frozenset([u'TheHacker'])
81 + second_admin_group = frozenset([u'TheHacker'])
82
83 - second_backend_groups = {u'UserGroup': self.user_group,
84 - u'CityGroup': self.city_group,
85 + second_backend_groups = {u'UserGroup': user_group,
86 + u'CityGroup': city_group,
87 # Here group name clash occurs.
88 # AdminGroup is defined in both
89 # first_backend and second_backend.
90 - u'AdminGroup': self.second_admin_group}
91 -
92 - self.Config.group_manager = lambda self, request: GroupManager(backends=[BackendManager(request, first_backend_groups),
93 - BackendManager(request, second_backend_groups)])
94 + u'AdminGroup': second_admin_group}
95 + def group_manager_init(self, request):
96 + return GroupManager(backends=[BackendManager(request, self.first_backend_groups),
97 + BackendManager(request, self.second_backend_groups)])
98
99 def setup_method(self, method):
100 - self.group_manager = self.request.cfg.group_manager(self.request)
101 + self.group_manager = self.request.group_manager
102
103 def test_getitem(self):
104 """
105 Tests __getitem__ API method. It should return a group by its name.
106 """
107 - assert self.fruit_group == self.group_manager[u'FruitGroup']
108 + assert self.request.cfg.fruit_group == self.group_manager[u'FruitGroup']
109 raises(KeyError, lambda: self.group_manager[u'not existing group'])
110
111 def test_clashed_getitem(self):
112 @@ -65,7 +63,7 @@
113 """
114 admin_group = self.group_manager[u'AdminGroup']
115
116 - assert self.admin_group == admin_group
117 + assert self.request.cfg.admin_group == admin_group
118
119 # Nevertheless, TheHacker added himself to the second backend,
120 # it must not be taken into consideration, because AdminGroup is defined
121 --- a/MoinMoin/groups/_tests/test_group_manager_acl.py
122 +++ b/MoinMoin/groups/_tests/test_group_manager_acl.py
123 @@ -18,16 +18,14 @@
124
125 from MoinMoin._tests import wikiconfig
126 class Config(wikiconfig.Config):
127 - pass
128 + def group_manager_init(self, request):
129 + groups = {u'FirstGroup': frozenset([u"ExampleUser", u"SecondUser", u"JoeDoe"]),
130 + u'SecondGroup': frozenset([u"ExampleUser", u"ThirdUser"])}
131
132 - def setup_class(self):
133 - groups = {u'FirstGroup': frozenset([u"ExampleUser", u"SecondUser", u"JoeDoe"]),
134 - u'SecondGroup': frozenset([u"ExampleUser", u"ThirdUser"])}
135 -
136 - self.Config.group_manager = lambda self, request: GroupManager(backends=[BackendManager(request=request, backend=groups)])
137 + return GroupManager(backends=[BackendManager(request=request, backend=groups)])
138
139 def setup_method(self, method):
140 - self.group_manager = self.request.cfg.group_manager(self.request)
141 + self.group_manager = self.request.group_manager
142
143 def testConfigBackendAcl(self):
144 """
145 @@ -45,33 +43,5 @@
146 # AnotherUser has no read rights because he is not a member of group FirstGroup
147 assert not allow
148
149 - def testConfigBackend(self):
150 - """
151 - tests getting a group from the group manager, does group
152 - membership tests.
153 - """
154 - # define config groups
155 - groups = {'A': set(['a1', 'a2']),
156 - 'B': set(['b1', 'b2']),
157 - }
158 -
159 - # create config group manager backend object
160 - group_manager_backend = GroupManager(BackendManager(request=self.request,
161 - backend=[groups]))
162 -
163 - # check that a group named 'A' is available via the config backend
164 - assert 'A' in group_manager_backend
165 -
166 - # check that a group named 'C' is not available via the config backend
167 - assert 'C' not in group_manager_backend
168 -
169 - # get group object for a group named 'A'
170 - group_A = group_manager_backend['A']
171 -
172 - # check that a1 is a member of group A
173 - assert 'a1' in group_A
174 -
175 - # check that b1 is not a member of group A
176 - assert 'b1' not in group_A
177
178 coverage_modules = ['MoinMoin.groups']
179 --- a/MoinMoin/security/__init__.py
180 +++ b/MoinMoin/security/__init__.py
181 @@ -308,7 +308,7 @@
182 else: # we have a #acl on the page (self.acl can be [] if #acl is empty!)
183 acl = self.acl
184
185 - group_manager = request.cfg.group_manager(request)
186 + group_manager = request.group_manager
187
188 allowed = None
189 for entry, rightsdict in acl:
190 --- a/MoinMoin/wsgiapp.py
191 +++ b/MoinMoin/wsgiapp.py
192 @@ -43,6 +43,13 @@
193 context.clock.stop('init')
194 return context
195
196 +def init_group_manager(context):
197 + """
198 + Initialize group manager which provides access to group backends.
199 + """
200 + from MoinMoin.groups import GroupManager
201 + context.group_manager = context.cfg.group_manager_init(context)
202 +
203 def run(context):
204 """ Run a context trough the application. """
205 context.clock.start('run')
206 @@ -241,6 +248,7 @@
207 try:
208 request = self.Request(environ)
209 context = init(request)
210 + init_group_manager(context)
211 response = run(context)
212 context.clock.stop('total')
213 except HTTPException, e: