Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

factor out addroutes function for use by tests rather than repeating,…

… use pyramid's built-in sessioning rather than beaker, use a sessionauthenticationpolicy rather than authtkt
  • Loading branch information...
commit 513e57cbea9576e3e1000992191ec71d53649acd 1 parent 5cc54b5
Chris McDonough authored
8  README.rst
Source Rendered
@@ -4,15 +4,15 @@ shootout
4 4
 Shootout is a demo app for the Pyramid web framework.  The concepts
5 5
 demonstrated in the code include:
6 6
 
7  
-- Urldispatch mechanism (similar to routes).
  7
+- Url dispatch mechanism.
8 8
 
9 9
 - Built-in authentication and authorization mechanism.
10 10
 
11  
-- Beaker encrypted sessions using pyramid_beaker.
  11
+- Usage of built-in sessioning machinery.
12 12
 
13 13
 - Integration with pyramid_simpleform for form handling.
14 14
 
15  
-- SQLAlchemy based models.
  15
+- SQLAlchemy based models and transaction management via pyramid_tm.
16 16
 
17 17
 Library Requirements
18 18
 --------------------
@@ -24,6 +24,8 @@ On a Debian system, these imply: build-essentials, libsqlite3-dev.
24 24
 Installing and Running
25 25
 ----------------------
26 26
 
  27
+Python 2.6 or 2.7 is required.
  28
+
27 29
 - virtualenv --no-site-packages env
28 30
 
29 31
 - cd env
18  development.ini
... ...
@@ -1,19 +1,15 @@
1 1
 [app:main]
2 2
 use = egg:shootout
3 3
 
4  
-reload_templates = true
5  
-debug_authorization = false
6  
-debug_notfound = false
7  
-debug_routematch = false
8  
-debug_templates = true
9  
-default_locale_name = en
10  
-session.type = cookie
11  
-session.key = shootout
12  
-session.encrypt_key = mysecretencryptionkey
13  
-session.validate_key = mysecretvalidationkey
14  
-session.cookie_on_exception = true
  4
+pyramid.reload_templates = true
  5
+pyramid.debug_authorization = false
  6
+pyramid.debug_notfound = false
  7
+pyramid.debug_routematch = false
  8
+pyramid.debug_templates = true
  9
+pyramid.default_locale_name = en
15 10
 pyramid.includes = pyramid_debugtoolbar
16 11
                    pyramid_tm
  12
+session.secret = ^5$309^vJ-*
17 13
 sqlalchemy.url = sqlite:///%(here)s/shootout.db
18 14
 
19 15
 [server:main]
14  production.ini
... ...
@@ -1,14 +1,14 @@
1 1
 [app:main]
2 2
 use = egg:shootout
3  
-reload_templates = false
4  
-debug_authorization = false
5  
-debug_notfound = false
6  
-debug_routematch = false
7  
-debug_templates = true
8  
-default_locale_name = en
  3
+pyramid.reload_templates = false
  4
+pyramid.debug_authorization = false
  5
+pyramid.debug_notfound = false
  6
+pyramid.debug_routematch = false
  7
+pyramid.debug_templates = true
  8
+pyramid.default_locale_name = en
9 9
 pyramid.includes = pyramid_tm
10 10
                    pyramid_exclog
11  
-
  11
+session.secret = ^5$309^vJ-*
12 12
 sqlalchemy.url = sqlite:///%(here)s/shootout.db
13 13
 
14 14
 [server:main]
1  setup.py
@@ -19,7 +19,6 @@
19 19
     'pyramid_simpleform',
20 20
     'cryptacular',
21 21
     'waitress',
22  
-    'pyramid_beaker',
23 22
     'pycrypto',
24 23
     ]
25 24
 
34  shootout/__init__.py
... ...
@@ -1,13 +1,14 @@
1 1
 from pyramid.config import Configurator
2  
-from pyramid.authentication import AuthTktAuthenticationPolicy
  2
+from pyramid.authentication import SessionAuthenticationPolicy
3 3
 from pyramid.authorization import ACLAuthorizationPolicy
4  
-
5  
-from pyramid_beaker import session_factory_from_settings
  4
+from pyramid.session import UnencryptedCookieSessionFactoryConfig
6 5
 
7 6
 from sqlalchemy import engine_from_config
8 7
 
9  
-from .models import DBSession
10  
-
  8
