[devtools] fix anonymous user handling in test classes stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 10 Mar 2011 14:59:21 +0100
branchstable
changeset 7071 db7608cb32bc
parent 7067 f239da8e7c39
child 7072 bcf96f2a4c5d
[devtools] fix anonymous user handling in test classes
devtools/__init__.py
devtools/cwwindmill.py
devtools/httptest.py
devtools/test/unittest_httptest.py
devtools/testlib.py
web/views/basetemplates.py
--- a/devtools/__init__.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/devtools/__init__.py	Thu Mar 10 14:59:21 2011 +0100
@@ -85,13 +85,6 @@
     read_instance_schema = False
     init_repository = True
     db_require_setup = True
-    options = cwconfig.merge_options(
-        ServerConfiguration.options +
-        tuple((opt, optdict) for opt, optdict in TwistedConfiguration.options
-              if opt in ('anonymous-user', 'anonymous-password')))
-    # By default anonymous login are allow but some test need to deny of to
-    # change the default user. Set it to None to prevent anonymous login.
-    anonymous_credential = ('anon', 'anon')
 
     def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10):
         # must be set before calling parent __init__
@@ -106,7 +99,20 @@
         # need this, usually triggered by cubicweb-ctl
         self.load_cwctl_plugins()
 
-    anonymous_user = TwistedConfiguration.anonymous_user.im_func
+    # By default anonymous login are allow but some test need to deny of to
+    # change the default user. Set it to None to prevent anonymous login.
+    anonymous_credential = ('anon', 'anon')
+
+    def anonymous_user(self):
+        if not self.anonymous_credential:
+            return None, None
+        return self.anonymous_credential
+
+    def set_anonymous_allowed(self, allowed, anonuser='anon'):
+        if allowed:
+            self.anonymous_credential = (anonuser, anonuser)
+        else:
+            self.anonymous_credential = None
 
     @property
     def apphome(self):
@@ -115,10 +121,6 @@
 
     def load_configuration(self):
         super(TestServerConfiguration, self).load_configuration()
-        if self.anonymous_credential:
-            user, password = self.anonymous_credential
-            self.global_set_option('anonymous-user', user)
-            self.global_set_option('anonymous-password', password)
         # no undo support in tests
         self.global_set_option('undo-support', '')
 
--- a/devtools/cwwindmill.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/devtools/cwwindmill.py	Thu Mar 10 14:59:21 2011 +0100
@@ -91,7 +91,7 @@
         browser = 'firefox'
         edit_test = "-i" in sys.argv # detection for pytest invocation
         # Windmill use case are written with no anonymous user
-        anonymous_logged = False
+        anonymous_allowed = False
 
         tags = CubicWebServerTC.tags & Tags(('windmill',))
 
--- a/devtools/httptest.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/devtools/httptest.py	Thu Mar 10 14:59:21 2011 +0100
@@ -89,12 +89,11 @@
     """Class for running test web server. See :class:`CubicWebServerConfig`.
 
     Class attributes:
-    * `anonymous_logged`: flag telling if anonymous user should be logged-in
-      by default (True by default) XXX (syt) s/logged-in/allowed/ ?
+    * `anonymous_allowed`: flag telling if anonymous browsing should be allowed
     """
     configcls = CubicWebServerConfig
     # anonymous is logged by default in cubicweb test cases
-    anonymous_logged = True
+    anonymous_allowed = True
 
     def start_server(self):
         # use a semaphore to avoid starting test while the http server isn't
@@ -189,6 +188,5 @@
 
     @classmethod
     def init_config(cls, config):
-        if not cls.anonymous_logged:
-            config.anonymous_credential = None
+        config.set_anonymous_allowed(cls.anonymous_allowed)
         super(CubicWebServerTC, cls).init_config(config)
--- a/devtools/test/unittest_httptest.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/devtools/test/unittest_httptest.py	Thu Mar 10 14:59:21 2011 +0100
@@ -42,7 +42,7 @@
 
 class TwistedCWIdentTC(CubicWebServerTC):
 
-    anonymous_logged = False
+    anonymous_allowed = False
     tags = CubicWebServerTC.tags | Tags(('auth',))
 
     def test_response_denied(self):
--- a/devtools/testlib.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/devtools/testlib.py	Thu Mar 10 14:59:21 2011 +0100
@@ -659,6 +659,10 @@
     def init_authentication(self, authmode, anonuser=None):
         self.set_option('auth-mode', authmode)
         self.set_option('anonymous-user', anonuser)
+        if anonuser is None:
+            self.config.anonymous_credential = None
+        else:
+            self.config.anonymous_credential = (anonuser, anonuser)
         req = self.request()
         origsession = req.session
         req.session = req.cnx = None
--- a/web/views/basetemplates.py	Thu Mar 10 11:56:35 2011 +0100
+++ b/web/views/basetemplates.py	Thu Mar 10 14:59:21 2011 +0100
@@ -74,7 +74,7 @@
         # FIXME Deprecated code ?
         msg = self._cw._('you have been logged out')
         w(u'<h2>%s</h2>\n' % msg)
-        if self._cw.vreg.config['anonymous-user']:
+        if self._cw.vreg.config.anonymous_user()[0]:
             indexurl = self._cw.build_url('view', vid='index', __message=msg)
             w(u'<p><a href="%s">%s</a><p>' % (
                 xml_escape(indexurl),