Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

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)

Patch with template unit tests against 1.0 branch

  • turbogears/qstemplates/quickstart/+package+/controllers.py_tmpl

     
    1 from turbogears import controllers, expose, flash 
     1from turbogears import controllers, expose, flash, config 
    22# from ${package} import model 
    33#if $sqlobject == 'True' 
    44import pkg_resources 
     
    4646 
    4747    ${b}expose(template="${package}.templates.login")${e} 
    4848    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 
    5857        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.") 
    6164        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.") 
    6468        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 
    6972        return dict(message=msg, previous_url=previous_url, logging_in=True, 
    7073                    original_parameters=request.params, 
    7174                    forward_url=forward_url) 
  • turbogears/qstemplates/quickstart/+package+/tests/test_controllers.py_tmpl

     
    11import unittest 
    2 import turbogears 
    3 from turbogears import testutil 
     2import cherrypy 
     3from turbogears import testutil, startup 
    44from ${package}.controllers import Root 
    5 import cherrypy 
     5#if $identity != "none" 
     6from ${package}.model import User 
     7#end if 
    68 
    79cherrypy.root = Root() 
    810 
    911class TestPages(unittest.TestCase): 
    1012 
    1113    def setUp(self): 
    12         turbogears.startup.startTurboGears() 
     14        startup.startTurboGears() 
    1315 
    1416    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() 
    2022 
    2123    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.""" 
    2425        result = testutil.call(cherrypy.root.index) 
    25         assert type(result["now"]) == types.StringType 
     26        assert isinstance(result["now"], str) 
    2627 
    27     def test_indextitle(self): 
    28         "The indexpage should have the right title" 
     28    def test_index_title(self): 
     29        """The index page should have the right title.""" 
    2930        testutil.createRequest("/") 
    30         response = cherrypy.response.body[0].lower()  
     31        response = cherrypy.response.body[0].lower() 
    3132        assert "<title>welcome to turbogears</title>" in response 
    3233 
    3334#if $identity != "none" 
    34     def test_logintitle(self): 
    35         "login page should have the right title" 
     35    def test_login_title(self): 
     36        """The login page should have the right title.""" 
    3637        testutil.createRequest("/login") 
    3738        response = cherrypy.response.body[0].lower() 
    3839        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 
    3997#end if 
  • turbogears/qstemplates/quickstart/+package+/tests/test_model.py_tmpl

     
    11# 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 to 
    3 # an appropriate uri for your testing database. sqlite is a good 
     2# 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 
    44# choice for testing, because you can use an in-memory database 
    55# which is very fast. 
    66 
    77from turbogears import testutil, database 
     8#if $identity != "none" 
     9from foo.model import User 
     10# from foo.model import YourDataClass 
     11 
     12class 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 
    826# from ${package}.model import YourDataClass, User 
    927 
    10 # database.set_db_uri("sqlite:///:memory:") 
    11  
    1228# class TestUser(testutil.DBTest): 
     29# 
    1330#     def get_model(self): 
    1431#         return User 
    1532# 
    1633#     def test_creation(self): 
    17 #         "Object creation should set the name" 
     34#         """Object creation should set the name""" 
    1835#         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") 
    2239#         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 
     1from turbogears import controllers, expose, flash, config 
    22# from ${package} import model 
    33#if $sqlobject == 'True' 
    44import pkg_resources 
     
    4646 
    4747    ${b}expose(template="${package}.templates.login")${e} 
    4848    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 
    5857        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.") 
    6164        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.") 
    6468        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 
    6972        return dict(message=msg, previous_url=previous_url, logging_in=True, 
    7073                    original_parameters=request.params, 
    7174                    forward_url=forward_url)