Permalink
Browse files

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...
1 parent 5cc54b5 commit 513e57cbea9576e3e1000992191ec71d53649acd @mcdonc mcdonc committed Feb 10, 2013
Showing with 67 additions and 79 deletions.
  1. +5 −3 README.rst
  2. +7 −11 development.ini
  3. +7 −7 production.ini
  4. +0 −1 setup.py
  5. +20 −14 shootout/__init__.py
  6. +2 −2 shootout/tests/test_models.py
  7. +26 −41 shootout/tests/test_views.py
View
@@ -4,15 +4,15 @@ shootout
Shootout is a demo app for the Pyramid web framework. The concepts
demonstrated in the code include:
-- Urldispatch mechanism (similar to routes).
+- Url dispatch mechanism.
- Built-in authentication and authorization mechanism.
-- Beaker encrypted sessions using pyramid_beaker.
+- Usage of built-in sessioning machinery.
- Integration with pyramid_simpleform for form handling.
-- SQLAlchemy based models.
+- SQLAlchemy based models and transaction management via pyramid_tm.
Library Requirements
--------------------
@@ -24,6 +24,8 @@ On a Debian system, these imply: build-essentials, libsqlite3-dev.
Installing and Running
----------------------
+Python 2.6 or 2.7 is required.
+
- virtualenv --no-site-packages env
- cd env
View
@@ -1,19 +1,15 @@
[app:main]
use = egg:shootout
-reload_templates = true
-debug_authorization = false
-debug_notfound = false
-debug_routematch = false
-debug_templates = true
-default_locale_name = en
-session.type = cookie
-session.key = shootout
-session.encrypt_key = mysecretencryptionkey
-session.validate_key = mysecretvalidationkey
-session.cookie_on_exception = true
+pyramid.reload_templates = true
+pyramid.debug_authorization = false
+pyramid.debug_notfound = false
+pyramid.debug_routematch = false
+pyramid.debug_templates = true
+pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar
pyramid_tm
+session.secret = ^5$309^vJ-*
sqlalchemy.url = sqlite:///%(here)s/shootout.db
[server:main]
View
@@ -1,14 +1,14 @@
[app:main]
use = egg:shootout
-reload_templates = false
-debug_authorization = false
-debug_notfound = false
-debug_routematch = false
-debug_templates = true
-default_locale_name = en
+pyramid.reload_templates = false
+pyramid.debug_authorization = false
+pyramid.debug_notfound = false
+pyramid.debug_routematch = false
+pyramid.debug_templates = true
+pyramid.default_locale_name = en
pyramid.includes = pyramid_tm
pyramid_exclog
-
+session.secret = ^5$309^vJ-*
sqlalchemy.url = sqlite:///%(here)s/shootout.db
[server:main]
View
@@ -19,7 +19,6 @@
'pyramid_simpleform',
'cryptacular',
'waitress',
- 'pyramid_beaker',
'pycrypto',
]
View
@@ -1,34 +1,44 @@
from pyramid.config import Configurator
-from pyramid.authentication import AuthTktAuthenticationPolicy
+from pyramid.authentication import SessionAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
-
-from pyramid_beaker import session_factory_from_settings
+from pyramid.session import UnencryptedCookieSessionFactoryConfig
from sqlalchemy import engine_from_config
-from .models import DBSession
-
+from .models import (
+ DBSession,
+ RootFactory,
+ )
def main(global_config, **settings): # pragma: no cover
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
- session_factory = session_factory_from_settings(settings)
+ session_factory = UnencryptedCookieSessionFactoryConfig(
+ settings['session.secret']
+ )
- authn_policy = AuthTktAuthenticationPolicy('s0secret')
+ authn_policy = SessionAuthenticationPolicy()
authz_policy = ACLAuthorizationPolicy()
config = Configurator(
settings=settings,
- root_factory='shootout.models.RootFactory',
+ root_factory=RootFactory,
authentication_policy=authn_policy,
authorization_policy=authz_policy,
session_factory=session_factory
- )
+ )
config.add_static_view('static', 'shootout:static')
+ config.include(addroutes)
+ config.scan()
+
+ return config.make_wsgi_app()
+
+def addroutes(config):
+ # broken out of main() so it can be used by unit tests
config.add_route('idea', '/ideas/{idea_id}')
config.add_route('user', '/users/{username}')
config.add_route('tag', '/tags/{tag_name}')
@@ -39,8 +49,4 @@ def main(global_config, **settings): # pragma: no cover
config.add_route('logout', '/logout')
config.add_route('about', '/about')
config.add_route('main', '/')
- config.scan()
-
- return config.make_wsgi_app()
-
-
+
@@ -4,7 +4,7 @@
from pyramid import testing
-def _initTestingDB():
+def init_db():
from shootout.models import (
DBSession,
Base,
@@ -20,7 +20,7 @@ def _initTestingDB():
class ModelsTestCase(unittest.TestCase):
def setUp(self):
- self.session = _initTestingDB()
+ self.session = init_db()
def tearDown(self):
import transaction
@@ -3,7 +3,7 @@
from pyramid import testing
-def _initTestingDB():
+def init_db():
from shootout.models import DBSession
from shootout.models import Base
from sqlalchemy import create_engine
@@ -14,30 +14,15 @@ def _initTestingDB():
session = DBSession()
return session
-
-def _registerRoutes(config):
- config.add_route('idea', '/ideas/{idea_id}')
- config.add_route('user', '/users/{username}')
- config.add_route('tag', '/tags/{tag_name}')
- config.add_route('idea_add', '/idea_add')
- config.add_route('idea_vote', '/idea_vote')
- config.add_route('register', '/register')
- config.add_route('login', '/login')
- config.add_route('logout', '/logout')
- config.add_route('about', '/about')
- config.add_route('main', '/')
-
-
-def _registerCommonTemplates(config):
+def register_templates(config):
config.testing_add_renderer('templates/login.pt')
config.testing_add_renderer('templates/toolbar.pt')
config.testing_add_renderer('templates/cloud.pt')
config.testing_add_renderer('templates/latest.pt')
-
class ViewTests(unittest.TestCase):
def setUp(self):
- self.session = _initTestingDB()
+ self.session = init_db()
self.config = testing.setUp()
def tearDown(self):
@@ -68,7 +53,7 @@ def _addIdea(self, target=None, user=None):
def test_main_view(self):
from shootout.views import main_view
self.config.testing_securitypolicy(u'username')
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest()
result = main_view(request)
self.assertEqual(result['username'], u'username')
@@ -77,7 +62,7 @@ def test_main_view(self):
def test_idea_add_nosubmit_idea(self):
from shootout.views import idea_add
self.config.testing_securitypolicy(u'username')
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest()
result = idea_add(request)
self.assertEqual(result['target'], None)
@@ -86,7 +71,7 @@ def test_idea_add_nosubmit_idea(self):
def test_idea_add_nosubmit_comment(self):
from shootout.views import idea_add
self.config.testing_securitypolicy(u'username')
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
idea = self._addIdea()
request = testing.DummyRequest(params={'target': idea.idea_id})
result = idea_add(request)
@@ -96,16 +81,16 @@ def test_idea_add_nosubmit_comment(self):
def test_idea_add_not_existing_target(self):
from shootout.views import idea_add
self.config.testing_securitypolicy(u'username')
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest(params={'target': 100})
result = idea_add(request)
self.assertEqual(result.code, 404)
def test_idea_add_submit_schema_fail_empty_params(self):
from shootout.views import idea_add
self.config.testing_securitypolicy(u'username')
- _registerCommonTemplates(self.config)
- _registerRoutes(self.config)
+ self.config.include(register_templates)
+ self.config.include('shootout.addroutes')
request = testing.DummyRequest(post={'form.submitted': 'Shoot'})
result = idea_add(request)
self.assertEqual(
@@ -121,7 +106,7 @@ def test_idea_add_submit_schema_succeed(self):
from shootout.views import idea_add
from shootout.models import Idea
self.config.testing_securitypolicy(u'username')
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
request = testing.DummyRequest(
post={
'form.submitted': u'Shoot',
@@ -148,7 +133,7 @@ def test_idea_add_submit_schema_succeed(self):
def test_vote_on_own_idea(self):
from shootout.views import idea_vote
from shootout.models import User
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
idea = self._addIdea()
self.session.query(User).one()
self.assertEqual(idea.user_voted(u'username'), False)
@@ -170,7 +155,7 @@ def test_vote_on_own_idea(self):
def test_positive_idea_voting(self):
from shootout.views import idea_vote
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
user = self._addUser()
idea = self._addIdea(user=user)
voter = self._addUser(u'votername')
@@ -194,7 +179,7 @@ def test_positive_idea_voting(self):
def test_negative_idea_voting(self):
from shootout.views import idea_vote
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
user = self._addUser()
idea = self._addIdea(user=user)
voter = self._addUser(u'votername')
@@ -218,14 +203,14 @@ def test_negative_idea_voting(self):
def test_registration_nosubmit(self):
from shootout.views import user_add
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest()
result = user_add(request)
self.assertTrue('form' in result)
def test_registration_submit_empty(self):
from shootout.views import user_add
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest()
result = user_add(request)
self.assertTrue('form' in result)
@@ -245,7 +230,7 @@ def test_registration_submit_empty(self):
def test_registration_submit_schema_succeed(self):
from shootout.views import user_add
from shootout.models import User
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
request = testing.DummyRequest(
post={
'form.submitted': u'Register',
@@ -273,8 +258,8 @@ def test_registration_submit_schema_succeed(self):
def test_user_view(self):
from shootout.views import user_view
self.config.testing_securitypolicy(u'username')
- _registerRoutes(self.config)
- _registerCommonTemplates(self.config)
+ self.config.include('shootout.addroutes')
+ self.config.include(register_templates)
request = testing.DummyRequest()
request.matchdict = {'username': u'username'}
self._addUser()
@@ -285,8 +270,8 @@ def test_user_view(self):
def test_idea_view(self):
from shootout.views import idea_view
self.config.testing_securitypolicy(u'username')
- _registerRoutes(self.config)
- _registerCommonTemplates(self.config)
+ self.config.include('shootout.addroutes')
+ self.config.include(register_templates)
self._addIdea()
request = testing.DummyRequest()
request.matchdict = {'idea_id': 1}
@@ -299,8 +284,8 @@ def test_tag_view(self):
from shootout.views import tag_view
from shootout.models import Tag
self.config.testing_securitypolicy(u'username')
- _registerRoutes(self.config)
- _registerCommonTemplates(self.config)
+ self.config.include('shootout.addroutes')
+ self.config.include(register_templates)
user = self._addUser()
tag1 = Tag(u'bar')
tag2 = Tag(u'foo')
@@ -329,13 +314,13 @@ def test_tag_view(self):
def test_about_view(self):
from shootout.views import about_view
- _registerCommonTemplates(self.config)
+ self.config.include(register_templates)
request = testing.DummyRequest()
about_view(request)
def test_login_view_submit_fail(self):
from shootout.views import login_view
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
self._addUser()
request = testing.DummyRequest(
post={
@@ -350,7 +335,7 @@ def test_login_view_submit_fail(self):
def test_login_view_submit_success(self):
from shootout.views import login_view
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
self._addUser()
request = testing.DummyRequest(
post={
@@ -365,7 +350,7 @@ def test_login_view_submit_success(self):
def test_logout_view(self):
from shootout.views import logout_view
- _registerRoutes(self.config)
+ self.config.include('shootout.addroutes')
request = testing.DummyRequest()
logout_view(request)
messages = request.session.peek_flash()

0 comments on commit 513e57c

Please sign in to comment.