+from .models import (
  9
+    DBSession,
  10
+    RootFactory,
  11
+    )
11 12
 
12 13
 def main(global_config, **settings):  # pragma: no cover
13 14
     """ This function returns a Pyramid WSGI application.
@@ -15,20 +16,29 @@ def main(global_config, **settings):  # pragma: no cover
15 16
     engine = engine_from_config(settings, 'sqlalchemy.')
16 17
     DBSession.configure(bind=engine)
17 18
 
18  
-    session_factory = session_factory_from_settings(settings)
  19
+    session_factory = UnencryptedCookieSessionFactoryConfig(
  20
+        settings['session.secret']
  21
+        )
19 22
 
20  
-    authn_policy = AuthTktAuthenticationPolicy('s0secret')
  23
+    authn_policy = SessionAuthenticationPolicy()
21 24
     authz_policy = ACLAuthorizationPolicy()
22 25
 
23 26
     config = Configurator(
24 27
         settings=settings,
25  
-        root_factory='shootout.models.RootFactory',
  28
+        root_factory=RootFactory,
26 29
         authentication_policy=authn_policy,
27 30
         authorization_policy=authz_policy,
28 31
         session_factory=session_factory
29  
-    )
  32
+        )
30 33
 
31 34
     config.add_static_view('static', 'shootout:static')
  35
+    config.include(addroutes)
  36
+    config.scan()
  37
+
  38
+    return config.make_wsgi_app()
  39
+
  40
+def addroutes(config):
  41
+    # broken out of main() so it can be used by unit tests
32 42
     config.add_route('idea', '/ideas/{idea_id}')
33 43
     config.add_route('user', '/users/{username}')
34 44
     config.add_route('tag', '/tags/{tag_name}')
@@ -39,8 +49,4 @@ def main(global_config, **settings):  # pragma: no cover
39 49
     config.add_route('logout', '/logout')
40 50
     config.add_route('about', '/about')
41 51
     config.add_route('main', '/')
42  
-    config.scan()
43  
-
44  
-    return config.make_wsgi_app()
45  
-
46  
-
  52
+    
4  shootout/tests/test_models.py
@@ -4,7 +4,7 @@
4 4
 from pyramid import testing
5 5
 
6 6
 
7  
-def _initTestingDB():
  7
+def init_db():
8 8
     from shootout.models import (
9 9
         DBSession,
10 10
         Base,
@@ -20,7 +20,7 @@ def _initTestingDB():
20 20
 
21 21
 class ModelsTestCase(unittest.TestCase):
22 22
     def setUp(self):
23  
-        self.session = _initTestingDB()
  23
+        self.session = init_db()
24 24
 
25 25
     def tearDown(self):
26 26
         import transaction
67  shootout/tests/test_views.py
@@ -3,7 +3,7 @@
3 3
 from pyramid import testing
4 4
 
5 5
 
6  
-def _initTestingDB():
  6
+def init_db():
7 7
     from shootout.models import DBSession
8 8
     from shootout.models import Base
9 9
     from sqlalchemy import create_engine
@@ -14,30 +14,15 @@ def _initTestingDB():
14 14
     session = DBSession()
15 15
     return session
16 16
 
17  
-
18  
-def _registerRoutes(config):
19  
-    config.add_route('idea', '/ideas/{idea_id}')
20  
-    config.add_route('user', '/users/{username}')
21  
-    config.add_route('tag', '/tags/{tag_name}')
22  
-    config.add_route('idea_add', '/idea_add')
23  
-    config.add_route('idea_vote', '/idea_vote')
24  
-    config.add_route('register', '/register')
25  
-    config.add_route('login', '/login')
26  
-    config.add_route('logout', '/logout')
27  
-    config.add_route('about', '/about')
28  
-    config.add_route('main', '/')
29  
-
30  
-
31  
-def _registerCommonTemplates(config):
  17
+def register_templates(config):
32 18
     config.testing_add_renderer('templates/login.pt')
33 19
     config.testing_add_renderer('templates/toolbar.pt')
34 20
     config.testing_add_renderer('templates/cloud.pt')
35 21
     config.testing_add_renderer('templates/latest.pt')
36 22
 
37  
-
38 23
 class ViewTests(unittest.TestCase):
39 24
     def setUp(self):
40  
-        self.session = _initTestingDB()
  25
+        self.session = init_db()
41 26
         self.config = testing.setUp()
42 27
 
43 28
     def tearDown(self):
@@ -68,7 +53,7 @@ def _addIdea(self, target=None, user=None):
68 53
     def test_main_view(self):
69 54
         from shootout.views import main_view
70 55
         self.config.testing_securitypolicy(u'username')
71  
-        _registerCommonTemplates(self.config)
  56
+        self.config.include(register_templates)
72 57
         request = testing.DummyRequest()
73 58
         result = main_view(request)
74 59
         self.assertEqual(result['username'], u'username')
@@ -77,7 +62,7 @@ def test_main_view(self):
77 62
     def test_idea_add_nosubmit_idea(self):
78 63
         from shootout.views import idea_add
79 64
         self.config.testing_securitypolicy(u'username')
80  
-        _registerCommonTemplates(self.config)
  65
+        self.config.include(register_templates)
81 66
         request = testing.DummyRequest()
82 67
         result = idea_add(request)
83 68
         self.assertEqual(result['target'], None)
@@ -86,7 +71,7 @@ def test_idea_add_nosubmit_idea(self):
86 71
     def test_idea_add_nosubmit_comment(self):
87 72
         from shootout.views import idea_add
88 73
         self.config.testing_securitypolicy(u'username')
89  
-        _registerCommonTemplates(self.config)
  74
+        self.config.include(register_templates)
90 75
         idea = self._addIdea()
91 76
         request = testing.DummyRequest(params={'target': idea.idea_id})
92 77
         result = idea_add(request)
@@ -96,7 +81,7 @@ def test_idea_add_nosubmit_comment(self):
96 81
     def test_idea_add_not_existing_target(self):
97 82
         from shootout.views import idea_add
98 83
         self.config.testing_securitypolicy(u'username')
99  
-        _registerCommonTemplates(self.config)
  84
+        self.config.include(register_templates)
100 85
         request = testing.DummyRequest(params={'target': 100})
101 86
         result = idea_add(request)
102 87
         self.assertEqual(result.code, 404)
@@ -104,8 +89,8 @@ def test_idea_add_not_existing_target(self):
104 89
     def test_idea_add_submit_schema_fail_empty_params(self):
105 90
         from shootout.views import idea_add
106 91
         self.config.testing_securitypolicy(u'username')
107  
-        _registerCommonTemplates(self.config)
108  
-        _registerRoutes(self.config)
  92
+        self.config.include(register_templates)
  93
+        self.config.include('shootout.addroutes')
109 94
         request = testing.DummyRequest(post={'form.submitted': 'Shoot'})
110 95
         result = idea_add(request)
111 96
         self.assertEqual(
@@ -121,7 +106,7 @@ def test_idea_add_submit_schema_succeed(self):
121 106
         from shootout.views import idea_add
122 107
         from shootout.models import Idea
123 108
         self.config.testing_securitypolicy(u'username')
124  
-        _registerRoutes(self.config)
  109
+        self.config.include('shootout.addroutes')
125 110
         request = testing.DummyRequest(
126 111
             post={
127 112
                 'form.submitted': u'Shoot',
@@ -148,7 +133,7 @@ def test_idea_add_submit_schema_succeed(self):
148 133
     def test_vote_on_own_idea(self):
149 134
         from shootout.views import idea_vote
150 135
         from shootout.models import User
151  
-        _registerRoutes(self.config)
  136
+        self.config.include('shootout.addroutes')
152 137
         idea = self._addIdea()
153 138
         self.session.query(User).one()
154 139
         self.assertEqual(idea.user_voted(u'username'), False)
@@ -170,7 +155,7 @@ def test_vote_on_own_idea(self):
170 155
 
171 156
     def test_positive_idea_voting(self):
172 157
         from shootout.views import idea_vote
173  
-        _registerRoutes(self.config)
  158
+        self.config.include('shootout.addroutes')
174 159
         user = self._addUser()
175 160
         idea = self._addIdea(user=user)
176 161
         voter = self._addUser(u'votername')
@@ -194,7 +179,7 @@ def test_positive_idea_voting(self):
194 179
 
195 180
     def test_negative_idea_voting(self):
196 181
         from shootout.views import idea_vote
197  
-        _registerRoutes(self.config)
  182
+        self.config.include('shootout.addroutes')
198 183
         user = self._addUser()
199 184
         idea = self._addIdea(user=user)
200 185
         voter = self._addUser(u'votername')
@@ -218,14 +203,14 @@ def test_negative_idea_voting(self):
218 203
 
219 204
     def test_registration_nosubmit(self):
220 205
         from shootout.views import user_add
221  
-        _registerCommonTemplates(self.config)
  206
+        self.config.include(register_templates)
222 207
         request = testing.DummyRequest()
223 208
         result = user_add(request)
224 209
         self.assertTrue('form' in result)
225 210
 
226 211
     def test_registration_submit_empty(self):
227 212
         from shootout.views import user_add
228  
-        _registerCommonTemplates(self.config)
  213
+        self.config.include(register_templates)
229 214
         request = testing.DummyRequest()
230 215
         result = user_add(request)
231 216
         self.assertTrue('form' in result)
@@ -245,7 +230,7 @@ def test_registration_submit_empty(self):
245 230
     def test_registration_submit_schema_succeed(self):
246 231
         from shootout.views import user_add
247 232
         from shootout.models import User
248  
-        _registerRoutes(self.config)
  233
+        self.config.include('shootout.addroutes')
249 234
         request = testing.DummyRequest(
250 235
             post={
251 236
                 'form.submitted': u'Register',
@@ -273,8 +258,8 @@ def test_registration_submit_schema_succeed(self):
273 258
     def test_user_view(self):
274 259
         from shootout.views import user_view
275 260
         self.config.testing_securitypolicy(u'username')
276  
-        _registerRoutes(self.config)
277  
-        _registerCommonTemplates(self.config)
  261
+        self.config.include('shootout.addroutes')
  262
+        self.config.include(register_templates)
278 263
         request = testing.DummyRequest()
279 264
         request.matchdict = {'username': u'username'}
280 265
         self._addUser()
@@ -285,8 +270,8 @@ def test_user_view(self):
285 270
     def test_idea_view(self):
286 271
         from shootout.views import idea_view
287 272
         self.config.testing_securitypolicy(u'username')
288  
-        _registerRoutes(self.config)
289  
-        _registerCommonTemplates(self.config)
  273
+        self.config.include('shootout.addroutes')
  274
+        self.config.include(register_templates)
290 275
         self._addIdea()
291 276
         request = testing.DummyRequest()
292 277
         request.matchdict = {'idea_id': 1}
@@ -299,8 +284,8 @@ def test_tag_view(self):
299 284
         from shootout.views import tag_view
300 285
         from shootout.models import Tag
301 286
         self.config.testing_securitypolicy(u'username')
302  
-        _registerRoutes(self.config)
303  
-        _registerCommonTemplates(self.config)
  287
+        self.config.include('shootout.addroutes')
  288
+        self.config.include(register_templates)
304 289
         user = self._addUser()
305 290
         tag1 = Tag(u'bar')
306 291
         tag2 = Tag(u'foo')
@@ -329,13 +314,13 @@ def test_tag_view(self):
329 314
 
330 315
     def test_about_view(self):
331 316
         from shootout.views import about_view
332  
-        _registerCommonTemplates(self.config)
  317
+        self.config.include(register_templates)
333 318
         request = testing.DummyRequest()
334 319
         about_view(request)
335 320
 
336 321
     def test_login_view_submit_fail(self):
337 322
         from shootout.views import login_view
338  
-        _registerRoutes(self.config)
  323
+        self.config.include('shootout.addroutes')
339 324
         self._addUser()
340 325
         request = testing.DummyRequest(
341 326
             post={
@@ -350,7 +335,7 @@ def test_login_view_submit_fail(self):
350 335
 
351 336
     def test_login_view_submit_success(self):
352 337
         from shootout.views import login_view
353  
-        _registerRoutes(self.config)
  338
+        self.config.include('shootout.addroutes')
354 339
         self._addUser()
355 340
         request = testing.DummyRequest(
356 341
             post={
@@ -365,7 +350,7 @@ def test_login_view_submit_success(self):
365 350
 
366 351
     def test_logout_view(self):
367 352
         from shootout.views import logout_view
368  
-        _registerRoutes(self.config)
  353
+        self.config.include('shootout.addroutes')
369 354
         request = testing.DummyRequest()
370 355
         logout_view(request)
371 356
         messages = request.session.peek_flash()

0 notes on commit 513e57c

Please sign in to comment.
Something went wrong with that request. Please try again.