Ticket #1628: no_cookie_patch_2_branch_1_0.patch
| File no_cookie_patch_2_branch_1_0.patch, 12.5 KB (added by chrisz, 4 years ago) |
|---|
-
turbogears/qstemplates/quickstart/+package+/controllers.py_tmpl
1 from turbogears import controllers, expose, flash 1 from turbogears import controllers, expose, flash, config 2 2 # from ${package} import model 3 3 #if $sqlobject == 'True' 4 4 import pkg_resources … … 46 46 47 47 ${b}expose(template="${package}.templates.login")${e} 48 48 def login(self, forward_url=None, previous_url=None, *args, **kw): 49 50 if not identity.current.anonymous \ 51 and identity.was_login_attempted() \ 52 and not identity.get_identity_errors(): 53 raise redirect(forward_url) 54 55 forward_url=None 56 previous_url= request.path 57 49 cookie_name = config.get("visit.cookie.name", "tg-visit") 50 if (not identity.current.anonymous 51 and identity.was_login_attempted() 52 and not identity.get_identity_errors() 53 and request.simple_cookie.has_key(cookie_name)): 54 raise redirect(forward_url or request.headers.get("Referer", "/")) 55 forward_url = None 56 previous_url = request.path 58 57 if identity.was_login_attempted(): 59 msg=_("The credentials you supplied were not correct or " 60 "did not grant access to this resource.") 58 if request.simple_cookie.has_key(cookie_name): 59 msg = _("The credentials you supplied were not correct" 60 " or did not grant access to this resource.") 61 else: 62 msg = _("Cannot log in because your browser " 63 " does not support session cookies.") 61 64 elif identity.get_identity_errors(): 62 msg=_("You must provide your credentials before accessing " 63 "this resource.") 65 if request.simple_cookie.has_key(cookie_name): 66 msg = _("You must provide your credentials" 67 " before accessing this resource.") 64 68 else: 65 msg=_("Please log in.") 66 forward_url= request.headers.get("Referer", "/") 67 68 response.status=403 69 msg = _("Please log in.") 70 forward_url = request.headers.get("Referer", "/") 71 response.status = 403 69 72 return dict(message=msg, previous_url=previous_url, logging_in=True, 70 73 original_parameters=request.params, 71 74 forward_url=forward_url) -
turbogears/qstemplates/quickstart/+package+/tests/test_controllers.py_tmpl
1 1 import unittest 2 import turbogears3 from turbogears import testutil 2 import cherrypy 3 from turbogears import testutil, startup 4 4 from ${package}.controllers import Root 5 import cherrypy 5 #if $identity != "none" 6 from ${package}.model import User 7 #end if 6 8 7 9 cherrypy.root = Root() 8 10 9 11 class TestPages(unittest.TestCase): 10 12 11 13 def setUp(self): 12 turbogears.startup.startTurboGears()14 startup.startTurboGears() 13 15 14 16 def tearDown(self): 15 """Tests for apps using identity need to stop CP/TG after each test to 16 stop the VisitManager thread.17 See http://trac.turbogears.org/turbogears/ticket/1217 for details.18 """ 19 turbogears.startup.stopTurboGears()17 #if $identity != "none" 18 # Tests for apps using identity need to stop TurboGears 19 # after each test to stop the VisitManager thread. 20 #end if 21 startup.stopTurboGears() 20 22 21 23 def test_method(self): 22 "the index method should return a string called now" 23 import types 24 """The index method should return a string called now.""" 24 25 result = testutil.call(cherrypy.root.index) 25 assert type(result["now"]) == types.StringType26 assert isinstance(result["now"], str) 26 27 27 def test_index title(self):28 " The indexpage should have the right title"28 def test_index_title(self): 29 """The index page should have the right title.""" 29 30 testutil.createRequest("/") 30 response = cherrypy.response.body[0].lower() 31 response = cherrypy.response.body[0].lower() 31 32 assert "<title>welcome to turbogears</title>" in response 32 33 33 34 #if $identity != "none" 34 def test_login title(self):35 " login page should have the right title"35 def test_login_title(self): 36 """The login page should have the right title.""" 36 37 testutil.createRequest("/login") 37 38 response = cherrypy.response.body[0].lower() 38 39 assert "<title>login</title>" in response 40 assert "please log in" in response 41 assert "session cookies" not in response 42 assert "credentials" not in response 43 assert "not correct" not in response 44 45 def test_login_errors(self): 46 """The login page should display the right errors.""" 47 login = "/login?user_name=nobody&password=wrong&login=Login" 48 testutil.createRequest(login) 49 response = cherrypy.response.body[0].lower() 50 assert "<title>login</title>" in response 51 assert "session cookies" in response 52 cookie = ', '.join(map(str, cherrypy.response.simple_cookie.values())) 53 testutil.create_request(login, headers=dict(Cookie=cookie)) 54 response = cherrypy.response.body[0].lower() 55 assert "<title>login</title>" in response 56 assert "credentials" in response 57 assert "not correct" in response 58 59 def test_login_and_logout(self): 60 """Login with correct credentials and then logout.""" 61 User(user_name = "scott", password = "tiger", 62 display_name = "Bruce Scott", 63 email_address = "scott@enterprise.com") 64 testutil.createRequest("/") 65 response = cherrypy.response.body[0].lower() 66 assert "<title>welcome to turbogears</title>" in response 67 assert 'href="/login"' in response 68 assert 'href="/logout"' not in response 69 testutil.createRequest("/login") 70 response = cherrypy.response.body[0].lower() 71 assert "<title>login</title>" in response 72 assert 'please log in' in response 73 cookie = ', '.join(map(str, cherrypy.response.simple_cookie.values())) 74 login = "/login?user_name=scott&password=tiger&login=Login" 75 testutil.create_request(login, headers=dict(Cookie=cookie)) 76 assert cherrypy.response.status, startswith('302 ') 77 location = cherrypy.response.headers['Location'] 78 testutil.create_request(location, headers=dict(Cookie=cookie)) 79 response = cherrypy.response.body[0].lower() 80 assert "<title>welcome to turbogears</title>" in response 81 assert "welcome bruce scott" in response 82 assert 'href="/login"' not in response 83 assert 'href="/logout"' in response 84 testutil.create_request("/", headers=dict(Cookie=cookie)) 85 assert "<title>welcome to turbogears</title>" in response 86 assert "welcome bruce scott" in response 87 assert 'href="/login"' not in response 88 assert 'href="/logout"' in response 89 testutil.create_request("/logout", headers=dict(Cookie=cookie)) 90 assert cherrypy.response.status, startswith('302 ') 91 location = cherrypy.response.headers['Location'] 92 testutil.create_request(location, headers=dict(Cookie=cookie)) 93 response = cherrypy.response.body[0].lower() 94 assert "<title>welcome to turbogears</title>" in response 95 assert 'href="/login"' in response 96 assert 'href="/logout"' not in response 39 97 #end if -
turbogears/qstemplates/quickstart/+package+/tests/test_model.py_tmpl
1 1 # If your project uses a database, you can set up database tests 2 # similar to what you see below. Be sure to set the db_uri to3 # an appropriate uri for your testing database. sqlite is a good2 # similar to what you see below. Be sure to set the db_uri in test.cfg 3 # to an appropriate uri for your testing database. sqlite is a good 4 4 # choice for testing, because you can use an in-memory database 5 5 # which is very fast. 6 6 7 7 from turbogears import testutil, database 8 #if $identity != "none" 9 from foo.model import User 10 # from foo.model import YourDataClass 11 12 class TestUser(testutil.DBTest): 13 14 def get_model(self): 15 return User 16 17 def test_creation(self): 18 """Object creation should set the name""" 19 obj = User(user_name = "creosote", 20 email_address = "spam@python.not", 21 display_name = "Mr Creosote", 22 password = "Wafer-thin Mint") 23 assert obj.display_name == "Mr Creosote" 24 25 #else 8 26 # from ${package}.model import YourDataClass, User 9 27 10 # database.set_db_uri("sqlite:///:memory:")11 12 28 # class TestUser(testutil.DBTest): 29 # 13 30 # def get_model(self): 14 31 # return User 15 32 # 16 33 # def test_creation(self): 17 # " Object creation should set the name"34 # """Object creation should set the name""" 18 35 # obj = User(user_name = "creosote", 19 # email_address = "spam@python.not",20 # display_name = "Mr Creosote",21 # password = "Wafer-thin Mint")36 # email_address = "spam@python.not", 37 # display_name = "Mr Creosote", 38 # password = "Wafer-thin Mint") 22 39 # assert obj.display_name == "Mr Creosote" 23 40 #end if 41 Kein Zeilenvorschub am Ende der Datei -
turbogears/qstemplates/quickstartbig/+package+/controllers/root.py_tmpl
1 from turbogears import controllers, expose, flash 1 from turbogears import controllers, expose, flash, config 2 2 # from ${package} import model 3 3 #if $sqlobject == 'True' 4 4 import pkg_resources … … 46 46 47 47 ${b}expose(template="${package}.templates.login")${e} 48 48 def login(self, forward_url=None, previous_url=None, *args, **kw): 49 50 if not identity.current.anonymous \ 51 and identity.was_login_attempted() \ 52 and not identity.get_identity_errors(): 53 raise redirect(forward_url) 54 55 forward_url=None 56 previous_url= request.path 57 49 cookie_name = config.get("visit.cookie.name", "tg-visit") 50 if (not identity.current.anonymous 51 and identity.was_login_attempted() 52 and not identity.get_identity_errors() 53 and request.simple_cookie.has_key(cookie_name)): 54 raise redirect(forward_url or request.headers.get("Referer", "/")) 55 forward_url = None 56 previous_url = request.path 58 57 if identity.was_login_attempted(): 59 msg=_("The credentials you supplied were not correct or " 60 "did not grant access to this resource.") 58 if request.simple_cookie.has_key(cookie_name): 59 msg = _("The credentials you supplied were not correct" 60 " or did not grant access to this resource.") 61 else: 62 msg = _("Cannot log in because your browser " 63 " does not support session cookies.") 61 64 elif identity.get_identity_errors(): 62 msg=_("You must provide your credentials before accessing " 63 "this resource.") 65 if request.simple_cookie.has_key(cookie_name): 66 msg = _("You must provide your credentials" 67 " before accessing this resource.") 64 68 else: 65 msg=_("Please log in.") 66 forward_url= request.headers.get("Referer", "/") 67 68 response.status=403 69 msg = _("Please log in.") 70 forward_url = request.headers.get("Referer", "/") 71 response.status = 403 69 72 return dict(message=msg, previous_url=previous_url, logging_in=True, 70 73 original_parameters=request.params, 71 74 forward_url=forward_url)